You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2002/02/06 10:37:19 UTC

cvs commit: jakarta-avalon-cornerstone/apps/enterprise/orb/src/java/org/apache/orb/CORBA/kernel DefaultLoader.java

mcconnell    02/02/06 01:37:19

  Added:       apps/enterprise/orb/src/java/org/apache/orb/CORBA/kernel
                        DefaultLoader.java
  Log:
  loader impementation that applies Avalon component lifecycle operation
  on initializers
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-cornerstone/apps/enterprise/orb/src/java/org/apache/orb/CORBA/kernel/DefaultLoader.java
  
  Index: DefaultLoader.java
  ===================================================================
  
  package org.apache.orb.CORBA.kernel;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.net.URL;
  
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.logger.LogEnabled;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.CascadingRuntimeException;
  
  import org.apache.orb.ORBContext;
  
  import org.omg.PortableInterceptor.ORBInitializer;
  
  import org.openorb.PI.ORBInitInfo;
  import org.openorb.PI.FeatureInitializer;
  import org.openorb.CORBA.MinorCodes;
  import org.omg.CORBA.CompletionStatus;
  import org.openorb.CORBA.kernel.ORBLoader;
  import org.openorb.CORBA.kernel.Properties;
  import org.openorb.CORBA.kernel.Property;
  import org.openorb.CORBA.kernel.PropertyNotFoundException;
  import org.openorb.CORBA.kernel.Configurator;
  import org.openorb.CORBA.kernel.ORBConnector;
  import org.openorb.CORBA.kernel.ORBBootstrapHelper;
  
  /**
   * Experimental ORB loader. 
   */
  
  public class DefaultLoader implements ORBLoader
  {
  
      //=========================================================================
      // static
      //=========================================================================
  
      private static final String ORBSingletonClassKey = "org.omg.CORBA.ORBSingletonClass";
      private static final String defaultORBSingleton = "org.openorb.CORBA.ORBSingleton";
  
  
      //=========================================================================
      // state
      //=========================================================================
  
      private Properties _properties = null;
      private org.openorb.CORBA.ORB _orb;
      private Logger m_logger;
  
      //=========================================================================
      // constructor
      //=========================================================================
  
      /**
       * Constructor
       */
      public DefaultLoader()
      {
      }
  
      //=========================================================================
      // implementation
      //=========================================================================
  
      /**
       * ORB Initialization.
       */
      public void init( String [] args, java.util.Properties properties, org.openorb.CORBA.ORB orb )
      {
          // ensure this is the only time.
          if ( _orb != null )
              throw new org.omg.CORBA.INITIALIZE( "Illegal attempt to reinitialize the ORB." );
  
          _orb = orb;
  
          // ensure the correct singleton orb is used.
          try
          {
              System.setProperty( ORBSingletonClassKey, defaultORBSingleton );
          }
          catch ( SecurityException ex )
          {}
  
          org.omg.CORBA.ORB singleton = org.omg.CORBA.ORB.init();
  
          if ( !( singleton instanceof org.openorb.CORBA.ORBSingleton ) )
             throw new org.omg.CORBA.INITIALIZE( 
               "Unable to complete init orb singleton is not openorb singleton.\n" 
               + "Please use: System.setProperty(\"" + ORBSingletonClassKey + "\", \"" 
               + defaultORBSingleton + "\");\n" 
               + "As the first statement in your application." );
  
          // Store the loader
          _orb.setFeature( "ORBLoader", this );
  
          //
          // Enable logging on the ORB
          //
  
          if(( properties != null ) && ( properties instanceof Context ))
          {
              try
              {
                  m_logger = (Logger) ((Context)properties).get("LOGGER");
              }
              catch( Throwable e )
              {
                  // ignore and apply default logger
              }
          }
  
          if( m_logger == null ) m_logger = ORBBootstrapHelper.getRootLogger().getChildLogger( 
             "" + System.identityHashCode( this ) ) ;
          _orb.enableLogging( m_logger );
          m_logger.debug("loading orb");
  
          // load the properties and the list of initializers
          Configurator conf = new Configurator( args, properties );
  
          _properties = conf.getProperties();
  
          // create the orb init info.
          ORBInitInfo init_info;
  
          try
          {
              Object [] cargs = new Object[ 4 ];
              Class [] targs = new Class [ 4 ];
              cargs[ 0 ] = ( args == null ) ? new String[ 0 ] : args;
              targs[ 0 ] = String[].class;
              cargs[ 1 ] = orb;
              targs[ 1 ] = org.openorb.CORBA.ORB.class;
  
              if( properties instanceof ORBContext )
              {
                  load_initializers( cargs, conf.getInitializers(), (ORBContext)properties );
              }
              else
              {
                  load_initializers( cargs, conf.getInitializers() );
              }
  
              targs[ 2 ] = ORBInitializer[].class;
              targs[ 3 ] = FeatureInitializer[].class;
  
              init_info = ( ORBInitInfo ) constructClass( 
                 "openorb.pi.ORBInitInfoClass", "org.openorb.PI.OpenORBInitInfo", cargs, targs );
          }
          catch ( Throwable ex )
          {
              throw new CascadingRuntimeException( 
               "Internal exception while attempting to create ORBInitInfo", ex );
          }
  
          init_info.pre_init();
  
          try
          {
              ORBConnector orb_connector = ( ORBConnector ) constructClass( 
               "openorb.kernel.ORBConnectorClass", "org.openorb.CORBA.kernel.OpenORBConnector", null );
  
              orb_connector.load_kernel( _orb, this );
          }
          catch ( Throwable ex )
          {
              throw new CascadingRuntimeException( 
                "Exception during construction of ORBConnectorClass", ex );
          }
  
          init_info.post_init();
      }
  
      /**
       * This operation is used to display an OpenORB configuration.
       */
      public void display_configuration( java.io.PrintWriter out )
      {
          _properties.display( out );
      }
  
      // ---------------------------------------------------------------------
      //
      // The following operations return properties stored into a profile
      //
      // ---------------------------------------------------------------------
  
      /**
       * Iterate over property values with the specified prefix. <p>
       * 
       * @param name parent of properties. Properies of the form name + "." + xxx
       * are returned, where xxx can be anything. May be null to iterate over all
       * properies.
       * @return unmodifiable iterator over the name's decendants. This iterator
       *    returns objects of type Property.
       */
      public Iterator properties( String name )
      {
          return _properties.properties( name );
      }
  
      /**
       * Get the Property object with the given name.
       * @param name the property name.
       */
      public Property getProperty( String name )
      {
          return _properties.getProperty( name );
      }
  
      /**
       * Get the string property with the given name.
       * @param name the property name.
       * @param defl default value to use if property not found.
       */
      public String getStringProperty( String name, String defl )
      {
          return _properties.getStringProperty( name, defl );
      }
  
      /**
       * Get the string property with the given name.
       * @param name the property name.
       * @throws PropertyNotFoundException the property cannot be found.
       */
      public String getStringProperty( String name )
      throws PropertyNotFoundException
      {
          return _properties.getStringProperty( name );
      }
  
      /**
       * Get the integer property with the given name.
       * @param name the property name.
       * @param defl default value to use if property not found.
       * @throws org.omg.CORBA.INITIALIZE The property value is not parsable to an int.
       */
      public int getIntProperty( String name, int defl )
      {
          return _properties.getIntProperty( name, defl );
      }
  
      /**
       * Get the integer property with the given name.
       * @param name the property name.
       * @throws PropertyNotFoundException the property cannot be found.
       * @throws org.omg.CORBA.INITIALIZE The property value is not parsable to an int.
       */
      public int getIntProperty( String name )
      throws PropertyNotFoundException
      {
          return _properties.getIntProperty( name );
      }
  
      /**
       * Get the boolean property with the given name.
       * @param name the property name.
       * @param defl default value to use if property not found.
       */
      public boolean getBooleanProperty( String name, boolean defl )
      {
          return _properties.getBooleanProperty( name, defl );
      }
  
      /**
       * Get the boolean property with the given name.
       * @param name the property name.
       * @throws PropertyNotFoundException the property cannot be found.
       */
      public boolean getBooleanProperty( String name )
      throws PropertyNotFoundException
      {
          return _properties.getBooleanProperty( name );
      }
  
      /**
       * Get the URL property with the given name.
       * @param name the property name.
       * @param defl default value to use if property not found.
       * @throws ClassCastException The property value is not parsable to a URL.
       */
      public URL getURLProperty( String name, URL defl )
      {
          return _properties.getURLProperty( name, defl );
      }
  
      /**
       * Get the URL property with the given name.
       * @param name the property name.
       * @throws PropertyNotFoundException the property cannot be found.
       * @throws ClassCastException The property value is not parsable to a URL.
       */
      public URL getURLProperty( String name )
      throws PropertyNotFoundException
      {
          return _properties.getURLProperty( name );
      }
  
      /**
       * Get the Class object property with the given name.
       * @param name the property name.
       * @param defl default value to use if property not found.
       * @throws org.omg.CORBA.INITIALIZE the property value cannot be loaded as a class.
       */
      public Class getClassProperty( String name, Class defl )
      {
          return _properties.getClassProperty( name, defl );
      }
  
      /**
       * Get the Class object property with the given name.
       * @param name the property name.
       * @param defl String name of default value to use if property not found.
       * @throws org.omg.CORBA.INITIALIZE the property value or default class cannot 
       *                    be loaded as a class.
       */
      public Class getClassProperty( String name, String defl )
      {
          return _properties.getClassProperty( name, defl );
      }
  
      /**
       * Get the integer property with the given name.
       * @param name the property name.
       * @throws PropertyNotFoundException the property cannot be found.
       * @throws org.omg.CORBA.INITIALIZE the property value cannot be loaded as a class.
       */
      public Class getClassProperty( String name )
      throws PropertyNotFoundException
      {
          return _properties.getClassProperty( name );
      }
  
      /**
       * This operation is used to load a class with the given property name and
       * default class name.
       *
       * @param prop_key Property name, this string property holds the name of the
       *    class. May be null if no property is used.
       * @param defl Default class name. Used if the named property is not found.
       *    May be null to indicate no load should be performed if property
       *    is missing.
       * @param args arguments to constructor. If any constructor arguments are 
       *    primative types then the four argument version of this function
       *    must be used.
       * @return the newly constructed object, or null if the property value is
       *    set to the empty string.
       * @throws java.lang.reflect.InvocationTargetException an exception occoured
       *    in the constructor.
       * @throws org.omg.CORBA.INITIALIZE the property value or default class cannot 
       *    be loaded as a class.
       * @throws IllegalArgumentException some other problem occoured.
       */
      public Object constructClass( String prop_key, String defl, Object [] args )
      throws java.lang.reflect.InvocationTargetException
      {
          return constructClass( prop_key, defl, args, null );
      }
  
      /**
       * This operation is used to load a class with the given property name and
       * default class name.
       *
       * @param prop_key Property name, this string property holds the name of the
       *    class. May be null if no property is used.
       * @param defl Default class name. Used if the named property is not found.
       *    May be null to indicate no load should be performed if property
       *    is missing.
       * @param args arguments to constructor. If any constructor arguments are 
       *    primative types then the four argument version of this function
       *    must be used.
       * @param args_t types of onstructor arguments. If any of these are null they
       *    will be determined from getClass on the matching arg. Length
       *    must match length of args.
       * @return the newly constructed object, or null if the property value is
       *    set to the empty string.
       * @throws java.lang.reflect.InvocationTargetException an exception occoured
       *    in the constructor.
       * @throws org.omg.CORBA.INITIALIZE the property value or default class cannot 
       *    be loaded as a class.
       * @throws IllegalArgumentException some other problem occoured.
       */
      public Object constructClass( String prop_key, String defl, Object [] args,
                                    Class [] args_t )
      throws java.lang.reflect.InvocationTargetException
      {
          if ( args != null )
          {
              if ( args_t == null )
                  args_t = new Class[ args.length ];
              else if ( args.length != args_t.length )
                  throw new IllegalArgumentException( "Length of args and args_t do not match" );
  
              for ( int i = 0; i < args.length; ++i )
                  if ( args_t[ i ] == null )
                      args_t[ i ] = ( args[ i ] == null ) ? Void.TYPE : args[ i ].getClass();
          }
  
          try
          {
              return classConstructor( prop_key, defl, args_t ).newInstance( args );
          }
          catch ( InstantiationException ex )
          {
              throw new IllegalArgumentException( "Illegal argument when constructing a class (" + ex + ")" );
          }
          catch ( IllegalAccessException ex )
          {
              throw new IllegalArgumentException( "Illegal access when constructing a class (" + ex + ")" );
          }
      }
  
      public java.lang.reflect.Constructor classConstructor( String prop_key, String defl, Class [] args_t )
      {
          Class clz = getClassProperty( prop_key, defl );
  
          try
          {
              return clz.getConstructor( args_t );
          }
          catch ( NoSuchMethodException ex )
          {
              throw new IllegalArgumentException( "No constructor found in " + clz.getName() + " (" + ex + ")" );
          }
      }
  
      private void load_initializers( Object [] args, String [] cls_names )
      {
          load_initializers( args, cls_names, null );
      }
  
      private void load_initializers( Object [] args, String [] cls_names, ORBContext context )
      {
          ArrayList orbInits = new ArrayList( cls_names.length );
          ArrayList openOrbInits = new ArrayList( cls_names.length );
          
          Logger logger = null;
          Context base = null;
  
          if( context != null )
          {
              logger = context.getBaseLogger();
              base = context.getBaseContext();
              if( logger != null ) if( logger.isDebugEnabled() ) logger.debug(
                "loading " + cls_names.length + " initializers" );
          }
  
          for ( int i = 0; i < cls_names.length; ++i )
          {
              Object instance;
  
              try
              {
                  instance = Class.forName( cls_names[ i ] ).newInstance();
                  if( logger != null ) if( logger.isDebugEnabled() ) logger.debug(
                     "loading initializer: " + cls_names[i] );
              }
              catch ( Exception ex )
              {
                  final String error = "Unable to load initializer class: " +  cls_names[ i ];
                  throw new CascadingRuntimeException( error, ex );
              }
  
              boolean ok = false;
  
              Configuration config = null;
              if( context != null )
              {
                  config = context.getConfigurationByClassName( cls_names[i] );
                  final String name = config.getAttribute("name", null );
                  System.out.println("Loading initializer config: " +  cls_names[i] + ", name: " + name );
  
                  //
                  // provide the initalizer with a logger
                  //
  
                  if( instance instanceof LogEnabled )
                  {
                      if( name != null )
                      {
                          ((LogEnabled)instance).enableLogging( logger.getChildLogger( name ) );
                      }
                      else
                      {
                          ((LogEnabled)instance).enableLogging( logger );
                      }
                  }
  
   
                  //
                  // contextualize the initalizer
                  //
  
                  if( instance instanceof Contextualizable )
                  {
                      try
                      {
                          ((Contextualizable)instance).contextualize( base );
                      }
                      catch( Exception e )
                      {
                          final String error = "Unexpected exeption while contextualizing interceptor.";
                          throw new CascadingRuntimeException( error, e ); 
                      }
                  }
  
                  //
                  // configure the initalizer
                  //
  
                  if( instance instanceof Configurable )
                  {
                      try
                      {
                          ((Configurable)instance).configure( config );
                      }
                      catch( Exception e )
                      {
                          final String error = "Unexpected exeption while configuring interceptor.";
                          throw new CascadingRuntimeException( error, e );
                      }
                  }
              }
  
              if( instance instanceof org.omg.PortableInterceptor.ORBInitializer )
              {
                  orbInits.add( instance );
                  ok = true;
              }
  
              if ( instance instanceof org.openorb.PI.FeatureInitializer )
              {
                  openOrbInits.add( instance );
                  ok = true;
              }
  
              if ( !ok )
              {
                  final String error = "Unknown initalizer type: '" + cls_names[i]  + "'.";
                  throw new IllegalStateException( error );
              }
          }
  
          args[ 2 ] = new org.omg.PortableInterceptor.ORBInitializer[ orbInits.size() ];
          orbInits.toArray( ( Object[] ) args[ 2 ] );
          args[ 3 ] = new org.openorb.PI.FeatureInitializer[ openOrbInits.size() ];
          openOrbInits.toArray( ( Object[] ) args[ 3 ] );
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>