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.net.MalformedURLException;
28  import java.net.URL;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import javax.swing.JOptionPane;
34  
35  import com.mindtree.techworks.insight.InsightConstants;
36  import com.mindtree.techworks.insight.gui.Insight;
37  import com.mindtree.techworks.insight.gui.action.LoadLocalFileAction;
38  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
39  import com.mindtree.techworks.insight.model.ReceiverFormat;
40  import com.mindtree.techworks.insight.preferences.util.Log4JPatternInterpeter;
41  import com.mindtree.techworks.insight.spi.LogNamespace;
42  
43  
44  /**
45   * TODO
46   * 
47   * @see com.mindtree.techworks.insight.download.RemoteClientFactory RemoteClientFactory
48   * @author Bindul Bhowmik
49   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
50   */
51  public class DownloadProvider extends Thread {
52  
53  	//
54  	// Static block to register all clients
55  	//
56  
57  	static {
58  		new FTPRemoteClient();
59  		new HTTPRemoteClient();
60  		new SFTPRemoteClient();
61  	}
62  
63  	//
64  	// Instance variables
65  	//
66  
67  	/**
68  	 * The insight instance
69  	 */
70  	private Insight insight;
71  
72  	/**
73  	 * The client to use for downloading files
74  	 */
75  	private RemoteClient remoteClient;
76  
77  	/**
78  	 * The fileset to use for download
79  	 */
80  	private Fileset fileset;
81  
82  	/**
83  	 * The files to download
84  	 */
85  	private List files;
86  
87  	//
88  	// Constructors
89  	//
90  
91  	/**
92  	 * Creates an instance of the DownloadProvier
93  	 * 
94  	 * @param insight The insight instance
95  	 */
96  	public DownloadProvider (Insight insight) {
97  
98  		this.insight = insight;
99  	  	// explicitly set the priority to normal. Otherwise
100 	  	// this thread will inherit the priority of the creator thread
101 	  	// i.e the AWT event dispatch thread that runs with max priority
102 		this.setPriority(Thread.NORM_PRIORITY);
103 	}
104 
105 	//
106 	// Public methods
107 	//
108 
109 	/**
110 	 * Loads all the remote files in the list taking host information from the
111 	 * Fileset
112 	 * 
113 	 * @param remoteFileset The fileset to take host information from
114 	 * @param filesToLoad The files to load
115 	 */
116 	public void loadRemoteFiles (Fileset remoteFileset, List filesToLoad) {
117 
118 		// Get the client
119 		try {
120 			remoteClient = RemoteClientFactory
121 					.getClientForFileset(remoteFileset);
122 		} catch (RemoteClientException e) {
123 			e.printStackTrace(System.err);
124 			JOptionPane.showMessageDialog(insight, InsightConstants
125 					.getLiteral("DOWNLOAD_FAILED")
126 					+ "\n" + e.getMessage(), InsightConstants
127 					.getLiteral("ERROR"), JOptionPane.ERROR_MESSAGE);
128 			return;
129 		}
130 
131 		// Set the files to download... and go into async!
132 		this.fileset = remoteFileset;
133 		this.files = filesToLoad;
134 
135 		this.start();
136 	}
137 
138 	//
139 	// Methods to be executed asynchronously
140 	//
141 
142 	/**
143 	 * @see java.lang.Runnable#run()
144 	 */
145 	public void run () {
146 
147 		// Download the files
148 		List localFiles = null;
149 		
150 		// The fileset does not care to have unique file locations.
151 		// However, downloading the same file multiple times is wastage
152 		// of network and system resources, specially between distant
153 		// hosts. Further this also causes problems with the load file
154 		// action (See Bugzill Bug #2960). 
155 		// Hence only unique files are being loaded.
156 		List uniqueFiles;
157 			
158 		try {
159 			localFiles = remoteClient.downloadFiles(fileset, files);
160 			//This code is updated such that it contains all the files from the 
161 			//local file set returned by the download provider.
162 			uniqueFiles = new ArrayList(localFiles.size());
163 			for (Iterator fileItr = localFiles.iterator(); fileItr.hasNext(); ){
164 				Object file = fileItr.next();
165 				if (!uniqueFiles.contains(file)) {
166 					uniqueFiles.add(file);
167 				}
168 			}
169 		} catch (RemoteClientException e) {
170 			e.printStackTrace(System.err);
171 			JOptionPane.showMessageDialog(insight, InsightConstants
172 					.getLiteral("DOWNLOAD_FAILED")
173 					+ "\n" + e.getMessage(), InsightConstants
174 					.getLiteral("ERROR"), JOptionPane.ERROR_MESSAGE);
175 			// If a download would have started, then the progress is being reported. Stop it.
176 			StatusBar.getInstance().clearDisplay(1);
177 			return;
178 		}
179 
180 		// Close the connection
181 		try {
182 			remoteClient.closeConnection();
183 		} catch (RemoteClientException e) {
184 			// Nothing wrong... can't do anything
185 			e.printStackTrace(System.err);
186 		}
187 
188 		// Load the files
189 		LoadLocalFileAction loadLocalFileAction = LoadLocalFileAction
190 				.getInstance();
191 		ReceiverFormat[] receiverFormat = Log4JPatternInterpeter.getInterpretedRecieverFormat();
192 		int count = 0;
193 		for (Iterator localFileItr = localFiles.iterator(); localFileItr
194 				.hasNext();) {
195 			String fileName = (String)localFileItr.next();
196 			String nodeId = null;
197 			switch(fileset.getType()) {
198 				case Fileset.FTP_FILESET:
199 					nodeId = ((FTPFileset)fileset).getHost();
200 					break;
201 				case Fileset.HTTP_FILESET:
202 					try {
203 						nodeId = new URL((String)uniqueFiles.get(count)).getHost();
204 					} catch(MalformedURLException mfe) {
205 						// consume this
206 					}
207 					break;
208 				case Fileset.SFTP_FILESET:
209 					nodeId = ((SFTPFileset) fileset).getHost();
210 					break;
211 			}
212 			loadLocalFileAction.addNamespaceForLoad(new LogNamespace((String)uniqueFiles.get(count), InsightConstants.FILE_PROTOCOL_PREFIX + fileName,
213 					receiverFormat, nodeId));
214 			count += 1;
215 		}
216 
217 		loadLocalFileAction.loadNamespaces();
218 	}
219 }