From 8e65edcdb1b7cdd9e4b1afc7893d1ad0b0d5673c Mon Sep 17 00:00:00 2001 From: Justin Edelson Date: Wed, 8 Jun 2016 15:35:56 -0400 Subject: [PATCH] implement CachingHttpClientBuilderFactory --- .../osgi/impl/HttpProxyConfigurationActivator.java | 30 +++++++++-- .../osgi/impl/OSGiCachingClientBuilderFactory.java | 59 +++++++++++++++++++++ .../osgi/impl/OSGiCachingHttpClientBuilder.java | 61 ++++++++++++++++++++++ .../services/CachingHttpClientBuilderFactory.java | 35 +++++++++++++ 4 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java create mode 100644 httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingHttpClientBuilder.java create mode 100644 httpclient-osgi/src/main/java/org/apache/http/osgi/services/CachingHttpClientBuilderFactory.java diff --git a/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java b/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java index 44f7e3b..e538890 100644 --- a/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java +++ b/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java @@ -37,6 +37,7 @@ import java.util.Map; import java.util.WeakHashMap; 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; @@ -51,9 +52,17 @@ import org.osgi.service.cm.ManagedServiceFactory; */ 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; @@ -80,12 +89,25 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M 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); } /** @@ -125,7 +147,7 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M */ @Override public String getName() { - return SERVICE_PID; + return PROXY_SERVICE_PID; } /** diff --git a/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java b/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java new file mode 100644 index 0000000..b9ca07c --- /dev/null +++ b/httpclient-osgi/src/main/java/org/apache/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.http.osgi.impl; + +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.cache.CachingHttpClientBuilder; +import org.apache.http.osgi.services.CachingHttpClientBuilderFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import java.util.Collection; +import java.util.Map; + +public class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory { + + private final BundleContext bundleContext; + + private final Map registeredConfigurations; + + private final Collection trackedHttpClients; + + public OSGiCachingClientBuilderFactory( + final BundleContext bundleContext, + final Map registeredConfigurations, + final Collection trackedHttpClients) { + this.bundleContext = bundleContext; + this.registeredConfigurations = registeredConfigurations; + this.trackedHttpClients = trackedHttpClients; + } + + @Override + public CachingHttpClientBuilder newBuilder() { + return new OSGiCachingHttpClientBuilder(bundleContext, registeredConfigurations, trackedHttpClients); + } +} diff --git a/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingHttpClientBuilder.java b/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingHttpClientBuilder.java new file mode 100644 index 0000000..3711de4 --- /dev/null +++ b/httpclient-osgi/src/main/java/org/apache/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.http.osgi.impl; + +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.cache.CachingHttpClientBuilder; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import java.util.Collection; +import java.util.Map; + +final class OSGiCachingHttpClientBuilder extends CachingHttpClientBuilder { + + private final Collection trackedHttpClients; + + public OSGiCachingHttpClientBuilder( + final BundleContext bundleContext, + final Map registeredConfigurations, + final Collection 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/httpclient-osgi/src/main/java/org/apache/http/osgi/services/CachingHttpClientBuilderFactory.java b/httpclient-osgi/src/main/java/org/apache/http/osgi/services/CachingHttpClientBuilderFactory.java new file mode 100644 index 0000000..1eba416 --- /dev/null +++ b/httpclient-osgi/src/main/java/org/apache/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.http.osgi.services; + +import org.apache.http.impl.client.cache.CachingHttpClientBuilder; + +public interface CachingHttpClientBuilderFactory { + + CachingHttpClientBuilder newBuilder(); + +} -- 2.8.4