1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
46
47
48
49
50
51 public class DownloadProvider extends Thread {
52
53
54
55
56
57 static {
58 new FTPRemoteClient();
59 new HTTPRemoteClient();
60 new SFTPRemoteClient();
61 }
62
63
64
65
66
67
68
69
70 private Insight insight;
71
72
73
74
75 private RemoteClient remoteClient;
76
77
78
79
80 private Fileset fileset;
81
82
83
84
85 private List files;
86
87
88
89
90
91
92
93
94
95
96 public DownloadProvider (Insight insight) {
97
98 this.insight = insight;
99
100
101
102 this.setPriority(Thread.NORM_PRIORITY);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public void loadRemoteFiles (Fileset remoteFileset, List filesToLoad) {
117
118
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
132 this.fileset = remoteFileset;
133 this.files = filesToLoad;
134
135 this.start();
136 }
137
138
139
140
141
142
143
144
145 public void run () {
146
147
148 List localFiles = null;
149
150
151
152
153
154
155
156 List uniqueFiles;
157
158 try {
159 localFiles = remoteClient.downloadFiles(fileset, files);
160
161
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
176 StatusBar.getInstance().clearDisplay(1);
177 return;
178 }
179
180
181 try {
182 remoteClient.closeConnection();
183 } catch (RemoteClientException e) {
184
185 e.printStackTrace(System.err);
186 }
187
188
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
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 }