1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.gui.action;
25
26 import java.awt.event.ActionEvent;
27 import java.io.File;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.util.LinkedList;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.swing.AbstractAction;
35 import javax.swing.JFileChooser;
36 import javax.swing.JOptionPane;
37
38 import com.mindtree.techworks.insight.InsightConstants;
39 import com.mindtree.techworks.insight.appender.ParsedEventAppender;
40 import com.mindtree.techworks.insight.gui.Insight;
41 import com.mindtree.techworks.insight.gui.widgets.StatusBar;
42 import com.mindtree.techworks.insight.model.ReceiverFormat;
43 import com.mindtree.techworks.insight.preferences.util.Log4JPatternInterpeter;
44 import com.mindtree.techworks.insight.preferences.util.PreferenceInterpreter;
45 import com.mindtree.techworks.insight.receiver.AbstractReceiver;
46 import com.mindtree.techworks.insight.receiver.FileStreamReceiver;
47 import com.mindtree.techworks.insight.receiver.ReceiverInitializationException;
48 import com.mindtree.techworks.insight.receiver.ReceiverInterpreter;
49 import com.mindtree.techworks.insight.receiver.ReceiverListener;
50 import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
51 import com.mindtree.techworks.insight.receiver.WildCardMatcher;
52 import com.mindtree.techworks.insight.spi.LogNamespace;
53
54
55
56
57
58
59
60
61
62
63
64
65 public class LoadLocalFileAction extends AbstractAction implements ReceiverListener {
66
67
68
69
70 private static final long serialVersionUID = -6169872455620799521L;
71
72
73
74
75 private static LoadLocalFileAction instance;
76
77
78
79
80 private LinkedList namespacesToBeLoaded = new LinkedList();
81
82
83
84
85 private AbstractReceiver receiver;
86
87
88
89
90
91
92 private final JFileChooser fileChooser = new JFileChooser(System.getProperty(InsightConstants.INSIGHT_HOME)); {
93 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
94 fileChooser.setMultiSelectionEnabled(true);
95 }
96
97
98
99
100 private Insight insight;
101
102
103
104
105
106 private LoadLocalFileAction() {
107
108 }
109
110
111
112
113
114 public static synchronized final LoadLocalFileAction getInstance() {
115 if (instance == null) {
116 instance = new LoadLocalFileAction();
117 }
118 return instance;
119 }
120
121
122
123
124
125
126 public void initialize(Insight insightInstance) {
127 if (this.insight == null) {
128 this.insight = insightInstance;
129 }
130 RuntimeNamespaceContainer.initializeAvailableNamespaceColorList();
131 }
132
133
134
135
136
137 public void actionPerformed(ActionEvent e) {
138 browseAndLoadLocalFiles();
139 }
140
141
142
143
144
145 public void startupReceiver(LogNamespace namespace) {
146 boolean isReceiverTailing = PreferenceInterpreter.getTailingStatus();
147
148
149 try {
150 boolean isLocalNamespace = (namespace.getNodeId().equalsIgnoreCase(InetAddress.getLocalHost().getHostName()));
151 isReceiverTailing = isReceiverTailing && isLocalNamespace;
152 } catch(UnknownHostException uhe) {
153
154 }
155 if (RuntimeNamespaceContainer.getLoadedNamespaces().contains(namespace)) {
156 StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
157
158 analyzeWildCardEntries(namespacesToBeLoaded);
159 loadNextAvailableNamespace();
160 return;
161 }
162 StatusBar.getInstance().clearDisplay(0);
163 StatusBar.getInstance().setDisplayText(1,InsightConstants.getLiteral("LOAD_FILE_STATUS") + namespace.getNamespaceAsString(), true);
164 try{
165 this.receiver = new FileStreamReceiver(namespace, isReceiverTailing);
166 }catch(ReceiverInitializationException rie){
167 StatusBar.getInstance().setDisplayText(1,rie.getMessage(),false);
168 return;
169 }
170 this.receiver.addAppender(ParsedEventAppender.getInstance(this.insight.getController()));
171 this.receiver.addReceiverListener(this);
172 this.receiver.addMutatorListener(this.insight.getController());
173 this.receiver.addMutatorListener(this.insight.getController().getCurrentLogEventModel().getPageSet());
174 this.receiver.startup();
175 }
176
177
178
179
180
181 public void startLoadEventNotification(LogNamespace[] namespaces) {
182
183 }
184
185
186
187
188
189 public void endLoadEventNotification(LogNamespace[] namespaces, int infoFlag) {
190 StatusBar.getInstance().clearDisplay(1);
191 if (infoFlag == ReceiverInterpreter.SUCCESS) {
192 for (int i = 0; i < namespaces.length; i++ ) {
193 RuntimeNamespaceContainer.getLoadedNamespaces().add(namespaces[i]);
194 }
195
196 } else {
197 StringBuffer namespaceNames = new StringBuffer();
198 for (int i = 0; i < namespaces.length; i++ ) {
199 namespaceNames.append(namespaces[i].getNamespaceAsString());
200 if ((i + 1) < namespaces.length) {
201 namespaceNames.append(", ");
202 }
203 }
204 JOptionPane.showMessageDialog(insight, InsightConstants.getLiteral("ERROR_NO_MATCHING_ENTRIES") + "\n" + namespaceNames.toString(),
205 InsightConstants.getLiteral("ERROR"), JOptionPane.ERROR_MESSAGE);
206 }
207
208 analyzeWildCardEntries(namespacesToBeLoaded);
209
210 loadNextAvailableNamespace();
211 }
212
213
214
215
216
217 public void addNamespaceForLoad(LogNamespace namespace) {
218 this.namespacesToBeLoaded.addLast(namespace);
219 }
220
221
222
223
224
225 public void loadNamespaces() {
226 analyzeWildCardEntries(namespacesToBeLoaded);
227 loadNextAvailableNamespace();
228 }
229
230
231
232
233
234 public void browseAndLoadLocalFiles() {
235
236 this.namespacesToBeLoaded.clear();
237 String nodeId = null;
238 ReceiverFormat[] receiverFormat = Log4JPatternInterpeter.getInterpretedRecieverFormat();
239 try {
240 nodeId = InetAddress.getLocalHost().getHostName();
241 } catch(UnknownHostException uhe) {
242 uhe.printStackTrace();
243 }
244 fileChooser.setMultiSelectionEnabled(!PreferenceInterpreter.getTailingStatus());
245 if (fileChooser.showOpenDialog(insight) == JFileChooser.APPROVE_OPTION) {
246 if(!fileChooser.isMultiSelectionEnabled()){
247
248
249 String fileAbsPath = fileChooser.getSelectedFile().getAbsolutePath();
250
251 this.namespacesToBeLoaded.addLast(new LogNamespace(fileAbsPath,InsightConstants.FILE_PROTOCOL_PREFIX+fileAbsPath,receiverFormat,nodeId));
252 }else{
253
254
255 File[] chosenFiles = fileChooser.getSelectedFiles();
256 for (int i = 0; i < chosenFiles.length; i++) {
257
258 this.namespacesToBeLoaded.addLast(new LogNamespace(chosenFiles[i].getAbsolutePath(),
259 InsightConstants.FILE_PROTOCOL_PREFIX + chosenFiles[i].getAbsolutePath(),
260 receiverFormat, nodeId));
261 }
262
263 }
264 analyzeWildCardEntries(namespacesToBeLoaded);
265 loadNextAvailableNamespace();
266 }
267 }
268
269
270
271
272 public void LoadOnFileSelection(String fileName) {
273 String nodeId = null;
274 ReceiverFormat[] receiverFormat = Log4JPatternInterpeter.getInterpretedRecieverFormat();
275 try {
276 nodeId = InetAddress.getLocalHost().getHostName();
277 } catch(UnknownHostException uhe) {
278 uhe.printStackTrace();
279 }
280 String fileAbsPath = fileName;
281 this.namespacesToBeLoaded.addLast(new LogNamespace(fileAbsPath,InsightConstants.FILE_PROTOCOL_PREFIX+fileAbsPath,receiverFormat,nodeId));
282 analyzeWildCardEntries(namespacesToBeLoaded);
283 loadNextAvailableNamespace();
284 }
285
286
287
288 private final void loadNextAvailableNamespace() {
289 if (namespacesToBeLoaded.size() == 0) {
290 return;
291 }
292
293 LogNamespace nextNamespace = (LogNamespace)namespacesToBeLoaded.removeFirst();
294 startupReceiver(nextNamespace);
295 }
296
297
298
299
300
301 private void analyzeWildCardEntries(LinkedList namespacesToBeLoaded){
302
303 List namespacesList = new ArrayList(10);
304
305 for(int i = 0; i < namespacesToBeLoaded.size(); i++){
306
307 String filePath = ((LogNamespace)(namespacesToBeLoaded.get(i))).getSourceString();
308
309 int lastFileSeparatorIndex = (filePath.lastIndexOf(File.separatorChar));
310 String fileName = filePath.substring(lastFileSeparatorIndex + 1);
311 String parentDirectoryPath = filePath.substring(0, lastFileSeparatorIndex);
312
313 if(WildCardMatcher.hasWildCardEntries(fileName)){
314 String wildCard = fileName;
315 File parentDirectory = new File(parentDirectoryPath);
316 if(parentDirectory.isDirectory()){
317
318 List matchingFiles = getMatchingFiles(parentDirectory, wildCard);
319 for(int j = 0; j < matchingFiles.size(); j++){
320 LogNamespace thisNamespace = (LogNamespace)namespacesToBeLoaded.get(i);
321 String newFilePath = ((File)(matchingFiles.get(j))).getAbsolutePath();
322 String newFileLocalPath = InsightConstants.FILE_PROTOCOL_PREFIX + newFilePath;
323 namespacesList.add(new LogNamespace(newFilePath, newFileLocalPath, thisNamespace.getReceiverFormat(), thisNamespace.getNodeId()));
324 }
325 namespacesToBeLoaded.remove(i);
326 }
327 }
328 }
329 for(int i = 0; i < namespacesList.size(); i++){
330 namespacesToBeLoaded.addFirst((LogNamespace)namespacesList.get(i));
331 }
332 }
333
334
335
336
337
338
339
340
341 private List getMatchingFiles(File parentDirectory, String pattern){
342
343 List filesList = new ArrayList(parentDirectory.listFiles().length);
344 File[] files = parentDirectory.listFiles();
345
346 for(int i = 0; i < files.length; i++){
347 if(files[i].isDirectory()){
348
349
350 }
351 else{
352 String filePath = files[i].getAbsolutePath();
353 String fileName = filePath.substring(filePath.lastIndexOf('/') == -1 ? filePath.lastIndexOf('\\') + 1 : filePath.lastIndexOf('/') + 1);
354 WildCardMatcher matcher = new WildCardMatcher(pattern);
355 if(matcher.matches(fileName)){
356 filesList.add(files[i]);
357 }
358 else{
359 }
360 }
361 }
362 return filesList;
363 }
364
365
366
367
368
369 public AbstractReceiver getReceiver() {
370 return receiver;
371 }
372 }