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/03/09 05:51:51 UTC

cvs commit: jakarta-avalon-apps/enterprise/time/src/java/org/apache/time Initializer.java Provider.java Provider.xinfo TimeServer.java TimeServer.xinfo

mcconnell    02/03/08 20:51:51

  Added:       enterprise/time/src/java/org/apache/time Initializer.java
                        Provider.java Provider.xinfo
  Removed:     enterprise/time/src/java/org/apache/time TimeServer.java
                        TimeServer.xinfo
  Log:
  addition of initializer support
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-apps/enterprise/time/src/java/org/apache/time/Initializer.java
  
  Index: Initializer.java
  ===================================================================
  /**
   * File: HelloInitializer.java
   * License: etc/LICENSE.TXT
   * Copyright: Copyright (C) The Apache Software Foundation. All rights reserved.
   * Copyright: OSM SARL 2001-2002, All Rights Reserved.
   */
  package org.apache.time;
  
  import org.omg.CORBA.ORB;
  import org.omg.CORBA.Policy;
  import org.omg.CORBA.LocalObject;
  import org.omg.PortableServer.LifespanPolicyValue;
  import org.omg.PortableInterceptor.ORBInitInfo;
  import org.omg.PortableInterceptor.ORBInitializer;
  
  import org.apache.avalon.framework.CascadingRuntimeException;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.activity.Disposable;
  
  import org.openorb.CORBA.LoggableLocalObject;
  
  /**
   * Initializer for an embedded time server.
   * @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
   */
  
  public class Initializer extends LoggableLocalObject
  implements Configurable, Contextualizable, ORBInitializer, Disposable
  {
  
      private Configuration m_config;
      private Context m_context;
      private Provider m_poa;
      private boolean m_flag = false;
  
      //=================================================================
      // Contextualizable
      //=================================================================
  
     /**
      * Method invoked by the Apache ORB initializer to declare the runtime context.
      * @param Context runtime application context
      */
      public void contextualize( Context context ) throws ContextException
      {
          m_context = context;
      }
  
      //=======================================================================
      // Configurable
      //=======================================================================
      
     /**
      * Method invoked by the ORB initializer to declare the static configuration.
      * @param Configuration application static configuration
      */
      public void configure( final Configuration config )
      throws ConfigurationException
      {
            m_config = config;
      }
  
      //==========================================================================
      // ORBInitializer
      //==========================================================================
  
      /**
       * Method invoked by the ORB.  The implementation establish the POA and servant
       * and passes the ORBInitInfo to the target POA as a context parameter for inital 
       * reference registration.
       * @param info 
       */
      public void pre_init( ORBInitInfo info )
      {
  
          if( getLogger().isDebugEnabled() ) getLogger().debug("Initializer" );
          DefaultContext context = new DefaultContext( m_context );
          context.put( "ORB_INIT_INFO", info );
          context.makeReadOnly();
          m_context = context;
  
          //
          // create an TimePOA
          //
          
          try
          {
              if( getLogger().isDebugEnabled() ) getLogger().debug("creating TimePOA" );
              m_poa = new Provider();
              m_poa.enableLogging( getLogger().getChildLogger("provider") );
              m_poa.configure( m_config.getChild("provider") );
              m_poa.contextualize( m_context );
              m_poa.initialize();
              m_flag = true;
          }
          catch( Throwable e)
          {
              throw new CascadingRuntimeException( "Unable to instantiate embedded server.", e);
          }
      }
    
      /**
       * Post initalization of the interceptor invoked by the
       * ORB in which this interceptor is installed.
       * @param info  
       */
      public void post_init( ORBInitInfo info ) 
      {
          if( !m_flag ) return;
          try
          {
              m_poa.start();
          }
          catch( Throwable e)
          {
              throw new CascadingRuntimeException( "Unable to start embedded server.", e);
          }
      }
  
      //=======================================================================
      // Disposable
      //=======================================================================
      
     /**
      * Disposal will be invoked by the ORB initializer following shutdown of the 
      * ORB we have been initialized within.
      */
      public void dispose()
      {
          if( getLogger().isDebugEnabled() ) getLogger().debug("initializer disposal" );
          try
          {
              m_poa.stop();
          }
          catch( Throwable e )
          {
              if( getLogger().isWarnEnabled() ) getLogger().warn(
                "ignoring exception while stopping Hello provider", e );
          }
          finally
          {
              m_poa = null;
              m_config = null;
              m_context = null;
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-apps/enterprise/time/src/java/org/apache/time/Provider.java
  
  Index: Provider.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.TXT file.
   *
   * Original contribution by OSM SARL, http://www.osm.net
   */
  
  package org.apache.time;
  
  import java.io.File;
  import java.io.OutputStream;
  import java.util.Iterator;
  import java.util.Properties;
  
  import org.omg.CORBA.ORB;
  import org.omg.CORBA.Policy;
  import org.omg.CORBA.LocalObject;
  import org.omg.PortableInterceptor.ORBInitInfo;
  import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
  import org.omg.PortableInterceptor.ORBInitializer;
  import org.omg.PortableServer.POA;
  import org.omg.PortableServer.POAHelper;
  import org.omg.PortableServer.ImplicitActivationPolicyValue;
  import org.omg.PortableServer.LifespanPolicyValue;
  import org.omg.PortableServer.IdUniquenessPolicyValue;
  import org.omg.CosTime.TimeServiceHelper;
  import org.omg.CosTime.TimeService;
  
  import org.apache.avalon.framework.CascadingException;
  import org.apache.avalon.framework.CascadingRuntimeException;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.activity.Startable;
  import org.apache.avalon.framework.logger.LogEnabled;
  import org.apache.avalon.phoenix.BlockContext;
  import org.apache.avalon.phoenix.Block;
  
  import org.apache.orb.ORBFactoryService;
  import org.apache.orb.util.IOR;
  
  import org.openorb.CORBA.LoggableLocalObject;
  
  /**
   * <code>TimeBlock</code> compliant with the OMG CosTime interface specification.
   *
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  
  public class Provider extends LoggableLocalObject
  implements Block, Configurable, Contextualizable, Initializable, Startable, Disposable, TimeService
  {
      //=======================================================================
      // static
      //=======================================================================
      
      private static final String ROOT_POA = "RootPOA";
  
      //=======================================================================
      // state
      //=======================================================================
  
      protected Configuration m_config;
      protected ORB m_orb;
      protected POA m_root;
      private String m_ior;
      private int m_inaccuracy = 10000;
      private org.omg.CosTime.TimeService m_time;
      private static Thread m_thread;
  
     /**
      * Application context
      */
      Context m_context;
  
      //=======================================================================
      // Configurable
      //=======================================================================
      
      public void configure( final Configuration config )
      throws ConfigurationException
      {
          m_config = config;
      }
      
      //=================================================================
      // Contextualizable
      //=================================================================
  
      public void contextualize( Context context ) throws ContextException
      {
  	  m_context = context;
      }
  
      //=======================================================================
      // Initializable
      //=======================================================================
      
      public void initialize()
      throws Exception
      {       
  
          //
          // Create a properties argument.  Using the apache ORB loader so we get automatic
          // addition of default properties, unpacking of the initializer declarations
          // from the configuration, and propergation of the logger, context and configuration.
          //
  
          Properties properties = new Properties();
  
          properties.setProperty("openorb.IgnoreXML","true");
          properties.setProperty("openorb.ORBLoader","org.apache.orb.CORBA.kernel.DefaultLoader");
          properties.setProperty("org.omg.CORBA.ORBClass", "org.openorb.CORBA.ORB" );
  	  properties.setProperty("org.omg.CORBA.ORBSingletonClass", "org.openorb.CORBA.ORBSingleton" );
  
          properties.put( "CONFIGURATION", m_config.getChild("orb") );
          properties.put( "LOGGER", getLogger().getChildLogger("orb") );
          properties.put( "CONTEXT", m_context );
  
          //
          // create an ORB
          //
          
          if( getLogger().isDebugEnabled() ) getLogger().debug("creating ORB" );
          try
          {
              m_orb = ORB.init( new String[0], properties );
          }
          catch( Throwable e)
          {
              throw new CascadingException( "Unable to instantiate an ORB.", e);
          }
  
          //
          // get the time service parameters
          //
          
          try
          {
              m_inaccuracy = m_config.getChild("profile").getAttributeAsInteger( 
  		  "inaccuracy", m_inaccuracy );
              if( getLogger().isDebugEnabled() ) getLogger().debug(
                "setting inaccuracy to " + m_inaccuracy );
          } catch (Exception e)
          {
              if( getLogger().isDebugEnabled() ) getLogger().debug(
                "setting inaccuracy to default " + m_inaccuracy );
          }
          
          m_ior = m_config.getChild("ior").getAttribute( "ior", null );
          if( getLogger().isDebugEnabled() )
          {
              if( m_ior != null ) 
              {
                  getLogger().debug( "setting IOR path to " + m_ior );
              }
              else
              {
                  getLogger().debug("IOR publication disabled" );
              }
          } 
          
          //
          // create the time server runtime POA
          //
          
          if( getLogger().isDebugEnabled() ) getLogger().debug("locating root POA" );
          try
          {
              m_root = POAHelper.narrow(m_orb.resolve_initial_references(ROOT_POA));
              POA timePOA = m_root.create_POA
              (
                "TimeServicePOA", // adapter name
                m_root.the_POAManager(), // manager
                new Policy[]
                {
                  m_root.create_implicit_activation_policy(
                  ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION),
                  m_root.create_lifespan_policy( LifespanPolicyValue.PERSISTENT ),
                  m_root.create_id_uniqueness_policy( IdUniquenessPolicyValue.UNIQUE_ID)
                }
              );
              
              org.omg.CosTime.TimeServicePOA servant =
              new org.apache.time.DefaultTimeService( m_orb, m_inaccuracy );
              byte[] servantID = timePOA.activate_object(servant);
              org.omg.CORBA.Object object = null;
              object = timePOA.id_to_reference(servantID);
              m_time = TimeServiceHelper.narrow( object );
              if( getLogger().isDebugEnabled() ) getLogger().debug("POA established" );
          }
          catch( Exception e)
          {
              throw new CascadingException("cannot instantiate POA", e);
          }
          
          //
          // register time service as an initial reference
          //
  
          Object info = null;
          try
          {
              info = m_context.get("ORB_INIT_INFO");
          }
          catch( Throwable e )
          {
              // ignore - just means that we don't need to register the time service
              // as an initial ref
          }
          
          if(( info != null ) && (info instanceof ORBInitInfo )) try
          {
              ((ORBInitInfo)info).register_initial_reference("TimeService", m_time);
          }
          catch( Throwable e )
          {
              final String error = 
                "Unexpected exception while attempting to register time service as an inital reference.";
               throw new CascadingException( error, e );
          }
          if( getLogger().isDebugEnabled() ) getLogger().debug("initialization complete" );
      }
      
      //=======================================================================
      // Startable
      //=======================================================================
      
      /**
       * Start the TimeServer.
       */
      public void start()
      throws Exception
      {
          if( getLogger().isDebugEnabled() ) getLogger().debug("starting server" );
  
          //
          // set object reference
          //
          
          if( m_ior != null )
          {
              if( getLogger().isDebugEnabled() ) getLogger().debug("creating external object reference" );
              try
              {
                  IOR.writeIOR( m_orb, m_time, m_ior );
                  if( getLogger().isDebugEnabled() ) getLogger().debug( "published IOR to: " + m_ior );
              }
              catch (Exception e)
              {
                  throw new CascadingException( "failed to create external IOR on " + m_ior );
              }
          }
          
          m_thread = new Thread(
          new Runnable() {
              public void run()
              {
                  if( getLogger().isDebugEnabled() ) getLogger().debug("starting time server" );
                  try
                  {
                      m_root.the_POAManager().activate();
                      m_orb.run();
                  }
                  catch (Exception e)
                  {
                      throw new CascadingRuntimeException( "failed to activate the server", e );
                  }
              }
            }
          );
          m_thread.start();
          String banner = "Time Service available ";
          if( getLogger().isInfoEnabled() ) getLogger().info( 
            banner + TimeUtils.convertToDate( universal_time().time() ));
      }
  
     /**
      * Stops the component.
      */
      public void stop()
      throws Exception
      {
          if( getLogger().isDebugEnabled() ) getLogger().debug("shutdown" );
          m_orb.shutdown( true );
          if( getLogger().isDebugEnabled() ) getLogger().debug("shutdown complete" );
      }
  
      //=======================================================================
      // Disposable
      //=======================================================================
      
      public void dispose()
      {
          if( getLogger().isDebugEnabled() ) getLogger().debug("dispose" );
          synchronized( m_orb )
          {
              m_orb = null;
              m_root = null;
          }
          m_config = null;
          m_ior = null;
          m_time = null;
          m_thread = null;
      }
      
      //=======================================================================
      // TimeService
      //=======================================================================
      
     /**
      * The universal_time operation returns the current time and an estimate of inaccuracy in
      * a UTO. It raises TimeUnavailable exceptions to indicate failure of an underlying time
      * provider. The time returned in the UTO by this operation is not guaranteed to be secure
      * or trusted. If any time is available at all, that time is returned by this operation.
      */
  
      public org.omg.CosTime.UTO universal_time()
      throws org.omg.CosTime.TimeUnavailable
      {
          return m_time.universal_time();
      }
      
     /**
      * The secure_universal_time operation returns the current time in a UTO only if the
      * time can be guaranteed to have been obtained securely. In order to make such a
      * guarantee, the underlying Time Service must meet the criteria to be followed for
      * secure time, presented in Appendix A, Implementation Guidelines. If there is any
      * uncertainty at all about meeting any aspect of these criteria, then this operation must
      * return the TimeUnavailable exception. Thus, time obtained through this operation can
      * always be trusted.
      */
  
      public org.omg.CosTime.UTO secure_universal_time()
      throws org.omg.CosTime.TimeUnavailable
      {
          return m_time.secure_universal_time();
      }
      
     /**
      * The new_universal_time operation is used for constructing a new UTO. The
      * parameters passed in are the time of type TimeT and inaccuracy of type InaccuracyT.
      * This is the only way to create a UTO with an arbitrary time from its components. This
      * is expected to be used for building UTOs that can be passed as the various time
      * arguments to the Timer Event Service, for example. CORBA::BAD_PARAM is
      * raised in the case of an out-of-range parameter value for inaccuracy.
      */
  
      public org.omg.CosTime.UTO new_universal_time(long t, long inac, short tdf)
      {
          return m_time.new_universal_time( t, inac, tdf );
      }
      
     /**
      * The uto_from_utc operation is used to create a UTO given a time in the UtcT form.
      * This has a single in parameter UTC, which contains a time together with inaccuracy
      * and tdf. The UTO returned is initialized with the values from the UTC parameter. This
      * operation is used to convert a UTC received over the wire into a UTO.
      */
  
      public org.omg.CosTime.UTO uto_from_utc(org.omg.TimeBase.UtcT utc)
      {
          return m_time.uto_from_utc( utc );
      }
      
     /**
      * The new_interval operation is used to construct a new TIO. The parameters are lower
      * and upper, both of type TimeT, holding the lower and upper bounds of the interval. If
      * the value of the lower parameter is greater than the value of the upper parameter, then
      * a CORBA::BAD_PARAM exception is raised.
      */
  
      public org.omg.CosTime.TIO new_interval(long lower, long upper)
      {
          return m_time.new_interval( lower, upper );
      }
  }
  
  
  
  1.1                  jakarta-avalon-apps/enterprise/time/src/java/org/apache/time/Provider.xinfo
  
  Index: Provider.xinfo
  ===================================================================
  <?xml version="1.0"?>
  
  <!--
  Copyright (C) The Apache Software Foundation. All rights reserved.
  
  This software is published under the terms of the Apache Software License
  version 1.1, a copy of which has been included with this distribution in
  the LICENSE.TXT file.
  
  Original contribution by OSM SARL, http://www.osm.net
  -->
  
  <blockinfo>
  
    <block name="time">
      <version>1.0</version>
    </block>
  
    <!-- 
    The Provider block provides a single service defined by the 
    net.osm.time.TimeService interface.
    -->
  
    <services>
      <service name="org.omg.CosTime.TimeService" version="1.0" />
    </services>
  
    <configuration>
  
      <ior file="time.ior"/>
  
      <!-- 
      The profile element declares the default inaccuracy values 
      that will be used by the time server. 
      -->
  
      <profile inaccuracy="10000" />
  
    </configuration>
  
  </blockinfo>
  
  
  

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