directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r1645870 - in /directory/shared/trunk/ldap/extras/codec: pom.xml src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java
Date Tue, 16 Dec 2014 07:39:41 GMT
Author: seelmann
Date: Tue Dec 16 07:39:41 2014
New Revision: 1645870

URL: http://svn.apache.org/r1645870
Log:
Fix NPE in case when o.a.d.a.l.extras.codec is started before o.a.d.a.l.code.core and no LdapApiService
is yet registered. Use ServiceTracker to decouple bundle start from service lookup and codec
registration.

Modified:
    directory/shared/trunk/ldap/extras/codec/pom.xml
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java

Modified: directory/shared/trunk/ldap/extras/codec/pom.xml
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/pom.xml?rev=1645870&r1=1645869&r2=1645870&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/pom.xml (original)
+++ directory/shared/trunk/ldap/extras/codec/pom.xml Tue Dec 16 07:39:41 2014
@@ -125,6 +125,7 @@
               org.apache.directory.api.asn1.ber.tlv;version=${project.version},
               org.apache.directory.api.i18n;version=${project.version},
               org.apache.directory.api.ldap.codec.api;version=${project.version},
+              org.apache.directory.api.ldap.codec.osgi;version=${project.version},
               org.apache.directory.api.ldap.extras.controls;version=${project.version},
               org.apache.directory.api.ldap.extras.controls.ad;version=${project.version},
               org.apache.directory.api.ldap.extras.controls.ppolicy;version=${project.version},
@@ -144,7 +145,9 @@
               org.apache.directory.api.ldap.model.name;version=${project.version},
               org.apache.directory.api.ldap.model.url;version=${project.version},
               org.apache.directory.api.util;version=${project.version},
-              org.slf4j;version=${slf4j.api.version}
+              org.slf4j;version=${slf4j.api.version},
+              org.osgi.framework;version="[1.0.0,2.0.0)",
+              org.osgi.util.tracker;version="[1.0.0,2.0.0)",
             </Import-Package>
             <Bundle-Activator>
               org.apache.directory.api.ldap.extras.ExtrasBundleActivator

Modified: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java?rev=1645870&r1=1645869&r2=1645870&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java
(original)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/ExtrasBundleActivator.java
Tue Dec 16 07:39:41 2014
@@ -53,6 +53,8 @@ import org.apache.directory.api.ldap.ext
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 
 /**
@@ -63,7 +65,45 @@ import org.osgi.framework.ServiceReferen
  */
 public class ExtrasBundleActivator implements BundleActivator
 {
-    private ServiceReference<?> codecServiceRef;
+
+    private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
+
+    class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
+    {
+
+        private BundleContext context;
+
+
+        public LdapApiServiceTracker( BundleContext context )
+        {
+            this.context = context;
+        }
+
+
+        @Override
+        public LdapApiService addingService( ServiceReference<LdapApiService> reference
)
+        {
+            LdapApiService ldapApiService = context.getService( reference );
+            registerExtrasControls( ldapApiService );
+            registerExtrasExtendedOps( ldapApiService );
+            return ldapApiService;
+        }
+
+
+        @Override
+        public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService
ldapApiService )
+        {
+        }
+
+
+        @Override
+        public void removedService( ServiceReference<LdapApiService> reference, LdapApiService
ldapApiService )
+        {
+            unregisterExtrasControls( ldapApiService );
+            unregisterExtrasExtendedOps( ldapApiService );
+        }
+
+    }
 
 
     /**
@@ -71,13 +111,12 @@ public class ExtrasBundleActivator imple
      */
     public void start( BundleContext context ) throws Exception
     {
-        codecServiceRef = context.getServiceReference( LdapApiService.class.getName() );
-        LdapApiService codec = ( LdapApiService ) context.getService( codecServiceRef );
-        registerExtrasControls( codec );
-        registerExtrasExtendedOps( codec );
+        LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker(context);
+        serviceTracker = new ServiceTracker<LdapApiService, LdapApiService>(
+            context, LdapApiService.class, ldapApiServiceTracker );
+        serviceTracker.open();
     }
-
-
+    
     /**
      * Registers all the extras controls present in this control pack.
      *
@@ -99,7 +138,7 @@ public class ExtrasBundleActivator imple
 
         ControlFactory<PasswordPolicy> passwordPolicyFactory = new PasswordPolicyFactory(
codec );
         codec.registerControl( passwordPolicyFactory );
-        
+
         ControlFactory<AdDirSync> adDirSyncFactory = new AdDirSyncFactory( codec );
         codec.registerControl( adDirSyncFactory );
     }
@@ -147,14 +186,23 @@ public class ExtrasBundleActivator imple
      */
     public void stop( BundleContext context ) throws Exception
     {
-        LdapApiService codec = ( LdapApiService ) context.getService( codecServiceRef );
+        serviceTracker.close();
+    }
 
+
+    private void unregisterExtrasControls( LdapApiService codec )
+    {
         codec.unregisterControl( SyncDoneValue.OID );
         codec.unregisterControl( SyncInfoValue.OID );
         codec.unregisterControl( SyncRequestValue.OID );
         codec.unregisterControl( SyncStateValue.OID );
         codec.unregisterControl( PasswordPolicy.OID );
+        codec.unregisterControl( AdDirSync.OID );
+    }
 
+
+    private void unregisterExtrasExtendedOps( LdapApiService codec )
+    {
         codec.unregisterExtendedRequest( CancelRequest.EXTENSION_OID );
         codec.unregisterExtendedRequest( CertGenerationRequest.EXTENSION_OID );
         codec.unregisterExtendedRequest( GracefulShutdownRequest.EXTENSION_OID );



Mime
View raw message