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