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.Toolkit;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.KeyEvent;
23  import java.io.BufferedOutputStream;
24  import java.io.BufferedWriter;
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.FileWriter;
28  import java.io.IOException;
29  import java.io.OutputStreamWriter;
30  import java.io.Writer;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.zip.ZipEntry;
35  import java.util.zip.ZipOutputStream;
36  
37  import javax.swing.AbstractAction;
38  import javax.swing.Action;
39  import javax.swing.ImageIcon;
40  import javax.swing.JFileChooser;
41  import javax.swing.KeyStroke;
42  
43  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
44  import org.apache.log4j.helpers.LogLog;
45  import org.apache.log4j.xml.XMLLayout;
46  
47  
48  /**
49   * Allows the user to specify a particular file to which the current tab's
50   * displayed events will be saved.
51   *
52   * @author Scott Deboy <sdeboy@apache.org>
53   * @author Paul Smith <psmith@apache.org>
54   * @author Stephen Pain
55   */
56  class FileSaveAction extends AbstractAction {
57    private LogUI parent;
58    private JFileChooser chooser = null;
59  
60    /**
61     * This action must have a reference to a LogUI
62     * in order to retrieve events to save
63     *
64     */
65    public FileSaveAction(LogUI parent) {
66      super("Save displayed events as...");
67  
68      putValue(
69        Action.ACCELERATOR_KEY,
70        KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
71      putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
72      putValue(
73        Action.SHORT_DESCRIPTION, "Saves displayed events for the current tab");
74      putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_SAVE_AS));
75      this.parent = parent;
76    }
77    
78    /*
79     * When the user chooses the Save action,
80     * a File chooser is presented to allow them to
81     * find an XML file to save events to.
82     *
83     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
84     */
85    public void actionPerformed(ActionEvent e) {
86  
87      if( chooser == null ){
88        chooser = new JFileChooser();
89      }
90      
91      chooser.setAcceptAllFileFilterUsed(true);
92      chooser.setDialogTitle("Save displayed events (XML or .zipped XML)...");
93      chooser.showSaveDialog(parent);
94  
95      File selectedFile = chooser.getSelectedFile();
96  
97      if (selectedFile != null) {
98      List v = parent.getCurrentLogPanel().getFilteredEvents();
99  
100     if (((v != null) && (v.size() == 0)) || (v == null)) {
101       //no events to save
102       return;
103     }
104 
105     XMLLayout layout = new XMLLayout();
106     layout.setProperties(true);
107     boolean saveAsZip = selectedFile.getName().toLowerCase(Locale.ENGLISH).endsWith(".zip");
108     Writer writer = null;
109     try {
110       if (saveAsZip) {
111           ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(selectedFile)));
112           ZipEntry entry = new ZipEntry(selectedFile.getName().substring(0, selectedFile.getName().length() - ".zip".length()) + ".xml");
113           zipOutput.putNextEntry(entry);
114           writer = new OutputStreamWriter(zipOutput);
115       } else {
116           writer = new BufferedWriter(new FileWriter(selectedFile));
117       }
118       Iterator iter = v.iterator();
119       while (iter.hasNext()) {
120         LoggingEventWrapper loggingEventWrapper = (LoggingEventWrapper) iter.next();
121         layout.setLocationInfo(loggingEventWrapper.getLoggingEvent().getThrowableInformation() != null);
122         writer.write(layout.format(loggingEventWrapper.getLoggingEvent()));
123       }
124     } catch (IOException ioe) {
125       LogLog.warn("Unable to save file", ioe);
126     } finally {
127       if (writer != null) {
128           try {
129               writer.flush();
130               writer.close();
131           }
132           catch (IOException e1) {
133               //ignore
134           }
135       }
136     }
137   }
138   }
139 }