This is an automated email from the ASF dual-hosted git repository. rmiddleton pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-chainsaw.git
The following commit(s) were added to refs/heads/master by this push: new 26146fe Receivers can be saved/loaded 26146fe is described below commit 26146fe4b4d848948b620d3a3fcc8aa11f284428 Author: Robert Middleton <robert.middle...@rm5248.com> AuthorDate: Thu Oct 12 23:25:32 2023 -0400 Receivers can be saved/loaded --- .../apache/log4j/chainsaw/ChainsawReceiver.java | 6 +- .../log4j/chainsaw/ChainsawReceiverSkeleton.java | 1 + .../java/org/apache/log4j/chainsaw/FileMenu.java | 10 ++-- src/main/java/org/apache/log4j/chainsaw/LogUI.java | 37 +++++++++++- .../log4j/chainsaw/prefs/SettingsManager.java | 70 +++++++++++++++++++++- 5 files changed, 113 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiver.java b/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiver.java index 2080359..69f34e7 100644 --- a/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiver.java +++ b/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiver.java @@ -17,11 +17,11 @@ package org.apache.log4j.chainsaw; import java.beans.PropertyChangeListener; +import org.apache.commons.configuration2.AbstractConfiguration; import org.apache.log4j.chainsaw.logevents.Level; /** - * A receiver receives log events from a source. A ChainsawReceiver will create - * from 1...N ChainsawReceiverNodes + * A receiver receives log events from a source. */ public interface ChainsawReceiver { @@ -67,4 +67,6 @@ public interface ChainsawReceiver { public void removePropertyChangeListener( final String propertyName, final PropertyChangeListener listener); + +// public void setConfiguration(AbstractConfiguration conf); } diff --git a/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiverSkeleton.java b/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiverSkeleton.java index 687b06e..589b32b 100644 --- a/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiverSkeleton.java +++ b/src/main/java/org/apache/log4j/chainsaw/ChainsawReceiverSkeleton.java @@ -20,6 +20,7 @@ import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.util.ArrayList; import java.util.List; +import org.apache.commons.configuration2.AbstractConfiguration; import org.apache.log4j.chainsaw.logevents.ChainsawLoggingEvent; import org.apache.log4j.chainsaw.logevents.Level; diff --git a/src/main/java/org/apache/log4j/chainsaw/FileMenu.java b/src/main/java/org/apache/log4j/chainsaw/FileMenu.java index 9278463..9dad1bc 100644 --- a/src/main/java/org/apache/log4j/chainsaw/FileMenu.java +++ b/src/main/java/org/apache/log4j/chainsaw/FileMenu.java @@ -42,7 +42,7 @@ import java.net.URL; * @author Scott Deboy <sde...@apache.org> */ class FileMenu extends JMenu { - private final Action loadConfigAction; + private final Action loadReceiverAction; private final Action exitAction; private final Action loadLog4JAction; private final Action loadUtilLoggingAction; @@ -54,9 +54,9 @@ class FileMenu extends JMenu { super("File"); setMnemonic(KeyEvent.VK_F); - loadConfigAction = new AbstractAction("Load Chainsaw configuration") { + loadReceiverAction = new AbstractAction("Load Receiver") { public void actionPerformed(ActionEvent actionEvent) { - logUI.showReceiverConfiguration(); + logUI.loadReceiver(); } }; @@ -87,7 +87,7 @@ class FileMenu extends JMenu { saveAction = new FileSaveAction(logUI); - JMenuItem loadChainsawConfig = new JMenuItem(loadConfigAction); + JMenuItem loadReceiver = new JMenuItem(loadReceiverAction); JMenuItem loadLog4JFile = new JMenuItem(loadLog4JAction); JMenuItem loadUtilLoggingFile = new JMenuItem(loadUtilLoggingAction); JMenuItem remoteLog4JFile = new JMenuItem(remoteLog4JAction); @@ -110,7 +110,7 @@ class FileMenu extends JMenu { JMenuItem menuItemExit = new JMenuItem(exitAction); -// add(loadChainsawConfig); + add(loadReceiver); // add(loadLog4JFile); // add(loadUtilLoggingFile); // addSeparator(); diff --git a/src/main/java/org/apache/log4j/chainsaw/LogUI.java b/src/main/java/org/apache/log4j/chainsaw/LogUI.java index 9e76b2d..08b0867 100644 --- a/src/main/java/org/apache/log4j/chainsaw/LogUI.java +++ b/src/main/java/org/apache/log4j/chainsaw/LogUI.java @@ -80,6 +80,7 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.ServiceLoader; import java.util.Set; @@ -1203,6 +1204,10 @@ public class LogUI extends JFrame { * Exits the application, ensuring Settings are saved. */ public boolean exit() { + for(ChainsawReceiver rx : m_receivers){ + getSettingsManager().saveSettingsForReceiver(rx); + } + getSettingsManager().saveAllSettings(); return shutdown(); @@ -1234,8 +1239,36 @@ public class LogUI extends JFrame { preferencesFrame.setVisible(true); } - public void showReceiverConfiguration() { - showReceiverConfigurationPanel(); + public void loadReceiver() { + Runnable r = () -> { + JFileChooser jfc = new JFileChooser(SettingsManager.getSettingsDirectory()); + int returnVal = jfc.showOpenDialog(this); + if(returnVal != JFileChooser.APPROVE_OPTION) { + return; + } + + logger.debug("Load file {}", jfc.getSelectedFile()); + + // Create the receiver + String fileToLoad = jfc.getSelectedFile().getName(); + String receiverName = fileToLoad.split( "-" )[0]; + AbstractConfiguration config = SettingsManager.getInstance().getSettingsForReceiverTab(receiverName); + String typeToLoad = config.getString("receiver.type"); + ServiceLoader<ChainsawReceiverFactory> sl = ServiceLoader.load(ChainsawReceiverFactory.class); + + for( ChainsawReceiverFactory crFactory : sl ){ + if(crFactory.getReceiverName().equals(typeToLoad)){ + ChainsawReceiver rx = crFactory.create(); + rx.setName(receiverName); + SettingsManager.getInstance().loadSettingsForReceiver(rx); + addReceiver(rx); + + rx.start(); + } + } + }; + + SwingUtilities.invokeLater(r); } public void showAboutBox() { diff --git a/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java b/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java index cb41cf2..a1cb3c1 100644 --- a/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java +++ b/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java @@ -16,14 +16,20 @@ */ package org.apache.log4j.chainsaw.prefs; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; import javax.swing.event.EventListenerList; import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.net.URLEncoder; import java.util.EventListener; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.ServiceLoader; +import java.util.logging.Level; import org.apache.commons.configuration2.AbstractConfiguration; import org.apache.commons.configuration2.CombinedConfiguration; import org.apache.commons.configuration2.PropertiesConfiguration; @@ -34,6 +40,8 @@ import org.apache.commons.configuration2.builder.fluent.Parameters; import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.configuration2.tree.OverrideCombiner; +import org.apache.log4j.chainsaw.ChainsawReceiver; +import org.apache.log4j.chainsaw.ChainsawReceiverFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -59,6 +67,10 @@ public final class SettingsManager { private FileBasedConfigurationBuilder<PropertiesConfiguration> m_builder; private Map<String,TabSettingsData> m_tabSettings; + private final Map<Class,PropertyDescriptor[]> m_classToProperties = + new HashMap<>(); + private final Map<Class, String> m_classToName = new HashMap<>(); + /** * Initialises the SettingsManager by loading the default Properties from * a resource @@ -94,10 +106,21 @@ public final class SettingsManager { PropertiesConfiguration config = m_builder.getConfiguration(); m_configuration = config; m_builder.getFileHandler().setFile(f); - return; }catch( ConfigurationException ex ){ } + ServiceLoader<ChainsawReceiverFactory> sl = ServiceLoader.load(ChainsawReceiverFactory.class); + + for( ChainsawReceiverFactory crFactory : sl ){ + ChainsawReceiver rx = crFactory.create(); + try { + m_classToProperties.put(rx.getClass(), crFactory.getPropertyDescriptors()); + m_classToName.put(rx.getClass(), crFactory.getReceiverName()); + } catch (IntrospectionException ex) { + logger.error(ex); + } + } + // If we get here, it is likely that we have not opened the file. // Force a save to create the file // try{ @@ -176,7 +199,7 @@ public final class SettingsManager { return null; } - public File getSettingsDirectory() { + public static File getSettingsDirectory() { return new File(System.getProperty("user.home"), ".chainsaw"); } @@ -189,6 +212,7 @@ public final class SettingsManager { } public void saveAllSettings(){ + logger.info("Saving all settings"); try{ m_builder.save(); }catch( ConfigurationException ex ){ @@ -206,4 +230,46 @@ public final class SettingsManager { } } } + + public void saveSettingsForReceiver(ChainsawReceiver rx){ + PropertyDescriptor[] desc = m_classToProperties.get(rx.getClass()); + + if(desc == null){ + return; + } + + AbstractConfiguration config = getSettingsForReceiverTab(rx.getName()); + + config.setProperty("receiver.type", m_classToName.get(rx.getClass())); + + for(PropertyDescriptor d : desc){ + Method readMethod = d.getReadMethod(); + + try{ + config.setProperty("receiver." + d.getDisplayName(), readMethod.invoke(rx)); + }catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex){ + logger.error(ex); + } + } + } + + public void loadSettingsForReceiver(ChainsawReceiver rx){ + PropertyDescriptor[] desc = m_classToProperties.get(rx.getClass()); + + if(desc == null){ + return; + } + + AbstractConfiguration config = getSettingsForReceiverTab(rx.getName()); + + for(PropertyDescriptor d : desc){ + Method writeMethod = d.getWriteMethod(); + + try{ + writeMethod.invoke(rx, config.get(d.getPropertyType(), "receiver." + d.getDisplayName())); + }catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex){ + logger.error(ex); + } + } + } }