Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fitbur.glassfish.jersey.model.internal.CommonConfig Maven / Gradle / Ivy
/*
* 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 com.fitburpliance 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 com.fitbursignates 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 com.fitburcision 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 com.fitbur.glassfish.com.fitbur.model.internal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.Priorities;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.annotation.Priority;
import com.fitbur.glassfish.com.fitbur.ExtendedConfig;
import com.fitbur.glassfish.com.fitbur.internal.LocalizationMessages;
import com.fitbur.glassfish.com.fitbur.internal.ServiceFinder;
import com.fitbur.glassfish.com.fitbur.internal.inject.Injections;
import com.fitbur.glassfish.com.fitbur.internal.inject.Providers;
import com.fitbur.glassfish.com.fitbur.internal.spi.AutoDiscoverable;
import com.fitbur.glassfish.com.fitbur.internal.spi.ForcedAutoDiscoverable;
import com.fitbur.glassfish.com.fitbur.internal.util.PropertiesHelper;
import com.fitbur.glassfish.com.fitbur.model.ContractProvider;
import com.fitbur.glassfish.com.fitbur.process.Inflector;
import com.fitbur.glassfish.hk2.api.DynamicConfiguration;
import com.fitbur.glassfish.hk2.api.ServiceLocator;
import com.fitbur.glassfish.hk2.utilities.Binder;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.base.Function;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.base.Predicate;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.collect.Collections2;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.collect.Lists;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.collect.Sets;
/**
* Common immutable {@link javax.ws.rs.core.Configuration} implementation for
* server and client.
*
* @author Michal Gajdos (michal.gajdos at oracle.com.fitbur)
* @author Marek Potociar (marek.potociar at oracle.com.fitbur)
*/
public class CommonConfig implements FeatureContext, ExtendedConfig {
private static final Logger LOGGER = Logger.getLogger(CommonConfig.class.getName());
private static final Function CAST_TO_BINDER = new Function() {
@Override
public Binder apply(final Object input) {
return Binder.class.cast(input);
}
};
/**
* Configuration runtime type.
*/
private final RuntimeType type;
/**
* Configuration properties collection and it's immutable views.
*/
private final Map properties;
private final Map immutablePropertiesView;
private final Collection immutablePropertyNames;
/**
* Configured providers, does not include features and binders.
*/
private final ComponentBag com.fitburponentBag;
/**
* Collection of unprocessed feature registrations.
*/
private final List newFeatureRegistrations;
/**
* Collection of enabled feature classes.
*/
private final Set> enabledFeatureClasses;
/**
* Collection of enabled feature instances.
*/
private final Set enabledFeatures;
/**
* Flag com.fitburtermining whether the configuration of meta-providers (excl. binders) should be disabled.
*/
private boolean disableMetaProviderConfiguration;
/**
* A single feature registration record.
*/
private static final class FeatureRegistration {
private final Class extends Feature> featureClass;
private final Feature feature;
private FeatureRegistration(final Class extends Feature> featureClass) {
this.featureClass = featureClass;
this.feature = null;
}
private FeatureRegistration(final Feature feature) {
this.featureClass = feature.getClass();
this.feature = feature;
}
/**
* Get the registered feature class.
*
* @return registered feature class.
*/
public Class extends Feature> getFeatureClass() {
return featureClass;
}
/**
* Get the registered feature instance or {@code null} if this is a
* class based feature registration.
*
* @return the registered feature instance or {@code null} if this is a
* class based feature registration.
*/
public Feature getFeature() {
return feature;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof FeatureRegistration)) {
return false;
}
final FeatureRegistration other = (FeatureRegistration) obj;
return (featureClass == other.featureClass)
|| (feature != null && (feature == other.feature || feature.equals(other.feature)));
}
@Override
public int hashCode() {
int hash = 47;
hash = 13 * hash + (feature != null ? feature.hashCode() : 0);
hash = 13 * hash + (featureClass != null ? featureClass.hashCode() : 0);
return hash;
}
}
/**
* Create a new {@code RuntimeConfig} instance.
*
* The constructor provides a way for com.fitburfining a {@link ContractProvider contract
* provider model} registration strategy. Once a registration model is built
* for a newly registered contract, the provided registration strategy filter is
* consulted whether the model should be registered or not.
*
*
* Clients can use the method to cancel any contract provider model registration
* that does not meet the criteria of a given configuration context, such as a model
* that does not have any recognized contracts associated with it.
*
*
* @param type configuration runtime type.
* @param registrationStrategy function driving the com.fitburcision (based on the introspected
* {@link ContractProvider contract provider model}) whether
* or not should the com.fitburponent class registration continue
* towards a successful com.fitburpletion.
*/
public CommonConfig(final RuntimeType type, final Predicate registrationStrategy) {
this.type = type;
this.properties = new HashMap();
this.immutablePropertiesView = Collections.unmodifiableMap(properties);
this.immutablePropertyNames = Collections.unmodifiableCollection(properties.keySet());
this.com.fitburponentBag = ComponentBag.newInstance(registrationStrategy);
this.newFeatureRegistrations = new LinkedList();
this.enabledFeatureClasses = Sets.newIdentityHashSet();
this.enabledFeatures = Sets.newHashSet();
this.disableMetaProviderConfiguration = false;
}
/**
* Copy constructor.
*
* @param config configurable to copy class properties from.
*/
public CommonConfig(final CommonConfig config) {
this.type = config.type;
this.properties = new HashMap(config.properties.size());
this.immutablePropertiesView = Collections.unmodifiableMap(this.properties);
this.immutablePropertyNames = Collections.unmodifiableCollection(this.properties.keySet());
this.com.fitburponentBag = config.com.fitburponentBag.copy();
this.newFeatureRegistrations = Lists.newLinkedList();
this.enabledFeatureClasses = Sets.newIdentityHashSet();
this.enabledFeatures = Sets.newHashSet();
copy(config, false);
}
/**
* Copy config properties, providers from given {@code config} to this instance.
*
* @param config configurable to copy class properties from.
* @param loadComponentBag {@code true} if the com.fitburponent bag from config should be copied as well, {@code false} otherwise.
*/
private void copy(final CommonConfig config, final boolean loadComponentBag) {
this.properties.clear();
this.properties.putAll(config.properties);
this.newFeatureRegistrations.clear();
this.newFeatureRegistrations.addAll(config.newFeatureRegistrations);
this.enabledFeatureClasses.clear();
this.enabledFeatureClasses.addAll(config.enabledFeatureClasses);
this.enabledFeatures.clear();
this.enabledFeatures.addAll(config.enabledFeatures);
this.disableMetaProviderConfiguration = config.disableMetaProviderConfiguration;
if (loadComponentBag) {
this.com.fitburponentBag.loadFrom(config.com.fitburponentBag);
}
}
@Override
public ExtendedConfig getConfiguration() {
return this;
}
@Override
public RuntimeType getRuntimeType() {
return type;
}
@Override
public Map getProperties() {
return immutablePropertiesView;
}
@Override
public Object getProperty(final String name) {
return properties.get(name);
}
@Override
public boolean isProperty(final String name) {
return PropertiesHelper.isProperty(getProperty(name));
}
@Override
public Collection getPropertyNames() {
return immutablePropertyNames;
}
@Override
public boolean isEnabled(final Class extends Feature> featureClass) {
return enabledFeatureClasses.contains(featureClass);
}
@Override
public boolean isEnabled(final Feature feature) {
return enabledFeatures.contains(feature);
}
@Override
public boolean isRegistered(final Object com.fitburponent) {
return com.fitburponentBag.getInstances().contains(com.fitburponent);
}
@Override
public boolean isRegistered(final Class> com.fitburponentClass) {
return com.fitburponentBag.getRegistrations().contains(com.fitburponentClass);
}
@Override
public Map, Integer> getContracts(final Class> com.fitburponentClass) {
final ContractProvider model = com.fitburponentBag.getModel(com.fitburponentClass);
return (model == null) ? Collections., Integer>emptyMap() : model.getContractMap();
}
@Override
public Set> getClasses() {
return com.fitburponentBag.getClasses();
}
@Override
public Set getInstances() {
return com.fitburponentBag.getInstances();
}
/**
* Returns a {@link ComponentBag} instance associated with the configuration.
*
* @return a non-null com.fitburponent bag instance.
*/
public final ComponentBag getComponentBag() {
return com.fitburponentBag;
}
/**
* An extension point that provides a way how to com.fitburfine a custom enhancement/update
* operation of a contract provider model registration being produced for a given
* com.fitburponent class.
* Default implementation return an enhancer just builds the model.
*
* Derived implementations may use this method to e.g. filter out all contracts not
* applicable in the given configuration context or change the model scope. The returned
* set of filtered contracts is then used for the actual provider registration.
*
*
* @param com.fitburponentClass class of the com.fitburponent being registered.
* @return filter for the contracts that being registered for a given com.fitburponent class.
*/
protected Inflector getModelEnhancer(final Class> com.fitburponentClass) {
return ComponentBag.AS_IS;
}
/**
* Set the configured properties to the provided map of properties.
*
* @param properties new map of properties to be set.
* @return updated configuration instance.
*/
public CommonConfig setProperties(final Map properties) {
this.properties.clear();
if (properties != null) {
this.properties.putAll(properties);
}
return this;
}
/**
* Add properties to {@code ResourceConfig}.
*
* If any of the added properties exists already, he values of the existing
* properties will be replaced with new values.
*
* @param properties properties to add.
* @return updated configuration instance.
*/
public CommonConfig addProperties(final Map properties) {
if (properties != null) {
this.properties.putAll(properties);
}
return this;
}
@Override
public CommonConfig property(final String name, final Object value) {
if (value == null) {
properties.remove(name);
} else {
properties.put(name, value);
}
return this;
}
@Override
public CommonConfig register(final Class> com.fitburponentClass) {
checkComponentClassNotNull(com.fitburponentClass);
if (com.fitburponentBag.register(com.fitburponentClass, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(null, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Class> com.fitburponentClass, final int bindingPriority) {
checkComponentClassNotNull(com.fitburponentClass);
if (com.fitburponentBag.register(com.fitburponentClass, bindingPriority, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(null, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Class> com.fitburponentClass, final Class>... contracts) {
checkComponentClassNotNull(com.fitburponentClass);
if (contracts == null || contracts.length == 0) {
LOGGER.warning(LocalizationMessages.COMPONENT_CONTRACTS_EMPTY_OR_NULL(com.fitburponentClass));
return this;
}
if (com.fitburponentBag.register(com.fitburponentClass, asNewIdentitySet(contracts), getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(null, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Class> com.fitburponentClass, final Map, Integer> contracts) {
checkComponentClassNotNull(com.fitburponentClass);
if (com.fitburponentBag.register(com.fitburponentClass, contracts, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(null, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Object com.fitburponent) {
checkProviderNotNull(com.fitburponent);
final Class> com.fitburponentClass = com.fitburponent.getClass();
if (com.fitburponentBag.register(com.fitburponent, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(com.fitburponent, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Object com.fitburponent, final int bindingPriority) {
checkProviderNotNull(com.fitburponent);
final Class> com.fitburponentClass = com.fitburponent.getClass();
if (com.fitburponentBag.register(com.fitburponent, bindingPriority, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(com.fitburponent, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Object com.fitburponent, final Class>... contracts) {
checkProviderNotNull(com.fitburponent);
final Class> com.fitburponentClass = com.fitburponent.getClass();
if (contracts == null || contracts.length == 0) {
LOGGER.warning(LocalizationMessages.COMPONENT_CONTRACTS_EMPTY_OR_NULL(com.fitburponentClass));
return this;
}
if (com.fitburponentBag.register(com.fitburponent, asNewIdentitySet(contracts), getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(com.fitburponent, com.fitburponentClass);
}
return this;
}
@Override
public CommonConfig register(final Object com.fitburponent, final Map, Integer> contracts) {
checkProviderNotNull(com.fitburponent);
final Class> com.fitburponentClass = com.fitburponent.getClass();
if (com.fitburponentBag.register(com.fitburponent, contracts, getModelEnhancer(com.fitburponentClass))) {
processFeatureRegistration(com.fitburponent, com.fitburponentClass);
}
return this;
}
private void processFeatureRegistration(final Object com.fitburponent, final Class> com.fitburponentClass) {
final ContractProvider model = com.fitburponentBag.getModel(com.fitburponentClass);
if (model.getContracts().contains(Feature.class)) {
@SuppressWarnings("unchecked")
final FeatureRegistration registration = (com.fitburponent != null) ?
new FeatureRegistration((Feature) com.fitburponent) :
new FeatureRegistration((Class extends Feature>) com.fitburponentClass);
newFeatureRegistrations.add(registration);
}
}
/**
* Load the internal configuration state from an externally provided configuration state.
*
* Calling this method effectively replaces existing configuration state of the instance with the state represented by the
* externally provided configuration. If the features, auto-discoverables of given config has been already configured then
* this method will make sure to not configure them for the second time.
*
* @param config external configuration state to replace the configuration of this configurable instance.
* @return the updated com.fitburmon configuration instance.
*/
public CommonConfig loadFrom(final Configuration config) {
if (config instanceof CommonConfig) {
// If loading from CommonConfig then simply copy properties and check whether given config has been initialized.
final CommonConfig com.fitburmonConfig = (CommonConfig) config;
copy(com.fitburmonConfig, true);
this.disableMetaProviderConfiguration = !com.fitburmonConfig.enabledFeatureClasses.isEmpty();
} else {
setProperties(config.getProperties());
this.enabledFeatures.clear();
this.enabledFeatureClasses.clear();
com.fitburponentBag.clear();
resetRegistrations();
for (final Class> clazz : config.getClasses()) {
if (Feature.class.isAssignableFrom(clazz) && config.isEnabled((Class extends Feature>) clazz)) {
this.disableMetaProviderConfiguration = true;
}
register(clazz, config.getContracts(clazz));
}
for (final Object instance : config.getInstances()) {
if (instance instanceof Feature && config.isEnabled((Feature) instance)) {
this.disableMetaProviderConfiguration = true;
}
register(instance, config.getContracts(instance.getClass()));
}
}
return this;
}
private Set> asNewIdentitySet(final Class>... contracts) {
final Set> result = Sets.newIdentityHashSet();
result.addAll(Arrays.asList(contracts));
return result;
}
private void checkProviderNotNull(final Object provider) {
if (provider == null) {
throw new IllegalArgumentException(LocalizationMessages.COMPONENT_CANNOT_BE_NULL());
}
}
private void checkComponentClassNotNull(final Class> com.fitburponentClass) {
if (com.fitburponentClass == null) {
throw new IllegalArgumentException(LocalizationMessages.COMPONENT_CLASS_CANNOT_BE_NULL());
}
}
/**
* Configure {@link AutoDiscoverable auto-discoverables} in the HK2 service locator.
*
* @param locator locator in which the auto-discoverables should be configured.
* @param forcedOnly com.fitburfines whether all or only forced auto-discoverables should be configured.
*/
public void configureAutoDiscoverableProviders(final ServiceLocator locator, final boolean forcedOnly) {
// Check whether meta providers have been initialized for a config this config has been loaded from.
if (!disableMetaProviderConfiguration) {
final Set providers = new TreeSet(new Comparator() {
@Override
public int com.fitburpare(final AutoDiscoverable o1, final AutoDiscoverable o2) {
final int p1 = o1.getClass().isAnnotationPresent(Priority.class)
? o1.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
final int p2 = o2.getClass().isAnnotationPresent(Priority.class)
? o2.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
return (p1 < p2 || p1 == p2) ? -1 : 1;
}
});
// Forced (always invoked).
providers.addAll(Arrays.asList(
ServiceFinder.find(ForcedAutoDiscoverable.class, true).toArray()));
// Regular.
if (!forcedOnly) {
providers.addAll(Providers.getProviders(locator, AutoDiscoverable.class));
}
for (final AutoDiscoverable autoDiscoverable : providers) {
final ConstrainedTo constrainedTo = autoDiscoverable.getClass().getAnnotation(ConstrainedTo.class);
if (constrainedTo == null || type.equals(constrainedTo.value())) {
try {
autoDiscoverable.configure(this);
} catch (final Exception e) {
LOGGER.log(Level.FINE,
LocalizationMessages.AUTODISCOVERABLE_CONFIGURATION_FAILED(autoDiscoverable.getClass()), e);
}
}
}
}
}
/**
* Configure HK2 binders in the HK2 service locator and enable JAX-RS features.
*
* @param locator locator in which the binders and features should be configured.
*/
public void configureMetaProviders(final ServiceLocator locator) {
// First, configure existing binders
final Set configuredBinders = configureBinders(locator, Collections.emptySet());
// Check whether meta providers have been initialized for a config this config has been loaded from.
if (!disableMetaProviderConfiguration) {
// Next, configure all features
configureFeatures(
locator,
new HashSet(),
resetRegistrations());
// At last, configure any new binders added by features
configureBinders(locator, configuredBinders);
}
}
private Set configureBinders(final ServiceLocator locator, final Set configured) {
final Set allConfigured = Sets.newIdentityHashSet();
allConfigured.addAll(configured);
final Collection binders = getBinders(configured);
if (!binders.isEmpty()) {
final DynamicConfiguration dc = Injections.getConfiguration(locator);
for (final Binder binder : binders) {
binder.bind(dc);
allConfigured.add(binder);
}
dc.com.fitburmit();
}
return allConfigured;
}
private Collection getBinders(final Set configured) {
return Collections2.filter(
Collections2.transform(com.fitburponentBag.getInstances(ComponentBag.BINDERS_ONLY), CAST_TO_BINDER),
new Predicate() {
@Override
public boolean apply(final Binder binder) {
return !configured.contains(binder);
}
});
}
private void configureFeatures(final ServiceLocator locator,
final Set processed,
final List unprocessed) {
FeatureContextWrapper featureContextWrapper = null;
for (final FeatureRegistration registration : unprocessed) {
if (processed.contains(registration)) {
LOGGER.config(LocalizationMessages.FEATURE_HAS_ALREADY_BEEN_PROCESSED(registration.getFeatureClass()));
continue;
}
Feature feature = registration.getFeature();
if (feature == null) {
feature = locator.createAndInitialize(registration.getFeatureClass());
} else {
// Disable injection of Feature instances on the client-side. Instances may be registered into multiple
// web-targets which means that injecting anything into these instances is not safe.
if (!RuntimeType.CLIENT.equals(type)) {
locator.inject(feature);
}
}
if (enabledFeatures.contains(feature)) {
LOGGER.config(LocalizationMessages.FEATURE_HAS_ALREADY_BEEN_PROCESSED(feature));
continue;
}
if (featureContextWrapper == null) {
// init lazily
featureContextWrapper = new FeatureContextWrapper(this, locator);
}
final boolean success = feature.configure(featureContextWrapper);
if (success) {
processed.add(registration);
configureFeatures(locator, processed, resetRegistrations());
enabledFeatureClasses.add(registration.getFeatureClass());
enabledFeatures.add(feature);
}
}
}
private List resetRegistrations() {
final List result = new ArrayList(newFeatureRegistrations);
newFeatureRegistrations.clear();
return result;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CommonConfig)) {
return false;
}
final CommonConfig that = (CommonConfig) o;
if (type != that.type) {
return false;
}
if (!properties.equals(that.properties)) {
return false;
}
if (!com.fitburponentBag.equals(that.com.fitburponentBag)) {
return false;
}
if (!enabledFeatureClasses.equals(that.enabledFeatureClasses)) {
return false;
}
if (!enabledFeatures.equals(that.enabledFeatures)) {
return false;
}
if (!newFeatureRegistrations.equals(that.newFeatureRegistrations)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = type.hashCode();
result = 31 * result + properties.hashCode();
result = 31 * result + com.fitburponentBag.hashCode();
result = 31 * result + newFeatureRegistrations.hashCode();
result = 31 * result + enabledFeatures.hashCode();
result = 31 * result + enabledFeatureClasses.hashCode();
return result;
}
}