org.glassfish.jersey.client.ClientConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jersey-all Show documentation
Show all versions of jersey-all Show documentation
jersey-all is a rebundled verison of Jersey as one OSGi bundle.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.jersey.client;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.Configurable;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.ExtendedConfig;
import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.internal.inject.JerseyClassAnalyzer;
import org.glassfish.jersey.internal.inject.ProviderBinder;
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.internal.util.collection.Value;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.model.internal.CommonConfig;
import org.glassfish.jersey.model.internal.ComponentBag;
import org.glassfish.hk2.api.DynamicConfiguration;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
/**
* Jersey externalized implementation of client-side JAX-RS {@link javax.ws.rs.core.Configurable
* configurable} contract.
*
* @author Marek Potociar (marek.potociar at oracle.com)
* @author Martin Matula (martin.matula at oracle.com)
* @author Libor Kramolis (libor.kramolis at oracle.com)
*/
public class ClientConfig implements Configurable, ExtendedConfig {
/**
* Internal configuration state.
*/
private State state;
/**
* Default encapsulation of the internal configuration state.
*/
private static class State implements Configurable, ExtendedConfig {
/**
* Strategy that returns the same state instance.
*/
private static final StateChangeStrategy IDENTITY = new StateChangeStrategy() {
@Override
public State onChange(final State state) {
return state;
}
};
/**
* Strategy that returns a copy of the state instance.
*/
private static final StateChangeStrategy COPY_ON_CHANGE = new StateChangeStrategy() {
@Override
public State onChange(final State state) {
return state.copy();
}
};
private volatile StateChangeStrategy strategy;
private final CommonConfig commonConfig;
private final JerseyClient client;
private volatile ConnectorProvider connectorProvider;
private final LazyValue runtime = Values.lazy(new Value() {
@Override
public ClientRuntime get() {
return initRuntime();
}
});
/**
* Configuration state change strategy.
*/
private static interface StateChangeStrategy {
/**
* Invoked whenever a mutator method is called in the given configuration
* state.
*
* @param state configuration state to be mutated.
* @return state instance that will be mutated and returned from the
* invoked configuration state mutator method.
*/
public State onChange(final State state);
}
/**
* Default configuration state constructor with {@link StateChangeStrategy "identity"}
* state change strategy.
*
* @param client bound parent Jersey client.
*/
State(JerseyClient client) {
this.strategy = IDENTITY;
this.commonConfig = new CommonConfig(RuntimeType.CLIENT, ComponentBag.EXCLUDE_EMPTY);
this.client = client;
this.connectorProvider = new HttpUrlConnectorProvider();
}
/**
* Copy the original configuration state while using the default state change
* strategy.
*
* @param client new Jersey client parent for the state.
* @param original configuration strategy to be copied.
*/
private State(JerseyClient client, State original) {
this.strategy = IDENTITY;
this.client = client;
this.commonConfig = new CommonConfig(original.commonConfig);
this.connectorProvider = original.connectorProvider;
}
/**
* Create a copy of the configuration state within the same parent Jersey
* client instance scope.
*
* @return configuration state copy.
*/
State copy() {
return new State(this.client, this);
}
/**
* Create a copy of the configuration state in a scope of the given
* parent Jersey client instance.
*
* @param client parent Jersey client instance.
* @return configuration state copy.
*/
State copy(JerseyClient client) {
return new State(client, this);
}
void markAsShared() {
strategy = COPY_ON_CHANGE;
}
State preInitialize() {
final State state = strategy.onChange(this);
state.strategy = COPY_ON_CHANGE;
state.runtime.get().preInitialize();
return state;
}
@Override
public State property(final String name, final Object value) {
final State state = strategy.onChange(this);
state.commonConfig.property(name, value);
return state;
}
public State loadFrom(Configuration config) {
final State state = strategy.onChange(this);
state.commonConfig.loadFrom(config);
return state;
}
@Override
public State register(final Class> providerClass) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass);
return state;
}
@Override
public State register(final Object provider) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider);
return state;
}
@Override
public State register(final Class> providerClass, final int bindingPriority) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, bindingPriority);
return state;
}
@Override
public State register(final Class> providerClass, final Class>... contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, contracts);
return state;
}
@Override
public State register(final Class> providerClass, final Map, Integer> contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, contracts);
return state;
}
@Override
public State register(final Object provider, final int bindingPriority) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, bindingPriority);
return state;
}
@Override
public State register(final Object provider, final Class>... contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, contracts);
return state;
}
@Override
public State register(final Object provider, final Map, Integer> contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, contracts);
return state;
}
State connectorProvider(ConnectorProvider provider) {
if (provider == null) {
throw new NullPointerException(LocalizationMessages.NULL_CONNECTOR_PROVIDER());
}
final State state = strategy.onChange(this);
state.connectorProvider = provider;
return state;
}
Connector getConnector() {
// Get the connector only if the runtime has been initialized.
return (runtime.isInitialized()) ? runtime.get().getConnector() : null;
}
ConnectorProvider getConnectorProvider() {
return connectorProvider;
}
JerseyClient getClient() {
return client;
}
@Override
public State getConfiguration() {
return this;
}
@Override
public RuntimeType getRuntimeType() {
return commonConfig.getConfiguration().getRuntimeType();
}
@Override
public Map getProperties() {
return commonConfig.getConfiguration().getProperties();
}
@Override
public Object getProperty(String name) {
return commonConfig.getConfiguration().getProperty(name);
}
@Override
public Collection getPropertyNames() {
return commonConfig.getConfiguration().getPropertyNames();
}
@Override
public boolean isProperty(String name) {
return commonConfig.getConfiguration().isProperty(name);
}
@Override
public boolean isEnabled(Feature feature) {
return commonConfig.getConfiguration().isEnabled(feature);
}
@Override
public boolean isEnabled(Class extends Feature> featureClass) {
return commonConfig.getConfiguration().isEnabled(featureClass);
}
@Override
public boolean isRegistered(Object component) {
return commonConfig.getConfiguration().isRegistered(component);
}
@Override
public boolean isRegistered(Class> componentClass) {
return commonConfig.getConfiguration().isRegistered(componentClass);
}
@Override
public Map, Integer> getContracts(Class> componentClass) {
return commonConfig.getConfiguration().getContracts(componentClass);
}
@Override
public Set> getClasses() {
return commonConfig.getConfiguration().getClasses();
}
@Override
public Set