directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r178402 - in /directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp: Activator.java NtpConfig.java NtpServer.java NtpServerFactory.java
Date Wed, 25 May 2005 07:20:15 GMT
Author: erodriguez
Date: Wed May 25 00:20:14 2005
New Revision: 178402

URL: http://svn.apache.org/viewcvs?rev=178402&view=rev
Log:
OSGi wrapper for NTP protocol provider.

Added:
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/Activator.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpConfig.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServer.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/Activator.java?rev=178402&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/Activator.java
(added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/Activator.java
Wed May 25 00:20:14 2005
@@ -0,0 +1,126 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.ntp;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+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;
+
+/**
+ * This class implements a simple bundle that starts and stops the Apache NTP service.
+ */
+public class Activator implements BundleActivator
+{
+    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)
+    {
+        System.out.println("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)
+    {
+        System.out.println("Stopping Apache NTP.");
+
+        registration.unregister();
+        registration = null;
+
+        serverFactory.destroy();
+        serverFactory = null;
+
+        registry = null;
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpConfig.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpConfig.java?rev=178402&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpConfig.java
(added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpConfig.java
Wed May 25 00:20:14 2005
@@ -0,0 +1,79 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.ntp;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+public class NtpConfig
+{
+    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 int port;
+
+    public NtpConfig(Dictionary configuration)
+    {
+        System.out.println("Got props " + configuration);
+
+        if (configuration == null)
+        {
+            configuration = getDefaultConfig();
+        }
+
+        port = ((Integer) configuration.get(NTP_PORT_KEY)).intValue();
+
+        System.out.println("NTP got port " + port);
+    }
+
+    boolean isDifferent(Dictionary config)
+    {
+        if (port == ((Integer) config.get(NTP_PORT_KEY)).intValue())
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    String getName()
+    {
+        return name;
+    }
+
+    int getPort()
+    {
+        System.out.println("Config returning " + port);
+
+        return port;
+    }
+
+    public static Dictionary getDefaultConfig()
+    {
+        Dictionary defaults = new Hashtable();
+
+        defaults.put(SERVICE_PID, PID);
+        defaults.put(NTP_PORT_KEY, Integer.getInteger(NTP_PORT_KEY, new Integer(NTP_PORT_DEFAULT)));
+
+        return defaults;
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServer.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServer.java?rev=178402&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServer.java
(added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServer.java
Wed May 25 00:20:14 2005
@@ -0,0 +1,91 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.ntp;
+
+import java.io.IOException;
+import java.util.Dictionary;
+
+import org.apache.mina.common.TransportType;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.registry.Service;
+import org.apache.mina.registry.ServiceRegistry;
+import org.apache.ntp.protocol.NtpProtocolProvider;
+
+public class NtpServer
+{
+    private ServiceRegistry registry;
+    private ProtocolProvider provider;
+    private Service tcpService;
+    private Service udpService;
+
+    private NtpConfig config;
+
+    private String name;
+    private int port = -1;
+
+    public NtpServer(NtpConfig config, ServiceRegistry registry)
+    {
+        this.config = config;
+        this.registry = registry;
+
+        init();
+    }
+
+    protected void init()
+    {
+        port = config.getPort();
+        name = config.getName();
+
+        try
+        {
+            System.out.println(name + " starting on " + port);
+
+            provider = new NtpProtocolProvider();
+
+            udpService = new Service(name, TransportType.DATAGRAM, port);
+            tcpService = new Service(name, TransportType.SOCKET, port);
+
+            registry.bind(udpService, provider);
+            registry.bind(tcpService, provider);
+
+            System.out.println(name + " listening on port " + port);
+        }
+        catch (IOException ioe)
+        {
+            ioe.printStackTrace();
+        }
+    }
+
+    public boolean isDifferent(Dictionary newConfig)
+    {
+        return config.isDifferent(newConfig);
+    }
+
+    public void destroy()
+    {
+        registry.unbind(udpService);
+        registry.unbind(tcpService);
+
+        registry = null;
+        provider = null;
+        udpService = null;
+        tcpService = null;
+
+        System.out.println(name + " has stopped listening on " + port);
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java?rev=178402&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java
(added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ntp/src/main/java/org/apache/ntp/NtpServerFactory.java
Wed May 25 00:20:14 2005
@@ -0,0 +1,112 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.ntp;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+public class NtpServerFactory implements ManagedServiceFactory
+{
+    final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
+
+    private final ServiceRegistry registry;
+    private final Map servers = new HashMap();
+
+    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());
+        }
+    }
+
+    public void updated(String pid, Dictionary config) throws ConfigurationException
+    {
+        int port = ((Integer) config.get(NtpConfig.NTP_PORT_KEY)).intValue();
+
+        if (port < 1 || port > 0xFFFF)
+        {
+            throw new ConfigurationException(NtpConfig.NTP_PORT_KEY, "invalid value=" + port);
+        }
+
+        synchronized (updateLock)
+        {
+            if (DEFAULT_PID.equals(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)))
+            {
+                deleted(DEFAULT_PID);
+            }
+
+            // for a given pid-config, do we have the service?
+            // if not, create it with the config
+            NtpServer ntpServer = (NtpServer) servers.get(pid);
+            if (ntpServer == null)
+            {
+                ntpServer = new NtpServer(new NtpConfig(config), registry);
+                servers.put(pid, ntpServer);
+            }
+            else
+            {
+                // 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);
+                }
+            }
+        }
+    }
+
+    public void deleted(String pid)
+    {
+        NtpServer ntpServer = (NtpServer) servers.remove(pid);
+
+        if (ntpServer != null)
+        {
+            ntpServer.destroy();
+        }
+    }
+
+    public String getName()
+    {
+        return "Apache NTP Service Factory";
+    }
+}



Mime
View raw message