HTTPCLIENT-1750: OSGi support for CachingHttpClientBuilder
Contributed by Justin Edelson <justin at justinedelson.com> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x@1747904 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
df95271189
commit
6e4a1bbbf1
|
@ -35,6 +35,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
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.HttpClientBuilderFactory;
|
||||||
import org.apache.http.osgi.services.ProxyConfiguration;
|
import org.apache.http.osgi.services.ProxyConfiguration;
|
||||||
import org.osgi.framework.BundleActivator;
|
import org.osgi.framework.BundleActivator;
|
||||||
|
@ -49,9 +50,17 @@ import org.osgi.service.cm.ManagedServiceFactory;
|
||||||
*/
|
*/
|
||||||
public final class HttpProxyConfigurationActivator implements BundleActivator, 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;
|
private ServiceRegistration configurator;
|
||||||
|
|
||||||
|
@ -78,12 +87,25 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M
|
||||||
final Hashtable<String, Object> props = new Hashtable<String, Object>();
|
final Hashtable<String, Object> props = new Hashtable<String, Object>();
|
||||||
props.put(Constants.SERVICE_PID, getName());
|
props.put(Constants.SERVICE_PID, getName());
|
||||||
props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders(Constants.BUNDLE_VENDOR));
|
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);
|
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(),
|
clientFactory = context.registerService(HttpClientBuilderFactory.class.getName(),
|
||||||
new OSGiClientBuilderFactory(context, registeredConfigurations, trackedHttpClients),
|
new OSGiClientBuilderFactory(context, registeredConfigurations, trackedHttpClients),
|
||||||
props);
|
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 final class HttpProxyConfigurationActivator implements BundleActivator, M
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return SERVICE_PID;
|
return PROXY_SERVICE_PID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory {
|
||||||
|
|
||||||
|
private final BundleContext bundleContext;
|
||||||
|
|
||||||
|
private final Map<String, ServiceRegistration> registeredConfigurations;
|
||||||
|
|
||||||
|
private final List<CloseableHttpClient> trackedHttpClients;
|
||||||
|
|
||||||
|
public OSGiCachingClientBuilderFactory(
|
||||||
|
final BundleContext bundleContext,
|
||||||
|
final Map<String, ServiceRegistration> registeredConfigurations,
|
||||||
|
final List<CloseableHttpClient> trackedHttpClients) {
|
||||||
|
this.bundleContext = bundleContext;
|
||||||
|
this.registeredConfigurations = registeredConfigurations;
|
||||||
|
this.trackedHttpClients = trackedHttpClients;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CachingHttpClientBuilder newBuilder() {
|
||||||
|
return new OSGiCachingHttpClientBuilder(bundleContext, registeredConfigurations, trackedHttpClients);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
final class OSGiCachingHttpClientBuilder extends CachingHttpClientBuilder {
|
||||||
|
|
||||||
|
private final List<CloseableHttpClient> trackedHttpClients;
|
||||||
|
|
||||||
|
public OSGiCachingHttpClientBuilder(
|
||||||
|
final BundleContext bundleContext,
|
||||||
|
final Map<String, ServiceRegistration> registeredConfigurations,
|
||||||
|
final List<CloseableHttpClient> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.apache.http.osgi.services;
|
||||||
|
|
||||||
|
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
|
||||||
|
|
||||||
|
public interface CachingHttpClientBuilderFactory {
|
||||||
|
|
||||||
|
CachingHttpClientBuilder newBuilder();
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue