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/05/25 09:18:08 UTC

svn commit: r178400 - in /directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos: Activator.java KerberosConfig.java KerberosServer.java KerberosServerFactory.java

Author: erodriguez
Date: Wed May 25 00:18:08 2005
New Revision: 178400

URL: http://svn.apache.org/viewcvs?rev=178400&view=rev
Log:
OSGi wrapper for Kerberos prototol provider.

Added:
    directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/Activator.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosConfig.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServer.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServerFactory.java

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/Activator.java?rev=178400&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/Activator.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/Activator.java Wed May 25 00:18:08 2005
@@ -0,0 +1,140 @@
+/*
+ *   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.kerberos;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+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 Kerberos service.
+ */
+public class Activator implements BundleActivator
+{
+    public static final String FACTORY_PID = "org.apache.kerberos";
+
+    private ServiceRegistry registry;
+    private ServiceTracker tracker;
+
+    private KerberosServerFactory serverFactory = null;
+    private ServiceRegistration registration = null;
+
+    /**
+     * 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) throws BundleException
+    {
+        System.out.println("Starting Apache Kerberos.");
+
+        tracker = new ServiceTracker(context, ServiceRegistry.class.getName(), null);
+        tracker.open();
+
+        registry = (ServiceRegistry) tracker.getService();
+
+        ServiceReference storeReference = context.getServiceReference(PrincipalStore.class
+                .getName());
+
+        Object ref = context.getService(storeReference);
+
+        System.out.println("PrincipalStore retrieval got " + ref);
+
+        PrincipalStore store = (PrincipalStore) ref;
+
+        serverFactory = new KerberosServerFactory(registry, store);
+
+        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(KerberosServerFactory.DEFAULT_PID, KerberosConfig
+                        .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 Kerberos.");
+
+        registration.unregister();
+        registration = null;
+
+        serverFactory.destroy();
+        serverFactory = null;
+
+        registry = null;
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosConfig.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosConfig.java?rev=178400&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosConfig.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosConfig.java Wed May 25 00:18:08 2005
@@ -0,0 +1,80 @@
+/*
+ *   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.kerberos;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+public class KerberosConfig
+{
+    private static String SERVICE_PID = "service.pid";
+
+    static String PID = "org.apache.kerberos";
+    static String KERBEROS_PORT_KEY = "kerberos.port";
+    static int KERBEROS_PORT_DEFAULT = 88;
+    static String name = "Apache Kerberos Service";
+
+    private int port;
+
+    public KerberosConfig(Dictionary configuration)
+    {
+        System.out.println("Got props " + configuration);
+
+        if (configuration == null)
+        {
+            configuration = getDefaultConfig();
+        }
+
+        port = ((Integer) configuration.get(KERBEROS_PORT_KEY)).intValue();
+
+        System.out.println("Kerberos got port " + port);
+    }
+
+    boolean isDifferent(Dictionary config)
+    {
+        if (port == ((Integer) config.get(KERBEROS_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(KERBEROS_PORT_KEY, Integer.getInteger(KERBEROS_PORT_KEY, new Integer(
+                KERBEROS_PORT_DEFAULT)));
+
+        return defaults;
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServer.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServer.java?rev=178400&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServer.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServer.java Wed May 25 00:18:08 2005
@@ -0,0 +1,95 @@
+/*
+ *   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.kerberos;
+
+import java.io.IOException;
+import java.util.Dictionary;
+
+import org.apache.kerberos.protocol.KerberosProtocolProvider;
+import org.apache.kerberos.service.KdcConfiguration;
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.registry.Service;
+import org.apache.mina.registry.ServiceRegistry;
+
+public class KerberosServer
+{
+    private ServiceRegistry registry;
+    private ProtocolProvider provider;
+    private Service tcpService;
+    private Service udpService;
+
+    private KerberosConfig config;
+    private PrincipalStore store;
+
+    private String name;
+    private int port = -1;
+
+    public KerberosServer(KerberosConfig config, ServiceRegistry registry, PrincipalStore store)
+    {
+        this.config = config;
+        this.registry = registry;
+        this.store = store;
+
+        init();
+    }
+
+    protected void init()
+    {
+        port = config.getPort();
+        name = config.getName();
+
+        try
+        {
+            System.out.println(name + " starting on " + port);
+
+            provider = new KerberosProtocolProvider(new KdcConfiguration(), store);
+
+            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/kerberos/src/main/java/org/apache/kerberos/KerberosServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServerFactory.java?rev=178400&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServerFactory.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/kerberos/src/main/java/org/apache/kerberos/KerberosServerFactory.java Wed May 25 00:18:08 2005
@@ -0,0 +1,116 @@
+/*
+ *   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.kerberos;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.kerberos.store.PrincipalStore;
+import org.apache.mina.registry.ServiceRegistry;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+public class KerberosServerFactory implements ManagedServiceFactory
+{
+    final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
+
+    private final PrincipalStore store;
+    private final ServiceRegistry registry;
+    private final Map servers = new HashMap();
+
+    Object updateLock = new Object();
+
+    KerberosServerFactory(ServiceRegistry registry, PrincipalStore store)
+    {
+        this.registry = registry;
+        this.store = store;
+    }
+
+    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(KerberosConfig.KERBEROS_PORT_KEY)).intValue();
+
+        if (port < 1 || port > 0xFFFF)
+        {
+            throw new ConfigurationException(KerberosConfig.KERBEROS_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
+            KerberosServer server = (KerberosServer) servers.get(pid);
+            if (server == null)
+            {
+                server = new KerberosServer(new KerberosConfig(config), registry, store);
+                servers.put(pid, server);
+            }
+            else
+            {
+                // we have the service, so ...
+                // for a given config, is the config different?
+                if (server.isDifferent(config))
+                {
+                    // the config for this service is different, so ...
+                    // destroy the listener and recreate it with the new config.
+                    deleted(pid);
+                    server = new KerberosServer(new KerberosConfig(config), registry, store);
+                    servers.put(pid, server);
+                }
+            }
+        }
+    }
+
+    public void deleted(String pid)
+    {
+        KerberosServer server = (KerberosServer) servers.remove(pid);
+
+        if (server != null)
+        {
+            server.destroy();
+        }
+    }
+
+    public String getName()
+    {
+        return "Apache Kerberos Service Factory";
+    }
+}