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 * https://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 private ServiceTracker<LdapApiService, LdapApiService> serviceTracker; 040 041 class LdapApiServiceTracker implements ServiceTrackerCustomizer<LdapApiService, LdapApiService> 042 { 043 private BundleContext bundleContext; 044 private ServiceRegistration<?> registration; 045 046 047 LdapApiServiceTracker( BundleContext context ) 048 { 049 this.bundleContext = context; 050 } 051 052 053 @Override 054 public LdapApiService addingService( ServiceReference<LdapApiService> reference ) 055 { 056 LdapApiService ldapApiService = bundleContext.getService( reference ); 057 LdapProtocolCodecFactory factory = new LdapProtocolCodecFactory( ldapApiService ); 058 registration = bundleContext.registerService( LdapProtocolCodecFactory.class.getName(), factory, null ); 059 ldapApiService.registerProtocolCodecFactory( factory ); 060 return ldapApiService; 061 } 062 063 064 @Override 065 public void modifiedService( ServiceReference<LdapApiService> reference, LdapApiService service ) 066 { 067 // Do nothing ATM 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 // Default constructor 087 } 088 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public void start( BundleContext bundleContext ) throws Exception 095 { 096 LdapApiServiceTracker ldapApiServiceTracker = new LdapApiServiceTracker( bundleContext ); 097 serviceTracker = new ServiceTracker<>( bundleContext, LdapApiService.class, 098 ldapApiServiceTracker ); 099 serviceTracker.open(); 100 } 101 102 103 /** 104 * {@inheritDoc} 105 */ 106 @Override 107 public void stop( BundleContext bundleContext ) throws Exception 108 { 109 serviceTracker.close(); 110 } 111}