001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.api.ldap.extras;
021
022
023import org.apache.directory.api.ldap.codec.api.ControlFactory;
024import org.apache.directory.api.ldap.codec.api.LdapApiService;
025import org.apache.directory.api.ldap.extras.controls.ad.AdDirSync;
026import org.apache.directory.api.ldap.extras.controls.ad_impl.AdDirSyncFactory;
027import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
028import org.apache.directory.api.ldap.extras.controls.ppolicy_impl.PasswordPolicyFactory;
029import org.apache.directory.api.ldap.extras.controls.syncrepl.syncDone.SyncDoneValue;
030import org.apache.directory.api.ldap.extras.controls.syncrepl.syncInfoValue.SyncInfoValue;
031import org.apache.directory.api.ldap.extras.controls.syncrepl.syncInfoValue.SyncRequestValue;
032import org.apache.directory.api.ldap.extras.controls.syncrepl.syncState.SyncStateValue;
033import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncDoneValueFactory;
034import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncInfoValueFactory;
035import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncRequestValueFactory;
036import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncStateValueFactory;
037import org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelFactory;
038import org.apache.directory.api.ldap.extras.extended.ads_impl.certGeneration.CertGenerationFactory;
039import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulDisconnectFactory;
040import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown.GracefulShutdownFactory;
041import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyFactory;
042import org.apache.directory.api.ldap.extras.extended.ads_impl.startTls.StartTlsFactory;
043import org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureFactory;
044import org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI.WhoAmIFactory;
045import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
046import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
047import org.apache.directory.api.ldap.extras.extended.gracefulDisconnect.GracefulDisconnectResponse;
048import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequest;
049import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
050import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
051import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest;
052import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIRequest;
053import org.osgi.framework.BundleActivator;
054import org.osgi.framework.BundleContext;
055import org.osgi.framework.ServiceReference;
056import org.osgi.util.tracker.ServiceTracker;
057import org.osgi.util.tracker.ServiceTrackerCustomizer;
058
059
060/**
061 * A BundleActivator for the ldap codec extras extension: extra ApacheDS and 
062 * Apache Directory Studio specific controls and extended operations. 
063 *
064 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
065 */
066public class ExtrasBundleActivator implements BundleActivator
067{
068
069    private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
070
071    class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
072    {
073
074        private BundleContext context;
075
076
077        public LdapApiServiceTracker( BundleContext context )
078        {
079            this.context = context;
080        }
081
082
083        @Override
084        public LdapApiService addingService( ServiceReference<LdapApiService> reference )
085        {
086            LdapApiService ldapApiService = context.getService( reference );
087            registerExtrasControls( ldapApiService );
088            registerExtrasExtendedOps( ldapApiService );
089            return ldapApiService;
090        }
091
092
093        @Override
094        public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
095        {
096        }
097
098
099        @Override
100        public void removedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
101        {
102            unregisterExtrasControls( ldapApiService );
103            unregisterExtrasExtendedOps( ldapApiService );
104        }
105
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    public void start( BundleContext context ) throws Exception
113    {
114        LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker(context);
115        serviceTracker = new ServiceTracker<LdapApiService, LdapApiService>(
116            context, LdapApiService.class, ldapApiServiceTracker );
117        serviceTracker.open();
118    }
119    
120    /**
121     * Registers all the extras controls present in this control pack.
122     *
123     * @param codec The codec service.
124     */
125    private void registerExtrasControls( LdapApiService codec )
126    {
127        ControlFactory<SyncDoneValue> syncDoneValuefactory = new SyncDoneValueFactory( codec );
128        codec.registerControl( syncDoneValuefactory );
129
130        ControlFactory<SyncInfoValue> syncInfoValueFactory = new SyncInfoValueFactory( codec );
131        codec.registerControl( syncInfoValueFactory );
132
133        ControlFactory<SyncRequestValue> syncRequestValueFactory = new SyncRequestValueFactory( codec );
134        codec.registerControl( syncRequestValueFactory );
135
136        ControlFactory<SyncStateValue> syncStateValuefactory = new SyncStateValueFactory( codec );
137        codec.registerControl( syncStateValuefactory );
138
139        ControlFactory<PasswordPolicy> passwordPolicyFactory = new PasswordPolicyFactory( codec );
140        codec.registerControl( passwordPolicyFactory );
141
142        ControlFactory<AdDirSync> adDirSyncFactory = new AdDirSyncFactory( codec );
143        codec.registerControl( adDirSyncFactory );
144    }
145
146
147    /**
148     * Registers all the extras extended operations present in this control pack.
149     *
150     * @param codec The codec service.
151     */
152    private void registerExtrasExtendedOps( LdapApiService codec )
153    {
154        // --------------------------------------------------------------------
155        // Register Extended Request Factories
156        // --------------------------------------------------------------------
157
158        CancelFactory cancelFactory = new CancelFactory( codec );
159        codec.registerExtendedRequest( cancelFactory );
160
161        CertGenerationFactory certGenerationFactory = new CertGenerationFactory( codec );
162        codec.registerExtendedRequest( certGenerationFactory );
163
164        GracefulShutdownFactory gracefulShutdownFactory = new GracefulShutdownFactory( codec );
165        codec.registerExtendedRequest( gracefulShutdownFactory );
166
167        StoredProcedureFactory storedProcedureFactory = new StoredProcedureFactory( codec );
168        codec.registerExtendedRequest( storedProcedureFactory );
169
170        PasswordModifyFactory passwordModifyFactory = new PasswordModifyFactory( codec );
171        codec.registerExtendedRequest( passwordModifyFactory );
172
173        GracefulDisconnectFactory gracefulDisconnectFactory = new GracefulDisconnectFactory( codec );
174        codec.registerExtendedRequest( gracefulDisconnectFactory );
175
176        WhoAmIFactory whoAmIFactory = new WhoAmIFactory( codec );
177        codec.registerExtendedRequest( whoAmIFactory );
178
179        StartTlsFactory startTlsFactory = new StartTlsFactory( codec );
180        codec.registerExtendedRequest( startTlsFactory );
181    }
182
183
184    /**
185     * {@inheritDoc}
186     */
187    public void stop( BundleContext context ) throws Exception
188    {
189        serviceTracker.close();
190    }
191
192
193    private void unregisterExtrasControls( LdapApiService codec )
194    {
195        codec.unregisterControl( SyncDoneValue.OID );
196        codec.unregisterControl( SyncInfoValue.OID );
197        codec.unregisterControl( SyncRequestValue.OID );
198        codec.unregisterControl( SyncStateValue.OID );
199        codec.unregisterControl( PasswordPolicy.OID );
200        codec.unregisterControl( AdDirSync.OID );
201    }
202
203
204    private void unregisterExtrasExtendedOps( LdapApiService codec )
205    {
206        codec.unregisterExtendedRequest( CancelRequest.EXTENSION_OID );
207        codec.unregisterExtendedRequest( CertGenerationRequest.EXTENSION_OID );
208        codec.unregisterExtendedRequest( GracefulShutdownRequest.EXTENSION_OID );
209        codec.unregisterExtendedRequest( StoredProcedureRequest.EXTENSION_OID );
210        codec.unregisterExtendedRequest( GracefulDisconnectResponse.EXTENSION_OID );
211        codec.unregisterExtendedRequest( PasswordModifyRequest.EXTENSION_OID );
212        codec.unregisterExtendedRequest( WhoAmIRequest.EXTENSION_OID );
213        codec.unregisterExtendedRequest( StartTlsRequest.EXTENSION_OID );
214    }
215}