directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcon...@apache.org
Subject svn commit: r476556 - /directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java
Date Sat, 18 Nov 2006 16:20:14 GMT
Author: jconlon
Date: Sat Nov 18 08:20:14 2006
New Revision: 476556

URL: http://svn.apache.org/viewvc?view=rev&rev=476556
Log:
Refactored configuration of log4j

Modified:
    directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java

Modified: directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java
URL: http://svn.apache.org/viewvc/directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java?view=diff&rev=476556&r1=476555&r2=476556
==============================================================================
--- directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java
(original)
+++ directory/sandbox/jconlon/osgi-services/logging-service/src/main/java/org/apache/directory/osgi/services/logging/Activator.java
Sat Nov 18 08:20:14 2006
@@ -1,12 +1,13 @@
 package org.apache.directory.osgi.services.logging;
 
-
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Dictionary;
 import java.util.Hashtable;
 
+import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.PropertyConfigurator;
 import org.osgi.framework.BundleActivator;
@@ -16,13 +17,28 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 /**
- * BundleActivator for logging services.
- *
+ * BundleActivator for logging services. 
+ * 
+ * A log4j configuration can be specified to this bundle 
+ * in a number of ways:
+ * <ul>
+ * <li>A log4j.properties file can be located in a conf subdirectory 
+ * of the working directory
+ * <li>The log4j.configuration bundle property can be set to the url of 
+ * the configuration file
+ * <li>If neither 1 or 2 above then a default basic configuration will be used
+ * </ul>
+ * <p>
+ * To observe the inner workings of log4j configuration set the log4j.debug system
+ * property to true.
+ * 
  */
-public class Activator implements BundleActivator
-{
+public class Activator implements BundleActivator {
+
+    private static final String DEFALT_LOG_CONF_PATH = System
+            .getProperty("user.dir")
+            + File.separator + "conf" + File.separator + "log4j.properties";
 
     private static final String VENDOR = "org.apache";
 
@@ -30,79 +46,84 @@
 
     private Logger log = null;
 
-
-    public Activator()
-    {
+    public Activator() {
         super();
     }
 
-
     /**
-     * On bundle start reload the log4J configuration and register the logService.
+     * On bundle start reload the log4J configuration and register the
+     * logService.
      */
-    public void start( BundleContext bundleContext ) throws Exception
-    {
-        try
-        {
-            resetLog4j( bundleContext );
-            log = LoggerFactory.getLogger( Activator.class );
-            log.debug( "Reset log configuration." );
-        }
-        catch ( Exception e )
-        {
+    public void start(BundleContext bundleContext) throws Exception {
+        try {
+            resetLog4j(bundleContext);
+
+        } catch (MalformedURLException e) {
+            System.err.println(Activator.class.getName()+": MalformedURLException - Failed
to find the log configuration file at path: "
+                            + e.getLocalizedMessage());
+        } catch (Exception e) {
+            System.err.println(Activator.class.getName()+": Exception - Failed to find the
log configuration file at path: "
+                    + e.getLocalizedMessage());
             e.printStackTrace();
         }
-        registerLogService( bundleContext );
+        registerLogService(bundleContext);
     }
 
-
-    private void registerLogService( BundleContext bundleContext )
-    {
+    private void registerLogService(BundleContext bundleContext) {
         Dictionary<String, String> parameters = new Hashtable<String, String>();
-        parameters.put( Constants.SERVICE_VENDOR, VENDOR );
-        bundleContext.registerService( LogService.class.getName(), new LogServiceImpl(),
parameters );
+        parameters.put(Constants.SERVICE_VENDOR, VENDOR);
+        bundleContext.registerService(LogService.class.getName(),
+                new LogServiceImpl(), parameters);
     }
 
-
-    public void stop( BundleContext arg0 ) throws Exception
-    {
+    public void stop(BundleContext arg0) throws Exception {
     }
 
-
     /**
      * @return url of the log4j.properties configuration file
      * 
      * @throws MalformedURLException
      * 
      */
-    private URL getLoggingProperty( BundleContext bundleContext ) throws MalformedURLException
-    {
-        final String logPropertiesLocation = bundleContext.getProperty( LOG_PROPERTIES_LOCATION
);
-        return new URL( logPropertiesLocation );
+    private URL getLoggingProperty(BundleContext bundleContext)
+            throws FileNotFoundException, MalformedURLException {
+        String logPropertiesLocation = bundleContext
+                .getProperty(LOG_PROPERTIES_LOCATION);
+        if (logPropertiesLocation == null) {
+            File defaultConf = new File(DEFALT_LOG_CONF_PATH);
+            if (defaultConf.exists()) {
+                logPropertiesLocation = defaultConf.toURI().toString();
+            } else {
+                return null;
+            }
+        }
+        return new URL(logPropertiesLocation);
     }
 
-
     /**
      * Reset the log4j configuration.
+     * 
      * @param bundleContext
      * @throws MalformedURLException
      * @throws FileNotFoundException
      */
-    private void resetLog4j( BundleContext bundleContext ) throws MalformedURLException,
FileNotFoundException
-    {
+    private void resetLog4j(BundleContext bundleContext)
+            throws MalformedURLException, FileNotFoundException {
         LogManager.resetConfiguration();
-        URL log4jprops = getLoggingProperty( bundleContext );
+        URL log4jprops = getLoggingProperty(bundleContext);
 
-        if ( log4jprops != null )
-        {
-            PropertyConfigurator.configure( log4jprops );
+        if (log4jprops != null) {
+            System.out.println(Activator.class.getName()+": Setting log4j configuration to:
"+log4jprops);
+            PropertyConfigurator.configure(log4jprops);
+            log = LoggerFactory.getLogger(Activator.class);
+            log.info("Reset log configuration, with configuration: "
+                    + log4jprops);
+        } else {
+            BasicConfigurator.configure();
+            log = LoggerFactory.getLogger(Activator.class);
+            log.warn("A basic logging configuration was created because the logging configuration
property: "+LOG_PROPERTIES_LOCATION+ 
+                    " was not set and the default log configuration "+DEFALT_LOG_CONF_PATH+"
was empty. ");
 
-        }
-        else
-        {
-            throw new FileNotFoundException( bundleContext.getProperty( LOG_PROPERTIES_LOCATION
)
-                + " could not be found. " + "Please specify add the file and restart the
"
-                + bundleContext.getBundle().getLocation() + " bundle." );
         }
     }
 



Mime
View raw message