org.openorb.util.service
Class ServerBase

java.lang.Object
  |
  +--org.openorb.util.service.ServerBase

public abstract class ServerBase
extends Object

A server controls the start-up of a servce. This abstract class must be overridden to handle specific command line arguments, or to create a special logger.

The default command line switches are:

 -d,--debug       enable debugging output. (Optional) Level
                         values: debug, info, warn, error. Default is debug.
 -e,--default            try to use the DefaultCorbalocService (IIOP port
                         683.)
 -f,--writeIORFile       write the ior of the service to a file in the
                         current working folder
 -h,--help               print this message and exit
 -i,--printIOR           print the ior of the service
 -l,--logfile      log to a file in the current working folder.
                         Default file is .log
 -n,--noBind    do not bind to the specified service.
                         Services: Naming, Corbaloc.
 -u,--printCorbalocURL   print the corbaloc url for the service
 -v,--version            print the version information and exit
 
A server class without any customization would look like this:
 public class Server
     extends ServerBase
 {
     public static void main( String[] args )
     {
         Server srv = new Server();
         srv.init( args );
         srv.run();
     }
 }
 
Adding further command line arguments is not complicated:
 public class Server
     extends ServerBase
 {
     public void handleArguments( CommandLine cmdline, Parameters params )
     {
         params.setParameter( Service.OPT_MY_SPECIAL,
               Boolean.toString( cmdline.hasOption( Service.OPT_MY_SPECIAL ) ) );
     }

     public static void main( String[] args )
     {
         Server srv = new Server();
         Options options = new Options();
         options.addOption( new Option( Service.MY_SPECIAL,
               Service.OPT_MY_SPECIAL_LONG, false,
               Service.OPT_MY_SPECIAL_DESCRIP ) );
         srv.init( args, options );
         srv.run();
     }
 }
 
A custom logger can be created by overriding the createLogger method.


Field Summary
static String DEFAULT_FILE_FORMAT
          The default logger file format.
static String DEFAULT_FORMAT
          The default logger format.
 
Constructor Summary
ServerBase()
           
 
Method Summary
protected  void configureLogger(Object logger, org.apache.avalon.framework.parameters.Parameters svc_params)
          Configure the logger.
protected  void consolePrintln(String msg)
          Ensures the message is displayed on System.out
protected  org.apache.avalon.framework.logger.Logger createLogger(String name, org.apache.avalon.framework.parameters.Parameters svc_params)
          Create the service's root logger.
protected  ServiceContext createServiceContext()
          Create the ServiceContext.
 org.apache.avalon.framework.logger.Logger getLogger()
          Return the process' logger instance.
 ORB getORB()
          Returns the ORB used by the Service.
 ServiceContext getServiceContext()
          Returns the ServiceContext.
 org.apache.avalon.framework.parameters.Parameters getServiceParameters()
          Returns the Parameters used by the Service.
protected  void handleArguments(org.apache.commons.cli.CommandLine cmdline, org.apache.avalon.framework.parameters.Parameters params)
          Handle service specific command line options.
 org.apache.commons.cli.CommandLine init(String[] args)
          Initialize the server.
 org.apache.commons.cli.CommandLine init(String[] args, org.apache.commons.cli.Options options)
          Initialize the server.
 org.apache.commons.cli.CommandLine init(String[] args, org.apache.commons.cli.Options options, Properties props)
          Initialize the server.
 org.apache.commons.cli.CommandLine init(String[] args, org.apache.commons.cli.Options options, Properties props, org.apache.avalon.framework.parameters.Parameters params)
          Initialize the server.
 org.apache.commons.cli.CommandLine init(String[] args, Properties props)
          Initialize the server.
protected  void preInitService(ServiceContext context, org.apache.avalon.framework.parameters.Parameters params)
          Service pre-initialization hook.
 void run()
          Install the shutdown hook for shuttding down the ORB and start the ORB's dispatch loop.
 void setServiceClassName(String classNameStr)
          Specifies the name of the service class to be used.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_FORMAT

public static final String DEFAULT_FORMAT
The default logger format.

DEFAULT_FILE_FORMAT

public static final String DEFAULT_FILE_FORMAT
The default logger file format.
Constructor Detail

ServerBase

public ServerBase()
Method Detail

init

public final org.apache.commons.cli.CommandLine init(String[] args)
Initialize the server.
Parameters:
args - The command line arguments for parsing.
Returns:
The CommandLine for service specific handling.

init

public final org.apache.commons.cli.CommandLine init(String[] args,
                                                     org.apache.commons.cli.Options options)
Initialize the server.
Parameters:
args - The command line arguments for parsing.
options - Service specific options.
Returns:
The CommandLine for service specific handling.

init

public final org.apache.commons.cli.CommandLine init(String[] args,
                                                     Properties props)
Initialize the server.
Parameters:
args - The command line arguments for parsing.
props - Properties for the orb init call.
Returns:
The CommandLine for service specific handling.

init

public final org.apache.commons.cli.CommandLine init(String[] args,
                                                     org.apache.commons.cli.Options options,
                                                     Properties props)
Initialize the server.
Parameters:
args - The command line arguments for parsing.
options - Service specific options.
props - Properties for the orb init call.
Returns:
The CommandLine for service specific handling.

init

public final org.apache.commons.cli.CommandLine init(String[] args,
                                                     org.apache.commons.cli.Options options,
                                                     Properties props,
                                                     org.apache.avalon.framework.parameters.Parameters params)
Initialize the server.
Parameters:
args - The command line arguments for parsing.
options - Service specific options.
props - Properties for the orb init call.
params - an initial set of service parameters.
Returns:
The CommandLine for service specific handling.

getLogger

public org.apache.avalon.framework.logger.Logger getLogger()
Return the process' logger instance.
Returns:
Framework logger to use for all logging

createLogger

protected org.apache.avalon.framework.logger.Logger createLogger(String name,
                                                                 org.apache.avalon.framework.parameters.Parameters svc_params)
Create the service's root logger. Uses the Avalon LogKit logger. Maybe overridden to create a special logger.
Parameters:
name - The name of the logger.
svc_params - service parameters
Returns:
The Avalon framework logger instance used for this process.

configureLogger

protected void configureLogger(Object logger,
                               org.apache.avalon.framework.parameters.Parameters svc_params)
Configure the logger. This method can be overriden to configure the logging priorities, etc.

By default, the cmdline option (--debug) is checked to set the default logging priority.

Parameters:
logger - real logger instance. The class depends on the actual logging package being used, e.g. LogKit (org.apache.log.Logger), log4J (org.apache.log4j.Logger) JDK1.4 (java.util.logging.Logger)
svc_params - service parameters. Passed here since getServiceParameters() will not work

createServiceContext

protected ServiceContext createServiceContext()
Create the ServiceContext. This method may be overriden to create a subclassed ServiceContext or to provide a ServiceContext with initial values.
Returns:
ServiceContext instance

handleArguments

protected void handleArguments(org.apache.commons.cli.CommandLine cmdline,
                               org.apache.avalon.framework.parameters.Parameters params)
Handle service specific command line options. This method may be overridden by the deriving class in order to add service specific command line option handling. The standard options have already been added to the parameters object. A typical line for adding a custom option is:
 params.setParameter( Service.OPT_MY_SPECIAL,
       Boolean.toString( cmdline.hasOption( Service.OPT_MY_SPECIAL ) ) );
 
Parameters:
cmdline - The parsed command line options.
params - The Parameters object of the service.

run

public final void run()
Install the shutdown hook for shuttding down the ORB and start the ORB's dispatch loop.

getORB

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

getServiceContext

public ServiceContext getServiceContext()
Returns the ServiceContext. Not available until after Server.init() has been invoked.
Returns:
service context

getServiceParameters

public org.apache.avalon.framework.parameters.Parameters getServiceParameters()
Returns the Parameters used by the Service. Not available until after Server.init() has been invoked.
Returns:
parameters

setServiceClassName

public void setServiceClassName(String classNameStr)
Specifies the name of the service class to be used. This can useful if the service class name cannot be derived from the server class name. This method needs to be called before the 'init()' method.
Parameters:
classNameStr - the name of the service class to use.

preInitService

protected void preInitService(ServiceContext context,
                              org.apache.avalon.framework.parameters.Parameters params)
Service pre-initialization hook. This method can be overridden to provide any necessary configuration or customization before the Service is initialized (i.e. logEnabled, contextualized, paramterized, and initialized).
Parameters:
context - service context
params - parameters

consolePrintln

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