View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.download;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import com.mindtree.techworks.insight.InsightConstants;
34  import com.mindtree.techworks.insight.gui.Insight;
35  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
36  
37  
38  /**
39   * TODO
40   * 
41   * @author Bindul Bhowmik
42   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
43   */
44  public abstract class RemoteClient {
45  
46  	//
47  	// Constants
48  	//
49  
50  	/**
51  	 * 
52  	 */
53  	private static final String DOWNLOADED_FILE_EXTENSION = ".log";
54  
55  	//
56  	// Instance variables
57  	//
58  
59  	/**
60  	 * The base directory to which files are downloaded
61  	 */
62  	protected File baseDownloadDirectory;
63  
64  	/**
65  	 * Is the instance of the client busy on an operation
66  	 */
67  	protected boolean isBusy = false;
68  	
69  	/**
70  	 * The instance of status bar used for notifications.
71  	 */
72  	protected StatusBar statusBar;
73  	
74  	/**
75  	 * The insight instance used for this class.
76  	 */
77  	protected Insight insight;
78  
79  	//
80  	// Constructor(s)
81  	//
82  
83  	/**
84  	 * Creates an instance of the RemoteClient
85  	 */
86  	public RemoteClient () {
87  
88  		statusBar = StatusBar.getInstance();
89  	}
90  
91  	//
92  	// Public methods
93  	//
94  	
95  	/**
96  	 * Sets the insight instance calling this class.
97  	 * @param insight The insight instance
98  	 */
99  	public void setInsight(Insight insight) {
100 		this.insight = insight;
101 	}
102 
103 	/**
104 	 * Set the base directory to which files are downloaded.
105 	 * 
106 	 * @param directoryName The name of the directory. It is created, if not
107 	 *            present.
108 	 * @throws IOException If the directory cannot be created, or the path does
109 	 *             not denote a directory or the application cannot write in
110 	 *             this directory.
111 	 */
112 	public void setBaseDownloadDirectory (String directoryName)
113 			throws IOException {
114 
115 		String baseDirectoryName = directoryName;
116 		if (baseDirectoryName.charAt(baseDirectoryName.length() - 1) != File.separatorChar) {
117 			baseDirectoryName = baseDirectoryName + File.separator;
118 		}
119 		baseDownloadDirectory = new File(baseDirectoryName);
120 		if (!baseDownloadDirectory.exists()) {
121 			if (!baseDownloadDirectory.mkdirs()) {
122 				throw new IOException(
123 				"The directory does not exist and cannot be created");
124 			}
125 		}
126 
127 		if (!baseDownloadDirectory.isDirectory()
128 				|| !baseDownloadDirectory.canWrite()) {
129 			throw new IOException(
130 					"The path is not a file or the application cannot write in it");
131 		}
132 	}
133 
134 	/**
135 	 * Downloads a single file to the directory mentioned. Returns the name of
136 	 * the file downloaded.
137 	 * 
138 	 * @param fileset The fileset to get host information from.
139 	 * @param file The file to download on the host. The actual format is client
140 	 *            dependent.
141 	 * @return The Complete path to the downloaded file.
142 	 * @throws RemoteClientException Remote exception if the client is busy or
143 	 *             download fails.
144 	 */
145 	public String[] downloadFile (Fileset fileset, String file)
146 			throws RemoteClientException {
147 
148 		if (!isBusy) {
149 			setFileset(fileset);
150 			statusBar.setDisplayText(1, InsightConstants.getLiteral("DOWNLOADING_FILE") + file, true);
151 			String[] downloadedFile =  downloadFile(file);
152 			statusBar.clearDisplay(1);
153 			return downloadedFile;
154 		} else {
155 			throw new RemoteClientException(
156 					"Client busy with previous operation");
157 		}
158 	}
159 
160 	/**
161 	 * Downloads a list of files from the remote host. Returns the names of the
162 	 * files downloaded.
163 	 * 
164 	 * @param fileset The fileset to get host information from.
165 	 * @param files The list of files to download on the host. The actual format
166 	 *            is client dependent.
167 	 * @return The list of complete path to the downloaded file.
168 	 * @throws RemoteClientException Remote exception if the client is busy or
169 	 *             download fails.
170 	 */
171 	public List downloadFiles (Fileset fileset, List files)
172 			throws RemoteClientException {
173 
174 		if (!isBusy) {
175 			setFileset(fileset);
176 			List downloadedFiles = new ArrayList(files.size());
177 			for (Iterator iter = files.iterator(); iter.hasNext();) {
178 				String file = (String) iter.next();
179 				statusBar.setDisplayText(1, InsightConstants.getLiteral("DOWNLOADING_FILE") + file, true);
180 				downloadedFiles = addDownloadedFiles(downloadedFiles, downloadFile(file));
181 				statusBar.clearDisplay(1);
182 			}
183 			return downloadedFiles;
184 		} else {
185 			throw new RemoteClientException(
186 					"Client busy with previous operation");
187 		}
188 
189 	}
190 	
191 	private List addDownloadedFiles(List downloadedFiles, String[] files){
192 		for(int i = 0; i < files.length; i++){
193 			downloadedFiles.add(files[i]);
194 		}
195 		return downloadedFiles;
196 	}
197 	
198 	//
199 	// Abstract methods.
200 	//
201 
202 	/**
203 	 * Sets the fileset. The actual implementation is client dependent, clients
204 	 * may extract information from the fileset. Most clients would check if the
205 	 * client instance created is compatible for the type of file being
206 	 * downloaded.
207 	 * 
208 	 * @param fileset The fileset being set
209 	 * @throws RemoteClientException If the information is not correct or the
210 	 *             client is incompatible for the fileset.
211 	 */
212 	protected abstract void setFileset (Fileset fileset)
213 			throws RemoteClientException;
214 
215 	/**
216 	 * Downloads a single file to the default download directory, and returns
217 	 * the {@link File#getAbsolutePath() complete path}of the file.
218 	 * 
219 	 * @param fileName The file path and name on the remote system to download.
220 	 *            The actual format is client dependent.
221 	 * @return The array of complete paths to the downloaded files.
222 	 * @throws RemoteClientException If the download fails.
223 	 */
224 	/*
225 	 * The return type of this method is modified from String to String[] 
226 	 * in support of wild cards.
227 	 */
228 	protected abstract String[] downloadFile (String fileName)
229 			throws RemoteClientException;
230 
231 	/**
232 	 * This method is to be called at the end of a transaction to complete any
233 	 * clean up operations on the connections, etc.
234 	 * 
235 	 * @throws RemoteClientException If there is any exception during the
236 	 *             cleanup.
237 	 */
238 	protected abstract void closeConnection () throws RemoteClientException;
239 
240 
241 	//
242 	// Utility methods
243 	//
244 
245 	/**
246 	 * This method creates a destination temporary file and returns it to the
247 	 * calling method.
248 	 * 
249 	 * @see File#createTempFile(java.lang.String, java.lang.String,
250 	 *      java.io.File) Temporary File creation
251 	 * @param fileName The filename which is being downloaded, part of the
252 	 *            destination filename is generated from this.
253 	 * @param seperatorChar The seperator character in the path specified in the
254 	 *            fileName passed.
255 	 * @return A handle to the file where the source file is to be downloaded.
256 	 * @throws IOException If the file cannot be downloaded.
257 	 */
258 	protected File getDestinationFile (String fileName, char seperatorChar)
259 			throws IOException {
260 
261 		// Get the file name only other than the directories
262 		String completeFileName = fileName;
263 		if (completeFileName.indexOf(seperatorChar) > -1) {
264 			completeFileName = completeFileName.substring(completeFileName
265 					.lastIndexOf(seperatorChar));
266 		}
267 
268 		// Check if the file exists... or else append a number to the end.
269 		File destinationFile = File.createTempFile(completeFileName,
270 				DOWNLOADED_FILE_EXTENSION, baseDownloadDirectory);
271 		destinationFile.deleteOnExit();
272 
273 		return destinationFile;
274 	}
275 }