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   *     https://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.extras;
21  
22  
23  import java.util.ArrayList;
24  
25  import org.apache.directory.api.ldap.codec.StockCodecFactoryUtil;
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.osgi.framework.BundleActivator;
28  import org.osgi.framework.BundleContext;
29  import org.osgi.framework.ServiceReference;
30  import org.osgi.util.tracker.ServiceTracker;
31  import org.osgi.util.tracker.ServiceTrackerCustomizer;
32  
33  
34  /**
35   * A BundleActivator for the ldap codec extras extension: extra ApacheDS and
36   * Apache Directory Studio specific controls and extended operations.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class ExtrasBundleActivator implements BundleActivator
41  {
42  
43      private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
44  
45      class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
46      {
47  
48          private BundleContext context;
49  
50  
51          LdapApiServiceTracker( BundleContext context )
52          {
53              this.context = context;
54          }
55  
56  
57          @Override
58          public LdapApiService addingService( ServiceReference<LdapApiService> reference )
59          {
60              LdapApiService ldapApiService = context.getService( reference );
61              StockCodecFactoryUtil.loadStockControls( ldapApiService );
62              ExtrasCodecFactoryUtil.loadExtrasControls( ldapApiService );
63              ExtrasCodecFactoryUtil.loadExtrasExtendedOperations( ldapApiService );
64              ExtrasCodecFactoryUtil.loadExtrasIntermediateResponses( ldapApiService );
65              return ldapApiService;
66          }
67  
68  
69          @Override
70          public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
71          {
72          }
73  
74  
75          @Override
76          public void removedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
77          {
78              // Request controls
79              for ( String oid : new ArrayList<>( ldapApiService.getRequestControlFactories().keySet() ) )
80              {
81                  ldapApiService.unregisterRequestControl( oid );
82              }
83              // Response controls
84              for ( String oid : new ArrayList<>( ldapApiService.getResponseControlFactories().keySet() ) )
85              {
86                  ldapApiService.unregisterResponseControl( oid );
87              }
88              // Extended requests
89              for ( String oid : new ArrayList<>( ldapApiService.getExtendedRequestFactories().keySet() ) )
90              {
91                  ldapApiService.unregisterExtendedRequest( oid );
92              }
93              // Extended responses
94              for ( String oid : new ArrayList<>( ldapApiService.getExtendedResponseFactories().keySet() ) )
95              {
96                  ldapApiService.unregisterExtendedResponse( oid );
97              }
98              // Intermediate responses
99              for ( String oid : new ArrayList<>( ldapApiService.getIntermediateResponseFactories().keySet() ) )
100             {
101                 ldapApiService.unregisterIntermediateResponse( oid );
102             }
103         }
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void start( BundleContext context ) throws Exception
112     {
113         LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( context );
114         serviceTracker = new ServiceTracker<>(
115             context, LdapApiService.class, ldapApiServiceTracker );
116         serviceTracker.open();
117     }
118 
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public void stop( BundleContext context ) throws Exception
125     {
126         serviceTracker.close();
127     }
128 }