org.openorb.util.service
Class ServiceBase

java.lang.Object
  |
  +--org.openorb.util.service.ServiceBase
All Implemented Interfaces:
org.apache.avalon.framework.context.Contextualizable, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.parameters.Parameterizable, org.apache.avalon.framework.activity.Startable

public abstract class ServiceBase
extends Object
implements org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.context.Contextualizable, org.apache.avalon.framework.parameters.Parameterizable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.activity.Startable, org.apache.avalon.framework.activity.Disposable

This abstract class is the base class for all Service classes. It provides the lifecycle framework for initializing, contextualizing, starting, stopping, and disposing a service.

The Avalon lifecyle startup phase consists of:

  1. constructor (as a consequence of instantiation)
  2. enableLogging
  3. contextualize
  4. parameterize
  5. initialize
  6. start
See Avalon Framework - The Component Lifecyle for more details.

Writing a Service with an additional command line argument would look like this:

 public class Service
     extends ServiceBase
 {
     private POA m_poa = null;

     public void initializeService()
     {
         try
         {
             ORB orb = getORB();
             if ( orb != null )
             {
                 m_poa = POAHelper.narrow( ( POA ) createPOA() );
                 MyServiceServant ms = new MyServiceServant();
                 org.omg.CORBA.Object obj = ms._this( orb );
                 registerObject( "MyService", obj );
             }
         }
         catch ( Exception ex )
         {
             // handle errors
         }
     }

     public void startService()
     {
         try
         {
             if ( m_poa != null )
             {
                 m_poa.the_POAManager().activate();
             }
         }
         catch ( AdapterInactive ex )
         {
             // handle errors
         }
     }

     public void stopService()
     {
         try
         {
             if ( m_poa != null )
             {
                 m_poa.the_POAManager().deactivate( false, true );
             }
         }
         catch ( AdapterInactive ex )
         {
             // handle errors
         }
     }
 }
 

The POA handling can't be moved into the ServiceBase class because JDK1.3.x does not have POA support and because OpenORB still supports JDK 1.3.x compilation under JDK 1.3.x would not be possible (This will eventually be changed when support for JDK 1.3.x will be dropped).

The protected xxxService() mehods are callback methods that are called by the actual methods from the Avalon lifecycle interfaces. The actual methods do some consistency checking before the callback methods are invoked. The callback methods can be overridden to adapt the behaviour of the service in the corresponding state of the Avalon lifecycle.

The registerObject() method tries to register an object at the NamingService and at the OpenORB CorbalocService. This method must be used because all the bootstrapping issues are hidden behind this method. When the NamingService can't be found the file-system fallback will be used instead. Also the registerObject() method keeps track of registered objects and will deregister them automatically when the service is taken down, i.e. disposed.


Field Summary
static String OPT_BIND_CORBALOC
           
static String OPT_BIND_NS
           
static String OPT_DEBUG
           
static String OPT_DEBUG_ARG
           
static String OPT_DEBUG_DESCRIP
           
static String OPT_DEBUG_LONG
           
static String OPT_DEFAULT_CS
           
static String OPT_DEFAULT_CS_DESCRIP
           
static String OPT_DEFAULT_CS_LONG
           
static String OPT_HELP
           
static String OPT_HELP_DESCRIP
           
static String OPT_HELP_LONG
           
static String OPT_LOG_FILE
           
static String OPT_LOG_FILE_ARG
           
static String OPT_LOG_FILE_DESCRIP
           
static String OPT_LOG_FILE_LONG
           
static String OPT_NOBIND
           
static String OPT_NOBIND_ARG
           
static String OPT_NOBIND_DESCRIP
           
static String OPT_NOBIND_LONG
           
static String OPT_PRINT_IOR
           
static String OPT_PRINT_IOR_DESCRIP
           
static String OPT_PRINT_IOR_LONG
           
static String OPT_PRINT_URL
           
static String OPT_PRINT_URL_DESCRIP
           
static String OPT_PRINT_URL_LONG
           
static String OPT_VERSION
           
static String OPT_VERSION_DESCRIP
           
static String OPT_VERSION_LONG
           
static String OPT_WRITE_IOR_FILE
           
static String OPT_WRITE_IOR_FILE_ARG
           
static String OPT_WRITE_IOR_FILE_DESCRIP
           
static String OPT_WRITE_IOR_FILE_LONG
           
static String OPT_WRITE_URL_FILE
           
static String OPT_WRITE_URL_FILE_ARG
           
static String OPT_WRITE_URL_FILE_DESCRIP
           
static String OPT_WRITE_URL_FILE_LONG
           
 
Constructor Summary
ServiceBase()
           
 
Method Summary
protected  boolean bindToNamingService(String name, Object obj, ORB orb)
          Bind into the Naming Service
protected  void consolePrintln(String msg)
          Ensures the message is displayed on System.out
 void contextualize(org.apache.avalon.framework.context.Context context)
          This is the actual contextualize operation that calls the contextualizeService method.
protected  void contextualizeService(org.apache.avalon.framework.context.Context context)
          This callback method is called from contextualize() and may be overridden by the actual Service class.
protected  Object createPOA()
          This method is used to get the parent POA.
 void dispose()
          This is the actual dispose operation which calls disposeService.
protected  void disposeService()
          This callback method is called from dispose() and may be overridden by the actual Service class.
 void enableLogging(org.apache.avalon.framework.logger.Logger logger)
          Provide the service with a logger.
protected  org.apache.avalon.framework.context.Context getContext()
          Return the context of this instance.
 org.apache.avalon.framework.logger.Logger getLogger()
          Return the logger instance associated with this service.
 String getLongName()
          Return the long name for the service.
 ORB getORB()
          Returns the ORB used by the Service
protected  org.apache.avalon.framework.parameters.Parameters getParameters()
          Return the parameters of this instance.
 String getShortName()
          Return the short name for the service.
 String getVersion()
          Return the version for the service.
 void initialize()
          This is the actual initialize operation which calls initializeService.
protected  void initializeService()
          This callback method is called from initialize() and may be overridden by the actual Service class.
 boolean isContextualized()
          Indicates whether the Service has been contextualized.
 boolean isInitialized()
          Indicates whether the Service has been initialized.
 boolean isLogEnabled()
          Indicates whether the Service has been logEnabled.
 boolean isParameterized()
          Indicates whether the Service has been parameterized.
 boolean isStarted()
          Indicates whether the Service has been started.
 void parameterize(org.apache.avalon.framework.parameters.Parameters params)
          This is the actual parameterize operation which calls parameterizeService.
protected  void parameterizeService(org.apache.avalon.framework.parameters.Parameters params)
          This callback method is called from parameterize() and may be overridden by the actual Service class.
protected  void registerObject(String name, Object obj)
          Register the object at the NamingService and/or at the CorbalocService.
 void start()
          This is the actual start operation which calls startService.
protected  void startService()
          This callback method is called from start() and may be overridden by the actual Service class.
 void stop()
          This is the actual stop operation which calls stopService.
protected  void stopService()
          This callback method is called from stop() and may be overridden by the actual Service class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPT_HELP

public static final String OPT_HELP

OPT_HELP_LONG

public static final String OPT_HELP_LONG

OPT_HELP_DESCRIP

public static final String OPT_HELP_DESCRIP

OPT_DEBUG

public static final String OPT_DEBUG

OPT_DEBUG_ARG

public static final String OPT_DEBUG_ARG

OPT_DEBUG_LONG

public static final String OPT_DEBUG_LONG

OPT_DEBUG_DESCRIP

public static final String OPT_DEBUG_DESCRIP

OPT_BIND_CORBALOC

public static final String OPT_BIND_CORBALOC

OPT_BIND_NS

public static final String OPT_BIND_NS

OPT_NOBIND

public static final String OPT_NOBIND

OPT_NOBIND_ARG

public static final String OPT_NOBIND_ARG

OPT_NOBIND_LONG

public static final String OPT_NOBIND_LONG

OPT_NOBIND_DESCRIP

public static final String OPT_NOBIND_DESCRIP

OPT_DEFAULT_CS

public static final String OPT_DEFAULT_CS

OPT_DEFAULT_CS_LONG

public static final String OPT_DEFAULT_CS_LONG

OPT_DEFAULT_CS_DESCRIP

public static final String OPT_DEFAULT_CS_DESCRIP

OPT_VERSION

public static final String OPT_VERSION

OPT_VERSION_LONG

public static final String OPT_VERSION_LONG

OPT_VERSION_DESCRIP

public static final String OPT_VERSION_DESCRIP

OPT_PRINT_URL

public static final String OPT_PRINT_URL

OPT_PRINT_URL_LONG

public static final String OPT_PRINT_URL_LONG

OPT_PRINT_URL_DESCRIP

public static final String OPT_PRINT_URL_DESCRIP

OPT_WRITE_URL_FILE

public static final String OPT_WRITE_URL_FILE

OPT_WRITE_URL_FILE_ARG

public static final String OPT_WRITE_URL_FILE_ARG

OPT_WRITE_URL_FILE_LONG

public static final String OPT_WRITE_URL_FILE_LONG

OPT_WRITE_URL_FILE_DESCRIP

public static final String OPT_WRITE_URL_FILE_DESCRIP

OPT_PRINT_IOR

public static final String OPT_PRINT_IOR

OPT_PRINT_IOR_LONG

public static final String OPT_PRINT_IOR_LONG

OPT_PRINT_IOR_DESCRIP

public static final String OPT_PRINT_IOR_DESCRIP

OPT_WRITE_IOR_FILE

public static final String OPT_WRITE_IOR_FILE

OPT_WRITE_IOR_FILE_ARG

public static final String OPT_WRITE_IOR_FILE_ARG

OPT_WRITE_IOR_FILE_LONG

public static final String OPT_WRITE_IOR_FILE_LONG

OPT_WRITE_IOR_FILE_DESCRIP

public static final String OPT_WRITE_IOR_FILE_DESCRIP

OPT_LOG_FILE

public static final String OPT_LOG_FILE

OPT_LOG_FILE_ARG

public static final String OPT_LOG_FILE_ARG

OPT_LOG_FILE_LONG

public static final String OPT_LOG_FILE_LONG

OPT_LOG_FILE_DESCRIP

public static final String OPT_LOG_FILE_DESCRIP
Constructor Detail

ServiceBase

public ServiceBase()
Method Detail

enableLogging

public void enableLogging(org.apache.avalon.framework.logger.Logger logger)
Provide the service with a logger. This must be the first operation in a service lifecycle.
Specified by:
enableLogging in interface org.apache.avalon.framework.logger.LogEnabled
Parameters:
logger - The logger instance for this service.

getLogger

public org.apache.avalon.framework.logger.Logger getLogger()
Return the logger instance associated with this service.
Returns:
The logger instance.

contextualizeService

protected void contextualizeService(org.apache.avalon.framework.context.Context context)
                             throws org.apache.avalon.framework.context.ContextException
This callback method is called from contextualize() and may be overridden by the actual Service class. It is called from the contextualize() method after doing some initial consistency check.
Parameters:
context - The context for the service.
Throws:
org.apache.avalon.framework.context.ContextException - When an error accessing the context occurs.

contextualize

public final void contextualize(org.apache.avalon.framework.context.Context context)
                         throws org.apache.avalon.framework.context.ContextException
This is the actual contextualize operation that calls the contextualizeService method. It will be called by the ServerBase.initService() method.
Specified by:
contextualize in interface org.apache.avalon.framework.context.Contextualizable
Parameters:
context - The context for the service.
Throws:
org.apache.avalon.framework.context.ContextException - When an error accessing the context occurs.

getContext

protected org.apache.avalon.framework.context.Context getContext()
Return the context of this instance.
Returns:
The context of the instance.

parameterizeService

protected void parameterizeService(org.apache.avalon.framework.parameters.Parameters params)
This callback method is called from parameterize() and may be overridden by the actual Service class. It is called from the parameterize() method after doing some initial consistency check.
Parameters:
params - configuration parameters

parameterize

public final void parameterize(org.apache.avalon.framework.parameters.Parameters params)
This is the actual parameterize operation which calls parameterizeService. It will be called by the ServerBase.initService() method.
Specified by:
parameterize in interface org.apache.avalon.framework.parameters.Parameterizable
Parameters:
params - Parameters

getParameters

protected org.apache.avalon.framework.parameters.Parameters getParameters()
Return the parameters of this instance.
Returns:
The parameters of the instance.

initializeService

protected void initializeService()
This callback method is called from initialize() and may be overridden by the actual Service class. It is called from the initialize() method after doing some initial consistency check.

initialize

public final void initialize()
This is the actual initialize operation which calls initializeService. It will be called by the ServerBase.initService() method.
Specified by:
initialize in interface org.apache.avalon.framework.activity.Initializable

startService

protected void startService()
This callback method is called from start() and may be overridden by the actual Service class. It is called from the start() method after doing some initial consistency check.

start

public final void start()
This is the actual start operation which calls startService. It will be called by the ServerBase.initService() method.
Specified by:
start in interface org.apache.avalon.framework.activity.Startable

stopService

protected void stopService()
This callback method is called from stop() and may be overridden by the actual Service class. It is called from the stop() method after doing some initial consistency check.

stop

public final void stop()
This is the actual stop operation which calls stopService. It will be called by the ServerBase.initService() method.
Specified by:
stop in interface org.apache.avalon.framework.activity.Startable

disposeService

protected void disposeService()
This callback method is called from dispose() and may be overridden by the actual Service class. It is called from the dispose() method after doing some initial consistency check.

dispose

public final void dispose()
This is the actual dispose operation which calls disposeService. It will be called by the ServerBase.initService() method.
Specified by:
dispose in interface org.apache.avalon.framework.activity.Disposable

isLogEnabled

public boolean isLogEnabled()
Indicates whether the Service has been logEnabled.
Returns:
true if logEnabled; false if not

isContextualized

public boolean isContextualized()
Indicates whether the Service has been contextualized.
Returns:
true if contextualized; false if not

isParameterized

public boolean isParameterized()
Indicates whether the Service has been parameterized.
Returns:
true if parameterized; false if not

isInitialized

public boolean isInitialized()
Indicates whether the Service has been initialized.
Returns:
true if initialized; false if not

isStarted

public boolean isStarted()
Indicates whether the Service has been started.
Returns:
true if started; false if not

getORB

public ORB getORB()
Returns the ORB used by the Service
Returns:
ORB object

getLongName

public final String getLongName()
Return the long name for the service.
Returns:
The long name.

getShortName

public final String getShortName()
Return the short name for the service.
Returns:
The short name.

getVersion

public final String getVersion()
Return the version for the service.
Returns:
The version.

createPOA

protected Object createPOA()
This method is used to get the parent POA. First, any "POA" passed in the Context will be used. Otherwise, the RootPOA will be taken from the orb passed.

This method may be overridden to create a special POA.

The return type is java.lang.Object because the POA class is not available in JDK 1.3.x. Using the POA class in the method signature would introduce a dependency to JDK 1.4.x.

Returns:
POA object (cast to org.omg.PortableServer.POA)

registerObject

protected final void registerObject(String name,
                                    Object obj)
Register the object at the NamingService and/or at the CorbalocService. Deregistration will be done automatically upon service shutdown.
Parameters:
name - The name of the object.
obj - The object itself.

bindToNamingService

protected boolean bindToNamingService(String name,
                                      Object obj,
                                      ORB orb)
Bind into the Naming Service
Parameters:
name - name to bind
obj - object to bind
orb - ORB
Returns:
true if need to print the IOR

consolePrintln

protected void consolePrintln(String msg)
Ensures the message is displayed on System.out
Parameters:
msg - message to print