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:19:12 UTC

svn commit: r178401 - in /directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap: Activator.java LdapConfig.java LdapServer.java LdapServerFactory.java

Author: erodriguez
Date: Wed May 25 00:19:12 2005
New Revision: 178401

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

Added:
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/Activator.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java
    directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/Activator.java?rev=178401&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/Activator.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/Activator.java Wed May 25 00:19:12 2005
@@ -0,0 +1,155 @@
+/*
+ *   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.ldap;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.naming.spi.InitialContextFactory;
+
+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 LDAP service.
+ */
+public class Activator implements BundleActivator
+{
+    public static final String FACTORY_PID = "org.apache.ldap";
+
+    private ServiceRegistry registry;
+    private ServiceTracker tracker;
+
+    private LdapServerFactory 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 LDAP.");
+
+        tracker = new ServiceTracker(context, ServiceRegistry.class.getName(), null);
+        tracker.open();
+
+        registry = (ServiceRegistry) tracker.getService();
+
+        ServiceReference storeReference = context.getServiceReference(InitialContextFactory.class
+                .getName());
+
+        InitialContextFactory factory = (InitialContextFactory) context.getService(storeReference);
+
+        Hashtable env = new Hashtable();
+        loadEnvironment(env);
+
+        env.put("server.use.factory.instance", factory);
+
+        serverFactory = new LdapServerFactory(registry, env);
+
+        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(LdapServerFactory.DEFAULT_PID, LdapConfig.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 LDAP.");
+
+        registration.unregister();
+        registration = null;
+
+        serverFactory.destroy();
+        serverFactory = null;
+
+        registry = null;
+    }
+
+    private void loadEnvironment(Hashtable env)
+    {
+        env.put("java.naming.provider.url", "ou=system");
+        env.put("java.naming.security.principal", "uid=admin,ou=system");
+        env.put("java.naming.security.authentication", "simple");
+        env.put("java.naming.security.credentials", "secret");
+        env.put("java.naming.factory.initial", "org.apache.ldap.server.jndi.CoreContextFactory");
+
+        env.put("asn.1.berlib.provider", "org.apache.ldap.common.berlib.asn1.SnickersProvider");
+        env.put("server.disable.anonymous", "true");
+        env.put("server.net.ldap.port", "389");
+        env.put("server.net.ldaps.port", "636");
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java?rev=178401&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java Wed May 25 00:19:12 2005
@@ -0,0 +1,81 @@
+/*
+ *   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.ldap;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+public class LdapConfig
+{
+    private static String SERVICE_PID = "service.pid";
+
+    static String PID = "org.apache.ldap";
+    static String LDAP_PORT_KEY = "ldap.port";
+    static int LDAP_PORT_DEFAULT = 389;
+    static int LDAPS_PORT_DEFAULT = 636;
+    static String name = "Apache LDAP Service";
+
+    private int port;
+
+    public LdapConfig(Dictionary configuration)
+    {
+        System.out.println("Got props " + configuration);
+
+        if (configuration == null)
+        {
+            configuration = getDefaultConfig();
+        }
+
+        port = ((Integer) configuration.get(LDAP_PORT_KEY)).intValue();
+
+        System.out.println("LDAP got port " + port);
+    }
+
+    boolean isDifferent(Dictionary config)
+    {
+        if (port == ((Integer) config.get(LDAP_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(LDAP_PORT_KEY, Integer.getInteger(LDAP_PORT_KEY,
+                new Integer(LDAP_PORT_DEFAULT)));
+
+        return defaults;
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java?rev=178401&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java Wed May 25 00:19:12 2005
@@ -0,0 +1,88 @@
+/*
+ *   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.ldap;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.ldap.server.protocol.LdapProtocolProvider;
+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 LdapServer
+{
+    private ServiceRegistry registry;
+    private ProtocolProvider provider;
+    private Service tcpService;
+
+    private LdapConfig config;
+    private Hashtable env;
+
+    private String name;
+    private int port = -1;
+
+    public LdapServer(LdapConfig config, ServiceRegistry registry, Hashtable env)
+    {
+        this.config = config;
+        this.registry = registry;
+        this.env = env;
+
+        init();
+    }
+
+    protected void init()
+    {
+        port = config.getPort();
+        name = config.getName();
+
+        try
+        {
+            System.out.println(name + " starting on " + port);
+
+            provider = new LdapProtocolProvider((Hashtable) env.clone());
+
+            tcpService = new Service(name, TransportType.SOCKET, port);
+
+            registry.bind(tcpService, provider);
+
+            System.out.println(name + " listening on port " + port);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    public boolean isDifferent(Dictionary newConfig)
+    {
+        return config.isDifferent(newConfig);
+    }
+
+    public void destroy()
+    {
+        registry.unbind(tcpService);
+
+        registry = null;
+        provider = null;
+        tcpService = null;
+
+        System.out.println(name + " has stopped listening on " + port);
+    }
+}

Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java?rev=178401&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java (added)
+++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java Wed May 25 00:19:12 2005
@@ -0,0 +1,115 @@
+/*
+ *   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.ldap;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+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 LdapServerFactory implements ManagedServiceFactory
+{
+    final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
+
+    private final Hashtable env;
+    private final ServiceRegistry registry;
+    private final Map servers = new HashMap();
+
+    Object updateLock = new Object();
+
+    LdapServerFactory(ServiceRegistry registry, Hashtable env)
+    {
+        this.registry = registry;
+        this.env = env;
+    }
+
+    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(LdapConfig.LDAP_PORT_KEY)).intValue();
+
+        if (port < 1 || port > 0xFFFF)
+        {
+            throw new ConfigurationException(LdapConfig.LDAP_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
+            LdapServer server = (LdapServer) servers.get(pid);
+            if (server == null)
+            {
+                server = new LdapServer(new LdapConfig(config), registry, env);
+                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 LdapServer(new LdapConfig(config), registry, env);
+                    servers.put(pid, server);
+                }
+            }
+        }
+    }
+
+    public void deleted(String pid)
+    {
+        LdapServer server = (LdapServer) servers.remove(pid);
+
+        if (server != null)
+        {
+            server.destroy();
+        }
+    }
+
+    public String getName()
+    {
+        return "Apache LDAP Service Factory";
+    }
+}