You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/03/28 18:03:04 UTC
svn commit: r523381 [2/4] - in /incubator/tuscany/java/sca/kernel:
core/src/main/java/org/apache/tuscany/core/bootstrap/
core/src/main/java/org/apache/tuscany/core/builder/
core/src/main/java/org/apache/tuscany/core/deployer/
core/src/main/java/org/apa...
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,66 +18,74 @@
*/
package org.apache.tuscany.core.implementation.processor;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllPublicAndProtectedFields;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.net.URI;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import org.osoa.sca.annotations.Reference;
-import org.osoa.sca.annotations.Remotable;
-import org.osoa.sca.annotations.Service;
+import javax.xml.namespace.QName;
+import org.apache.tuscany.api.annotation.Monitor;
+import org.apache.tuscany.api.annotation.Resource;
+import org.apache.tuscany.core.idl.java.IllegalCallbackException;
+import org.apache.tuscany.core.util.JavaIntrospectionHelper;
import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension;
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.idl.InvalidServiceContractException;
-import org.apache.tuscany.spi.model.TypeInfo;
import org.apache.tuscany.spi.implementation.java.ConstructorDefinition;
import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension;
-import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
import org.apache.tuscany.spi.implementation.java.JavaMappedService;
+import org.apache.tuscany.spi.implementation.java.Parameter;
import org.apache.tuscany.spi.implementation.java.PojoComponentType;
import org.apache.tuscany.spi.implementation.java.ProcessingException;
-
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllPublicAndProtectedFields;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+import org.apache.tuscany.spi.model.ServiceContract;
+import org.apache.tuscany.spi.model.TypeInfo;
+import org.osoa.sca.annotations.Callback;
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Reference;
+import org.osoa.sca.annotations.Remotable;
+import org.osoa.sca.annotations.Service;
/**
- * Heuristically evaluates an un-annotated Java implementation type to determine services, references, and properties
- * according to the algorithm described in the SCA Java Client and Implementation Model Specification <p/> TODO
- * Implement:
- * <p/>
- * When no service inteface is annotated, need to calculate a single service comprising all public methods that are not
- * reference or property injection sites. If that service can be exactly mapped to an interface implemented by the class
- * then the service interface will be defined in terms of that interface.
- *
+ * Heuristically evaluates an un-annotated Java implementation type to determine
+ * services, references, and properties according to the algorithm described in
+ * the SCA Java Client and Implementation Model Specification <p/> TODO
+ * Implement: <p/> When no service inteface is annotated, need to calculate a
+ * single service comprising all public methods that are not reference or
+ * property injection sites. If that service can be exactly mapped to an
+ * interface implemented by the class then the service interface will be defined
+ * in terms of that interface.
+ *
* @version $Rev$ $Date$
*/
public class HeuristicPojoProcessor extends ImplementationProcessorExtension {
private SimpleTypeMapperExtension typeMapper = new SimpleTypeMapperExtension();
- private ImplementationProcessorService implService;
- public HeuristicPojoProcessor(@Reference ImplementationProcessorService service) {
- this.implService = service;
+ public HeuristicPojoProcessor() {
}
- public <T> void visitEnd(
- Class<T> clazz,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
+ public <T> void visitEnd(Class<T> clazz,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
Map<String, JavaMappedService> services = type.getServices();
if (services.isEmpty()) {
// heuristically determine the service
@@ -85,21 +93,10 @@
Set<Class> interfaces = getAllInterfaces(clazz);
if (interfaces.size() == 0) {
// class is the interface
- JavaMappedService service;
- try {
- service = implService.createService(clazz);
- } catch (InvalidServiceContractException e) {
- throw new ProcessingException(e);
- }
- type.getServices().put(service.getUri().getFragment(), service);
+ addService(type, clazz);
} else if (interfaces.size() == 1) {
- JavaMappedService service;
- try {
- service = implService.createService(interfaces.iterator().next());
- } catch (InvalidServiceContractException e) {
- throw new ProcessingException(e);
- }
- type.getServices().put(service.getUri().getFragment(), service);
+ // Only one interface, take it
+ addService(type, interfaces.iterator().next());
}
}
Set<Method> methods = getAllUniquePublicProtectedMethods(clazz);
@@ -118,25 +115,48 @@
evaluateConstructor(type, clazz);
}
+ private void addService(PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ Class<?> clazz) throws ProcessingException {
+ try {
+ JavaMappedService service = createService(clazz);
+ type.getServices().put(service.getUri().getFragment(), service);
+ } catch (InvalidServiceContractException e) {
+ throw new ProcessingException(e);
+ }
+ }
+
+ private boolean isPublicSetter(Method method) {
+ return method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers())
+ && method.getName().startsWith("set")
+ && method.getReturnType() == void.class;
+ }
+
+ private boolean isProtectedSetter(Method method) {
+ return method.getParameterTypes().length == 1 && Modifier.isProtected(method.getModifiers())
+ && method.getName().startsWith("set")
+ && method.getReturnType() == void.class;
+ }
+
private <T> void calcPropRefs(Set<Method> methods,
Map<String, JavaMappedService> services,
PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
Class<T> clazz) throws ProcessingException {
// heuristically determine the properties references
// make a first pass through all public methods with one param
+ Set<String> setters = new HashSet<String>();
for (Method method : methods) {
- if (method.getParameterTypes().length != 1 || !Modifier.isPublic(method.getModifiers())
- || !method.getName().startsWith("set")
- || method.getReturnType() != void.class) {
+ if (!isPublicSetter(method)) {
continue;
}
if (!isInServiceInterface(method, services)) {
+ // Not part of the service interface
String name = toPropertyName(method.getName());
+ setters.add(name);
// avoid duplicate property or ref names
if (!type.getProperties().containsKey(name) && !type.getReferences().containsKey(name)) {
Class<?> param = method.getParameterTypes()[0];
Type genericType = method.getGenericParameterTypes()[0];
- if (isReferenceType(genericType)) {
+ if (isReferenceType(param, genericType)) {
type.add(createReference(name, method, param));
} else {
type.add(createProperty(name, method, param));
@@ -146,26 +166,32 @@
}
// second pass for protected methods with one param
for (Method method : methods) {
- if (method.getParameterTypes().length != 1 || !Modifier.isProtected(method.getModifiers())
- || !method.getName().startsWith("set")
- || method.getReturnType() != void.class) {
+ if (!isProtectedSetter(method)) {
continue;
}
Class<?> param = method.getParameterTypes()[0];
String name = toPropertyName(method.getName());
+ setters.add(name);
// avoid duplicate property or ref names
if (!type.getProperties().containsKey(name) && !type.getReferences().containsKey(name)) {
- if (isReferenceType(param)) {
+ if (isReferenceType(param, method.getGenericParameterTypes()[0])) {
type.add(createReference(name, method, param));
} else {
type.add(createProperty(name, method, param));
}
}
}
+
+ // Public or protected fields unless there is a public or protected
+ // setter method
+ // for the same name
Set<Field> fields = getAllPublicAndProtectedFields(clazz);
for (Field field : fields) {
+ if (setters.contains(field.getName())) {
+ continue;
+ }
Class<?> paramType = field.getType();
- if (isReferenceType(paramType)) {
+ if (isReferenceType(paramType, field.getGenericType())) {
type.add(createReference(field.getName(), field, paramType));
} else {
type.add(createProperty(field.getName(), field, paramType));
@@ -174,24 +200,24 @@
}
/**
- * Determines the constructor to use based on the component type's references and properties
- *
- * @param type the component type
+ * Determines the constructor to use based on the component type's
+ * references and properties
+ *
+ * @param type the component type
* @param clazz the implementation class corresponding to the component type
- * @throws NoConstructorException if no suitable constructor is found
- * @throws AmbiguousConstructorException if the parameters of a constructor cannot be unambiguously mapped to
- * references and properties
+ * @throws NoConstructorException if no suitable constructor is found
+ * @throws AmbiguousConstructorException if the parameters of a constructor
+ * cannot be unambiguously mapped to references and properties
*/
@SuppressWarnings("unchecked")
- private <T> void evaluateConstructor(
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- Class<T> clazz) throws ProcessingException {
+ private <T> void evaluateConstructor(PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ Class<T> clazz) throws ProcessingException {
// determine constructor if one is not annotated
ConstructorDefinition<?> definition = type.getConstructorDefinition();
Constructor constructor;
boolean explict = false;
- if (definition != null
- && definition.getConstructor().getAnnotation(org.osoa.sca.annotations.Constructor.class) != null) {
+ if (definition != null && definition.getConstructor()
+ .isAnnotationPresent(org.osoa.sca.annotations.Constructor.class)) {
// the constructor was already defined explicitly
return;
} else if (definition != null) {
@@ -203,6 +229,7 @@
if (constructors.length == 0) {
throw new NoConstructorException("No public constructor for class", clazz.getName());
} else if (constructors.length == 1) {
+ // Only one constructor, take it
constructor = constructors[0];
} else {
// FIXME multiple constructors, none yet done
@@ -223,92 +250,90 @@
throw new NoConstructorException();
}
constructor = selected;
- definition = new ConstructorDefinition<T>(selected);
+ definition = type.getConstructors().get(selected);
type.setConstructorDefinition(definition);
// return;
}
- definition = new ConstructorDefinition<T>(constructor);
+ definition = type.getConstructors().get(constructor);
type.setConstructorDefinition(definition);
}
- Class[] params = constructor.getParameterTypes();
- if (params.length == 0) {
+ Parameter[] parameters = definition.getParameters();
+ if (parameters.length == 0) {
return;
}
- List<String> paramNames = definition.getInjectionNames();
Map<String, JavaMappedProperty<?>> props = type.getProperties();
Map<String, JavaMappedReference> refs = type.getReferences();
Annotation[][] annotations = constructor.getParameterAnnotations();
if (!explict) {
- // the constructor wasn't defined by an annotation, so check to see if any of the params have an annotation
- // which we can impute as explicitly defining the constructor, e.g. @Property, @Reference, or @Autowire
- explict = implService.injectionAnnotationsPresent(annotations);
+ // the constructor wasn't defined by an annotation, so check to see
+ // if any of the params have an annotation
+ // which we can impute as explicitly defining the constructor, e.g.
+ // @Property, @Reference, or @Autowire
+ explict = injectionAnnotationsPresent(annotations);
}
if (explict) {
- for (int i = 0; i < params.length; i++) {
- Class param = params[i];
- implService.processParam(param,
- constructor.getGenericParameterTypes()[i],
- annotations[i],
- new String[0],
- i,
- type,
- paramNames);
+ for (int i = 0; i < parameters.length; i++) {
+ if (isAnnotated(parameters[i])) {
+ continue;
+ } else if (!findReferenceOrProperty(parameters[i], props, refs)) {
+ throw new AmbiguousConstructorException(parameters[i].toString());
+ }
}
} else {
- if (!implService.areUnique(params)) {
+ if (!areUnique(parameters)) {
throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor");
}
if (!calcPropRefUniqueness(props.values(), refs.values())) {
throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor");
}
- boolean empty = props.size() + refs.size() == 0;
- if (!empty) {
- calcParamNames(params, props, refs, paramNames);
+ if (!(props.isEmpty() && refs.isEmpty())) {
+ calcParamNames(parameters, props, refs);
} else {
- heuristicParamNames(params, refs, props, paramNames);
+ heuristicParamNames(parameters, refs, props);
}
}
}
- private void calcParamNames(Class[] params,
+ private void calcParamNames(Parameter[] parameters,
Map<String, JavaMappedProperty<?>> props,
- Map<String, JavaMappedReference> refs,
- List<String> paramNames)
- throws AmbiguousConstructorException {
- // the constructor param types must unambiguously match defined reference or property types
- for (Class param : params) {
- String name = findReferenceOrProperty(param, props, refs);
- if (name == null) {
+ Map<String, JavaMappedReference> refs) throws AmbiguousConstructorException {
+ // the constructor param types must unambiguously match defined
+ // reference or property types
+ for (Parameter param : parameters) {
+ if (!findReferenceOrProperty(param, props, refs)) {
throw new AmbiguousConstructorException(param.getName());
}
- paramNames.add(name);
}
}
- private void heuristicParamNames(Class[] params,
+ private void heuristicParamNames(Parameter[] parameters,
Map<String, JavaMappedReference> refs,
- Map<String, JavaMappedProperty<?>> props,
- List<String> paramNames)
- throws ProcessingException {
+ Map<String, JavaMappedProperty<?>> props) throws ProcessingException {
// heuristically determine refs and props from the parameter types
- for (Class<?> param : params) {
- String name = getBaseName(param).toLowerCase();
- if (isReferenceType(param)) {
- refs.put(name, createReference(name, null, param));
+ for (Parameter p : parameters) {
+ String name = p.getType().getSimpleName().toLowerCase();
+ if (isReferenceType(p.getType(), p.getGenericType())) {
+ refs.put(name, createReference(name, null, p.getType()));
+ p.setClassifer(Reference.class);
} else {
- props.put(name, createProperty(name, null, param));
+ props.put(name, createProperty(name, null, p.getType()));
+ p.setClassifer(Property.class);
}
- paramNames.add(name);
+ p.setName(name);
}
}
+ private static boolean areUnique(Class[] collection) {
+ Set<Class> set = new HashSet<Class>(Arrays.asList(collection));
+ return set.size() == collection.length;
+ }
+
/**
- * Returns true if the union of the given collections of properties and references have unique Java types
+ * Returns true if the union of the given collections of properties and
+ * references have unique Java types
*/
- private boolean calcPropRefUniqueness(
- Collection<JavaMappedProperty<?>> props,
- Collection<JavaMappedReference> refs) {
+ private boolean calcPropRefUniqueness(Collection<JavaMappedProperty<?>> props, Collection<JavaMappedReference> refs) {
Class[] classes = new Class[props.size() + refs.size()];
int i = 0;
@@ -320,147 +345,157 @@
classes[i] = reference.getServiceContract().getInterfaceClass();
i++;
}
- return implService.areUnique(classes);
+ return areUnique(classes);
}
/**
- * Unambiguously finds the reference or property associated with the given type
- *
+ * Unambiguously finds the reference or property associated with the given
+ * type
+ *
* @return the name of the reference or property if found, null if not
- * @throws AmbiguousConstructorException if the constructor parameter cannot be resolved to a property or reference
+ * @throws AmbiguousConstructorException if the constructor parameter cannot
+ * be resolved to a property or reference
*/
- private String findReferenceOrProperty(
- Class<?> type,
- Map<String, JavaMappedProperty<?>> props,
- Map<String, JavaMappedReference> refs) throws AmbiguousConstructorException {
-
- String name = null;
+ private boolean findReferenceOrProperty(Parameter parameter,
+ Map<String, JavaMappedProperty<?>> props,
+ Map<String, JavaMappedReference> refs) throws AmbiguousConstructorException {
+
+ boolean found = false;
+ if (!"".equals(parameter.getName())) {
+ // Match by name
+ JavaMappedProperty<?> prop = props.get(parameter.getName());
+ if (prop != null && prop.getJavaType() == parameter.getType()) {
+ parameter.setClassifer(Property.class);
+ return true;
+ }
+ JavaMappedReference ref = refs.get(parameter.getName());
+ if (ref != null && ref.getServiceContract().getInterfaceClass() == parameter.getType()) {
+ parameter.setClassifer(Reference.class);
+ return true;
+ }
+ }
for (JavaMappedProperty<?> property : props.values()) {
- if (property.getJavaType().equals(type)) {
- if (name != null) {
+ if (property.getJavaType() == parameter.getType()) {
+ if (found) {
throw new AmbiguousConstructorException("Ambiguous property or reference for constructor type",
- type.getName());
+ parameter.toString());
}
- name = property.getName();
- // do not break since ambiguities must be checked, i.e. more than one prop or ref of the same type
+ parameter.setClassifer(Property.class);
+ parameter.setName(property.getName());
+ found = true;
+ // do not break since ambiguities must be checked, i.e. more
+ // than one prop or ref of the same type
}
}
for (JavaMappedReference reference : refs.values()) {
- if (reference.getServiceContract().getInterfaceClass().equals(type)) {
- if (name != null) {
+ if (reference.getServiceContract().getInterfaceClass().equals(parameter.getType())) {
+ if (found) {
throw new AmbiguousConstructorException("Ambiguous property or reference for constructor type",
- type.getName());
+ parameter.toString());
}
- name = reference.getUri().getFragment();
- // do not break since ambiguities must be checked, i.e. more than one prop or ref of the same type
+ parameter.setClassifer(Reference.class);
+ parameter.setName(reference.getUri().getFragment());
+ found = true;
+ // do not break since ambiguities must be checked, i.e. more
+ // than one prop or ref of the same type
}
}
- return name;
+ return found;
}
/**
- * Returns true if a given type is reference according to the SCA specification rules for determining reference
- * types
+ * Returns true if a given type is reference according to the SCA
+ * specification rules for determining reference types The following rules
+ * are used to determine whether an unannotated field or setter method is a
+ * property or reference:
+ * <ol>
+ * <li>If its type is simple, then it is a property.
+ * <li>If its type is complex, then if the type is an interface marked by
+ *
+ * @Remotable, then it is a reference; otherwise, it is a property.
+ * <li>Otherwise, if the type associated with the member is an
+ * array or a java.util.Collection, the basetype is the element
+ * type of the array or the parameterized type of the
+ * Collection; otherwise the basetype is the member type. If the
+ * basetype is an interface with an
+ * @Remotable or
+ * @Service annotation then the memberis defined as a reference. Otherwise,
+ * it is defined as a property.
+ * </ol>
+ * <p>
+ * The name of the reference or of the property is derived from the
+ * name found on the setter method or on the field.
*/
- private boolean isReferenceType(Type operationType) {
- Class<?> rawType;
- Class<?> referenceType = null;
- if (operationType instanceof ParameterizedType) {
- ParameterizedType parameterizedType = (ParameterizedType) operationType;
- rawType = (Class<?>) parameterizedType.getRawType();
- Type[] typeArgs = parameterizedType.getActualTypeArguments();
- if (typeArgs.length == 1) {
- referenceType = (Class<?>) typeArgs[0];
- }
- } else {
- rawType = (Class<?>) operationType;
- }
- if (rawType.isArray()) {
- referenceType = rawType.getComponentType();
- } else if (Collection.class.isAssignableFrom(rawType) && referenceType == null) {
- return true;
- }
- if (referenceType != null) {
- return referenceType.getAnnotation(Remotable.class) != null
- || referenceType.getAnnotation(Service.class) != null;
- } else {
- return rawType.getAnnotation(Remotable.class) != null || rawType.getAnnotation(Service.class) != null;
- }
+ private boolean isReferenceType(Class<?> cls, Type genericType) {
+ Class<?> baseType = JavaIntrospectionHelper.getBaseType(cls, genericType);
+ return baseType.isInterface() && (baseType.isAnnotationPresent(Remotable.class) || baseType
+ .isAnnotationPresent(Service.class));
}
/**
- * Returns true if the given operation is defined in the collection of service interfaces
+ * Returns true if the given operation is defined in the collection of
+ * service interfaces
*/
private boolean isInServiceInterface(Method operation, Map<String, JavaMappedService> services) {
for (JavaMappedService service : services.values()) {
Class<?> clazz = service.getServiceContract().getInterfaceClass();
- if (operation.getDeclaringClass().equals(clazz)) {
+ if (isMethodMatched(clazz, operation)) {
return true;
}
- Method[] methods = service.getServiceContract().getInterfaceClass().getMethods();
- for (Method method : methods) {
- if (operation.getName().equals(method.getName())
- && operation.getParameterTypes().length == method.getParameterTypes().length) {
- Class<?>[] methodTypes = method.getParameterTypes();
- for (int i = 0; i < operation.getParameterTypes().length; i++) {
- Class<?> paramType = operation.getParameterTypes()[i];
- if (!paramType.equals(methodTypes[i])) {
- break;
- } else if (i == operation.getParameterTypes().length - 1) {
- return true;
- }
- }
- }
- }
}
return false;
}
/**
- * Creates a mapped reference
- *
- * @param name the reference name
- * @param member the injection site the reference maps to
- * @param paramType the service interface of the reference
+ * Test if the class declares a method which matches the signature of the
+ * given method
+ *
+ * @param clazz
+ * @param method
+ * @return
*/
- private JavaMappedReference createReference(String name, Member member, Class<?> paramType)
- throws ProcessingException {
- return implService.createReference(name, member, paramType);
+ private boolean isMethodMatched(Class<?> clazz, Method method) {
+ if (method.getDeclaringClass() == clazz) {
+ return true;
+ }
+ Method[] methods = clazz.getMethods();
+ for (Method m : methods) {
+ if (JavaIntrospectionHelper.exactMethodMatch(method, m)) {
+ return true;
+ }
+ }
+ return false;
}
/**
* Creates a mapped property
- *
- * @param name the property name
- * @param member the injection site the reference maps to
+ *
+ * @param name the property name
+ * @param member the injection site the reference maps to
* @param paramType the property type
*/
private <T> JavaMappedProperty<T> createProperty(String name, Member member, Class<T> paramType) {
- JavaMappedProperty<T> property = new JavaMappedProperty<T>();
- property.setName(name);
- property.setMember(member);
- property.setJavaType(paramType);
- TypeInfo xmlType = typeMapper.getXMLType(paramType);
- if (xmlType != null) {
- property.setXmlType(xmlType.getQName());
+ QName xmlType = null;
+ TypeInfo xmlTypeInfo = typeMapper.getXMLType(paramType);
+ if (xmlTypeInfo != null) {
+ xmlType = xmlTypeInfo.getQName();
}
-
- return property;
+ return new JavaMappedProperty<T>(name, xmlType, paramType, member);
}
/**
- * Populates a component type with a service whose interface type is determined by examining all implemented
- * interfaces of the given class and chosing one whose operations match all of the class's non-property and
+ * Populates a component type with a service whose interface type is
+ * determined by examining all implemented interfaces of the given class and
+ * chosing one whose operations match all of the class's non-property and
* non-reference methods
- *
- * @param clazz the class to examine
- * @param type the component type
+ *
+ * @param clazz the class to examine
+ * @param type the component type
* @param methods all methods in the class to examine
*/
- private void calculateServiceInterface(
- Class<?> clazz,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- Set<Method> methods) throws ProcessingException {
+ private void calculateServiceInterface(Class<?> clazz,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ Set<Method> methods) throws ProcessingException {
List<Method> nonPropRefMethods = new ArrayList<Method>();
// Map<String, JavaMappedService> services = type.getServices();
Map<String, JavaMappedReference> references = type.getReferences();
@@ -472,7 +507,8 @@
nonPropRefMethods.add(method);
}
}
- // determine if an implemented interface matches all of the non-property and non-reference methods
+ // determine if an implemented interface matches all of the non-property
+ // and non-reference methods
Class[] interfaces = clazz.getInterfaces();
if (interfaces.length == 0) {
return;
@@ -481,7 +517,7 @@
if (analyzeInterface(interfaze, nonPropRefMethods)) {
JavaMappedService service;
try {
- service = implService.createService(interfaze);
+ service = createService(interfaze);
} catch (InvalidServiceContractException e) {
throw new ProcessingException(e);
}
@@ -491,9 +527,10 @@
}
/**
- * Determines if the methods of a given interface match the given list of methods
- *
- * @param interfaze the interface to examine
+ * Determines if the methods of a given interface match the given list of
+ * methods
+ *
+ * @param interfaze the interface to examine
* @param nonPropRefMethods the list of methods to match against
* @return true if the interface matches
*/
@@ -503,31 +540,7 @@
return false;
}
for (Method method : nonPropRefMethods) {
- boolean found = false;
- for (Method interfaceMethod : interfaceMethods) {
- if (interfaceMethod.getName().equals(method.getName())) {
- Class<?>[] interfaceParamTypes = interfaceMethod.getParameterTypes();
- Class<?>[] methodParamTypes = method.getParameterTypes();
- if (interfaceParamTypes.length == methodParamTypes.length) {
- if (interfaceParamTypes.length == 0) {
- found = true;
- } else {
- for (int i = 0; i < methodParamTypes.length; i++) {
- Class<?> param = methodParamTypes[i];
- if (!param.equals(interfaceParamTypes[i])) {
- break;
- }
- if (i == methodParamTypes.length - 1) {
- found = true;
- }
- }
- }
- }
- if (found) {
- break;
- }
- }
- }
+ boolean found = isMethodMatched(interfaze, method);
if (!found) {
return false;
}
@@ -535,14 +548,79 @@
return true;
}
-}
+ private boolean isAnnotated(Parameter parameter) {
+ for (Annotation annotation : parameter.getAnnotations()) {
+ Class<? extends Annotation> annotType = annotation.annotationType();
+ if (annotType.equals(Property.class) || annotType.equals(Reference.class)
+ || annotType.equals(Resource.class)) {
+ return true;
+ }
+ }
+ return false;
+ }
-/*
- * 1) public setter methods that are not included in any service interface 2) protected setter methods 3) public or
- * protected fields unless there is a setter method for the same name If the type associated with the member is an array
- * or a java.util.Collection, then the basetype will be the element type of the array or the parameterized type of the
- * Collection, otherwise the basetype will be the member type. If the basetype is an interface with an @Remotable or
- * @Service annotation then the member will be defined as a reference, otherwise it will be defined as a property.
- *
- *
- */
+ public boolean areUnique(Parameter[] parameters) {
+ Set<Class> set = new HashSet<Class>(parameters.length);
+ for (Parameter p : parameters) {
+ if (!set.add(p.getType())) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public JavaMappedReference createReference(String name, Member member, Class<?> paramType)
+ throws ProcessingException {
+ JavaMappedReference reference = new JavaMappedReference();
+ reference.setUri(URI.create("#" + name));
+ reference.setMember(member);
+ reference.setRequired(false);
+ ServiceContract contract;
+ try {
+ contract = interfaceProcessorRegistry.introspect(paramType);
+ } catch (InvalidServiceContractException e1) {
+ throw new ProcessingException(e1);
+ }
+ try {
+ processCallback(paramType, contract);
+ } catch (IllegalCallbackException e) {
+ throw new ProcessingException(e);
+ }
+ reference.setServiceContract(contract);
+ return reference;
+ }
+
+ public JavaMappedService createService(Class<?> interfaze) throws InvalidServiceContractException {
+ JavaMappedService service = new JavaMappedService();
+ // create a relative URI
+ service.setUri(URI.create("#" + interfaze.getSimpleName()));
+ service.setRemotable(interfaze.getAnnotation(Remotable.class) != null);
+ ServiceContract<?> contract = interfaceProcessorRegistry.introspect(interfaze);
+ service.setServiceContract(contract);
+ return service;
+ }
+
+ public void processCallback(Class<?> interfaze, ServiceContract<?> contract) throws IllegalCallbackException {
+ Callback callback = interfaze.getAnnotation(Callback.class);
+ if (callback != null && !Void.class.equals(callback.value())) {
+ Class<?> callbackClass = callback.value();
+ contract.setCallbackClass(callbackClass);
+ contract.setCallbackName(getBaseName(callbackClass));
+ } else if (callback != null && Void.class.equals(callback.value())) {
+ throw new IllegalCallbackException("No callback interface specified on annotation", interfaze.getName());
+ }
+ }
+
+ public boolean injectionAnnotationsPresent(Annotation[][] annots) {
+ for (Annotation[] annotations : annots) {
+ for (Annotation annotation : annotations) {
+ Class<? extends Annotation> annotType = annotation.annotationType();
+ if (annotType.equals(Property.class) || annotType.equals(Reference.class)
+ || annotType.equals(Resource.class) || annotType.equals(Monitor.class)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,16 +18,12 @@
*/
package org.apache.tuscany.core.implementation.processor;
-import org.osoa.sca.annotations.Reference;
-
-import org.apache.tuscany.spi.deployer.DeploymentContext;
-import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor;
-import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
-import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
-
import org.apache.tuscany.api.annotation.Monitor;
import org.apache.tuscany.core.injection.SingletonObjectFactory;
import org.apache.tuscany.host.MonitorFactory;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
+import org.osoa.sca.annotations.Reference;
/**
* Processes an {@link @Monitor} annotation, updating the component type with corresponding {@link
@@ -38,8 +34,8 @@
public class MonitorProcessor extends AbstractPropertyProcessor<Monitor> {
private MonitorFactory monitorFactory;
- public MonitorProcessor(@Reference MonitorFactory factory, @Reference ImplementationProcessorService service) {
- super(Monitor.class, service);
+ public MonitorProcessor(@Reference MonitorFactory factory) {
+ super(Monitor.class);
this.monitorFactory = factory;
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,21 +18,11 @@
*/
package org.apache.tuscany.core.implementation.processor;
-import java.lang.reflect.Constructor;
-
-import org.osoa.sca.annotations.Property;
-import org.osoa.sca.annotations.Reference;
-
import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension;
import org.apache.tuscany.spi.deployer.DeploymentContext;
-import org.apache.tuscany.spi.model.TypeInfo;
-import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor;
-import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
-import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
-import org.apache.tuscany.spi.implementation.java.JavaMappedService;
-import org.apache.tuscany.spi.implementation.java.PojoComponentType;
-import org.apache.tuscany.spi.implementation.java.ProcessingException;
+import org.apache.tuscany.spi.model.TypeInfo;
+import org.osoa.sca.annotations.Property;
/**
* Processes an {@link @Property} annotation, updating the component type with corresponding {@link JavaMappedProperty}
@@ -42,8 +32,8 @@
public class PropertyProcessor extends AbstractPropertyProcessor<Property> {
private SimpleTypeMapperExtension typeMapper = new SimpleTypeMapperExtension();
- public PropertyProcessor(@Reference ImplementationProcessorService service) {
- super(Property.class, service);
+ public PropertyProcessor() {
+ super(Property.class);
}
protected String getName(Property annotation) {
@@ -60,10 +50,4 @@
}
}
- public <T> void visitConstructor(Constructor<T> constructor,
- PojoComponentType<JavaMappedService, JavaMappedReference,
- JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
- // override since heuristic pojo processor evalautes properties
- }
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,18 +18,24 @@
*/
package org.apache.tuscany.core.implementation.processor;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+
+import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
import java.net.URI;
import java.util.Collection;
-import org.osoa.sca.annotations.Reference;
-
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.idl.InvalidServiceContractException;
import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry;
+import org.apache.tuscany.spi.implementation.java.ConstructorDefinition;
import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension;
+import org.apache.tuscany.spi.implementation.java.Parameter;
+import org.apache.tuscany.spi.implementation.java.JavaElement;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
import org.apache.tuscany.spi.implementation.java.JavaMappedService;
@@ -37,27 +43,20 @@
import org.apache.tuscany.spi.implementation.java.ProcessingException;
import org.apache.tuscany.spi.model.Multiplicity;
import org.apache.tuscany.spi.model.ServiceContract;
-
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+import org.osoa.sca.annotations.Reference;
/**
- * Processes an {@link @Reference} annotation, updating the component type with corresponding {@link
+ * Processes an {@link @Reference} annotation, updating the component type with
+ * corresponding {@link
* org.apache.tuscany.spi.implementation.java.JavaMappedReference}
- *
+ *
* @version $Rev$ $Date$
*/
public class ReferenceProcessor extends ImplementationProcessorExtension {
- private JavaInterfaceProcessorRegistry regsitry;
-
- public ReferenceProcessor(@Reference JavaInterfaceProcessorRegistry registry) {
- this.regsitry = registry;
- }
-
- public void visitMethod(
- Method method,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
+ public void visitMethod(Method method,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
Reference annotation = method.getAnnotation(Reference.class);
if (annotation == null) {
return; // Not a reference annotation.
@@ -65,73 +64,83 @@
if (method.getParameterTypes().length != 1) {
throw new IllegalReferenceException("Setter must have one parameter", method.toString());
}
- String name = null;
- if (annotation.name() != null && annotation.name().length() > 0) {
- name = annotation.name();
- }
- boolean required = annotation.required();
- if (name == null) {
+ String name = annotation.name();
+ if ("".equals(name)) {
name = toPropertyName(method.getName());
}
if (type.getReferences().get(name) != null) {
throw new DuplicateReferenceException(name);
}
- JavaMappedReference reference = new JavaMappedReference();
- reference.setMember(method);
- reference.setRequired(required);
- reference.setUri(URI.create("#" + name));
- ServiceContract contract;
- try {
- Class<?> rawType = method.getParameterTypes()[0];
- if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
- if (required) {
- reference.setMultiplicity(Multiplicity.ONE_N);
- } else {
- reference.setMultiplicity(Multiplicity.ZERO_N);
- }
- } else {
- if (required) {
- reference.setMultiplicity(Multiplicity.ONE_ONE);
- } else {
- reference.setMultiplicity(Multiplicity.ZERO_ONE);
- }
- }
- Class<?> baseType = getBaseType(rawType, method.getGenericParameterTypes()[0]);
- contract = regsitry.introspect(baseType);
- } catch (InvalidServiceContractException e) {
- throw new ProcessingException(e);
- }
- reference.setServiceContract(contract);
+ JavaElement element = new JavaElement(method, 0);
+ JavaMappedReference reference = createReference(element, name);
type.getReferences().put(name, reference);
}
- public void visitField(
- Field field,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
+ public void visitField(Field field,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
Reference annotation = field.getAnnotation(Reference.class);
if (annotation == null) {
return;
}
- String name = field.getName();
- if (annotation.name() != null) {
- name = annotation.name();
- }
- boolean required = annotation.required();
- if (name.length() == 0) {
+ String name = annotation.name();
+ if ("".equals(name)) {
name = field.getName();
}
if (type.getReferences().get(name) != null) {
throw new DuplicateReferenceException(name);
}
+ JavaElement element = new JavaElement(field);
+ JavaMappedReference reference = createReference(element, name);
+ type.getReferences().put(name, reference);
+ }
+
+ public void visitConstructorParameter(Parameter parameter,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
+ Reference refAnnotation = parameter.getAnnotation(Reference.class);
+ if (refAnnotation == null) {
+ return;
+ }
+ String paramName = parameter.getName();
+ String name = getReferenceName(paramName, parameter.getIndex(), refAnnotation.name());
+ if (type.getReferences().get(name) != null) {
+ throw new DuplicateReferenceException(name);
+ }
+ JavaMappedReference reference = createReference(parameter, name);
+ type.getReferences().put(name, reference);
+ parameter.setClassifer(Reference.class);
+ parameter.setName(name);
+ }
+
+ private String getReferenceName(String paramName, int pos, String name) throws InvalidConstructorException {
+ if ("".equals(name)) {
+ name = paramName;
+ }
+ if ("".equals(name)) {
+ return "_ref" + pos;
+ }
+ if (!"".equals(paramName) && !name.equals(paramName)) {
+ throw new InvalidConstructorException("Mismatching names specified for reference parameter " + pos);
+ } else {
+ return name;
+ }
+ }
+
+ private JavaMappedReference createReference(JavaElement element, String name) throws ProcessingException {
JavaMappedReference reference = new JavaMappedReference();
- reference.setMember(field);
+ reference.setMember((Member)element.getAnchor());
+ boolean required = false;
+ Reference ref = element.getAnnotation(Reference.class);
+ if (ref != null) {
+ required = ref.required();
+ }
reference.setRequired(required);
reference.setUri(URI.create("#" + name));
ServiceContract contract;
try {
- Class<?> rawType = field.getType();
+ Class<?> rawType = element.getType();
if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) {
if (required) {
reference.setMultiplicity(Multiplicity.ONE_N);
@@ -145,20 +154,13 @@
reference.setMultiplicity(Multiplicity.ZERO_ONE);
}
}
- Class<?> baseType = getBaseType(rawType, field.getGenericType());
- contract = regsitry.introspect(baseType);
+ Type genericType = element.getGenericType();
+ Class<?> baseType = getBaseType(rawType, genericType);
+ contract = interfaceProcessorRegistry.introspect(baseType);
} catch (InvalidServiceContractException e) {
throw new ProcessingException(e);
}
reference.setServiceContract(contract);
- type.getReferences().put(name, reference);
- }
-
- public <T> void visitConstructor(
- Constructor<T> constructor,
- PojoComponentType<JavaMappedService,
- JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
-
+ return reference;
}
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ResourceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ResourceProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ResourceProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ResourceProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,26 +18,27 @@
*/
package org.apache.tuscany.core.implementation.processor;
-import java.lang.reflect.Constructor;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension;
+import org.apache.tuscany.spi.implementation.java.JavaElement;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
import org.apache.tuscany.spi.implementation.java.JavaMappedService;
+import org.apache.tuscany.spi.implementation.java.Parameter;
import org.apache.tuscany.spi.implementation.java.PojoComponentType;
import org.apache.tuscany.spi.implementation.java.ProcessingException;
import org.apache.tuscany.spi.implementation.java.Resource;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
-
/**
- * Processes an {@link @Resource} annotation, updating the component type with corresponding {@link
- * org.apache.tuscany.spi.implementation.java.Resource}
- *
+ * Processes an {@link @Resource} annotation, updating the component type with
+ * corresponding {@link org.apache.tuscany.spi.implementation.java.Resource}
+ *
* @version $Rev$ $Date$
*/
public class ResourceProcessor extends ImplementationProcessorExtension {
@@ -45,21 +46,17 @@
public ResourceProcessor() {
}
- public void visitMethod(
- Method method,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context)
- throws ProcessingException {
- org.apache.tuscany.api.annotation.Resource annotation =
- method.getAnnotation(org.apache.tuscany.api.annotation.Resource.class);
+ public void visitMethod(Method method,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
+ org.apache.tuscany.api.annotation.Resource annotation = method
+ .getAnnotation(org.apache.tuscany.api.annotation.Resource.class);
if (annotation == null) {
return;
}
if (method.getParameterTypes().length != 1) {
throw new IllegalResourceException("Resource setter must have one parameter", method.toString());
}
- Class<?> resourceType = method.getParameterTypes()[0];
-
String name = annotation.name();
if (name.length() < 1) {
name = toPropertyName(method.getName());
@@ -69,7 +66,7 @@
}
String mappedName = annotation.mappedName();
- Resource<?> resource = createResource(name, resourceType, method);
+ Resource<?> resource = createResource(name, new JavaElement(method, 0));
resource.setOptional(annotation.optional());
if (mappedName.length() > 0) {
resource.setMappedName(mappedName);
@@ -77,13 +74,12 @@
type.add(resource);
}
- public void visitField(
- Field field,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
+ public void visitField(Field field,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
- org.apache.tuscany.api.annotation.Resource annotation =
- field.getAnnotation(org.apache.tuscany.api.annotation.Resource.class);
+ org.apache.tuscany.api.annotation.Resource annotation = field
+ .getAnnotation(org.apache.tuscany.api.annotation.Resource.class);
if (annotation == null) {
return;
}
@@ -98,7 +94,7 @@
Class<?> fieldType = field.getType();
String mappedName = annotation.mappedName();
- Resource<?> resource = createResource(name, fieldType, field);
+ Resource<?> resource = createResource(name, new JavaElement(field));
resource.setOptional(annotation.optional());
if (mappedName.length() > 0) {
resource.setMappedName(mappedName);
@@ -106,17 +102,43 @@
type.add(resource);
}
- public <T> Resource<T> createResource(String name, Class<T> type, Member member) {
- return new Resource<T>(name, type, member);
+ public <T> Resource<T> createResource(String name, JavaElement element) {
+ return new Resource<T>(name, (Class<T>)element.getType(), (Member)element.getAnchor());
}
- public <T> void visitConstructor(
- Constructor<T> constructor,
- PojoComponentType<JavaMappedService, JavaMappedReference,
- JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
-
+ public void visitConstructorParameter(Parameter parameter,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
+ org.apache.tuscany.api.annotation.Resource resourceAnnotation = parameter
+ .getAnnotation(org.apache.tuscany.api.annotation.Resource.class);
+ if (resourceAnnotation != null) {
+ String name = resourceAnnotation.name();
+ if ("".equals(name)) {
+ name = parameter.getName();
+ }
+ if ("".equals(name)) {
+ throw new InvalidResourceException("Missing resource name", parameter.toString());
+ }
+
+ if (!"".equals(parameter.getName()) && !name.equals(parameter.getName())) {
+ throw new InvalidConstructorException("Mismatched resource name: " + parameter);
+ }
+
+ if (type.getResources().get(name) != null) {
+ throw new DuplicateResourceException(name);
+ }
+
+ String mappedName = resourceAnnotation.mappedName();
+
+ Resource<?> resource = createResource(name, parameter);
+ resource.setOptional(resourceAnnotation.optional());
+ if (mappedName.length() > 0) {
+ resource.setMappedName(mappedName);
+ }
+ type.add(resource);
+ parameter.setClassifer(org.apache.tuscany.api.annotation.Resource.class);
+ parameter.setName(name);
+ }
}
-
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java Wed Mar 28 09:03:01 2007
@@ -18,42 +18,35 @@
*/
package org.apache.tuscany.core.implementation.processor;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces;
+import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.net.URI;
import java.util.Set;
-import org.osoa.sca.annotations.Callback;
-import org.osoa.sca.annotations.Reference;
-import org.osoa.sca.annotations.Remotable;
-
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.idl.InvalidServiceContractException;
import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension;
-import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
import org.apache.tuscany.spi.implementation.java.JavaMappedService;
import org.apache.tuscany.spi.implementation.java.PojoComponentType;
import org.apache.tuscany.spi.implementation.java.ProcessingException;
import org.apache.tuscany.spi.model.ServiceContract;
-
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces;
-import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+import org.osoa.sca.annotations.Callback;
+import org.osoa.sca.annotations.Remotable;
/**
- * Processes an {@link org.osoa.sca.annotations.Service} annotation and updates the component type with corresponding
- * {@link JavaMappedService}s. Also processes related {@link org.osoa.sca.annotations.Callback} annotations.
- *
+ * Processes an {@link org.osoa.sca.annotations.Service} annotation and updates
+ * the component type with corresponding {@link JavaMappedService}s. Also
+ * processes related {@link org.osoa.sca.annotations.Callback} annotations.
+ *
* @version $Rev$ $Date$
*/
public class ServiceProcessor extends ImplementationProcessorExtension {
- private ImplementationProcessorService implService;
-
- public ServiceProcessor(@Reference ImplementationProcessorService implService) {
- this.implService = implService;
- }
-
public <T> void visitClass(Class<T> clazz,
PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
DeploymentContext context) throws ProcessingException {
@@ -65,7 +58,7 @@
if (interfaze.isAnnotationPresent(Remotable.class) || interfaze.isAnnotationPresent(Callback.class)) {
JavaMappedService service;
try {
- service = implService.createService(interfaze);
+ service = createService(interfaze);
} catch (InvalidServiceContractException e) {
throw new ProcessingException(e);
}
@@ -90,7 +83,7 @@
}
JavaMappedService service;
try {
- service = implService.createService(interfaze);
+ service = createService(interfaze);
} catch (InvalidServiceContractException e) {
throw new ProcessingException(e);
}
@@ -98,11 +91,9 @@
}
}
-
- public void visitMethod(
- Method method,
- PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
- DeploymentContext context) throws ProcessingException {
+ public void visitMethod(Method method,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
Callback annotation = method.getAnnotation(Callback.class);
if (annotation == null) {
@@ -151,5 +142,14 @@
callbacksService.setCallbackMember(field);
}
+ public JavaMappedService createService(Class<?> interfaze) throws InvalidServiceContractException {
+ JavaMappedService service = new JavaMappedService();
+ // create a relative URI
+ service.setUri(URI.create("#" + interfaze.getSimpleName()));
+ service.setRemotable(interfaze.getAnnotation(Remotable.class) != null);
+ ServiceContract<?> contract = interfaceProcessorRegistry.introspect(interfaze);
+ service.setServiceContract(contract);
+ return service;
+ }
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java Wed Mar 28 09:03:01 2007
@@ -24,8 +24,12 @@
import java.util.Collection;
import java.util.Map;
-import org.osoa.sca.annotations.Reference;
-
+import org.apache.tuscany.core.implementation.PojoConfiguration;
+import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl;
+import org.apache.tuscany.core.implementation.system.model.SystemImplementation;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.injection.ResourceObjectFactory;
import org.apache.tuscany.spi.ObjectFactory;
import org.apache.tuscany.spi.builder.BuilderConfigException;
import org.apache.tuscany.spi.component.AtomicComponent;
@@ -35,18 +39,13 @@
import org.apache.tuscany.spi.implementation.java.ConstructorDefinition;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
+import org.apache.tuscany.spi.implementation.java.Parameter;
import org.apache.tuscany.spi.implementation.java.PojoComponentType;
import org.apache.tuscany.spi.implementation.java.Resource;
import org.apache.tuscany.spi.model.ComponentDefinition;
import org.apache.tuscany.spi.model.PropertyValue;
import org.apache.tuscany.spi.model.ServiceDefinition;
-
-import org.apache.tuscany.core.implementation.PojoConfiguration;
-import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl;
-import org.apache.tuscany.core.implementation.system.model.SystemImplementation;
-import org.apache.tuscany.core.injection.MethodEventInvoker;
-import org.apache.tuscany.core.injection.PojoObjectFactory;
-import org.apache.tuscany.core.injection.ResourceObjectFactory;
+import org.osoa.sca.annotations.Reference;
/**
* Produces system atomic components from a component definition
@@ -113,9 +112,9 @@
Constructor<?> constr = ctorDef.getConstructor();
PojoObjectFactory<?> instanceFactory = new PojoObjectFactory(constr);
configuration.setInstanceFactory(instanceFactory);
- configuration.getConstructorParamNames().addAll(ctorDef.getInjectionNames());
- for (Class<?> clazz : constr.getParameterTypes()) {
- configuration.addConstructorParamType(clazz);
+ for (Parameter param : ctorDef.getParameters()) {
+ configuration.getConstructorParamNames().add(param.getName());
+ configuration.addConstructorParamType(param.getType());
}
configuration.setName(definition.getUri());
SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration);
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/AssemblyServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/AssemblyServiceImpl.java?view=diff&rev=523381&r1=523380&r2=523381
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/AssemblyServiceImpl.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/AssemblyServiceImpl.java Wed Mar 28 09:03:01 2007
@@ -37,6 +37,7 @@
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Reference;
+import org.osoa.sca.annotations.Service;
import org.apache.tuscany.spi.component.ScopeContainer;
import org.apache.tuscany.spi.component.ScopeRegistry;
@@ -78,6 +79,7 @@
* @version $Rev$ $Date$
*/
@EagerInit
+@Service(AssemblyService.class)
public class AssemblyServiceImpl implements AssemblyService, ChangeSetHandlerRegistry {
private static final URI DOMAIN_URI = URI.create("tuscany://./domain");
private final GeneratorRegistry generatorRegistry;
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org