logging-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From psm...@apache.org
Subject svn commit: r568852 - in /logging/chainsaw/trunk/src: changes/changes.xml main/java/org/apache/log4j/chainsaw/LogUI.java main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java
Date Thu, 23 Aug 2007 07:10:36 GMT
Author: psmith
Date: Thu Aug 23 00:10:35 2007
New Revision: 568852

URL: http://svn.apache.org/viewvc?rev=568852&view=rev
Log:
Bug 43158 applied patch provided by Isuru to allow last set of configured Receivers to be
remmebered.

Modified:
    logging/chainsaw/trunk/src/changes/changes.xml
    logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogUI.java
    logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java
    logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java

Modified: logging/chainsaw/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/chainsaw/trunk/src/changes/changes.xml?rev=568852&r1=568851&r2=568852&view=diff
==============================================================================
--- logging/chainsaw/trunk/src/changes/changes.xml (original)
+++ logging/chainsaw/trunk/src/changes/changes.xml Thu Aug 23 00:10:35 2007
@@ -30,7 +30,9 @@
       <action dev="psmith" issue="42883" type="add" due-to="Isuru Suriarachchi" due-to-email="">
 		Added ability to hide Welcome and Drag and Drop panels.
       </action>
-
+	<action dev="psmith" issue="43158" type="add due-to="Isuru Suriarachchi" due-to-email="">
+		Added ability to remember last created receivers in an xml file, and load them at startup.
+	</action>
     </release>
   </body>
 </document>

Modified: logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogUI.java
URL: http://svn.apache.org/viewvc/logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogUI.java?rev=568852&r1=568851&r2=568852&view=diff
==============================================================================
--- logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogUI.java (original)
+++ logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/LogUI.java Thu Aug 23 00:10:35
2007
@@ -923,6 +923,7 @@
     getSettingsManager().addSettingsListener(this);
     getSettingsManager().addSettingsListener(new ApplicationPreferenceModelSaver(applicationPreferenceModel));
     getSettingsManager().addSettingsListener(MRUFileListPreferenceSaver.getInstance());
+    getSettingsManager().addSettingsListener(receiversPanel);
     getSettingsManager().loadSettings();
 
     setVisible(true);
@@ -1399,9 +1400,14 @@
               MessageCenter.getInstance().getLogger().info(
                 "An error occurred creating your Receiver");
             }
-          } else if (noReceiversWarningPanel.getModel().isLoadConfig()) {
-            final URL url =
-              noReceiversWarningPanel.getModel().getConfigToLoad();
+          } else if (noReceiversWarningPanel.getModel().isLoadConfig() ||
+                  noReceiversWarningPanel.getModel().isLoadSavedConfigs()) {
+            final URL url;
+            if (noReceiversWarningPanel.getModel().isLoadSavedConfigs()) {
+                url = noReceiversWarningPanel.getModel().getSavedConfigToLoad();
+            } else {
+                url = noReceiversWarningPanel.getModel().getConfigToLoad();
+            }
 
             if (url != null) {
               MessageCenter.getInstance().getLogger().debug(

Modified: logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java
URL: http://svn.apache.org/viewvc/logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java?rev=568852&r1=568851&r2=568852&view=diff
==============================================================================
--- logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java
(original)
+++ logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/NoReceiversWarningPanel.java
Thu Aug 23 00:10:35 2007
@@ -52,6 +52,7 @@
 
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
+import org.apache.log4j.chainsaw.prefs.SettingsManager;
 import org.apache.log4j.net.PortBased;
 import org.apache.log4j.net.SocketAppender;
 import org.apache.log4j.net.SocketHubReceiver;
@@ -77,6 +78,8 @@
             "I'm fine thanks, don't worry");
     private final JRadioButton searchOption = new JRadioButton(
             "Let me search for a configuration file");
+    private final JRadioButton chainsawSavedConfigOption = new JRadioButton(
+            "Load configuration file saved by Chainsaw");
     private final JRadioButton manualOption = new JRadioButton(
             "Let me define Receivers manually");
     private final JButton okButton = new JButton("Ok");
@@ -144,7 +147,7 @@
         add(Box.createVerticalStrut(20), gc);
 
         JPanel optionpanel = new JPanel();
-        optionpanel.setLayout(new GridLayout(4, 1, 3, 3));
+        optionpanel.setLayout(new GridLayout(5, 1, 3, 3));
         optionpanel.setBackground(getBackground());
         optionpanel.setBorder(BorderFactory.createEtchedBorder());
 
@@ -158,6 +161,12 @@
             "Allows you to choose a Log4J Configuration file that contains Receiver definitions");
 
         searchOption.setMnemonic('S');
+        
+        chainsawSavedConfigOption.setToolTipText(
+             "Allows you to load Receiver definitions saved by Chanisaw previously");
+ 
+        chainsawSavedConfigOption.setMnemonic('C');
+
 
         manualOption.setToolTipText(
             "Opens the Receivers panel so you can define them via a GUI");
@@ -174,10 +183,12 @@
         justLoadingFile.setOpaque(false);
 
         optionGroup.add(searchOption);
+        optionGroup.add(chainsawSavedConfigOption);
         optionGroup.add(manualOption);
         optionGroup.add(justLoadingFile);
         optionGroup.add(simpleReceiver);
 
+        chainsawSavedConfigOption.setEnabled(getModel().isChinsawConfigFileExists());
 
         gc.gridy = 3;
 
@@ -373,6 +384,7 @@
             };
 
         searchOption.addActionListener(al);
+        chainsawSavedConfigOption.addActionListener(al);
         manualOption.addActionListener(al);
         justLoadingFile.addActionListener(al);
         simpleReceiver.addActionListener(al);
@@ -415,6 +427,7 @@
         optionpanel.add(justLoadingFile);
         optionpanel.add(simpleSocketPanel);
         optionpanel.add(searchOptionPanel);
+        optionpanel.add(chainsawSavedConfigOption);
         optionpanel.add(manualOption);
 
         add(optionpanel, gc);
@@ -531,6 +544,11 @@
     class PanelModel {
 
         private URL configUrl;
+        private File file;
+
+        public PanelModel(){
+            file = new File(SettingsManager.getInstance().getSettingsDirectory(), "receiver-configs.xml");
+        }
 
         boolean isLoadLogFile() {
 
@@ -557,6 +575,11 @@
             return searchOption.isSelected();
         }
 
+        boolean isLoadSavedConfigs() {
+
+            return chainsawSavedConfigOption.isSelected();
+        }
+
         boolean isManualMode() {
 
             return manualOption.isSelected();
@@ -588,6 +611,25 @@
         URL getConfigToLoad() {
 
             return configUrl;
+        }
+
+        boolean isChinsawConfigFileExists(){
+
+            return file.exists();
+
+        }
+
+        URL getSavedConfigToLoad() {
+            try {
+                if (file.exists()){
+                    return file.toURL();
+                } else {
+                    logger.debug("No configuration file found");
+                }
+            } catch (MalformedURLException e) {
+                logger.error("Error laoding saved configurations by Chainsaw", e);
+            }
+            return null;
         }
     }
 }

Modified: logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java
URL: http://svn.apache.org/viewvc/logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java?rev=568852&r1=568851&r2=568852&view=diff
==============================================================================
--- logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java
(original)
+++ logging/chainsaw/trunk/src/main/java/org/apache/log4j/chainsaw/receivers/ReceiversPanel.java
Thu Aug 23 00:10:35 2007
@@ -24,11 +24,18 @@
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.io.File;
+import java.io.FileOutputStream;
+
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.BorderFactory;
@@ -57,12 +64,22 @@
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.ExpandVetoException;
 import javax.swing.tree.TreePath;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.apache.log4j.chainsaw.PopupListener;
 import org.apache.log4j.chainsaw.SmallButton;
+import org.apache.log4j.chainsaw.prefs.SettingsManager;
+import org.apache.log4j.chainsaw.prefs.SettingsListener;
+import org.apache.log4j.chainsaw.prefs.SaveSettingsEvent;
+import org.apache.log4j.chainsaw.prefs.LoadSettingsEvent;
 import org.apache.log4j.chainsaw.help.HelpManager;
 import org.apache.log4j.chainsaw.helper.SwingHelper;
 import org.apache.log4j.chainsaw.icons.ChainsawIcons;
@@ -79,6 +96,8 @@
 import org.apache.log4j.plugins.Receiver;
 import org.apache.log4j.spi.LoggerRepository;
 import org.apache.log4j.spi.LoggerRepositoryEx;
+import org.w3c.dom.Element;
+import org.w3c.dom.Document;
 
 
 /**
@@ -88,7 +107,7 @@
  * @author Paul Smith <psmith@apache.org>
  * @author Scott Deboy <sdeboy@apache.org>
  */
-public class ReceiversPanel extends JPanel {
+public class ReceiversPanel extends JPanel implements SettingsListener {
   final Action newReceiverButtonAction;
   final Action pauseReceiverButtonAction;
   final Action playReceiverButtonAction;
@@ -609,6 +628,76 @@
     boolean oldValue = isVisible();
     super.setVisible(aFlag);
     firePropertyChange("visible", oldValue, isVisible());
+  }
+
+  public void loadSettings(LoadSettingsEvent event){}
+
+   /**
+    * Saves all the receivers which are active at shut down as a configuration
+    * file which can be loaded when Chainsaw will be restarted.
+    */
+
+  public void saveSettings(SaveSettingsEvent event){
+      List pluginList = pluginRegistry.getPlugins();
+
+      try {
+          if (pluginList.size() != 0) {
+              File file = new File(SettingsManager.getInstance().getSettingsDirectory(),
"receiver-configs.xml");
+              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+              factory.setNamespaceAware(true);
+              DocumentBuilder builder = factory.newDocumentBuilder();
+              Document document = builder.newDocument();
+              Element rootElement = document.createElementNS("http://jakarta.apache.org/log4j/",
"configuration");
+              rootElement.setPrefix("log4j");
+              rootElement.setAttribute("xmlns:log4j", "http://jakarta.apache.org/log4j/");
+              rootElement.setAttribute("debug", "true");
+
+              for (int i = 0; i < pluginList.size(); i++) {
+                  Receiver receiver;
+
+                  if (pluginList.get(i) instanceof Receiver) {
+                      receiver = (Receiver) pluginList.get(i);
+                  } else {
+                      continue;
+                  }
+
+                  Element pluginElement = document.createElement("plugin");
+                  pluginElement.setAttribute("name", receiver.getName());
+                  pluginElement.setAttribute("class", receiver.getClass().getName());
+
+                  BeanInfo beanInfo = Introspector.getBeanInfo(receiver.getClass());
+                  List list = new ArrayList(Arrays.asList(beanInfo.getPropertyDescriptors()));
+
+                  for (int j = 0; j < list.size(); j++) {
+                      PropertyDescriptor d = (PropertyDescriptor) list.get(j);
+
+                      Object o = d.getReadMethod().invoke(receiver, new Object[] {} );
+                      if (o != null) {
+                          Element paramElement = document.createElement("param");
+                          paramElement.setAttribute("name", d.getName());
+                          paramElement.setAttribute("value", o.toString());
+                          pluginElement.appendChild(paramElement);
+                      }
+                  }
+
+                  rootElement.appendChild(pluginElement);
+
+              }
+
+              TransformerFactory transformerFactory = TransformerFactory.newInstance();
+              Transformer transformer = transformerFactory.newTransformer();
+              DOMSource source = new DOMSource(rootElement);
+              FileOutputStream stream = new FileOutputStream(file);
+              StreamResult result = new StreamResult(stream);
+              transformer.transform(source, result);
+              stream.close();
+          }
+
+      } catch (Exception e) {
+          e.printStackTrace();
+          logger.error("Error while writing receiver configurations to the configuration
file");
+      }
+
   }
 
   /**



Mime
View raw message