1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw;
19
20 import java.awt.event.ActionEvent;
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Vector;
28
29 import javax.swing.AbstractAction;
30 import javax.swing.JOptionPane;
31
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.chainsaw.helper.SwingHelper;
34 import org.apache.log4j.chainsaw.prefs.MRUFileList;
35 import org.apache.log4j.helpers.Constants;
36 import org.apache.log4j.spi.Decoder;
37 import org.apache.log4j.spi.LoggingEvent;
38
39
40
41
42
43
44
45
46
47 class FileLoadAction extends AbstractAction {
48 private static final Logger LOG = Logger.getLogger(FileLoadAction.class);
49
50
51
52
53
54
55 Decoder decoder = null;
56
57 private LogUI parent;
58
59 private boolean remoteURL = false;
60
61 public FileLoadAction(LogUI parent, Decoder decoder, String title,
62 boolean isRemoteURL) {
63 super(title);
64 remoteURL = isRemoteURL;
65 this.decoder = decoder;
66 this.parent = parent;
67 }
68
69
70
71
72
73
74
75
76
77 public void actionPerformed(ActionEvent e) {
78 String name = "";
79 URL url = null;
80
81 if (!remoteURL) {
82 try {
83 File selectedFile = SwingHelper.promptForFile(parent, null, "Load Events from XML file or zipped XML file...", true);
84 if (selectedFile != null) {
85 url = selectedFile.toURI().toURL();
86 name = selectedFile.getName();
87 }
88 } catch (Exception ex) {
89
90 }
91 } else {
92 String urltext = JOptionPane
93 .showInputDialog(parent,
94 "<html>Please type in the <b>complete</b> URL to the remote XML source.</html>");
95
96 if (urltext != null) {
97 try {
98 url = new URL(urltext);
99 } catch (Exception ex) {
100 JOptionPane.showMessageDialog(parent, "'" + urltext
101 + "' is not a valid URL.");
102 }
103 }
104 }
105
106 if (url != null) {
107 importURL(parent.handler, decoder, name, url);
108 MRUFileList.log4jMRU().opened(url);
109 }
110 }
111
112
113
114
115
116
117
118
119 public static void importURL(final ChainsawAppenderHandler handler, final Decoder decoder, String name, URL url) {
120 Map additionalProperties = new HashMap();
121 additionalProperties.put(Constants.HOSTNAME_KEY, "file");
122 additionalProperties.put(Constants.APPLICATION_KEY, name);
123 decoder.setAdditionalProperties(additionalProperties);
124
125 final URL urlToUse = url;
126 new Thread(new Runnable() {
127 public void run() {
128 try {
129 Vector events = decoder.decode(urlToUse);
130 Iterator iter = events.iterator();
131 while (iter.hasNext()) {
132 handler.append((LoggingEvent) iter.next());
133 }
134 } catch (IOException e1) {
135
136 LOG.error(e1);
137 }
138 MRUFileList.log4jMRU().opened(urlToUse);
139 }
140 }).start();
141 }
142 }