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.JFileChooser;
31 import javax.swing.JOptionPane;
32 import javax.swing.filechooser.FileFilter;
33
34 import org.apache.log4j.Logger;
35 import org.apache.log4j.chainsaw.prefs.MRUFileList;
36 import org.apache.log4j.helpers.Constants;
37 import org.apache.log4j.spi.Decoder;
38 import org.apache.log4j.spi.LoggingEvent;
39
40 /***
41 * Allows the user to specify a particular file to open and import the events
42 * into a new tab.
43 *
44 * @author Paul Smith <psmith@apache.org>
45 * @author Scott Deboy <sdeboy@apache.org>
46 *
47 */
48 class FileLoadAction extends AbstractAction {
49 private static final Logger LOG = Logger.getLogger(FileLoadAction.class);
50
51 /***
52 * This action must have a reference to a LogUI window so that it can append
53 * the events it loads
54 *
55 */
56 Decoder decoder = null;
57
58 private LogUI parent;
59
60 private JFileChooser chooser = null;
61
62 private boolean remoteURL = false;
63
64 public FileLoadAction(LogUI parent, Decoder decoder, String title,
65 boolean isRemoteURL) {
66 super(title);
67 remoteURL = isRemoteURL;
68 this.decoder = decoder;
69 this.parent = parent;
70 }
71
72
73
74
75
76
77
78
79
80 public void actionPerformed(ActionEvent e) {
81 String name = "";
82 URL url = null;
83
84 if (!remoteURL) {
85 if (chooser == null) {
86 chooser = new JFileChooser();
87 }
88
89 chooser.setDialogTitle("Load Events from XML file...");
90
91 chooser.setAcceptAllFileFilterUsed(true);
92
93 chooser.setFileFilter(new FileFilter() {
94 public boolean accept(File f) {
95 return (f.getName().toLowerCase().endsWith(".xml") || f
96 .isDirectory());
97 }
98
99 public String getDescription() {
100 return "XML files (*.xml)";
101 }
102 });
103
104 int i = chooser.showOpenDialog(parent);
105 if (i != JFileChooser.APPROVE_OPTION) {
106 return;
107 }
108 File selectedFile = chooser.getSelectedFile();
109
110 try {
111 url = selectedFile.toURI().toURL();
112 name = selectedFile.getName();
113 } catch (Exception ex) {
114
115 }
116 } else {
117 String urltext = JOptionPane
118 .showInputDialog(parent,
119 "<html>Please type in the <b>complete</b> URL to the remote XML source.</html>");
120
121 if (urltext != null) {
122 try {
123 url = new URL(urltext);
124 } catch (Exception ex) {
125 JOptionPane.showMessageDialog(parent, "'" + urltext
126 + "' is not a valid URL.");
127 }
128 }
129 }
130
131 if (url != null) {
132 importURL(parent.handler, decoder, name, url);
133 MRUFileList.log4jMRU().opened(url);
134 }
135 }
136
137 /***
138 * Imports a URL into Chainsaw, by using the Decoder, and
139 * using the name value as the Application key which (usually) determines
140 * the Tab name
141 * @param name
142 * @param url URL to import
143 */
144 public static void importURL(final ChainsawAppenderHandler handler, final Decoder decoder, String name, URL url) {
145 Map additionalProperties = new HashMap();
146 additionalProperties.put(Constants.HOSTNAME_KEY, "file");
147 additionalProperties.put(Constants.APPLICATION_KEY, name);
148 decoder.setAdditionalProperties(additionalProperties);
149
150 final URL urlToUse = url;
151 new Thread(new Runnable() {
152 public void run() {
153 try {
154 Vector events = decoder.decode(urlToUse);
155 Iterator iter = events.iterator();
156 while (iter.hasNext()) {
157 handler.append((LoggingEvent) iter.next());
158 }
159 } catch (IOException e1) {
160
161 LOG.error(e1);
162 }
163 MRUFileList.log4jMRU().opened(urlToUse);
164 }
165 }).start();
166 }
167 }