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.AdShowDeleted;
026import org.apache.directory.api.ldap.extras.controls.ad.AdDirSync;
027import org.apache.directory.api.ldap.extras.controls.ad_impl.AdShowDeletedFactory;
028import org.apache.directory.api.ldap.extras.controls.ad_impl.AdDirSyncFactory;
029import org.apache.directory.api.ldap.extras.controls.changeNotifications.ChangeNotifications;
030import org.apache.directory.api.ldap.extras.controls.changeNotifications_impl.ChangeNotificationsFactory;
031import org.apache.directory.api.ldap.extras.controls.permissiveModify.PermissiveModify;
032import org.apache.directory.api.ldap.extras.controls.permissiveModify_impl.PermissiveModifyFactory;
033import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
034import org.apache.directory.api.ldap.extras.controls.ppolicy_impl.PasswordPolicyFactory;
035import org.apache.directory.api.ldap.extras.controls.syncrepl.syncDone.SyncDoneValue;
036import org.apache.directory.api.ldap.extras.controls.syncrepl.syncInfoValue.SyncInfoValue;
037import org.apache.directory.api.ldap.extras.controls.syncrepl.syncInfoValue.SyncRequestValue;
038import org.apache.directory.api.ldap.extras.controls.syncrepl.syncState.SyncStateValue;
039import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncDoneValueFactory;
040import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncInfoValueFactory;
041import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncRequestValueFactory;
042import org.apache.directory.api.ldap.extras.controls.syncrepl_impl.SyncStateValueFactory;
043import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewRequest;
044import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResponse;
045import org.apache.directory.api.ldap.extras.controls.vlv_impl.VirtualListViewRequestFactory;
046import org.apache.directory.api.ldap.extras.controls.vlv_impl.VirtualListViewResponseFactory;
047import org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelFactory;
048import org.apache.directory.api.ldap.extras.extended.ads_impl.certGeneration.CertGenerationFactory;
049import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulDisconnectFactory;
050import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown.GracefulShutdownFactory;
051import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyFactory;
052import org.apache.directory.api.ldap.extras.extended.ads_impl.startTls.StartTlsFactory;
053import org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureFactory;
054import org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI.WhoAmIFactory;
055import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
056import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
057import org.apache.directory.api.ldap.extras.extended.gracefulDisconnect.GracefulDisconnectResponse;
058import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequest;
059import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
060import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
061import org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest;
062import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIRequest;
063import org.osgi.framework.BundleActivator;
064import org.osgi.framework.BundleContext;
065import org.osgi.framework.ServiceReference;
066import org.osgi.util.tracker.ServiceTracker;
067import org.osgi.util.tracker.ServiceTrackerCustomizer;
068
069
070/**
071 * A BundleActivator for the ldap codec extras extension: extra ApacheDS and 
072 * Apache Directory Studio specific controls and extended operations. 
073 *
074 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
075 */
076public class ExtrasBundleActivator implements BundleActivator
077{
078
079    private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
080
081    class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
082    {
083
084        private BundleContext context;
085
086
087        public LdapApiServiceTracker( BundleContext context )
088        {
089            this.context = context;
090        }
091
092
093        @Override
094        public LdapApiService addingService( ServiceReference<LdapApiService> reference )
095        {
096            LdapApiService ldapApiService = context.getService( reference );
097            registerExtrasControls( ldapApiService );
098            registerExtrasExtendedOps( ldapApiService );
099            return ldapApiService;
100        }
101
102
103        @Override
104        public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
105        {
106        }
107
108
109        @Override
110        public void removedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
111        {
112            unregisterExtrasControls( ldapApiService );
113            unregisterExtrasExtendedOps( ldapApiService );
114        }
115
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    public void start( BundleContext context ) throws Exception
123    {
124        LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( context );
125        serviceTracker = new ServiceTracker<LdapApiService, LdapApiService>(
126            context, LdapApiService.class, ldapApiServiceTracker );
127        serviceTracker.open();
128    }
129
130
131    /**
132     * Registers all the extras controls present in this control pack.
133     *
134     * @param codec The codec service.
135     */
136    private void registerExtrasControls( LdapApiService codec )
137    {
138        ControlFactory<AdDirSync> adDirSyncFactory = new AdDirSyncFactory( codec );
139        codec.registerControl( adDirSyncFactory );
140        
141        ControlFactory<AdShowDeleted> adDeletedFactory = new AdShowDeletedFactory( codec );
142        codec.registerControl( adDeletedFactory );
143        
144        ControlFactory<ChangeNotifications> changeNotificationsFactory = new ChangeNotificationsFactory( codec );
145        codec.registerControl( changeNotificationsFactory );
146
147        ControlFactory<PasswordPolicy> passwordPolicyFactory = new PasswordPolicyFactory( codec );
148        codec.registerControl( passwordPolicyFactory );
149
150        ControlFactory<PermissiveModify> permissiveModifyFactory = new PermissiveModifyFactory( codec );
151        codec.registerControl( permissiveModifyFactory );
152        
153        ControlFactory<SyncDoneValue> syncDoneValuefactory = new SyncDoneValueFactory( codec );
154        codec.registerControl( syncDoneValuefactory );
155
156        ControlFactory<SyncInfoValue> syncInfoValueFactory = new SyncInfoValueFactory( codec );
157        codec.registerControl( syncInfoValueFactory );
158
159        ControlFactory<SyncRequestValue> syncRequestValueFactory = new SyncRequestValueFactory( codec );
160        codec.registerControl( syncRequestValueFactory );
161
162        ControlFactory<SyncStateValue> syncStateValuefactory = new SyncStateValueFactory( codec );
163        codec.registerControl( syncStateValuefactory );
164
165        ControlFactory<VirtualListViewRequest> virtualListViewRequestFactory = new VirtualListViewRequestFactory( codec );
166        codec.registerControl( virtualListViewRequestFactory );
167
168        ControlFactory<VirtualListViewResponse> virtualListViewResponseFactory = new VirtualListViewResponseFactory(
169            codec );
170        codec.registerControl( virtualListViewResponseFactory );
171
172        
173    }
174
175
176    /**
177     * Registers all the extras extended operations present in this control pack.
178     *
179     * @param codec The codec service.
180     */
181    private void registerExtrasExtendedOps( LdapApiService codec )
182    {
183        // --------------------------------------------------------------------
184        // Register Extended Request Factories
185        // --------------------------------------------------------------------
186
187        CancelFactory cancelFactory = new CancelFactory( codec );
188        codec.registerExtendedRequest( cancelFactory );
189
190        CertGenerationFactory certGenerationFactory = new CertGenerationFactory( codec );
191        codec.registerExtendedRequest( certGenerationFactory );
192
193        GracefulShutdownFactory gracefulShutdownFactory = new GracefulShutdownFactory( codec );
194        codec.registerExtendedRequest( gracefulShutdownFactory );
195
196        StoredProcedureFactory storedProcedureFactory = new StoredProcedureFactory( codec );
197        codec.registerExtendedRequest( storedProcedureFactory );
198
199        PasswordModifyFactory passwordModifyFactory = new PasswordModifyFactory( codec );
200        codec.registerExtendedRequest( passwordModifyFactory );
201
202        GracefulDisconnectFactory gracefulDisconnectFactory = new GracefulDisconnectFactory( codec );
203        codec.registerExtendedRequest( gracefulDisconnectFactory );
204
205        WhoAmIFactory whoAmIFactory = new WhoAmIFactory( codec );
206        codec.registerExtendedRequest( whoAmIFactory );
207
208        StartTlsFactory startTlsFactory = new StartTlsFactory( codec );
209        codec.registerExtendedRequest( startTlsFactory );
210    }
211
212
213    /**
214     * {@inheritDoc}
215     */
216    public void stop( BundleContext context ) throws Exception
217    {
218        serviceTracker.close();
219    }
220
221
222    private void unregisterExtrasControls( LdapApiService codec )
223    {
224        codec.unregisterControl( SyncDoneValue.OID );
225        codec.unregisterControl( SyncInfoValue.OID );
226        codec.unregisterControl( SyncRequestValue.OID );
227        codec.unregisterControl( SyncStateValue.OID );
228        codec.unregisterControl( PasswordPolicy.OID );
229        codec.unregisterControl( AdDirSync.OID );
230        codec.unregisterControl( AdShowDeleted.OID );
231    }
232
233
234    private void unregisterExtrasExtendedOps( LdapApiService codec )
235    {
236        codec.unregisterExtendedRequest( CancelRequest.EXTENSION_OID );
237        codec.unregisterExtendedRequest( CertGenerationRequest.EXTENSION_OID );
238        codec.unregisterExtendedRequest( GracefulShutdownRequest.EXTENSION_OID );
239        codec.unregisterExtendedRequest( StoredProcedureRequest.EXTENSION_OID );
240        codec.unregisterExtendedRequest( GracefulDisconnectResponse.EXTENSION_OID );
241        codec.unregisterExtendedRequest( PasswordModifyRequest.EXTENSION_OID );
242        codec.unregisterExtendedRequest( WhoAmIRequest.EXTENSION_OID );
243        codec.unregisterExtendedRequest( StartTlsRequest.EXTENSION_OID );
244    }
245}