View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.protocol.mina;
21  
22  
23  import org.apache.directory.api.ldap.codec.api.LdapApiService;
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.osgi.framework.ServiceReference;
27  import org.osgi.framework.ServiceRegistration;
28  import org.osgi.util.tracker.ServiceTracker;
29  import org.osgi.util.tracker.ServiceTrackerCustomizer;
30  
31  
32  /**
33   * The {@link org.osgi.framework.BundleActivator} for the codec.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class LdapProtocolCodecActivator implements BundleActivator
38  {
39  
40      private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
41  
42      class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
43      {
44          private BundleContext bundleContext;
45          private ServiceRegistration<?> registration;
46  
47  
48          public LdapApiServiceTracker( BundleContext context )
49          {
50              this.bundleContext = context;
51          }
52  
53  
54          @Override
55          public LdapApiService addingService( ServiceReference<LdapApiService> reference )
56          {
57              LdapApiService ldapApiService = bundleContext.getService( reference );
58              LdapProtocolCodecFactory factory = new LdapProtocolCodecFactory( ldapApiService );
59              registration = bundleContext.registerService( LdapProtocolCodecFactory.class.getName(), factory, null );
60              ldapApiService.registerProtocolCodecFactory( factory );
61              return ldapApiService;
62          }
63  
64  
65          @Override
66          public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService service )
67          {
68          }
69  
70  
71          @Override
72          public void removedService( ServiceReference<LdapApiService> reference, LdapApiService service )
73          {
74              // TODO should we unregister the LdapProtocolCodecFactory at LdapApiService?
75              // ldapApiService.unregisterProtocolCodecFactory( factory );
76              registration.unregister();
77          }
78      }
79  
80  
81      /**
82       * Create a new instance of a LdapProtocolCodecActivator 
83       */
84      public LdapProtocolCodecActivator()
85      {
86      }
87  
88  
89      /**
90       * This class does nothing. It's just a nasty hack to force the bundle
91       * to get started lazy by calling this method.
92       */
93      public static void lazyStart()
94      {
95          // Does nothing
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     public void start( BundleContext bundleContext ) throws Exception
103     {
104         LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( bundleContext );
105         serviceTracker = new ServiceTracker<LdapApiService, LdapApiService>( bundleContext, LdapApiService.class,
106             ldapApiServiceTracker );
107         serviceTracker.open();
108     }
109 
110 
111     /**
112      * {@inheritDoc}
113      */
114     public void stop( BundleContext bundleContext ) throws Exception
115     {
116         serviceTracker.close();
117     }
118 }