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