org.testifyproject.glassfish.hk2.utilities.BuilderHelper Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007-2012 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 org.testifyproject.testifyprojectpliance with the License. You can
* obtain a copy of the License at
* https://glassfish.org.testifyproject.testifyprojectv.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 org.testifyproject.testifyprojectsignates 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 org.testifyproject.testifyprojectcision 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.testifyproject.glassfish.hk2.utilities;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.testifyproject.glassfish.hk2.api.ActiveDescriptor;
import org.testifyproject.glassfish.hk2.api.Descriptor;
import org.testifyproject.glassfish.hk2.api.DescriptorType;
import org.testifyproject.glassfish.hk2.api.DescriptorVisibility;
import org.testifyproject.glassfish.hk2.api.Factory;
import org.testifyproject.glassfish.hk2.api.Filter;
import org.testifyproject.glassfish.hk2.api.IndexedFilter;
import org.testifyproject.glassfish.hk2.api.Metadata;
import org.testifyproject.glassfish.hk2.api.MultiException;
import org.testifyproject.glassfish.hk2.api.PerLookup;
import org.testifyproject.glassfish.hk2.api.ProxyForSameScope;
import org.testifyproject.glassfish.hk2.api.UseProxy;
import org.testifyproject.glassfish.hk2.api.Visibility;
import org.testifyproject.glassfish.hk2.internal.ActiveDescriptorBuilderImpl;
import org.testifyproject.glassfish.hk2.internal.ConstantActiveDescriptor;
import org.testifyproject.glassfish.hk2.internal.DescriptorBuilderImpl;
import org.testifyproject.glassfish.hk2.internal.IndexedFilterImpl;
import org.testifyproject.glassfish.hk2.internal.SpecificFilterImpl;
import org.testifyproject.glassfish.hk2.internal.StarFilter;
import org.testifyproject.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.testifyproject.jvnet.hk2.annotations.Contract;
import org.testifyproject.jvnet.hk2.annotations.Service;
/**
* This class is used to generate DescriptorBuilders to be used
* as a simple mechanism to create a Filter or Descriptor.
*/
public class BuilderHelper {
/**
* Returns an indexed filter that will return all org.testifyproject.testifyprojectscriptors that
* have contract as an advertised contract
*
* @param contract The advertised contract to look for
* @return The indexed filter that can be used to calls to ServiceLocator methods
*/
public static IndexedFilter createContractFilter(String contract) {
return new IndexedFilterImpl(contract, null);
}
/**
* Returns an indexed filter that will return all org.testifyproject.testifyprojectscriptors that
* have the given name
*
* @param name The name to look for
* @return The indexed filter that can be used to calls to ServiceLocator methods
*/
public static IndexedFilter createNameFilter(String name) {
return new IndexedFilterImpl(null, name);
}
/**
* Returns an indexed filter that will return all org.testifyproject.testifyprojectscriptors that
* have the given name and given contract
*
* @param contract The advertised contract to look for
* @param name The name to look for
* @return The indexed filter that can be used to calls to ServiceLocator methods
*/
public static IndexedFilter createNameAndContractFilter(String contract, String name) {
return new IndexedFilterImpl(contract, name);
}
/** The key for the name field of the tokenized string */
public final static String NAME_KEY = "name";
/** The key for the qualifier field of the tokenized string */
public final static String QUALIFIER_KEY = "qualifier";
/** The token separator */
public final static String TOKEN_SEPARATOR = ";";
/**
* Creates a filter from a token string as per the following rules.
* - The token org.testifyproject.testifyprojectlimiter is ;
* - The first token is the contract. If the tokenString starts with ; there is no contract
* - All other tokens are in key = value form (with = being the separator)
* - A valid key that can appear only once is "name"
* - A valid key that can appear any number of times is "qualifier"
*
*
* The following are examples of valid token strings:
* - org.testifyproject.testifyproject.acme.product.RocketShoes
* - org.testifyproject.testifyproject.acme.product.Sneakers;name=Nike
* - org.testifyproject.testifyproject.acme.product.Sneakers;name=Nike;qualifier=org.testifyproject.testifyproject.acme.color.Red;qualifier=org.testifyproject.testifyproject.acme.style.HighTop
* - ;name=Narcissus
*
*
* @param tokenString A non-null string following the rules stated above
* @return A filter that will match the given string
* @throws IllegalArgumentException If the token string is invalid in some way
*/
public static IndexedFilter createTokenizedFilter(String tokenString) throws IllegalArgumentException {
if (tokenString == null) throw new IllegalArgumentException("null passed to createTokenizedFilter");
StringTokenizer st = new StringTokenizer(tokenString, TOKEN_SEPARATOR);
String contract = null;
String name = null;
final Set qualifiers = new LinkedHashSet();
boolean firstToken = true;
if (tokenString.startsWith(TOKEN_SEPARATOR)) {
firstToken = false;
}
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (firstToken) {
firstToken = false;
if (token.length() <= 0) continue;
contract = token;
}
else {
int index = token.indexOf('=');
if (index < 0) {
throw new IllegalArgumentException("No = character found in token " + token);
}
String leftHandSide = token.substring(0, index);
String rightHandSide = token.substring(index + 1);
if (rightHandSide.length() <= 0) {
throw new IllegalArgumentException("No value found in token " + token);
}
if (NAME_KEY.equals(leftHandSide)) {
name = rightHandSide;
}
else if (QUALIFIER_KEY.equals(leftHandSide)) {
qualifiers.add(rightHandSide);
}
else {
throw new IllegalArgumentException("Unknown key: " + leftHandSide);
}
}
}
final String fContract = contract;
final String fName = name;
return new IndexedFilter() {
@Override
public boolean matches(Descriptor d) {
if (qualifiers.isEmpty()) return true;
return d.getQualifiers().containsAll(qualifiers);
}
@Override
public String getAdvertisedContract() {
return fContract;
}
@Override
public String getName() {
return fName;
}
@Override
public String toString() {
String cField = (fContract == null) ? "" : fContract;
String nField = (fName == null) ? "" : ";name=" + fName;
StringBuffer sb = new StringBuffer();
for (String q : qualifiers) {
sb.append(";qualifier=" + q);
}
return "TokenizedFilter(" + cField + nField + sb.toString() + ")";
}
};
}
/**
* This method creates a filter that will match one and only one org.testifyproject.testifyprojectscriptor. The passed
* in org.testifyproject.testifyprojectscriptor must have both its serviceID and locatorId filled in, or else this
* method will throw an IllegalArgumentException
*
* @param org.testifyproject.testifyprojectscriptor The org.testifyproject.testifyprojectscriptor from which to create a filter
* @return A filter to use that will match this org.testifyproject.testifyprojectscriptor exactly
*/
public static IndexedFilter createSpecificDescriptorFilter(Descriptor org.testifyproject.testifyprojectscriptor) {
String contract = ServiceLocatorUtilities.getBestContract(org.testifyproject.testifyprojectscriptor);
String name = org.testifyproject.testifyprojectscriptor.getName();
if (org.testifyproject.testifyprojectscriptor.getServiceId() == null) {
throw new IllegalArgumentException("The org.testifyproject.testifyprojectscriptor must have a specific service ID");
}
if (org.testifyproject.testifyprojectscriptor.getLocatorId() == null) {
throw new IllegalArgumentException("The org.testifyproject.testifyprojectscriptor must have a specific locator ID");
}
return new SpecificFilterImpl(contract, name,
org.testifyproject.testifyprojectscriptor.getServiceId(),
org.testifyproject.testifyprojectscriptor.getLocatorId());
}
/**
* Returns a filter of type Descriptor that matches
* all org.testifyproject.testifyprojectscriptors
*
* @return A filter that matches all org.testifyproject.testifyprojectscriptors
*/
public static Filter allFilter() {
return StarFilter.getDescriptorFilter();
}
/**
* This method links an implementation class with a {@link DescriptorBuilder}, to
* be used to further build the {@link Descriptor}.
*
* @param implementationClass The fully qualified name of the implementation
* class to be associated with the DescriptorBuilder.
* @param addToContracts if true, this implementation class will be added to the
* list of contracts
*
* @return A {@link DescriptorBuilder} that can be used to further build up the
* {@link Descriptor}
* @throws IllegalArgumentException if implementationClass is null
*/
public static DescriptorBuilder link(String implementationClass, boolean addToContracts) throws IllegalArgumentException {
if (implementationClass == null) throw new IllegalArgumentException();
return new DescriptorBuilderImpl(implementationClass, addToContracts);
}
/**
* This method links an implementation class with a {@link DescriptorBuilder}, to
* be used to further build the {@link Descriptor}. This method will automatically
* put the implementationClass into the list of advertised contracts.
*
* @param implementationClass The fully qualified name of the implementation
* class to be associated with the PredicateBuilder.
*
* @return A {@link DescriptorBuilder} that can be used to further build up the
* {@link Descriptor}
* @throws IllegalArgumentException if implementationClass is null
*/
public static DescriptorBuilder link(String implementationClass) throws IllegalArgumentException {
return link(implementationClass, true);
}
/**
* This method links an implementation class with a {@link DescriptorBuilder}, to
* be used to further build the {@link Descriptor}
*
* @param implementationClass The implementation class to be associated
* with the {@link DescriptorBuilder}.
* @param addToContracts true if this impl class should be automatically added to
* the list of contracts
* @return A {@link DescriptorBuilder} that can be used to further build up the
* {@link Descriptor}
* @throws IllegalArgumentException if implementationClass is null
*/
public static DescriptorBuilder link(Class> implementationClass, boolean addToContracts) throws IllegalArgumentException {
if (implementationClass == null) throw new IllegalArgumentException();
DescriptorBuilder builder = link(implementationClass.getName(), addToContracts);
return builder;
}
/**
* This method links an implementation class with a {@link DescriptorBuilder}, to
* be used to further build the {@link Descriptor}.
*
* @param implementationClass The implementation class to be associated
* with the {@link DescriptorBuilder}.
* @return A {@link DescriptorBuilder} that can be used to further build up the
* {@link Descriptor}
* @throws IllegalArgumentException if implementationClass is null
*/
public static DescriptorBuilder link(Class> implementationClass) throws IllegalArgumentException {
if (implementationClass == null) throw new IllegalArgumentException();
boolean isFactory = (Factory.class.isAssignableFrom(implementationClass));
DescriptorBuilder db = link(implementationClass, !isFactory);
return db;
}
/**
* This method creates an {@link ActiveDescriptorBuilder}, whose job it
* is to create an unreified {@link ActiveDescriptor}. The implementation
* class given will NOT automatically be added to the set of contracts
* of the {@link ActiveDescriptor}.
*
* @param implementationClass The implementation class to be associated
* with the {@link ActiveDescriptorBuilder}.
* @return A {@link ActiveDescriptorBuilder} that can be used to further build up the
* {@link Descriptor}
* @throws IllegalArgumentException if implementationClass is null
*/
public static ActiveDescriptorBuilder activeLink(Class> implementationClass) throws IllegalArgumentException {
if (implementationClass == null) throw new IllegalArgumentException();
return new ActiveDescriptorBuilderImpl(implementationClass);
}
/**
* This creates a org.testifyproject.testifyprojectscriptor that will always return the given object. The
* set of types in the advertised contracts will contain the class of the
* constant along with:
* - Any superclass of the constant marked with {@link Contract}
* - Any interface of the constant marked with {@link Contract}
*
*
* @param constant The non-null constant that should always be returned from
* the create method of this ActiveDescriptor.
* @return The org.testifyproject.testifyprojectscriptor returned can be used in calls to
* DynamicConfiguration.addActiveDescriptor
* @throws IllegalArgumentException if constant is null
*/
public static AbstractActiveDescriptor createConstantDescriptor(T constant) {
if (constant == null) throw new IllegalArgumentException();
Set contracts = ReflectionHelper.getAdvertisedTypesFromObject(constant, Contract.class);
return createConstantDescriptor(constant,
ReflectionHelper.getName(constant.getClass()),
contracts.toArray(new Type[contracts.size()]));
}
/**
* This creates a org.testifyproject.testifyprojectscriptor that will always return the given object.
* The advertised contracts is given in the incoming parameter and the
* name on the org.testifyproject.testifyprojectscriptor also org.testifyproject.testifyprojectes from the incoming parameter.
*
* @param constant The non-null constant that should always be returned from
* the create method of this ActiveDescriptor.
* @param name The possibly null name that should be associated with this constant org.testifyproject.testifyprojectscriptor
* @param contracts The possibly empty set of contracts that should be associated with this
* org.testifyproject.testifyprojectscriptor
* @return The org.testifyproject.testifyprojectscriptor returned can be used in calls to
* DynamicConfiguration.addActiveDescriptor
* @throws IllegalArgumentException if constant is null
*/
public static AbstractActiveDescriptor createConstantDescriptor(T constant, String name, Type... contracts) {
if (constant == null) throw new IllegalArgumentException();
Annotation scope =
ReflectionHelper.getScopeAnnotationFromObject(constant);
Class extends Annotation> scopeClass = (scope == null) ? PerLookup.class :
scope.annotationType();
Set qualifiers =
ReflectionHelper.getQualifiersFromObject(constant);
Map> metadata = new HashMap>();
if (scope != null) {
getMetadataValues(scope, metadata);
}
for (Annotation qualifier : qualifiers) {
getMetadataValues(qualifier, metadata);
}
Set contractsAsSet = new LinkedHashSet();
for (Type cType : contracts) {
contractsAsSet.add(cType);
}
Boolean proxy = null;
UseProxy up = constant.getClass().getAnnotation(UseProxy.class);
if (up != null) {
proxy = up.value();
}
Boolean proxyForSameScope = null;
ProxyForSameScope pfss = constant.getClass().getAnnotation(ProxyForSameScope.class);
if (pfss != null) {
proxyForSameScope = pfss.value();
}
DescriptorVisibility visibility = DescriptorVisibility.NORMAL;
Visibility vi = constant.getClass().getAnnotation(Visibility.class);
if (vi != null) {
visibility = vi.value();
}
String classAnalysisName = null;
Service service = constant.getClass().getAnnotation(Service.class);
if (service != null) {
classAnalysisName = service.analyzer();
}
return new ConstantActiveDescriptor(
constant,
contractsAsSet,
scopeClass,
name,
qualifiers,
visibility,
proxy,
proxyForSameScope,
classAnalysisName,
metadata);
}
/**
* This returns a DescriptorImpl based on the given class. The returned
* org.testifyproject.testifyprojectscriptor will include the class itself as an advertised contract and
* all implemented interfaces that are marked @Contract
*
* @param clazz The class to analyze
* @return The DescriptorImpl corresponding to this class
*/
public static DescriptorImpl createDescriptorFromClass(Class> clazz) {
if (clazz == null) return new DescriptorImpl();
Set contracts = ReflectionHelper.getContractsFromClass(clazz, Contract.class);
String name = ReflectionHelper.getName(clazz);
String scope = ReflectionHelper.getScopeFromClass(clazz, PerLookup.class).getName();
Set qualifiers = ReflectionHelper.getQualifiersFromClass(clazz);
DescriptorType type = DescriptorType.CLASS;
if (Factory.class.isAssignableFrom(clazz)) {
type = DescriptorType.PROVIDE_METHOD;
}
Boolean proxy = null;
UseProxy up = clazz.getAnnotation(UseProxy.class);
if (up != null) {
proxy = Boolean.valueOf(up.value());
}
Boolean proxyForSameScope = null;
ProxyForSameScope pfss = clazz.getAnnotation(ProxyForSameScope.class);
if (pfss != null) {
proxyForSameScope = Boolean.valueOf(pfss.value());
}
DescriptorVisibility visibility = DescriptorVisibility.NORMAL;
Visibility vi = clazz.getAnnotation(Visibility.class);
if (vi != null) {
visibility = vi.value();
}
// TODO: Can we get metadata from @Service?
return new DescriptorImpl(
contracts,
name,
scope,
clazz.getName(),
new HashMap>(),
qualifiers,
type,
visibility,
null,
0,
proxy,
proxyForSameScope,
null,
null,
null);
}
/**
* Makes a org.testifyproject.testifyprojectep copy of the incoming org.testifyproject.testifyprojectscriptor
*
* @param copyMe The org.testifyproject.testifyprojectscriptor to copy
* @return A new org.testifyproject.testifyprojectscriptor with all fields copied
*/
public static DescriptorImpl org.testifyproject.testifyprojectepCopyDescriptor(Descriptor copyMe) {
return new DescriptorImpl(copyMe);
}
/**
* This is a helper method that gets the metadata values from the
* {@link Metadata} annotations found in an annotation.
*
* @param annotation The annotation to find {@link Metadata} values
* from. May not be null.
* @param metadata A non-null metadata map. The values found in the
* annotation will be added to this metadata map
* @throws IllegalArgumentException if annotation or metadata is null
* @throws MultiException if there was an error invoking the methods of the annotation
*/
public static void getMetadataValues(Annotation annotation, Map> metadata) {
if (annotation == null || metadata == null) {
throw new IllegalArgumentException();
}
final Class extends Annotation> annotationClass = annotation.annotationType();
Method annotationMethods[] = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Method[] run() {
return annotationClass.getDeclaredMethods();
}
});
for (Method annotationMethod : annotationMethods) {
Metadata metadataAnno = annotationMethod.getAnnotation(Metadata.class);
if (metadataAnno == null) continue;
String key = metadataAnno.value();
Object addMe;
try {
addMe = ReflectionHelper.invoke(annotation, annotationMethod, new Object[0], false);
}
catch (Throwable th) {
throw new MultiException(th);
}
if (addMe == null) continue;
String addMeString;
if (addMe instanceof Class) {
addMeString = ((Class>) addMe).getName();
}
else {
addMeString = addMe.toString();
}
ReflectionHelper.addMetadata(metadata, key, addMeString);
}
}
}