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 java.util.ArrayList;
024
025import org.apache.directory.api.ldap.codec.StockCodecFactoryUtil;
026import org.apache.directory.api.ldap.codec.api.LdapApiService;
027import org.osgi.framework.BundleActivator;
028import org.osgi.framework.BundleContext;
029import org.osgi.framework.ServiceReference;
030import org.osgi.util.tracker.ServiceTracker;
031import org.osgi.util.tracker.ServiceTrackerCustomizer;
032
033
034/**
035 * A BundleActivator for the ldap codec extras extension: extra ApacheDS and
036 * Apache Directory Studio specific controls and extended operations.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class ExtrasBundleActivator implements BundleActivator
041{
042
043    private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
044
045    class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
046    {
047
048        private BundleContext context;
049
050
051        LdapApiServiceTracker( BundleContext context )
052        {
053            this.context = context;
054        }
055
056
057        @Override
058        public LdapApiService addingService( ServiceReference<LdapApiService> reference )
059        {
060            LdapApiService ldapApiService = context.getService( reference );
061            StockCodecFactoryUtil.loadStockControls( ldapApiService );
062            ExtrasCodecFactoryUtil.loadExtrasControls( ldapApiService );
063            ExtrasCodecFactoryUtil.loadExtrasExtendedOperations( ldapApiService );
064            ExtrasCodecFactoryUtil.loadExtrasIntermediateResponses( ldapApiService );
065            return ldapApiService;
066        }
067
068
069        @Override
070        public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
071        {
072        }
073
074
075        @Override
076        public void removedService( ServiceReference<LdapApiService> reference, LdapApiService ldapApiService )
077        {
078            // Request controls
079            for ( String oid : new ArrayList<>( ldapApiService.getRequestControlFactories().keySet() ) )
080            {
081                ldapApiService.unregisterRequestControl( oid );
082            }
083            // Response controls
084            for ( String oid : new ArrayList<>( ldapApiService.getResponseControlFactories().keySet() ) )
085            {
086                ldapApiService.unregisterResponseControl( oid );
087            }
088            // Extended requests
089            for ( String oid : new ArrayList<>( ldapApiService.getExtendedRequestFactories().keySet() ) )
090            {
091                ldapApiService.unregisterExtendedRequest( oid );
092            }
093            // Extended responses
094            for ( String oid : new ArrayList<>( ldapApiService.getExtendedResponseFactories().keySet() ) )
095            {
096                ldapApiService.unregisterExtendedResponse( oid );
097            }
098            // Intermediate responses
099            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}