directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r291725 - in /directory/standalone/trunk/osgi/ldap: ./ src/main/java/org/apache/ldap/
Date Mon, 26 Sep 2005 20:04:06 GMT
Author: erodriguez
Date: Mon Sep 26 13:04:02 2005
New Revision: 291725

URL: http://svn.apache.org/viewcvs?rev=291725&view=rev
Log:
Update to LDAP protocol provider OSGi bundle:
o  Updated to use Service Binder.

Added:
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml   (with
props)
Removed:
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfiguration.java
Modified:
    directory/standalone/trunk/osgi/ldap/project.xml
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/Activator.java
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfig.java
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServer.java
    directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java

Modified: directory/standalone/trunk/osgi/ldap/project.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/project.xml?rev=291725&r1=291724&r2=291725&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ldap/project.xml (original)
+++ directory/standalone/trunk/osgi/ldap/project.xml Mon Sep 26 13:04:02 2005
@@ -6,7 +6,8 @@
   <currentVersion>0.1.1</currentVersion>
   <properties>
     <osgi.bundle.category>Network Service</osgi.bundle.category>
-    <osgi.import.package>org.apache.ldap.server.jndi,org.apache.ldap.common.filter,org.apache.ldap.server.configuration,org.osgi.framework,org.apache.mina.registry,org.apache.mina.protocol,org.apache.mina.protocol.handler,org.apache.mina.protocol.codec,org.apache.mina.common,org.osgi.service.cm,org.osgi.util.tracker</osgi.import.package>
+    <osgi.bundle.activator>org.apache.ldap.Activator</osgi.bundle.activator>
+    <osgi.import.package>org.ungoverned.gravity.servicebinder,org.apache.ldap.server.jndi,org.apache.ldap.common.filter,org.apache.ldap.server.configuration,org.osgi.framework,org.apache.mina.registry,org.apache.mina.protocol,org.apache.mina.protocol.handler,org.apache.mina.protocol.codec,org.apache.mina.common,org.osgi.service.cm</osgi.import.package>
   </properties>
   <package>org.apache.ldap</package>
   <shortDescription>Apache LDAP Network Service</shortDescription>

Modified: directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/Activator.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/Activator.java?rev=291725&r1=291724&r2=291725&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/Activator.java (original)
+++ directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/Activator.java Mon
Sep 26 13:04:02 2005
@@ -17,140 +17,8 @@
 
 package org.apache.ldap;
 
-import java.util.Dictionary;
-import java.util.Hashtable;
+import org.ungoverned.gravity.servicebinder.GenericActivator;
 
-import javax.naming.Context;
-import javax.naming.spi.InitialContextFactory;
-
-import org.apache.ldap.server.configuration.StartupConfiguration;
-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;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * This class implements a simple bundle that starts and stops the Apache LDAP 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.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
-    {
-        log.debug( "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( new StartupConfiguration().toJndiEnvironment() );
-        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 )
-    {
-        log.debug( "Stopping Apache LDAP." );
-
-        registration.unregister();
-        registration = null;
-
-        serverFactory.destroy();
-        serverFactory = null;
-
-        registry = null;
-    }
-
-    private void loadEnvironment( Hashtable env )
-    {
-        env.put( Context.PROVIDER_URL, "ou=system" );
-        env.put( Context.INITIAL_CONTEXT_FACTORY, "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" );
-    }
 }

Modified: directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfig.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfig.java?rev=291725&r1=291724&r2=291725&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfig.java (original)
+++ directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapConfig.java Mon
Sep 26 13:04:02 2005
@@ -20,28 +20,43 @@
 import java.util.Dictionary;
 import java.util.Hashtable;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.ldap.server.configuration.Configuration;
+import org.apache.ldap.server.jndi.ContextFactoryService;
+import org.osgi.service.cm.ConfigurationException;
 
-public class LdapConfig
+public class LdapConfig extends Configuration
 {
-    /** the log for this class */
-    private static final Logger log = LoggerFactory.getLogger( LdapConfig.class );
-
     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 static String PID = "org.apache.ldap";
+    private static String LDAP_PORT_KEY = "ldap.port";
+    private static String LDAPS_PORT_KEY = "ldaps.port";
+    private static int LDAP_PORT_DEFAULT = 389;
+    private static int LDAPS_PORT_DEFAULT = 636;
+    private static String name = "Apache LDAP Service";
 
     private int port;
+    private int securePort;
+
+    /**
+     * Creates a new instance with default settings.
+     */
+    public LdapConfig() throws ConfigurationException
+    {
+        this( getDefaultConfig() );
+    }
 
-    public LdapConfig( Dictionary configuration )
+    /**
+     * Creates a new instance with default settings that operates on the
+     * {@link ContextFactoryService} with the specified ID.
+     */
+    public LdapConfig( String instanceId ) throws ConfigurationException
     {
-        log.debug( "Got props " + configuration );
+        this( getDefaultConfig() );
+        setInstanceId( instanceId );
+    }
 
+    public LdapConfig( Dictionary configuration ) throws ConfigurationException
+    {
         if ( configuration == null )
         {
             configuration = getDefaultConfig();
@@ -49,37 +64,56 @@
 
         port = ( (Integer) configuration.get( LDAP_PORT_KEY ) ).intValue();
 
-        log.debug( "LDAP got port " + port );
+        if ( port < 1 || port > 0xFFFF )
+        {
+            throw new ConfigurationException( LDAP_PORT_KEY, "invalid value=" + port );
+        }
+
+        securePort = ( (Integer) configuration.get( LDAPS_PORT_KEY ) ).intValue();
+
+        if ( securePort < 1 || securePort > 0xFFFF )
+        {
+            throw new ConfigurationException( LDAPS_PORT_KEY, "invalid value=" + securePort
);
+        }
     }
 
-    boolean isDifferent( Dictionary config )
+    public boolean isDifferent( Dictionary config )
     {
-        if ( port == ( (Integer) config.get( LDAP_PORT_KEY ) ).intValue() )
+        if ( port != ( (Integer) config.get( LDAP_PORT_KEY ) ).intValue() )
+        {
+            return true;
+        }
+
+        if ( securePort != ( (Integer) config.get( LDAPS_PORT_KEY ) ).intValue() )
         {
-            return false;
+            return true;
         }
 
         return true;
     }
 
-    String getName()
+    public String getName()
     {
         return name;
     }
 
-    int getPort()
+    public int getPort()
     {
-        log.debug( "Config returning " + port );
-
         return port;
     }
 
+    public int getSecurePort()
+    {
+        return securePort;
+    }
+
     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
) ) );
+        defaults.put( LDAPS_PORT_KEY, Integer.getInteger( LDAPS_PORT_KEY, new Integer( LDAPS_PORT_DEFAULT
) ) );
 
         return defaults;
     }

Modified: directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServer.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServer.java?rev=291725&r1=291724&r2=291725&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServer.java (original)
+++ directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServer.java Mon
Sep 26 13:04:02 2005
@@ -17,9 +17,11 @@
 
 package org.apache.ldap;
 
+import java.io.IOException;
 import java.util.Dictionary;
 import java.util.Hashtable;
 
+import org.apache.ldap.common.exception.LdapNamingException;
 import org.apache.ldap.server.protocol.LdapProtocolProvider;
 import org.apache.mina.common.TransportType;
 import org.apache.mina.protocol.ProtocolProvider;
@@ -38,7 +40,6 @@
     private Service tcpService;
 
     private LdapConfig config;
-    private Hashtable env;
 
     private String name;
     private int port = -1;
@@ -47,13 +48,7 @@
     {
         this.config = config;
         this.registry = registry;
-        this.env = env;
 
-        init();
-    }
-
-    protected void init()
-    {
         port = config.getPort();
         name = config.getName();
 
@@ -69,9 +64,13 @@
 
             log.debug( name + " listening on port " + port );
         }
-        catch ( Exception e )
+        catch ( LdapNamingException lne )
+        {
+            log.error( lne.getMessage(), lne );
+        }
+        catch ( IOException ioe )
         {
-            e.printStackTrace();
+            log.error( ioe.getMessage(), ioe );
         }
     }
 
@@ -88,6 +87,6 @@
         provider = 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/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java?rev=291725&r1=291724&r2=291725&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java
(original)
+++ directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/LdapServerFactory.java
Mon Sep 26 13:04:02 2005
@@ -17,99 +17,194 @@
 
 package org.apache.ldap;
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Map;
 
+import javax.naming.Context;
+import javax.naming.spi.InitialContextFactory;
+
+import org.apache.ldap.server.configuration.StartupConfiguration;
 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 LdapServerFactory implements ManagedServiceFactory
+public class LdapServerFactory implements ManagedServiceFactory, Lifecycle
 {
-    final static String DEFAULT_PID = Activator.FACTORY_PID + ".default";
-
-    private final Hashtable env;
-    private final ServiceRegistry registry;
-    private final Map servers = new HashMap();
+    /** the log for this class */
+    private static final Logger log = LoggerFactory.getLogger( LdapServerFactory.class );
 
-    Object updateLock = new Object();
+    private static final String FACTORY_PID = "org.apache.ldap";
+    private static final String DEFAULT_PID = FACTORY_PID + ".default";
 
-    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() );
-        }
-    }
+    private Map servers = Collections.synchronizedMap( new HashMap() );
+    private ServiceRegistry registry;
+    private ConfigurationAdmin cm;
+    private InitialContextFactory factory;
+    private Hashtable env;
 
     public void updated( String pid, Dictionary config ) throws ConfigurationException
     {
-        int port = ( (Integer) config.get( LdapConfig.LDAP_PORT_KEY ) ).intValue();
+        log.debug( getName() + " updating with " + config );
 
-        if ( port < 1 || port > 0xFFFF )
-        {
-            throw new ConfigurationException( LdapConfig.LDAP_PORT_KEY, "invalid value="
+ port );
-        }
+        LdapConfig ldapConfig = new LdapConfig( config );
 
-        synchronized ( updateLock )
+        synchronized ( servers )
         {
-            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
) ) )
+            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?
             LdapServer server = (LdapServer) servers.get( pid );
-            if ( server == null )
+
+            // If we don't have the service, create it with the config.
+            // Or, if we do have the service, re-create it if the config is different.
+            if ( server == null || server.isDifferent( config ) )
             {
-                server = new LdapServer( new LdapConfig( config ), registry, env );
+                deleted( pid );
+                server = new LdapServer( ldapConfig, 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 )
+        synchronized ( servers )
         {
-            server.destroy();
+            LdapServer server = (LdapServer) servers.remove( pid );
+
+            if ( server != null )
+            {
+                server.destroy();
+            }
         }
     }
 
     public String getName()
     {
         return "Apache LDAP 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
+        {
+            env = new Hashtable( new StartupConfiguration().toJndiEnvironment() );
+            loadEnvironment( env );
+
+            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 );
+            }
+
+            if ( cm == null || configs == null || configs.length == 0 )
+            {
+                updated( DEFAULT_PID, LdapConfig.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()
+    {
+        synchronized ( servers )
+        {
+            Iterator it = servers.values().iterator();
+
+            while ( it.hasNext() )
+            {
+                LdapServer ldapServer = (LdapServer) it.next();
+                ldapServer.destroy();
+            }
+
+            servers.clear();
+        }
+    }
+
+    public void setInitialContextFactory( InitialContextFactory factory )
+    {
+        this.factory = factory;
+        log.debug( getName() + " has bound to " + factory );
+    }
+
+    public void unsetInitialContextFactory( InitialContextFactory factory )
+    {
+        this.factory = null;
+        log.debug( getName() + " has unbound from " + factory );
+    }
+
+    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 );
+    }
+
+    private void loadEnvironment( Hashtable env )
+    {
+        env.put( Context.PROVIDER_URL, "ou=system" );
+        env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory"
);
+
+        env.put( "server.use.factory.instance", factory );
+        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/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml?rev=291725&view=auto
==============================================================================
--- directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml (added)
+++ directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml Mon Sep
26 13:04:02 2005
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--<!DOCTYPE bundle SYSTEM "metadata.dtd">-->
+<bundle>
+  <component class="org.apache.ldap.LdapServerFactory">
+    <provides service="org.osgi.service.cm.ManagedServiceFactory"/>
+    <property name="service.pid" value="org.apache.ldap.factory" 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..1"
+        policy="static"
+        bind-method="setInitialContextFactory"
+        unbind-method="unsetInitialContextFactory"
+    />
+  </component>
+</bundle>

Propchange: directory/standalone/trunk/osgi/ldap/src/main/java/org/apache/ldap/metadata.xml
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message