mcconnell 02/02/22 08:29:04
Added: apps/enterprise/orb/src/examples/hello/java/hello
HelloProvider.java HelloInitializer.java
HelloException.java HelloDemo.xml HelloDemo.java
Log:
hello Initializer with embedded ORB example
Revision Changes Path
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/examples/hello/java/hello/HelloProvider.java
Index: HelloProvider.java
===================================================================
/*
* HelloProvider.java
*/
package hello;
import java.util.Properties;
import java.util.MissingResourceException;
import java.util.Enumeration;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
import org.omg.CORBA.LocalObject;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.ImplicitActivationPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableInterceptor.ORBInitInfo;
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName;
import org.omg.PortableInterceptor.ORBInitializer;
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.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.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
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.CascadingConfiguration;
/**
* This is a minimal demonstration server.
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
*/
public class HelloProvider extends HelloPOA
implements LogEnabled, Configurable, Contextualizable, Initializable,
Startable, Disposable
{
private Logger m_logger;
private Configuration m_config;
private Context m_context;
private HelloProvider m_this;
private ORB m_orb;
private Hello m_hello;
private POA m_root;
//=======================================================================
// Loggable
//=======================================================================
/**
* Method invoked by the ORB initializer to assign a logging channel.
* @param Logger the logging channel
*/
public void enableLogging( final Logger logger )
{
m_logger = logger;
}
/**
* Returns the logging channel.
* @return Logger the current logging channel
*/
protected Logger getLogger()
{
return m_logger;
}
//=================================================================
// Contextualizable
//=================================================================
/**
* Method invoked by the 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;
}
//=======================================================================
// Initializable
//=======================================================================
/**
* Initialization of the server. The initialization validates that a
* logging channel is available, that configuration exists, and that
* context is not null. Folowing validation, the implementation
* creates an ORB based on a child configuration named 'orb' supplied
* during Initializer establishment. Once the ORB is obtained, the
* implementation handles the establishment of the POA and servant.
*
* @exception Exception if initialization fails
*/
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", m_logger.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 HelloException( "Unable to instantiate an ORB.", e);
}
//
// create the POA
//
if( getLogger().isDebugEnabled() ) getLogger().debug("creating POA" );
final String HELLO_WORLD = "HELLO_WORLD";
try
{
m_root = POAHelper.narrow(
m_orb.resolve_initial_references("RootPOA") );
final POA poa = m_root.create_POA(
HELLO_WORLD,
m_root.the_POAManager(),
new Policy[] {
m_root.create_id_assignment_policy(
IdAssignmentPolicyValue.USER_ID ),
m_root.create_lifespan_policy(
LifespanPolicyValue.PERSISTENT )
}
);
final byte[] ID = HELLO_WORLD.getBytes();
poa.activate_object_with_id( ID, this );
m_hello = HelloHelper.narrow( poa.id_to_reference(ID) );
}
catch( Exception e )
{
throw new HelloException("Failed to instantiate POA.", e );
}
//
// add a shutdown hook so we can stop the server and dispose of
// resources
//
m_this = this;
Runtime.getRuntime().addShutdownHook(
new Thread()
{
public void run()
{
try
{
if( m_this != null ) m_this.stop();
}
catch( Throwable e )
{
// ignore it
}
finally
{
if( m_this != null ) m_this.dispose();
}
}
}
);
//
// register server as an initial reference
//
Object info = m_context.get("ORB_INIT_INFO");
if( info != null ) if( info instanceof ORBInitInfo )
{
try
{
((ORBInitInfo)info).register_initial_reference("HELLO",
m_hello);
}
catch( Throwable e )
{
final String error = "Unexpected exception while attempting
to register Hello as an inital reference.";
throw new CascadingException( error, e );
}
}
}
//=======================================================================
// Startable
//=======================================================================
/**
* Start the HelloWorldServer.
*/
public void start()
throws Exception
{
if( getLogger().isDebugEnabled() ) getLogger().debug( "starting
HelloWorld" );
m_root.the_POAManager().activate();
final Thread thread = new Thread(
new Runnable() {
public void run()
{
if( getLogger().isDebugEnabled() ) getLogger().debug(
"starting" );
try
{
m_orb.run();
}
catch (Exception e)
{
final String error = "unexpected exception raised by
the ORB";
if( getLogger().isErrorEnabled() ) getLogger().error(
error, e );
throw new CascadingRuntimeException( error, e );
}
}
}
);
thread.start();
final String debug = "startup complete";
if( getLogger().isDebugEnabled() ) getLogger().debug( debug );
}
/**
* Stops the component.
*/
public void stop()
throws Exception
{
if( getLogger().isDebugEnabled() ) getLogger().debug( "stopping Hello
World Provider" );
try
{
m_orb.shutdown( true );
}
catch( Throwable e )
{
final String warning = "Internal error while shutting down the
ORB.";
if( getLogger().isWarnEnabled() ) getLogger().warn( warning, e );
}
}
//=======================================================================
// Disposable
//=======================================================================
/**
* Disposal of the server. This method is triggered by a shutdown hook
* becuase initializers are not cleared by an ORB on shutdown.
*/
public void dispose()
{
if( getLogger().isDebugEnabled() ) getLogger().debug("disposal of
embedded Hello World" );
m_config = null;
m_context = null;
m_hello = null;
m_orb = null;
m_logger = null;
}
//=======================================================================
// HelloWorld
//=======================================================================
public String get_welcome_message()
{
return m_config.getChild("welcome").getAttribute(
"message","Welcome to the hello World POA.");
}
}
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/examples/hello/java/hello/HelloInitializer.java
Index: HelloInitializer.java
===================================================================
/*
* HelloServer.java
*/
package hello;
import java.util.Properties;
import java.util.MissingResourceException;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.omg.CORBA.Policy;
import org.omg.CORBA.LocalObject;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.ImplicitActivationPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableInterceptor.ORBInitInfo;
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName;
import org.omg.PortableInterceptor.ORBInitializer;
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.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.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
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.ORB;
import org.apache.orb.ORBFactoryService;
import org.apache.orb.CascadingConfiguration;
/**
* This is a minimal demonstration server.
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
*/
public class HelloInitializer extends LocalObject
implements LogEnabled, Configurable, Contextualizable, ORBInitializer
{
private Logger m_logger;
private Configuration m_config;
private Context m_context;
private HelloProvider m_poa;
private boolean m_flag = false;
//=======================================================================
// Loggable
//=======================================================================
/**
* Method invoked by the Apache ORB initializer to assign a logging
channel.
* @param Logger the logging channel
*/
public void enableLogging( final Logger logger )
{
m_logger = logger;
}
/**
* Returns the logging channel.
* @return Logger the current logging channel
*/
protected Logger getLogger()
{
return m_logger;
}
//=================================================================
// 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("initialize" );
DefaultContext context = new DefaultContext( m_context );
context.put( "ORB_INIT_INFO", info );
context.makeReadOnly();
m_context = context;
//
// create an HelloPOA
//
try
{
if( getLogger().isDebugEnabled() ) getLogger().debug("creating
HelloProvider" );
m_poa = new HelloProvider();
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);
}
finally
{
//
// dispose of ourselves now because we have nothing more to
// contribute
//
dispose();
}
}
//=======================================================================
// Disposable
//=======================================================================
private void dispose()
{
if( getLogger().isDebugEnabled() ) getLogger().debug("initializer
disposal" );
m_config = null;
m_context = null;
m_poa = null;
m_logger = null;
}
}
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/examples/hello/java/hello/HelloException.java
Index: HelloException.java
===================================================================
/*
* HelloException.java
*
* Created on March 28, 2001, 0:39 AM
*/
package hello;
import org.apache.avalon.framework.CascadingException;
/**
* The <code>HelloException</code>.
*
* @author mcconnell
* @version 1.0
*/
public class HelloException extends CascadingException {
/**
* Construct a new <code>HelloException </code> instance with the
* supplied message parameter and a null value for the cause exception.
*
* @param message Message summarising the exception.
*/
public HelloException( final String message )
{
this( message, null );
}
/**
* Construct a new <code>HelloException</code> instance with the
* supplied message parameter and a supplied cause exception.
*
* @param message The detail message for this exception.
* @param cause the root cause of the exception
*/
public HelloException( final String message, final Throwable cause )
{
super( message, cause );
}
}
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/examples/hello/java/hello/HelloDemo.xml
Index: HelloDemo.xml
===================================================================
<?xml version="1.0"?>
<!--
HelloDemo.xml
-->
<config>
<initializer class="hello.HelloInitializer" name="hello">
<provider>
<orb>
<property name="iiop.port" value="2506" />
<property name="openorb.debug" value="0" />
</orb>
<welcome message="Welcome to the hello World Embedded Server."/>
</provider>
</initializer>
</config>
1.1
jakarta-avalon-cornerstone/apps/enterprise/orb/src/examples/hello/java/hello/HelloDemo.java
Index: HelloDemo.java
===================================================================
/*
* HelloDemo.java
*/
package hello;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.MissingResourceException;
import java.util.Properties;
import org.apache.log.Hierarchy;
import org.apache.log.Priority;
import org.apache.log.output.io.StreamTarget;
import org.apache.avalon.framework.CascadingException;
import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.AvalonFormatter;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.LogKitLogger;
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.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.orb.CascadingConfiguration;
import org.omg.CORBA.ORB;
import org.xml.sax.SAXException;
/**
* This is a minimal demonstration server. The demo is normally
* executed via a component pipeline processor, however, the demo
* has built-in support for execution using main.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
*/
public class HelloDemo extends AbstractLogEnabled
implements Configurable, Contextualizable, Initializable, Disposable
{
//=================================================================
// static
//=================================================================
private static final String DEFAULT_FORMAT = "[%7.7{priority}]
(%{category}): %{message}\\n%{throwable}";
private static OutputStream m_out = System.out;
private static String m_priority = "DEBUG";
public static void main( String[] args )
{
final HelloDemo demo = new HelloDemo();
Runtime.getRuntime().addShutdownHook(
new Thread()
{
public void run()
{
demo.dispose();
}
}
);
try
{
demo.enableLogging( createBootstrapLogger( "hello" ) );
demo.configure( loadDefaultConfiguration() );
demo.contextualize( new DefaultContext() );
demo.initialize();
}
catch( Throwable e )
{
final String error = "Internal error while attempt to launch the
hello demo";
System.out.println( error );
e.printStackTrace();
}
}
//=================================================================
// state
//=================================================================
private Configuration m_config;
private Context m_context;
private ORB m_orb;
private boolean m_disposed = false;
//=================================================================
// Contextualizable
//=================================================================
public void contextualize( Context context ) throws ContextException
{
m_context = context;
}
//=======================================================================
// Configurable
//=======================================================================
public void configure( final Configuration config )
throws ConfigurationException
{
try
{
m_config = new CascadingConfiguration( config,
loadDefaultConfiguration() );
}
catch( Throwable e )
{
final String error = "Unable to establish configuration.";
throw new ConfigurationException( error, e );
}
}
//=======================================================================
// 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 );
properties.put( "LOGGER", getLogger().getChildLogger("orb") );
properties.put( "CONTEXT", m_context );
try
{
m_orb = ORB.init( new String[0], properties );
org.omg.CORBA.Object object =
m_orb.resolve_initial_references("HELLO");
Hello hello = HelloHelper.narrow( object );
getLogger().info( hello.get_welcome_message() );
}
catch( Throwable e)
{
throw new HelloException( "Client initization failed.", e);
}
finally
{
dispose();
}
}
public void dispose()
{
if( m_disposed ) return;
try
{
if( getLogger().isDebugEnabled() ) getLogger().debug( "shutting
down demo" );
if( m_orb instanceof org.apache.orb.ORB )
{
((org.apache.orb.ORB)m_orb).stop();
}
else
{
m_orb.shutdown( true );
}
}
catch( Throwable e)
{
e.printStackTrace();
}
if( getLogger().isDebugEnabled() ) getLogger().debug( "demo disposal"
);
m_orb = null;
m_config = null;
m_context = null;
m_disposed = true;
}
//=======================================================================
// utilities
//=======================================================================
/**
* Returns the default configuration resource.
*/
private static Configuration loadDefaultConfiguration( )
throws MissingResourceException, ConfigurationException
{
try
{
InputStream is =
HelloDemo.class.getClassLoader().getResourceAsStream(
"hello/HelloDemo.xml" );
if( is != null )
{
DefaultConfigurationBuilder builder = new
DefaultConfigurationBuilder( );
return builder.build( is );
}
else
{
throw new MissingResourceException(
"Null input stream.", HelloDemo.class.getName(),
"hello/HelloDemo.xml" );
}
}
catch( SAXException e )
{
throw new CascadingRuntimeException(
"Internal SAX exception while attempting to load
configuration.", e );
}
catch( IOException e )
{
throw new CascadingRuntimeException(
"Internal IO exception while attempting to load
configuration.", e );
}
}
private static Logger createBootstrapLogger( String catagory )
{
try
{
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
hierarchy.setDefaultLogTarget(
new StreamTarget( m_out, new AvalonFormatter( DEFAULT_FORMAT )
) );
hierarchy.setDefaultPriority( Priority.getPriorityForName(
m_priority ) );
return new LogKitLogger( hierarchy.getLoggerFor( catagory ) );
}
catch( Throwable e )
{
final String error = "Unexpected exception while creating
bootstrap logger.";
throw new CascadingRuntimeException( error, e );
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>