mcconnell 02/03/12 14:20:08
Added: enterprise/time/src/java/org/apache/time TimeProvider.java
TimeProvider.xinfo
Removed: enterprise/time/src/java/org/apache/time Provider.java
Provider.xinfo
Log:
no message
Revision Changes Path
1.1
jakarta-avalon-apps/enterprise/time/src/java/org/apache/time/TimeProvider.java
Index: TimeProvider.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.Executable;
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.
* <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
* <tr bgcolor="#ccccff">
* <td colspan="2"><b><font size="+2">Lifecycle</font></b></td>
* <tr><td width="20%"><b>Phase</b></td><td><b>Description</b></td></tr>
* <tr>
* <td valign="top">Configurable</td>
* <td>
* The configuration contains the default TimeProvider bootstrap properties.
* The default provider configuration is detailed below:
* <pre>
* <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>
* </pre>
* </td></tr>
* <tr>
* <td valign="top">Contextualizable</td>
* <td>
* The <code>Context</code> value passed to the provider is used to determin
if
* the time server has deplyed as an Initializer in which case the provider
will
* be registered as an initial reference.</td></tr>
* <tr><td width="20%">Initalizable</td>
* <td>
* Creation of the Time POA and optional registration of the service as an
initial reference.
* </td></tr>
* <tr><td valign="top">Startable</td>
* <td>
* Starts the time server.
* </td></tr>
* <tr><td valign="top">Executable</td>
* <td>
* Logs the current time to the assigned logger.
* </td></tr>
* <tr><td valign="top">Disposable</td>
* <td>
* Cleanup and disposal of state members.
* </td></tr>
* </table>
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
*/
public class TimeProvider extends LoggableLocalObject
implements Block, Configurable, Contextualizable, Initializable, Startable,
Executable, Disposable, TimeService
{
//=======================================================================
// static
//=======================================================================
private static final String ROOT_POA = "RootPOA";
//=======================================================================
// state
//=======================================================================
private Configuration m_config;
private ORB m_orb;
private 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
//=================================================================
/**
* Invoked by the container to provide the application context.
* @param context the application context
*/
public void contextualize( Context context ) throws ContextException
{
m_context = context;
}
//=======================================================================
// Initializable
//=======================================================================
/**
* Invoked by the container to initalize the provider.
* @exception Exception if an initialization exception occurs.
*/
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 );
}
/**
* 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" );
}
//=======================================================================
// Executable
//=======================================================================
/**
* Logs the current time.
*/
public void execute() throws Exception
{
if( getLogger().isInfoEnabled() ) getLogger().info(
"current time: " + TimeUtils.convertToDate(
this.universal_time().time() ));
}
//=======================================================================
// Disposable
//=======================================================================
/**
* Invoked by the contained to signal request for disposal of the time
provider
* and associated resources.
*/
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/TimeProvider.xinfo
Index: TimeProvider.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 TimeProvider component 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: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>