You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ne...@apache.org on 2004/05/06 13:00:44 UTC

cvs commit: avalon-excalibur/fortress/bean/src/java/org/apache/avalon/fortress/tools FortressBean.java

neeme       2004/05/06 04:00:44

  Modified:    fortress/bean/src/java/org/apache/avalon/fortress/tools
                        FortressBean.java
  Log:
  changes:
   * added back the possibility to redirect commons logging output to avalon logger, if libraries are available. uses reflection, no hard dependencies
   * made it possible to run this class from command line, most of parameters are then passed in through system properties - now it is easier to run fortress with ant <java> task
  
  TODO:
   * add javadocs to document the command line functionality
  
  Revision  Changes    Path
  1.7       +88 -6     avalon-excalibur/fortress/bean/src/java/org/apache/avalon/fortress/tools/FortressBean.java
  
  Index: FortressBean.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/fortress/bean/src/java/org/apache/avalon/fortress/tools/FortressBean.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FortressBean.java	2 Mar 2004 16:20:41 -0000	1.6
  +++ FortressBean.java	6 May 2004 11:00:44 -0000	1.7
  @@ -18,6 +18,7 @@
   package org.apache.avalon.fortress.tools;
   
   import java.lang.reflect.Method;
  +import java.util.Properties;
   
   import org.apache.avalon.fortress.impl.DefaultContainer;
   import org.apache.avalon.fortress.impl.DefaultContainerManager;
  @@ -40,6 +41,9 @@
    */
   public class FortressBean implements Initializable, LogEnabled, Serviceable, Disposable {
   
  +    private static final String COMMONS_LOG_PROPERTY = "org.apache.commons.logging.Log";
  +    private static final String COMMONS_AVALON_LOGGER = "org.apache.commons.logging.impl.AvalonLogger";
  +
       private final FortressConfig config = new FortressConfig();
       private Logger logger = null;
       private DefaultContainerManager cm;
  @@ -51,6 +55,8 @@
   
       private boolean systemExitOnDispose = true;
   
  +    private Properties properties = null;
  +
       /**
        * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
        */
  @@ -69,9 +75,11 @@
       public void initialize() throws Exception {
           //only initialize if we do not already have a servicemanager passed in from outside
           if (this.sm == null) {
  +            ClassLoader cl = null;
  +            if (this.properties != null) initialize(this.properties);
               if (Thread.currentThread().getContextClassLoader() == null) {
                   if (this.getClass().getClassLoader() != null) {
  -                    ClassLoader cl = this.getClass().getClassLoader();
  +                    cl = this.getClass().getClassLoader();
                       config.setContextClassLoader(cl);
                       Thread.currentThread().setContextClassLoader(cl);
                   } else {
  @@ -81,14 +89,76 @@
               // Get the root container initialized
               this.cm = new DefaultContainerManager(config.getContext());
               ContainerUtil.initialize(cm);
  -            // set the static logger for commons logging 
  -            System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.AvalonLogger");
  -
  +            initializeCommonsLogging(cl);
               this.container = (DefaultContainer) cm.getContainer();
               this.sm = container.getServiceManager();
           }
       }
   
  +    /**
  +     * Use reflection to set up commons logging. If commons logging is available, it will be set up;
  +     * if it is not available, this section is ignored. This needs version 1.0.4 (or later) of commons
  +     * logging, earlier versions do not have avalon support.
  +     * 
  +     * @param cl class loader to look for commons logging classes
  +     */
  +    private void initializeCommonsLogging(ClassLoader cl) {
  +        try {
  +            //if commons logging is available, set the static logger for commons logging
  +            Class commonsLoggerClass;
  +            if (cl != null) {
  +                commonsLoggerClass = cl.loadClass(COMMONS_AVALON_LOGGER);
  +            } else {
  +                commonsLoggerClass = Class.forName(COMMONS_AVALON_LOGGER);
  +            }
  +            Method setDefaultLoggerMethod = commonsLoggerClass.getMethod("setDefaultLogger", new Class[] {Logger.class});
  +            setDefaultLoggerMethod.invoke(null, new Object[] {cm.getLogger()});
  +            //set the system property to use avalon logger
  +            System.setProperty(COMMONS_LOG_PROPERTY, COMMONS_AVALON_LOGGER);
  +        } catch (Exception e) {
  +            if (getLogger().isDebugEnabled()) getLogger().debug("error while initializing commons logging: " + e.getClass().getName() + ", " + e.getMessage());
  +        }
  +    }
  +
  +    private void disposeCommonsLogging() {
  +        System.getProperties().remove(COMMONS_LOG_PROPERTY);
  +    }
  +
  +    public static final String PROPERTY_CONTAINER_CLASS = "container.class";
  +    public static final String PROPERTY_CONTAINER_CONFIGURATION = "container.configuration";
  +    public static final String PROPERTY_CONTEXT_DIRECTORY = "context.directory";
  +    public static final String PROPERTY_INSTRUMENT_MANAGER_CONFIGURATION = 
  +                                          "instrument.manager.configuration";
  +    public static final String PROPERTY_INVOKE_METHOD = "invoke.method";
  +    public static final String PROPERTY_LOGGER_MANAGER_CONFIGURATION = "logger.manager.configuration";
  +    public static final String PROPERTY_LOOKUP_COMPONENT_ROLE = "lookup.component.role";
  +    public static final String PROPERTY_ROLE_MANAGER_CONFIGURATION = "role.manager.configuration";
  +    public static final String PROPERTY_SYSTEM_EXIT_ON_DISPOSE = "system.exit.on.dispose";
  +    public static final String PROPERTY_WORK_DIRECTORY = "work.directory";
  +
  +    public void initialize(Properties p) throws Exception {
  +        if (p.getProperty(PROPERTY_CONTAINER_CLASS) != null)
  +            this.setContainerClass(p.getProperty(PROPERTY_CONTAINER_CLASS));
  +        if (p.getProperty(PROPERTY_CONTAINER_CONFIGURATION) != null)
  +            this.setContainerConfiguration(p.getProperty(PROPERTY_CONTAINER_CONFIGURATION));
  +        if (p.getProperty(PROPERTY_CONTEXT_DIRECTORY) != null)
  +            this.setContextDirectory(p.getProperty(PROPERTY_CONTEXT_DIRECTORY));
  +        if (p.getProperty(PROPERTY_INSTRUMENT_MANAGER_CONFIGURATION) != null)
  +            this.setInstrumentManagerConfiguration(p.getProperty(PROPERTY_INSTRUMENT_MANAGER_CONFIGURATION));
  +        if (p.getProperty(PROPERTY_INVOKE_METHOD) != null)
  +            this.setInvokeMethod(p.getProperty(PROPERTY_INVOKE_METHOD));
  +        if (p.getProperty(PROPERTY_LOGGER_MANAGER_CONFIGURATION) != null)
  +            this.setLoggerManagerConfiguration(p.getProperty(PROPERTY_LOGGER_MANAGER_CONFIGURATION));
  +        if (p.getProperty(PROPERTY_LOOKUP_COMPONENT_ROLE) != null)
  +            this.setLookupComponentRole(p.getProperty(PROPERTY_LOOKUP_COMPONENT_ROLE));
  +        if (p.getProperty(PROPERTY_ROLE_MANAGER_CONFIGURATION) != null)
  +            this.setRoleManagerConfiguration(p.getProperty(PROPERTY_ROLE_MANAGER_CONFIGURATION));
  +        if (p.getProperty(PROPERTY_SYSTEM_EXIT_ON_DISPOSE) != null)
  +            this.setSystemExitOnDispose(Boolean.valueOf(p.getProperty(PROPERTY_SYSTEM_EXIT_ON_DISPOSE)).booleanValue());
  +        if (p.getProperty(PROPERTY_WORK_DIRECTORY) != null)
  +            this.setWorkDirectory(p.getProperty(PROPERTY_WORK_DIRECTORY));
  +    }
  +
       public void run() throws Exception {
           Object component = getServiceManager().lookup(lookupComponentRole);
           Method method = component.getClass().getMethod(invokeMethod, null);
  @@ -125,7 +195,7 @@
       public void dispose() {
           // Properly clean up when we are done
           org.apache.avalon.framework.container.ContainerUtil.dispose( cm );
  -        
  +        disposeCommonsLogging();
           //system exit, in case we were running some GUI and some thread is still active
           if (this.systemExitOnDispose) {
               Thread.yield();
  @@ -192,6 +262,18 @@
        */
       public void setSystemExitOnDispose(boolean systemExitOnDispose) {
           this.systemExitOnDispose = systemExitOnDispose;
  +    }
  +
  +    public static void main(String[] args) {
  +        FortressBean bean = new FortressBean();
  +        bean.setProperties(System.getProperties());
  +        if (args.length > 0) bean.setLookupComponentRole(args[0]);
  +        if (args.length > 1) bean.setInvokeMethod(args[1]);
  +        bean.execute();
  +    }
  +
  +    public void setProperties(Properties properties) {
  +        this.properties = properties;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org