Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 25215 invoked from network); 25 May 2005 07:19:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 25 May 2005 07:19:29 -0000 Received: (qmail 94618 invoked by uid 500); 25 May 2005 07:19:27 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 94580 invoked by uid 500); 25 May 2005 07:19:26 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 94539 invoked by uid 99); 25 May 2005 07:19:26 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Wed, 25 May 2005 00:19:20 -0700 Received: (qmail 19180 invoked by uid 65534); 25 May 2005 07:19:13 -0000 Message-ID: <20050525071913.19179.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: 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 Date: Wed, 25 May 2005 07:19:12 -0000 To: commits@directory.apache.org From: erodriguez@apache.org X-Mailer: svnmailer-1.0.0-dev X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: erodriguez Date: Wed May 25 00:19:12 2005 New Revision: 178401 URL: http://svn.apache.org/viewcvs?rev=3D178401&view=3Drev Log: OSGi wrapper for LDAP prototol provider. Added: directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/Activator.java directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/LdapConfig.java directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/LdapServer.java directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/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-pr= oviders/trunk/ldap/src/main/java/org/apache/ldap/Activator.java?rev=3D17840= 1&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/Activator.java (added) +++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/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 impli= ed. + * 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 =3D "org.apache.ldap"; + + private ServiceRegistry registry; + private ServiceTracker tracker; + + private LdapServerFactory serverFactory =3D null; + private ServiceRegistration registration =3D 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 =3D new ServiceTracker(context, ServiceRegistry.class.getN= ame(), null); + tracker.open(); + + registry =3D (ServiceRegistry) tracker.getService(); + + ServiceReference storeReference =3D context.getServiceReference(In= itialContextFactory.class + .getName()); + + InitialContextFactory factory =3D (InitialContextFactory) context.= getService(storeReference); + + Hashtable env =3D new Hashtable(); + loadEnvironment(env); + + env.put("server.use.factory.instance", factory); + + serverFactory =3D new LdapServerFactory(registry, env); + + Dictionary parameters =3D new Hashtable(); + parameters.put(ConfigurationAdmin.SERVICE_FACTORYPID, FACTORY_PID); + registration =3D context.registerService(ManagedServiceFactory.cla= ss.getName(), + serverFactory, parameters); + + ServiceReference adminRef =3D null; + try + { + ConfigurationAdmin admin =3D null; + Configuration[] configs =3D null; + try + { + adminRef =3D context.getServiceReference(ConfigurationAdmi= n=2Eclass.getName()); + + // Potential start order problem! + if (adminRef !=3D null) + { + admin =3D (ConfigurationAdmin) context.getService(admi= nRef); + String filter =3D "(&(service.factoryPid=3D" + FACTORY= _PID + ")" + + "(|(service.bundleLocation=3D" + context.get= Bundle().getLocation() + + ")" + "(service.bundleLocation=3DNULL)" + + "(!(service.bundleLocation=3D*))))"; + configs =3D admin.listConfigurations(filter); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + + if (admin =3D=3D null || configs =3D=3D null || configs.length= =3D=3D 0) + { + serverFactory.updated(LdapServerFactory.DEFAULT_PID, LdapC= onfig.getDefaultConfig()); + } + } + catch (ConfigurationException ce) + { + ce.printStackTrace(); + } + finally + { + if (adminRef !=3D 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 =3D null; + + serverFactory.destroy(); + serverFactory =3D null; + + registry =3D null; + } + + private void loadEnvironment(Hashtable env) + { + env.put("java.naming.provider.url", "ou=3Dsystem"); + env.put("java.naming.security.principal", "uid=3Dadmin,ou=3Dsystem= "); + env.put("java.naming.security.authentication", "simple"); + env.put("java.naming.security.credentials", "secret"); + env.put("java.naming.factory.initial", "org.apache.ldap.server.jnd= i=2ECoreContextFactory"); + + env.put("asn.1.berlib.provider", "org.apache.ldap.common.berlib.as= n1.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-pr= oviders/trunk/ldap/src/main/java/org/apache/ldap/LdapConfig.java?rev=3D1784= 01&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/LdapConfig.java (added) +++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/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 impli= ed. + * 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 =3D "service.pid"; + + static String PID =3D "org.apache.ldap"; + static String LDAP_PORT_KEY =3D "ldap.port"; + static int LDAP_PORT_DEFAULT =3D 389; + static int LDAPS_PORT_DEFAULT =3D 636; + static String name =3D "Apache LDAP Service"; + + private int port; + + public LdapConfig(Dictionary configuration) + { + System.out.println("Got props " + configuration); + + if (configuration =3D=3D null) + { + configuration =3D getDefaultConfig(); + } + + port =3D ((Integer) configuration.get(LDAP_PORT_KEY)).intValue(); + + System.out.println("LDAP got port " + port); + } + + boolean isDifferent(Dictionary config) + { + if (port =3D=3D ((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 =3D 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-pr= oviders/trunk/ldap/src/main/java/org/apache/ldap/LdapServer.java?rev=3D1784= 01&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/LdapServer.java (added) +++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/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 impli= ed. + * 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 =3D -1; + + public LdapServer(LdapConfig config, ServiceRegistry registry, Hashtab= le env) + { + this.config =3D config; + this.registry =3D registry; + this.env =3D env; + + init(); + } + + protected void init() + { + port =3D config.getPort(); + name =3D config.getName(); + + try + { + System.out.println(name + " starting on " + port); + + provider =3D new LdapProtocolProvider((Hashtable) env.clone()); + + tcpService =3D 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 =3D null; + provider =3D null; + tcpService =3D 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-pr= oviders/trunk/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java?rev= =3D178401&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/org/apache/ldap/LdapServerFactory.java (added) +++ directory/sandbox/trunk/osgi-protocol-providers/trunk/ldap/src/main/jav= a/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 impli= ed. + * 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 =3D Activator.FACTORY_PID + ".default"; + + private final Hashtable env; + private final ServiceRegistry registry; + private final Map servers =3D new HashMap(); + + Object updateLock =3D new Object(); + + LdapServerFactory(ServiceRegistry registry, Hashtable env) + { + this.registry =3D registry; + this.env =3D env; + } + + public void destroy() + { + Iterator it =3D servers.keySet().iterator(); + while (it.hasNext()) + { + deleted((String) it.next()); + } + } + + public void updated(String pid, Dictionary config) throws Configuratio= nException + { + int port =3D ((Integer) config.get(LdapConfig.LDAP_PORT_KEY)).intV= alue(); + + if (port < 1 || port > 0xFFFF) + { + throw new ConfigurationException(LdapConfig.LDAP_PORT_KEY, "in= valid value=3D" + 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 !=3D 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 =3D (LdapServer) servers.get(pid); + if (server =3D=3D null) + { + server =3D 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 c= onfig. + deleted(pid); + server =3D new LdapServer(new LdapConfig(config), regi= stry, env); + servers.put(pid, server); + } + } + } + } + + public void deleted(String pid) + { + LdapServer server =3D (LdapServer) servers.remove(pid); + + if (server !=3D null) + { + server.destroy(); + } + } + + public String getName() + { + return "Apache LDAP Service Factory"; + } +}