diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java index 4e60b85d7..32f647118 100644 --- a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java @@ -35,6 +35,7 @@ import java.util.Map; import org.apache.hc.client5.http.impl.sync.CloseableHttpClient; +import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory; import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory; import org.apache.hc.client5.http.osgi.services.ProxyConfiguration; import org.osgi.framework.BundleActivator; @@ -49,9 +50,17 @@ */ public final class HttpProxyConfigurationActivator implements BundleActivator, ManagedServiceFactory { - private static final String SERVICE_FACTORY_NAME = "Apache HTTP Client Proxy Configuration Factory"; + private static final String PROXY_SERVICE_FACTORY_NAME = "Apache HTTP Client Proxy Configuration Factory"; - private static final String SERVICE_PID = "org.apache.http.proxyconfigurator"; + private static final String PROXY_SERVICE_PID = "org.apache.http.proxyconfigurator"; + + private static final String BUILDER_FACTORY_SERVICE_NAME = "Apache HTTP Client Client Factory"; + + 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; @@ -78,12 +87,25 @@ public void start(final BundleContext context) throws Exception { final Hashtable props = new Hashtable<>(); props.put(Constants.SERVICE_PID, getName()); props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders(Constants.BUNDLE_VENDOR)); - props.put(Constants.SERVICE_DESCRIPTION, SERVICE_FACTORY_NAME); + props.put(Constants.SERVICE_DESCRIPTION, PROXY_SERVICE_FACTORY_NAME); configurator = context.registerService(ManagedServiceFactory.class.getName(), this, props); + + props.clear(); + props.put(Constants.SERVICE_PID, BUILDER_FACTORY_SERVICE_PID); + props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders(Constants.BUNDLE_VENDOR)); + props.put(Constants.SERVICE_DESCRIPTION, BUILDER_FACTORY_SERVICE_NAME); clientFactory = context.registerService(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(Constants.BUNDLE_VENDOR)); + props.put(Constants.SERVICE_DESCRIPTION, CACHEABLE_BUILDER_FACTORY_SERVICE_NAME); + clientFactory = context.registerService(CachingHttpClientBuilderFactory.class.getName(), + new OSGiCachingClientBuilderFactory(context, registeredConfigurations, trackedHttpClients), + props); } /** @@ -123,7 +145,7 @@ public void stop(final BundleContext context) throws Exception { */ @Override public String getName() { - return SERVICE_PID; + return PROXY_SERVICE_PID; } /** diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java new file mode 100644 index 000000000..d216a4bc1 --- /dev/null +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java @@ -0,0 +1,59 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.client5.http.osgi.impl; + +import java.util.List; +import java.util.Map; + +import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder; +import org.apache.hc.client5.http.impl.sync.CloseableHttpClient; +import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory { + + private final BundleContext bundleContext; + + private final Map registeredConfigurations; + + private final List trackedHttpClients; + + public OSGiCachingClientBuilderFactory( + final BundleContext bundleContext, + final Map registeredConfigurations, + final List trackedHttpClients) { + this.bundleContext = bundleContext; + this.registeredConfigurations = registeredConfigurations; + this.trackedHttpClients = trackedHttpClients; + } + + @Override + public CachingHttpClientBuilder newBuilder() { + return new OSGiCachingHttpClientBuilder(bundleContext, registeredConfigurations, trackedHttpClients); + } +} diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingHttpClientBuilder.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingHttpClientBuilder.java new file mode 100644 index 000000000..9b960d7bf --- /dev/null +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingHttpClientBuilder.java @@ -0,0 +1,61 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.client5.http.osgi.impl; + +import java.util.List; +import java.util.Map; + +import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder; +import org.apache.hc.client5.http.impl.sync.CloseableHttpClient; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +final class OSGiCachingHttpClientBuilder extends CachingHttpClientBuilder { + + private final List trackedHttpClients; + + public OSGiCachingHttpClientBuilder( + final BundleContext bundleContext, + final Map registeredConfigurations, + final List trackedHttpClients) { + this.trackedHttpClients = trackedHttpClients; + setDefaultCredentialsProvider( + new OSGiCredentialsProvider(bundleContext, registeredConfigurations)); + setRoutePlanner( + new OSGiHttpRoutePlanner(bundleContext, registeredConfigurations)); + } + + @Override + public CloseableHttpClient build() { + final CloseableHttpClient httpClient = super.build(); + synchronized (trackedHttpClients) { + trackedHttpClients.add(httpClient); + } + return httpClient; + } + +} diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/services/CachingHttpClientBuilderFactory.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/services/CachingHttpClientBuilderFactory.java new file mode 100644 index 000000000..d94e84b2f --- /dev/null +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/services/CachingHttpClientBuilderFactory.java @@ -0,0 +1,35 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.client5.http.osgi.services; + +import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder; + +public interface CachingHttpClientBuilderFactory { + + CachingHttpClientBuilder newBuilder(); + +}