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 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   * Allows the user to specify a particular file to open and import the events
    41   * into a new tab.
    42   *
    43   * @author Paul Smith &lt;psmith@apache.org&gt;
    44   * @author Scott Deboy &lt;sdeboy@apache.org&gt;
    45   *
    46   */
    47  class FileLoadAction extends AbstractAction {
    48      private static final Logger LOG = Logger.getLogger(FileLoadAction.class);
    49
    50      /**
    51       * This action must have a reference to a LogUI window so that it can append
    52       * the events it loads
    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       * When the user chooses the Load action, a File chooser is presented to
    71       * allow them to find an XML file to load events from.
    72       *
    73       * Any events decoded from this file are added to one of the tabs.
    74       *
    75       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    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                  // TODO: handle exception
    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      * Imports a URL into Chainsaw, by using the Decoder, and
    114      * using the name value as the Application key which (usually) determines
    115      * the Tab name
    116      * @param name
    117      * @param url URL to import
    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                     // TODO Handle the error with a nice msg
    136                     LOG.error(e1);
    137                 }
    138                 MRUFileList.log4jMRU().opened(urlToUse);
    139             }
    140         }).start();
    141     }
    142 }