Skip to content

Commit

Permalink
Receivers can be saved/loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
rm5248 committed Oct 13, 2023
1 parent 5ccb3c8 commit 26146fe
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/apache/log4j/chainsaw/ChainsawReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -67,4 +67,6 @@ public void removePropertyChangeListener(
public void removePropertyChangeListener(
final String propertyName,
final PropertyChangeListener listener);

// public void setConfiguration(AbstractConfiguration conf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/apache/log4j/chainsaw/FileMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @author Scott Deboy <[email protected]>
*/
class FileMenu extends JMenu {
private final Action loadConfigAction;
private final Action loadReceiverAction;
private final Action exitAction;
private final Action loadLog4JAction;
private final Action loadUtilLoggingAction;
Expand All @@ -54,9 +54,9 @@ public FileMenu(final LogUI logUI) {
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();
}
};

Expand Down Expand Up @@ -87,7 +87,7 @@ public void actionPerformed(ActionEvent actionEvent) {

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);
Expand All @@ -110,7 +110,7 @@ public void actionPerformed(ActionEvent e) {

JMenuItem menuItemExit = new JMenuItem(exitAction);

// add(loadChainsawConfig);
add(loadReceiver);
// add(loadLog4JFile);
// add(loadUtilLoggingFile);
// addSeparator();
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/org/apache/log4j/chainsaw/LogUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;


Expand Down Expand Up @@ -1203,6 +1204,10 @@ private void showReceiverConfigurationPanel() {
* Exits the application, ensuring Settings are saved.
*/
public boolean exit() {
for(ChainsawReceiver rx : m_receivers){
getSettingsManager().saveSettingsForReceiver(rx);
}

getSettingsManager().saveAllSettings();

return shutdown();
Expand Down Expand Up @@ -1234,8 +1239,36 @@ public void showApplicationPreferences() {
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() {
Expand Down
70 changes: 68 additions & 2 deletions src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +40,8 @@
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;

Expand All @@ -59,6 +67,10 @@ private class TabSettingsData{
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
Expand Down Expand Up @@ -94,10 +106,21 @@ private 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{
Expand Down Expand Up @@ -176,7 +199,7 @@ public AbstractConfiguration getSettingsForReceiverTab(String identifier){
return null;
}

public File getSettingsDirectory() {
public static File getSettingsDirectory() {
return new File(System.getProperty("user.home"), ".chainsaw");
}

Expand All @@ -189,6 +212,7 @@ public void saveGlobalSettings(){
}

public void saveAllSettings(){
logger.info("Saving all settings");
try{
m_builder.save();
}catch( ConfigurationException ex ){
Expand All @@ -206,4 +230,46 @@ public void saveAllSettings(){
}
}
}

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);
}
}
}
}

0 comments on commit 26146fe

Please sign in to comment.