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        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            registration.unregister();
075        }
076    }
077
078
079    /**
080     * Create a new instance of a LdapProtocolCodecActivator 
081     */
082    public LdapProtocolCodecActivator()
083    {
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public void start( BundleContext bundleContext ) throws Exception
092    {
093        LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( bundleContext );
094        serviceTracker = new ServiceTracker<>( bundleContext, LdapApiService.class,
095            ldapApiServiceTracker );
096        serviceTracker.open();
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public void stop( BundleContext bundleContext ) throws Exception
105    {
106        serviceTracker.close();
107    }
108}