diff --git httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java index b96c8ac..6654b88 100644 --- httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java +++ httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java @@ -22,33 +22,34 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.http.osgi.impl; -import java.io.Closeable; -import java.io.IOException; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.osgi.services.CachingHttpClientBuilderFactory; import org.apache.http.osgi.services.HttpClientBuilderFactory; import org.apache.http.osgi.services.ProxyConfiguration; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedServiceFactory; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + /** * @since 4.3 */ public final class HttpProxyConfigurationActivator implements BundleActivator, ManagedServiceFactory { private static final String PROXY_SERVICE_FACTORY_NAME = "Apache HTTP Client Proxy Configuration Factory"; @@ -58,17 +59,15 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M private static final String BUILDER_FACTORY_SERVICE_PID = "org.apache.http.httpclientfactory"; private static final String CACHEABLE_BUILDER_FACTORY_SERVICE_NAME = "Apache HTTP Client Caching Client Factory"; private static final String CACHEABLE_BUILDER_FACTORY_SERVICE_PID = "org.apache.http.cachinghttpclientfactory"; - private ServiceRegistration configurator; - - private ServiceRegistration clientFactory; + private final List registrations = new ArrayList(); private BundleContext context; private final Map registeredConfigurations = new LinkedHashMap(); private final List trackedHttpClients; @@ -84,52 +83,56 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M this.context = context; // ensure we receive configurations for the proxy selector final Hashtable props = new Hashtable(); props.put(Constants.SERVICE_PID, getName()); props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR)); props.put(Constants.SERVICE_DESCRIPTION, PROXY_SERVICE_FACTORY_NAME); - - configurator = context.registerService(ManagedServiceFactory.class.getName(), this, props); + register(ManagedServiceFactory.class.getName(), this, props); props.clear(); props.put(Constants.SERVICE_PID, BUILDER_FACTORY_SERVICE_PID); props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR)); props.put(Constants.SERVICE_DESCRIPTION, BUILDER_FACTORY_SERVICE_NAME); - clientFactory = context.registerService(HttpClientBuilderFactory.class.getName(), - new OSGiClientBuilderFactory(context, registeredConfigurations, trackedHttpClients), - props); + register( + HttpClientBuilderFactory.class.getName(), + new OSGiClientBuilderFactory(context, registeredConfigurations, trackedHttpClients), + props + ); props.clear(); props.put(Constants.SERVICE_PID, CACHEABLE_BUILDER_FACTORY_SERVICE_PID); props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR)); props.put(Constants.SERVICE_DESCRIPTION, CACHEABLE_BUILDER_FACTORY_SERVICE_NAME); - clientFactory = context.registerService(CachingHttpClientBuilderFactory.class.getName(), + register( + CachingHttpClientBuilderFactory.class.getName(), new OSGiCachingClientBuilderFactory(context, registeredConfigurations, trackedHttpClients), - props); + props + ); + } + + private void register(final String name, final Object service, final Hashtable props) { + registrations.add(context.registerService(name, service, props)); } /** * {@inheritDoc} */ @Override public void stop(final BundleContext context) throws Exception { // unregister services for (final ServiceRegistration registeredConfiguration : registeredConfigurations.values()) { registeredConfiguration.unregister(); } - // unregister service factory - if (configurator != null) { - configurator.unregister(); - } - - if (clientFactory != null) { - clientFactory.unregister(); + // unregister registered services + for (final ServiceRegistration registration : registrations) { + registration.unregister(); } + registrations.clear(); // ensure all http clients - generated with the - are terminated for (final CloseableHttpClient client : trackedHttpClients) { if (null != client) { closeQuietly(client); } }