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.codec.protocol.mina;
021
022
023import org.apache.directory.api.ldap.codec.api.LdapApiService;
024import org.osgi.framework.BundleActivator;
025import org.osgi.framework.BundleContext;
026import org.osgi.framework.ServiceReference;
027import org.osgi.framework.ServiceRegistration;
028import org.osgi.util.tracker.ServiceTracker;
029import org.osgi.util.tracker.ServiceTrackerCustomizer;
030
031
032/**
033 * The {@link org.osgi.framework.BundleActivator} for the codec.
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class LdapProtocolCodecActivator implements BundleActivator
038{
039
040    private ServiceTracker<LdapApiService, LdapApiService> serviceTracker;
041
042    class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService>
043    {
044        private BundleContext bundleContext;
045        private ServiceRegistration<?> registration;
046
047
048        public LdapApiServiceTracker( BundleContext context )
049        {
050            this.bundleContext = context;
051        }
052
053
054        @Override
055        public LdapApiService addingService( ServiceReference<LdapApiService> reference )
056        {
057            LdapApiService ldapApiService = bundleContext.getService( reference );
058            LdapProtocolCodecFactory factory = new LdapProtocolCodecFactory( ldapApiService );
059            registration = bundleContext.registerService( LdapProtocolCodecFactory.class.getName(), factory, null );
060            ldapApiService.registerProtocolCodecFactory( factory );
061            return ldapApiService;
062        }
063
064
065        @Override
066        public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService service )
067        {
068        }
069
070
071        @Override
072        public void removedService( ServiceReference<LdapApiService> reference, LdapApiService service )
073        {
074            // TODO should we unregister the LdapProtocolCodecFactory at LdapApiService?
075            // ldapApiService.unregisterProtocolCodecFactory( factory );
076            registration.unregister();
077        }
078    }
079
080
081    /**
082     * Create a new instance of a LdapProtocolCodecActivator 
083     */
084    public LdapProtocolCodecActivator()
085    {
086    }
087
088
089    /**
090     * This class does nothing. It's just a nasty hack to force the bundle
091     * to get started lazy by calling this method.
092     */
093    public static void lazyStart()
094    {
095        // Does nothing
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public void start( BundleContext bundleContext ) throws Exception
103    {
104        LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( bundleContext );
105        serviceTracker = new ServiceTracker<LdapApiService, LdapApiService>( bundleContext, LdapApiService.class,
106            ldapApiServiceTracker );
107        serviceTracker.open();
108    }
109
110
111    /**
112     * {@inheritDoc}
113     */
114    public void stop( BundleContext bundleContext ) throws Exception
115    {
116        serviceTracker.close();
117    }
118}