You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/09/06 02:58:03 UTC
svn commit: r278897 - in /directory/standalone/trunk/osgi/ntp: ./
src/main/java/org/apache/ntp/
Author: erodriguez
Date: Mon Sep 5 17:57:54 2005
New Revision: 278897
URL: http://svn.apache.org/viewcvs?rev=278897&view=rev
Log:
Rewrote the NTP OSGi wrapper to use the declarative Service Binder.
Added:
directory/standalone/trunk/osgi/ntp/manifest.mf (with props)
directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml (with props)
Modified:
directory/standalone/trunk/osgi/ntp/project.properties
directory/standalone/trunk/osgi/ntp/project.xml
directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/Activator.java
directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpConfig.java
directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServer.java
directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java
Added: directory/standalone/trunk/osgi/ntp/manifest.mf
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/manifest.mf?rev=278897&view=auto
==============================================================================
--- directory/standalone/trunk/osgi/ntp/manifest.mf (added)
+++ directory/standalone/trunk/osgi/ntp/manifest.mf Mon Sep 5 17:57:54 2005
@@ -0,0 +1 @@
+Metadata-Location: org/apache/ntp/metadata.xml
Propchange: directory/standalone/trunk/osgi/ntp/manifest.mf
------------------------------------------------------------------------------
svn:eol-style = native
Modified: directory/standalone/trunk/osgi/ntp/project.properties
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/project.properties?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/project.properties (original)
+++ directory/standalone/trunk/osgi/ntp/project.properties Mon Sep 5 17:57:54 2005
@@ -1 +1,2 @@
module.path=apache-ntp
+maven.jar.manifest=manifest.mf
Modified: directory/standalone/trunk/osgi/ntp/project.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/project.xml?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/project.xml (original)
+++ directory/standalone/trunk/osgi/ntp/project.xml Mon Sep 5 17:57:54 2005
@@ -6,7 +6,8 @@
<currentVersion>0.1.1</currentVersion>
<properties>
<osgi.bundle.category>Network Service</osgi.bundle.category>
- <osgi.import.package>org.osgi.framework,org.apache.mina.registry,org.apache.mina.protocol,org.apache.mina.common,org.osgi.service.cm,org.osgi.util.tracker</osgi.import.package>
+ <osgi.bundle.activator>org.apache.ntp.Activator</osgi.bundle.activator>
+ <osgi.import.package>org.ungoverned.gravity.servicebinder,org.osgi.framework,org.apache.mina.registry,org.apache.mina.protocol,org.apache.mina.common,org.osgi.service.cm</osgi.import.package>
</properties>
<package>org.apache.ntp</package>
<shortDescription>Apache NTP Network Service</shortDescription>
@@ -20,6 +21,10 @@
<osgi.jar.bundle>true</osgi.jar.bundle>
</properties>
</dependency>
+ <dependency>
+ <groupId>org.ungoverned</groupId>
+ <artifactId>servicebinder</artifactId>
+ <version>1.1.2</version>
+ </dependency>
</dependencies>
</project>
-
Modified: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/Activator.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/Activator.java?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/Activator.java (original)
+++ directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/Activator.java Mon Sep 5 17:57:54 2005
@@ -17,113 +17,8 @@
package org.apache.ntp;
-import java.util.Dictionary;
-import java.util.Hashtable;
+import org.ungoverned.gravity.servicebinder.GenericActivator;
-import org.apache.mina.registry.ServiceRegistry;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedServiceFactory;
-import org.osgi.util.tracker.ServiceTracker;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * This class implements a simple bundle that starts and stops the Apache NTP service.
- */
-public class Activator implements BundleActivator
+public class Activator extends GenericActivator
{
- /** the log for this class */
- private static final Logger log = LoggerFactory.getLogger( Activator.class );
-
- public static final String FACTORY_PID = "org.apache.ntp";
-
- private ServiceRegistry registry;
- private NtpServerFactory serverFactory;
- private ServiceRegistration registration;
- private ServiceTracker tracker;
-
- /**
- * Implements BundleActivator.start(). Logs that this service is starting and
- * starts this service.
- * @param context the framework context for the bundle.
- */
- public void start( BundleContext context )
- {
- log.debug( "Starting Apache NTP." );
-
- tracker = new ServiceTracker( context, ServiceRegistry.class.getName(), null );
- tracker.open();
-
- registry = (ServiceRegistry) tracker.getService();
-
- serverFactory = new NtpServerFactory( registry );
-
- Dictionary parameters = new Hashtable();
- parameters.put( ConfigurationAdmin.SERVICE_FACTORYPID, FACTORY_PID );
- registration = context.registerService( ManagedServiceFactory.class.getName(), serverFactory, parameters );
-
- ServiceReference adminRef = null;
- try
- {
- ConfigurationAdmin admin = null;
- Configuration[] configs = null;
- try
- {
- adminRef = context.getServiceReference( ConfigurationAdmin.class.getName() );
-
- // Potential start order problem!
- if ( adminRef != null )
- {
- admin = (ConfigurationAdmin) context.getService( adminRef );
- String filter = "(&(service.factoryPid=" + FACTORY_PID + ")" + "(|(service.bundleLocation="
- + context.getBundle().getLocation() + ")" + "(service.bundleLocation=NULL)"
- + "(!(service.bundleLocation=*))))";
- configs = admin.listConfigurations( filter );
- }
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
-
- if ( admin == null || configs == null || configs.length == 0 )
- {
- serverFactory.updated( NtpServerFactory.DEFAULT_PID, NtpConfig.getDefaultConfig() );
- }
- }
- catch ( ConfigurationException ce )
- {
- ce.printStackTrace();
- }
- finally
- {
- if ( adminRef != null )
- {
- context.ungetService( adminRef );
- }
- }
- }
-
- /**
- * Implements BundleActivator.stop(). Logs that this service has stopped.
- * @param context the framework context for the bundle.
- */
- public void stop( BundleContext context )
- {
- log.debug( "Stopping Apache NTP." );
-
- registration.unregister();
- registration = null;
-
- serverFactory.destroy();
- serverFactory = null;
-
- registry = null;
- }
}
Modified: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpConfig.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpConfig.java?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpConfig.java (original)
+++ directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpConfig.java Mon Sep 5 17:57:54 2005
@@ -20,27 +20,20 @@
import java.util.Dictionary;
import java.util.Hashtable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.osgi.service.cm.ConfigurationException;
public class NtpConfig
{
- /** the log for this class */
- private static final Logger log = LoggerFactory.getLogger( NtpConfig.class );
-
- private static String SERVICE_PID = "service.pid";
-
- static String PID = "org.apache.ntp";
- static String NTP_PORT_KEY = "ntp.port";
- static int NTP_PORT_DEFAULT = 123;
- static String name = "Apache NTP Service";
+ private static final String SERVICE_PID = "service.pid";
+ private static final String PID = "org.apache.ntp";
+ private static final String NTP_PORT_KEY = "ntp.port";
+ private static final int NTP_DEFAULT_PORT = 123;
+ private static final String name = "Apache NTP Service";
private int port;
- public NtpConfig( Dictionary configuration )
+ public NtpConfig( Dictionary configuration ) throws ConfigurationException
{
- log.debug( "Got props " + configuration );
-
if ( configuration == null )
{
configuration = getDefaultConfig();
@@ -48,10 +41,13 @@
port = ( (Integer) configuration.get( NTP_PORT_KEY ) ).intValue();
- log.debug( "NTP got port " + port );
+ if ( port < 1 || port > 0xFFFF )
+ {
+ throw new ConfigurationException( NtpConfig.NTP_PORT_KEY, "invalid value=" + port );
+ }
}
- boolean isDifferent( Dictionary config )
+ public boolean isDifferent( Dictionary config )
{
if ( port == ( (Integer) config.get( NTP_PORT_KEY ) ).intValue() )
{
@@ -61,15 +57,13 @@
return true;
}
- String getName()
+ public String getName()
{
return name;
}
- int getPort()
+ public int getPort()
{
- log.debug( "Config returning " + port );
-
return port;
}
@@ -78,7 +72,7 @@
Dictionary defaults = new Hashtable();
defaults.put( SERVICE_PID, PID );
- defaults.put( NTP_PORT_KEY, Integer.getInteger( NTP_PORT_KEY, new Integer( NTP_PORT_DEFAULT ) ) );
+ defaults.put( NTP_PORT_KEY, Integer.getInteger( NTP_PORT_KEY, new Integer( NTP_DEFAULT_PORT ) ) );
return defaults;
}
Modified: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServer.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServer.java?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServer.java (original)
+++ directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServer.java Mon Sep 5 17:57:54 2005
@@ -48,17 +48,12 @@
this.config = config;
this.registry = registry;
- init();
- }
-
- protected void init()
- {
port = config.getPort();
name = config.getName();
try
{
- log.debug( name + " starting on " + port );
+ log.debug( name + " starting on port " + port );
provider = new NtpProtocolProvider();
@@ -72,7 +67,7 @@
}
catch ( IOException ioe )
{
- ioe.printStackTrace();
+ log.error( ioe.getMessage(), ioe );
}
}
@@ -91,6 +86,6 @@
udpService = null;
tcpService = null;
- log.debug( name + " has stopped listening on " + port );
+ log.debug( name + " has stopped listening on port " + port );
}
}
Modified: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java?rev=278897&r1=278896&r2=278897&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java (original)
+++ directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java Mon Sep 5 17:57:54 2005
@@ -23,74 +23,63 @@
import java.util.Map;
import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.ungoverned.gravity.servicebinder.Lifecycle;
-public class NtpServerFactory implements ManagedServiceFactory
+public class NtpServerFactory implements ManagedServiceFactory, Lifecycle
{
- final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
+ /** the log for this class */
+ private static final Logger log = LoggerFactory.getLogger( NtpServerFactory.class );
- private final ServiceRegistry registry;
- private final Map servers = new HashMap();
+ private static final String FACTORY_PID = "org.apache.ntp";
+ private static final String DEFAULT_PID = FACTORY_PID + ".default";
- Object updateLock = new Object();
-
- NtpServerFactory( ServiceRegistry registry )
- {
- this.registry = registry;
- }
-
- public void destroy()
- {
- Iterator it = servers.keySet().iterator();
- while ( it.hasNext() )
- {
- deleted( (String) it.next() );
- }
- }
+ private Map servers = new HashMap();
+ private Object updateLock = new Object();
+ private ServiceRegistry registry;
+ private ConfigurationAdmin cm;
public void updated( String pid, Dictionary config ) throws ConfigurationException
{
- int port = ( (Integer) config.get( NtpConfig.NTP_PORT_KEY ) ).intValue();
+ log.debug( getName() + " updating with " + config );
- if ( port < 1 || port > 0xFFFF )
- {
- throw new ConfigurationException( NtpConfig.NTP_PORT_KEY, "invalid value=" + port );
- }
+ NtpConfig ntpConfig = new NtpConfig( config );
synchronized ( updateLock )
{
- if ( DEFAULT_PID.equals( pid ) && servers.size() > 0 )
+ if ( pid.equals( DEFAULT_PID ) && servers.size() > 0 )
{
return;
}
- // As soon as we get a "non-default"-config, delete default
- if ( !DEFAULT_PID.equals( pid ) && ( null != servers.get( DEFAULT_PID ) ) )
+ // As soon as we get a non-default config, delete the default.
+ if ( !pid.equals( DEFAULT_PID ) )
{
deleted( DEFAULT_PID );
}
- // for a given pid-config, do we have the service?
- // if not, create it with the config
+ // For a given pid, do we have the service?
NtpServer ntpServer = (NtpServer) servers.get( pid );
+
+ // If we don't have the service, create it with the config.
if ( ntpServer == null )
{
- ntpServer = new NtpServer( new NtpConfig( config ), registry );
+ ntpServer = new NtpServer( ntpConfig, registry );
servers.put( pid, ntpServer );
+ return;
}
- else
+
+ // If we do have the service, re-create it if the config is different.
+ if ( ntpServer.isDifferent( config ) )
{
- // we have the service, so ...
- // for a given config, is the config different?
- if ( ntpServer.isDifferent( config ) )
- {
- // the config for this service is different, so ...
- // destroy the listener and recreate it with the new config.
- deleted( pid );
- ntpServer = new NtpServer( new NtpConfig( config ), registry );
- servers.put( pid, ntpServer );
- }
+ deleted( pid );
+ ntpServer = new NtpServer( ntpConfig, registry );
+ servers.put( pid, ntpServer );
}
}
}
@@ -108,5 +97,79 @@
public String getName()
{
return "Apache NTP Service Factory";
+ }
+
+ /**
+ * All required services have been bound, but our service(s) are not yet
+ * registered. So, we check the Config Admin service for configs or we
+ * start a server with its default properties.
+ */
+ public void activate()
+ {
+ try
+ {
+ Configuration[] configs = null;
+
+ if ( cm != null )
+ {
+ String filter = "(service.factoryPid=" + FACTORY_PID + ")";
+ configs = cm.listConfigurations( filter );
+
+ log.debug( "filter: " + filter );
+ log.debug( "configs.length: " + configs.length );
+ log.debug( "configs[ 0 ]: " + configs[ 0 ] );
+ }
+
+ if ( cm == null || configs == null || configs.length == 0 )
+ {
+ updated( NtpServerFactory.DEFAULT_PID, NtpConfig.getDefaultConfig() );
+ }
+ }
+ catch ( Exception e )
+ {
+ log.error( e.getMessage(), e );
+ }
+ finally
+ {
+ cm = null;
+ }
+ }
+
+ /**
+ * Invalidation has started and our services have been unregistered, but
+ * any required services have not been unbound yet.
+ */
+ public void deactivate()
+ {
+ Iterator it = servers.keySet().iterator();
+
+ while ( it.hasNext() )
+ {
+ deleted( (String) it.next() );
+ }
+ }
+
+ public void setServiceRegistry( ServiceRegistry registry )
+ {
+ this.registry = registry;
+ log.debug( getName() + " has bound to " + registry );
+ }
+
+ public void unsetServiceRegistry( ServiceRegistry registry )
+ {
+ this.registry = null;
+ log.debug( getName() + " has unbound from " + registry );
+ }
+
+ public void setConfigurationAdmin( ConfigurationAdmin cm )
+ {
+ this.cm = cm;
+ log.debug( getName() + " has bound to " + cm );
+ }
+
+ public void unsetConfigurationAdmin( ConfigurationAdmin cm )
+ {
+ this.cm = null;
+ log.debug( getName() + " has unbound from " + cm );
}
}
Added: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml?rev=278897&view=auto
==============================================================================
--- directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml (added)
+++ directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml Mon Sep 5 17:57:54 2005
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--<!DOCTYPE bundle SYSTEM "metadata.dtd">-->
+<bundle>
+ <component class="org.apache.ntp.NtpServerFactory">
+ <provides service="org.osgi.service.cm.ManagedServiceFactory"/>
+ <property name="service.factoryPid" value="org.apache.ntp" type="string"/>
+ <requires
+ service="org.apache.mina.registry.ServiceRegistry"
+ filter=""
+ cardinality="1..1"
+ policy="static"
+ bind-method="setServiceRegistry"
+ unbind-method="unsetServiceRegistry"
+ />
+ <requires
+ service="org.osgi.service.cm.ConfigurationAdmin"
+ filter=""
+ cardinality="0..n"
+ policy="dynamic"
+ bind-method="setConfigurationAdmin"
+ unbind-method="unsetConfigurationAdmin"
+ />
+ <!--<requires
+ service="javax.naming.spi.InitialContextFactory"
+ filter=""
+ cardinality="1..n"
+ policy="dynamic"
+ bind-method="setInitialContextFactory"
+ unbind-method="unsetInitialContextFactory"
+ />-->
+ </component>
+</bundle>
Propchange: directory/standalone/trunk/osgi/ntp/src/main/java/org/apache/ntp/metadata.xml
------------------------------------------------------------------------------
svn:eol-style = native