dain 2004/06/25 13:48:54
Modified: modules/assembly/src/plan deployer-system-plan.xml system-plan.xml modules/system/src/test/org/apache/geronimo/system/url/file FileProtocolTest.java modules/system/src/java/org/apache/geronimo/system/url/file Handler.java modules/system/src/java/org/apache/geronimo/system/logging/log4j Log4jService.java URLConfigurator.java Log: Fixed broken logging code and configuration Geronimo's logging config can be overriden with a file at var/log/log4j.xml Revision Changes Path 1.3 +4 -3 incubator-geronimo/modules/assembly/src/plan/deployer-system-plan.xml Index: deployer-system-plan.xml =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/assembly/src/plan/deployer-system-plan.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- deployer-system-plan.xml 10 Mar 2004 09:58:19 -0000 1.2 +++ deployer-system-plan.xml 25 Jun 2004 20:48:53 -0000 1.3 @@ -45,9 +45,10 @@ <!-- Logging service --> <gbean name="geronimo.deployment:role=Logger" class="org.apache.geronimo.system.logging.log4j.Log4jService"> - <attribute name="RefreshPeriod" type="int">60</attribute> - <attribute name="ConfigurationURL" type="java.net.URL">log4j.xml</attribute> - <reference name="ServerInfo">geronimo.deployment:role=ServerInfo</reference> + <attribute name="rootLoggerLevel" type="java.lang.String">DEBUG</attribute> + <attribute name="refreshPeriod" type="int">60</attribute> + <attribute name="configurationFile" type="java.lang.String">var/log/log4j.xml</attribute> + <reference name="ServerInfo">geronimo.system:role=ServerInfo</reference> </gbean> <gbean name="geronimo.deployments:role=LogAppender,type=ConsoleAppender" class="org.apache.geronimo.system.logging.log4j.appender.ConsoleAppenderService"> 1.10 +3 -2 incubator-geronimo/modules/assembly/src/plan/system-plan.xml Index: system-plan.xml =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/assembly/src/plan/system-plan.xml,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- system-plan.xml 5 Jun 2004 16:29:29 -0000 1.9 +++ system-plan.xml 25 Jun 2004 20:48:53 -0000 1.10 @@ -57,8 +57,9 @@ <!-- Logging service --> <gbean name="geronimo.system:role=Logger" class="org.apache.geronimo.system.logging.log4j.Log4jService"> - <attribute name="RefreshPeriod" type="int">60</attribute> - <attribute name="ConfigurationURL" type="java.net.URL">log4j.xml</attribute> + <attribute name="rootLoggerLevel" type="java.lang.String">DEBUG</attribute> + <attribute name="refreshPeriod" type="int">60</attribute> + <attribute name="configurationFile" type="java.lang.String">var/log/log4j.xml</attribute> <reference name="ServerInfo">geronimo.system:role=ServerInfo</reference> </gbean> 1.3 +3 -2 incubator-geronimo/modules/system/src/test/org/apache/geronimo/system/url/file/FileProtocolTest.java Index: FileProtocolTest.java =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/system/src/test/org/apache/geronimo/system/url/file/FileProtocolTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- FileProtocolTest.java 10 Mar 2004 09:59:31 -0000 1.2 +++ FileProtocolTest.java 25 Jun 2004 20:48:53 -0000 1.3 @@ -61,7 +61,8 @@ } public void testURLConnectionType() throws Exception { - URL url = new URL("file:/some/file"); + File tempFile = File.createTempFile("foo", "bar"); + URL url = new URL(tempFile.toURL().toExternalForm()); URLConnection c = url.openConnection(); assertEquals(FileURLConnection.class, c.getClass()); } 1.3 +4 -1 incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/url/file/Handler.java Index: Handler.java =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/url/file/Handler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Handler.java 10 Mar 2004 09:59:31 -0000 1.2 +++ Handler.java 25 Jun 2004 20:48:53 -0000 1.3 @@ -46,6 +46,9 @@ String path = ParseUtil.decode(url.getPath()); path = path.replace('/', File.separatorChar).replace('|', ':'); File file = new File(path); + if (!file.exists()) { + throw new FileNotFoundException(file.toString()); + } // Handle the hostname of the URL if given, puke if not valid String hostname = url.getHost(); 1.7 +96 -40 incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/logging/log4j/Log4jService.java Index: Log4jService.java =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/logging/log4j/Log4jService.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- Log4jService.java 5 Jun 2004 07:53:22 -0000 1.6 +++ Log4jService.java 25 Jun 2004 20:48:54 -0000 1.7 @@ -43,7 +43,7 @@ /** * The URL to the configuration file. */ - private URL configURL; + private String configurationFile; /** * The time (in seconds) between checking for new config. @@ -71,19 +71,57 @@ private long lastChanged = -1; /** + * The level used for the root logger + */ + private Level rootLoggerLevel; + + /** + * Is this service running? + */ + private boolean running = false; + + /** * Construct a <code>Log4jService</code>. * - * @param configURL The configuration URL. + * @param configurationFile The log4j configuration file. * @param refreshPeroid The refresh refreshPeroid (in seconds). */ - public Log4jService(final URL configURL, final int refreshPeroid, ServerInfo serverInfo) { - setRefreshPeriod(refreshPeroid); - setConfigurationURL(configURL); - this.serverInfo = serverInfo; + public Log4jService(final String rootLoggerLevel, final String configurationFile, final int refreshPeroid, ServerInfo serverInfo) { LogFactory logFactory = LogFactory.getFactory(); if (!(logFactory instanceof GeronimoLogFactory)) { throw new IllegalStateException("Commons log factory is not a GeronimoLogFactory"); } + this.rootLoggerLevel = XLevel.toLevel(rootLoggerLevel); + this.refreshPeriod = refreshPeroid; + this.configurationFile = configurationFile; + this.serverInfo = serverInfo; + } + + /** + * Gets the level of the root logger. + */ + public synchronized String getRootLoggerLevel() { + if (rootLoggerLevel != null) { + return rootLoggerLevel.toString(); + } + + return null; + } + + /** + * Sets the level of the root logger. + * + * @param level The level to change the logger to. + */ + public synchronized void setRootLoggerLevel(final String level) { + if (level == null) { + rootLoggerLevel = null; + } else { + rootLoggerLevel = XLevel.toLevel(level); + if (running) { + Logger.getRootLogger().setLevel(rootLoggerLevel); + } + } } /** @@ -153,58 +191,64 @@ * * @return the logging configuration URL */ - public synchronized URL getConfigurationURL() { - return configURL; + public synchronized String getConfigurationFile() { + return configurationFile; } /** * Set the logging configuration URL. * - * @param url the logging configuration URL + * @param configurationFile the logging configuration file */ - public synchronized void setConfigurationURL(final URL url) { - if (url == null) { - throw new IllegalArgumentException("url is null"); + public synchronized void setConfigurationFile(final String configurationFile) { + if (this.configurationFile == null) { + throw new IllegalArgumentException("configurationFile is null"); } - this.configURL = url; + this.configurationFile = configurationFile; } /** * Force the logging system to reconfigure. */ public void reconfigure() { - URL url; - synchronized (this) { - url = configURL; + URL url = resolveURL(); + if (url == null) { + return; } URLConfigurator.configure(url); } - private void schedule() { + private synchronized void schedule() { if (timer != null) { - TimerTask task; - synchronized (this) { - // kill the old monitor - if (monitor != null) { - monitor.cancel(); - } - - // start the new one - monitor = new URLMonitorTask(); - task = monitor; - timer.schedule(monitor, 1000 * refreshPeriod, 1000 * refreshPeriod); + // kill the old monitor + if (monitor != null) { + monitor.cancel(); } + + // start the new one + monitor = new URLMonitorTask(); + TimerTask task = monitor; + timer.schedule(monitor, 1000 * refreshPeriod, 1000 * refreshPeriod); task.run(); } } public void doStart() { - // Peroidally check the configuration file - schedule(); + synchronized (this) { + // Peroidally check the configuration file + schedule(); - // Make sure the root Logger has loaded - Logger.getRootLogger(); + // Make sure the root Logger has loaded + Logger.getRootLogger(); + + // set the root logger level + if (rootLoggerLevel != null) { + Logger.getRootLogger().setLevel(rootLoggerLevel); + } + + reconfigure(); + } // Change all of the loggers over to use log4j GeronimoLogFactory logFactory = (GeronimoLogFactory) LogFactory.getFactory(); @@ -213,9 +257,14 @@ logFactory.setLogFactory(new CachingLog4jLogFactory()); } } + + synchronized (this) { + running = true; + } } public synchronized void doStop() { + running = false; if (monitor != null) { monitor.cancel(); monitor = null; @@ -232,9 +281,7 @@ private synchronized URL resolveURL() { try { - URI configURI = null; - URI baseURI = new URI(serverInfo.getBaseDirectory()); - return baseURI.resolve(configURI).toURL(); + return serverInfo.resolve(URI.create(configurationFile)).toURL(); } catch (Exception e) { return null; } @@ -245,7 +292,15 @@ try { long lastModified; synchronized (this) { - URLConnection connection = resolveURL().openConnection(); + if (running == false) { + return; + } + + URL url = resolveURL(); + if (url == null) { + return; + } + URLConnection connection = url.openConnection(); lastModified = connection.getLastModified(); } @@ -290,8 +345,9 @@ static { GBeanInfoFactory infoFactory = new GBeanInfoFactory(Log4jService.class); - infoFactory.addAttribute("ConfigurationURL", URL.class, true); - infoFactory.addAttribute("RefreshPeriod", int.class, true); + infoFactory.addAttribute("rootLoggerLevel", String.class, true); + infoFactory.addAttribute("configurationFile", String.class, true); + infoFactory.addAttribute("refreshPeriod", int.class, true); infoFactory.addReference("ServerInfo", ServerInfo.class); @@ -299,7 +355,7 @@ infoFactory.addOperation("setLoggerLevel", new Class[]{String.class, String.class}); infoFactory.addOperation("getLoggerLevel", new Class[]{String.class}); - infoFactory.setConstructor(new String[]{"ConfigurationURL", "RefreshPeriod", "ServerInfo"}); + infoFactory.setConstructor(new String[]{"rootLoggerLevel", "configurationFile", "refreshPeriod", "ServerInfo"}); GBEAN_INFO = infoFactory.getBeanInfo(); } 1.4 +12 -5 incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/logging/log4j/URLConfigurator.java Index: URLConfigurator.java =================================================================== RCS file: /home/cvs/incubator-geronimo/modules/system/src/java/org/apache/geronimo/system/logging/log4j/URLConfigurator.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- URLConfigurator.java 10 Mar 2004 09:59:30 -0000 1.3 +++ URLConfigurator.java 25 Jun 2004 20:48:54 -0000 1.4 @@ -20,6 +20,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.FileNotFoundException; import java.net.URL; import java.net.URLConnection; @@ -36,15 +37,14 @@ * * @version $Revision$ $Date$ */ -public class URLConfigurator - implements Configurator { +public class URLConfigurator implements Configurator { private static final Log log = LogFactory.getLog(URLConfigurator.class); public static void configure(final URL url) { new URLConfigurator().doConfigure(url, LogManager.getLoggerRepository()); } - private Configurator getConfigurator(final URL url) { + private Configurator getConfigurator(final URL url) throws FileNotFoundException { String contentType = null; // Get the content type to see if it is XML or not @@ -55,6 +55,8 @@ if (log.isTraceEnabled()) { log.trace("Content type: " + contentType); } + } catch (FileNotFoundException e) { + throw e; } catch (IOException e) { log.warn("Could not determine content type from URL; ignoring", e); } @@ -99,7 +101,12 @@ } // Get the config delegate and target repository to config with - Configurator delegate = getConfigurator(url); + Configurator delegate = null; + try { + delegate = getConfigurator(url); + } catch (FileNotFoundException e) { + return; + } if (log.isTraceEnabled()) { log.trace("Configuring Log4j using configurator: " +