View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Allows the user to specify a particular file to open and import the events
38   * into a new tab.
39   *
40   * @author Paul Smith <psmith@apache.org>
41   * @author Scott Deboy <sdeboy@apache.org>
42   */
43  class FileLoadAction extends AbstractAction {
44      private static final Logger LOG = Logger.getLogger(FileLoadAction.class);
45  
46      /**
47       * This action must have a reference to a LogUI window so that it can append
48       * the events it loads
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       * When the user chooses the Load action, a File chooser is presented to
66       * allow them to find an XML file to load events from.
67       *
68       * Any events decoded from this file are added to one of the tabs.
69       *
70       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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                  // TODO: handle exception
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      * Imports a URL into Chainsaw, by using the Decoder, and
109      * using the name value as the Application key which (usually) determines
110      * the Tab name
111      *
112      * @param name
113      * @param url  URL to import
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                 // TODO Handle the error with a nice msg
130                 LOG.error(e1);
131             }
132             MRUFileList.log4jMRU().opened(urlToUse);
133         }).start();
134     }
135 }