diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-12-04 23:08:49 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-12-04 23:08:49 +0000 |
commit | d15ee677eeba5b76444202ed18862faa19296d63 (patch) | |
tree | 0737ece92d7d6066869966aab9a981ffdf2a6d7f /java/sca/modules/implementation-java/src | |
parent | bc1c8ab4c48c3a81824dac27ba71a8d1277797b9 (diff) |
Java implementation clean up in preparation for some refactoring
- removal of unused code
- review of code visibility
- renaming of exception to follow best practices naming convention
- etc
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@723484 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/implementation-java/src')
10 files changed, 427 insertions, 320 deletions
diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java index 6d27dff9c9..e7f2638255 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java @@ -50,7 +50,7 @@ public abstract class AbstractPropertyProcessor<A extends Annotation> extends Ba this.annotationClass = annotationClass; } - private boolean removeProperty(JavaElementImpl prop, JavaImplementation type) { + private static boolean removeProperty(JavaElementImpl prop, JavaImplementation type) { if(prop==null) { return false; } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConversationProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConversationProcessor.java index 327bd97564..814ca8ca6c 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConversationProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConversationProcessor.java @@ -65,7 +65,7 @@ public class ConversationProcessor extends BaseJavaClassVisitor { type.setMaxAge(maxAge); } } catch (NumberFormatException e) { - throw new InvalidConversationalImplementation("Invalid maximum age", e); + throw new InvalidConversationalImplementationException("Invalid maximum age", e); } try { if (maxIdleTimeVal.length() > 0) { @@ -73,7 +73,7 @@ public class ConversationProcessor extends BaseJavaClassVisitor { type.setMaxIdleTime(maxIdleTime); } } catch (NumberFormatException e) { - throw new InvalidConversationalImplementation("Invalid maximum idle time", e); + throw new InvalidConversationalImplementationException("Invalid maximum idle time", e); } } @@ -99,7 +99,7 @@ public class ConversationProcessor extends BaseJavaClassVisitor { type.addConversationIDMember(field); } - protected long convertTimeMillis(String expr) throws NumberFormatException { + static long convertTimeMillis(String expr) throws NumberFormatException { expr = expr.trim().toUpperCase(); int i = expr.lastIndexOf(SECONDS); if (i >= 0) { diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java index f4c88e61ad..ab07870086 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java @@ -134,17 +134,6 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { } } - 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, List<org.apache.tuscany.sca.assembly.Service> services, @@ -360,10 +349,7 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { } } - 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 @@ -440,6 +426,119 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { } /** + * Creates a mapped property. + * + * @param name the property name + * @param paramType the property type + */ + private org.apache.tuscany.sca.assembly.Property createProperty(String name, Class<?> paramType) { + org.apache.tuscany.sca.assembly.Property property = assemblyFactory.createProperty(); + property.setName(name); + property.setXSDType(JavaXMLMapper.getXMLType(paramType)); + return property; + } + + private org.apache.tuscany.sca.assembly.Reference createReference(String name, Class<?> paramType) + throws IntrospectionException { + org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory.createReference(); + reference.setName(name); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + try { + JavaInterface callInterface = javaFactory.createJavaInterface(paramType); + reference.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); + reference.getInterfaceContract().setCallbackInterface(callbackInterface); + } + reference.setMultiplicity(Multiplicity.ZERO_ONE); + } catch (InvalidInterfaceException e1) { + throw new IntrospectionException(e1); + } + + // FIXME: This part seems to have already been taken care above!! + try { + processCallback(paramType, reference); + } catch (InvalidServiceTypeException e) { + throw new IntrospectionException(e); + } + return reference; + } + + private org.apache.tuscany.sca.assembly.Service createService(Class<?> interfaze) throws InvalidInterfaceException { + org.apache.tuscany.sca.assembly.Service service = assemblyFactory.createService(); + service.setName(interfaze.getSimpleName()); + + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + JavaInterface callInterface = javaFactory.createJavaInterface(interfaze); + service.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } + + Interface javaInterface = service.getInterfaceContract().getInterface(); + javaInterface.setRemotable(interfaze.getAnnotation(Remotable.class) != null); + service.getInterfaceContract().setInterface(javaInterface); + return service; + } + + private void processCallback(Class<?> interfaze, Contract contract) throws InvalidServiceTypeException { + Callback callback = interfaze.getAnnotation(Callback.class); + if (callback != null && !Void.class.equals(callback.value())) { + Class<?> callbackClass = callback.value(); + JavaInterface javaInterface; + try { + javaInterface = javaFactory.createJavaInterface(callbackClass); + contract.getInterfaceContract().setCallbackInterface(javaInterface); + } catch (InvalidInterfaceException e) { + throw new InvalidServiceTypeException("Invalid callback interface "+callbackClass, interfaze); + } + } else if (callback != null && Void.class.equals(callback.value())) { + throw new InvalidServiceTypeException("No callback interface specified on annotation", interfaze); + } + } + + + /** + * Utility methods + */ + + + /** + * Verify if the method is a public setter + * @param method + * @return + */ + private static boolean isPublicSetter(Method method) { + return method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) + && method.getName().startsWith("set") + && method.getReturnType() == void.class; + } + + /** + * Verify if the method is a protected setter + * @param method + * @return + */ + private static boolean isProtectedSetter(Method method) { + return method.getParameterTypes().length == 1 && Modifier.isProtected(method.getModifiers()) + && method.getName().startsWith("set") + && method.getReturnType() == void.class; + } + + /** + * @param collection + * @return + */ + private static boolean areUnique(Class<?>[] collection) { + Set<Class<?>> set = new HashSet<Class<?>>(Arrays.asList(collection)); + return set.size() == collection.length; + } + + /** * 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 @@ -462,7 +561,7 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { * 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(Class<?> cls, Type genericType) { + private static boolean isReferenceType(Class<?> cls, Type genericType) { Class<?> baseType = JavaIntrospectionHelper.getBaseType(cls, genericType); return baseType.isInterface() && baseType.isAnnotationPresent(Remotable.class); } @@ -470,8 +569,11 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { /** * Returns true if the given operation is defined in the collection of * service interfaces + * @param operation + * @param services + * @return */ - private boolean isInServiceInterface(Method operation, List<org.apache.tuscany.sca.assembly.Service> services) { + private static boolean isInServiceInterface(Method operation, List<org.apache.tuscany.sca.assembly.Service> services) { for (org.apache.tuscany.sca.assembly.Service service : services) { Interface interface1 = service.getInterfaceContract().getInterface(); if (interface1 instanceof JavaInterface) { @@ -492,7 +594,7 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { * @param method * @return */ - private boolean isMethodMatched(Class<?> clazz, Method method) { + private static boolean isMethodMatched(Class<?> clazz, Method method) { if (method.getDeclaringClass() == clazz) { return true; } @@ -506,19 +608,11 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { } /** - * Creates a mapped property. - * - * @param name the property name - * @param paramType the property type + * Verify if there is any SCA annotation on the parameter + * @param parameter + * @return */ - private org.apache.tuscany.sca.assembly.Property createProperty(String name, Class<?> paramType) { - org.apache.tuscany.sca.assembly.Property property = assemblyFactory.createProperty(); - property.setName(name); - property.setXSDType(JavaXMLMapper.getXMLType(paramType)); - return property; - } - - private boolean isAnnotated(JavaParameterImpl parameter) { + private static boolean isAnnotated(JavaParameterImpl parameter) { for (Annotation annotation : parameter.getAnnotations()) { Class<? extends Annotation> annotType = annotation.annotationType(); if (annotType.equals(Property.class) || annotType.equals(Reference.class) @@ -529,7 +623,12 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { return false; } - public boolean areUnique(JavaParameterImpl[] parameters) { + /** + * Verify if the parameters are unique + * @param parameters + * @return + */ + private static boolean areUnique(JavaParameterImpl[] parameters) { Set<Class<?>> set = new HashSet<Class<?>>(parameters.length); for (JavaParameterImpl p : parameters) { if (!set.add(p.getType())) { @@ -538,71 +637,13 @@ public class HeuristicPojoProcessor extends BaseJavaClassVisitor { } return true; } - - public org.apache.tuscany.sca.assembly.Reference createReference(String name, Class<?> paramType) - throws IntrospectionException { - org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory.createReference(); - reference.setName(name); - JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); - reference.setInterfaceContract(interfaceContract); - try { - JavaInterface callInterface = javaFactory.createJavaInterface(paramType); - reference.getInterfaceContract().setInterface(callInterface); - if (callInterface.getCallbackClass() != null) { - JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); - reference.getInterfaceContract().setCallbackInterface(callbackInterface); - } - reference.setMultiplicity(Multiplicity.ZERO_ONE); - } catch (InvalidInterfaceException e1) { - throw new IntrospectionException(e1); - } - - // FIXME: This part seems to have already been taken care above!! - try { - processCallback(paramType, reference); - } catch (InvalidServiceType e) { - throw new IntrospectionException(e); - } - return reference; - } - - public org.apache.tuscany.sca.assembly.Service createService(Class<?> interfaze) throws InvalidInterfaceException { - org.apache.tuscany.sca.assembly.Service service = assemblyFactory.createService(); - service.setName(interfaze.getSimpleName()); - - JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); - service.setInterfaceContract(interfaceContract); - - JavaInterface callInterface = javaFactory.createJavaInterface(interfaze); - service.getInterfaceContract().setInterface(callInterface); - if (callInterface.getCallbackClass() != null) { - JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); - service.getInterfaceContract().setCallbackInterface(callbackInterface); - } - - Interface javaInterface = service.getInterfaceContract().getInterface(); - javaInterface.setRemotable(interfaze.getAnnotation(Remotable.class) != null); - service.getInterfaceContract().setInterface(javaInterface); - return service; - } - - public void processCallback(Class<?> interfaze, Contract contract) throws InvalidServiceType { - Callback callback = interfaze.getAnnotation(Callback.class); - if (callback != null && !Void.class.equals(callback.value())) { - Class<?> callbackClass = callback.value(); - JavaInterface javaInterface; - try { - javaInterface = javaFactory.createJavaInterface(callbackClass); - contract.getInterfaceContract().setCallbackInterface(javaInterface); - } catch (InvalidInterfaceException e) { - throw new InvalidServiceType("Invalid callback interface "+callbackClass, interfaze); - } - } else if (callback != null && Void.class.equals(callback.value())) { - throw new InvalidServiceType("No callback interface specified on annotation", interfaze); - } - } - - public boolean injectionAnnotationsPresent(Annotation[][] annots) { + + /** + * Verify if the annotations are SCA annotation + * @param annots + * @return + */ + private static boolean injectionAnnotationsPresent(Annotation[][] annots) { for (Annotation[] annotations : annots) { for (Annotation annotation : annotations) { Class<? extends Annotation> annotType = annotation.annotationType(); diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementation.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementationException.java index a1e1d01ab3..079495e4db 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementation.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementationException.java @@ -25,14 +25,14 @@ import org.apache.tuscany.sca.implementation.java.IntrospectionException; * * @version $Rev$ $Date$ */ -public class InvalidConversationalImplementation extends IntrospectionException { +public class InvalidConversationalImplementationException extends IntrospectionException { private static final long serialVersionUID = -5487291552769408149L; - public InvalidConversationalImplementation(String message) { + public InvalidConversationalImplementationException(String message) { super(message); } - public InvalidConversationalImplementation(String message, Throwable cause) { + public InvalidConversationalImplementationException(String message, Throwable cause) { super(message, cause); } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceType.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceTypeException.java index 7147da55b4..c65e7179f5 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceType.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceTypeException.java @@ -26,15 +26,15 @@ import org.apache.tuscany.sca.implementation.java.IntrospectionException; * * @version $Rev$ $Date$ */ -public class InvalidServiceType extends IntrospectionException { +public class InvalidServiceTypeException extends IntrospectionException { private static final long serialVersionUID = -1076466639416644386L; private Class<?> serviceType; - public InvalidServiceType(String message) { + public InvalidServiceTypeException(String message) { super(message); } - public InvalidServiceType(String message, Class<?> clazz) { + public InvalidServiceTypeException(String message, Class<?> clazz) { super(message); this.serviceType = clazz; } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java index c2e1e33432..2344ce49fc 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java @@ -58,93 +58,45 @@ public class PolicyProcessor extends BaseJavaClassVisitor { this.policyFactory = policyFactory; } - private QName getQName(String intentName) { - QName qname; - if (intentName.startsWith("{")) { - int i = intentName.indexOf('}'); - if (i != -1) { - qname = new QName(intentName.substring(1, i), intentName.substring(i + 1)); - } else { - qname = new QName("", intentName); - } - } else { - qname = new QName("", intentName); - } - return qname; - } - /** - * Read policy intents on the given interface or class - * @param clazz - * @param requiredIntents - */ - private void readIntentsAndPolicySets(Class<?> clazz, - List<Intent> requiredIntents, - List<PolicySet> policySets) { - Requires intentAnnotation = clazz.getAnnotation(Requires.class); - if (intentAnnotation != null) { - String[] intentNames = intentAnnotation.value(); - if (intentNames.length != 0) { - for (String intentName : intentNames) { - - // Add each intent to the list - Intent intent = policyFactory.createIntent(); - intent.setName(getQName(intentName)); - requiredIntents.add(intent); - } - } + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + org.osoa.sca.annotations.Reference annotation = + field.getAnnotation( org.osoa.sca.annotations.Reference.class); + if (annotation == null) { + return; + } + String name = annotation.name(); + if ("".equals(name)) { + name = field.getName(); } - PolicySets policySetAnnotation = clazz.getAnnotation(PolicySets.class); - if (policySetAnnotation != null) { - String[] policySetNames = policySetAnnotation.value(); - if (policySetNames.length != 0) { - for (String policySetName : policySetNames) { - - // Add each intent to the list - PolicySet policySet = policyFactory.createPolicySet(); - policySet.setName(getQName(policySetName)); - policySets.add(policySet); - } - } + Reference reference = null; + if ( (reference = getReferenceByName(name, type)) != null ) { + readIntents(field.getAnnotation(Requires.class), reference.getRequiredIntents()); + readPolicySets(field.getAnnotation(PolicySets.class), reference.getPolicySets()); } } - - private void readIntents(Requires intentAnnotation, List<Intent> requiredIntents) { - //Requires intentAnnotation = method.getAnnotation(Requires.class); - if (intentAnnotation != null) { - String[] intentNames = intentAnnotation.value(); - if (intentNames.length != 0) { - //Operation operation = assemblyFactory.createOperation(); - //operation.setName(method.getName()); - //operation.setUnresolved(true); - for (String intentName : intentNames) { - // Add each intent to the list, associated with the - // operation corresponding to the annotated method - Intent intent = policyFactory.createIntent(); - intent.setName(getQName(intentName)); - //intent.getOperations().add(operation); - requiredIntents.add(intent); - } - } - } - } - - private void readPolicySets(PolicySets policySetAnnotation, List<PolicySet> policySets) { - if (policySetAnnotation != null) { - String[] policySetNames = policySetAnnotation.value(); - if (policySetNames.length != 0) { - //Operation operation = assemblyFactory.createOperation(); - //operation.setName(method.getName()); - //operation.setUnresolved(true); - for (String policySetName : policySetNames) { - // Add each intent to the list, associated with the - // operation corresponding to the annotated method - PolicySet policySet = policyFactory.createPolicySet(); - policySet.setName(getQName(policySetName)); - //intent.getOperations().add(operation); - policySets.add(policySet); + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Reference reference = null; + if ( (reference = getReference(method, type)) != null ) { + readIntents(method.getAnnotation(Requires.class), reference.getRequiredIntents()); + readPolicySets(method.getAnnotation(PolicySets.class), reference.getPolicySets()); + } else { + if ( type instanceof OperationsConfigurator ) { + //Read the intents specified on the given implementation method + if ( (method.getAnnotation(Requires.class) != null || + method.getAnnotation(PolicySets.class) != null ) && + (type instanceof PolicySetAttachPoint )) { + ConfiguredOperation confOp = assemblyFactory.createConfiguredOperation(); + confOp.setName(method.getName()); + ((OperationsConfigurator)type).getConfiguredOperations().add(confOp); + + + readIntents(method.getAnnotation(Requires.class), confOp.getRequiredIntents()); + readPolicySets(method.getAnnotation(PolicySets.class), confOp.getPolicySets()); } } } @@ -223,24 +175,126 @@ public class PolicyProcessor extends BaseJavaClassVisitor { } } - private Reference getReference(Method method, JavaImplementation type) { - //since the ReferenceProcessor is called ahead of the PolicyProcessor the type should have - //picked up the reference setter method - org.osoa.sca.annotations.Reference annotation = - method.getAnnotation(org.osoa.sca.annotations.Reference.class); - if (annotation != null) { - if (JavaIntrospectionHelper.isSetter(method)) { - String name = annotation.name(); - if ("".equals(name)) { - name = JavaIntrospectionHelper.toPropertyName(method.getName()); + /** + * Read policy intents on the given interface or class + * @param clazz + * @param requiredIntents + */ + private void readIntentsAndPolicySets(Class<?> clazz, + List<Intent> requiredIntents, + List<PolicySet> policySets) { + Requires intentAnnotation = clazz.getAnnotation(Requires.class); + if (intentAnnotation != null) { + String[] intentNames = intentAnnotation.value(); + if (intentNames.length != 0) { + for (String intentName : intentNames) { + + // Add each intent to the list + Intent intent = policyFactory.createIntent(); + intent.setName(getQName(intentName)); + requiredIntents.add(intent); + } + } + } + + PolicySets policySetAnnotation = clazz.getAnnotation(PolicySets.class); + if (policySetAnnotation != null) { + String[] policySetNames = policySetAnnotation.value(); + if (policySetNames.length != 0) { + for (String policySetName : policySetNames) { + + // Add each intent to the list + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(getQName(policySetName)); + policySets.add(policySet); } - return getReferenceByName(name, type); } } - return null; } - private Reference getReferenceByName(String name, JavaImplementation type) { + /** + * Read intent annotations on the given interface or class + * @param intentAnnotation + * @param requiredIntents + */ + private void readIntents(Requires intentAnnotation, List<Intent> requiredIntents) { + //Requires intentAnnotation = method.getAnnotation(Requires.class); + if (intentAnnotation != null) { + String[] intentNames = intentAnnotation.value(); + if (intentNames.length != 0) { + //Operation operation = assemblyFactory.createOperation(); + //operation.setName(method.getName()); + //operation.setUnresolved(true); + for (String intentName : intentNames) { + + // Add each intent to the list, associated with the + // operation corresponding to the annotated method + Intent intent = policyFactory.createIntent(); + intent.setName(getQName(intentName)); + //intent.getOperations().add(operation); + requiredIntents.add(intent); + } + } + } + } + + + /** + * Read policy set annotations on a given interface or class + * @param policySetAnnotation + * @param policySets + */ + private void readPolicySets(PolicySets policySetAnnotation, List<PolicySet> policySets) { + if (policySetAnnotation != null) { + String[] policySetNames = policySetAnnotation.value(); + if (policySetNames.length != 0) { + //Operation operation = assemblyFactory.createOperation(); + //operation.setName(method.getName()); + //operation.setUnresolved(true); + for (String policySetName : policySetNames) { + // Add each intent to the list, associated with the + // operation corresponding to the annotated method + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(getQName(policySetName)); + //intent.getOperations().add(operation); + policySets.add(policySet); + } + } + } + } + + /** + * Utility methods + */ + + /** + * + * @param intentName + * @return + */ + private static QName getQName(String intentName) { + QName qname; + if (intentName.startsWith("{")) { + int i = intentName.indexOf('}'); + if (i != -1) { + qname = new QName(intentName.substring(1, i), intentName.substring(i + 1)); + } else { + qname = new QName("", intentName); + } + } else { + qname = new QName("", intentName); + } + return qname; + } + + + /** + * + * @param name + * @param type + * @return + */ + private static Reference getReferenceByName(String name, JavaImplementation type) { for ( Reference reference : type.getReferences() ) { if ( reference.getName().equals(name) ) { return reference; @@ -249,46 +303,27 @@ public class PolicyProcessor extends BaseJavaClassVisitor { return null; } - @Override - public void visitField(Field field, JavaImplementation type) throws IntrospectionException { - org.osoa.sca.annotations.Reference annotation = - field.getAnnotation( org.osoa.sca.annotations.Reference.class); - if (annotation == null) { - return; - } - String name = annotation.name(); - if ("".equals(name)) { - name = field.getName(); - } - - Reference reference = null; - if ( (reference = getReferenceByName(name, type)) != null ) { - readIntents(field.getAnnotation(Requires.class), reference.getRequiredIntents()); - readPolicySets(field.getAnnotation(PolicySets.class), reference.getPolicySets()); - } - } - @Override - public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { - Reference reference = null; - if ( (reference = getReference(method, type)) != null ) { - readIntents(method.getAnnotation(Requires.class), reference.getRequiredIntents()); - readPolicySets(method.getAnnotation(PolicySets.class), reference.getPolicySets()); - } else { - if ( type instanceof OperationsConfigurator ) { - //Read the intents specified on the given implementation method - if ( (method.getAnnotation(Requires.class) != null || - method.getAnnotation(PolicySets.class) != null ) && - (type instanceof PolicySetAttachPoint )) { - ConfiguredOperation confOp = assemblyFactory.createConfiguredOperation(); - confOp.setName(method.getName()); - ((OperationsConfigurator)type).getConfiguredOperations().add(confOp); - - - readIntents(method.getAnnotation(Requires.class), confOp.getRequiredIntents()); - readPolicySets(method.getAnnotation(PolicySets.class), confOp.getPolicySets()); + /** + * + * @param method + * @param type + * @return + */ + private static Reference getReference(Method method, JavaImplementation type) { + //since the ReferenceProcessor is called ahead of the PolicyProcessor the type should have + //picked up the reference setter method + org.osoa.sca.annotations.Reference annotation = + method.getAnnotation(org.osoa.sca.annotations.Reference.class); + if (annotation != null) { + if (JavaIntrospectionHelper.isSetter(method)) { + String name = annotation.name(); + if ("".equals(name)) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); } + return getReferenceByName(name, type); } } + return null; } } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java index 911f1f1bb9..29a872e27a 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java @@ -83,19 +83,6 @@ public class ReferenceProcessor extends BaseJavaClassVisitor { type.getReferenceMembers().put(name, element); } - private boolean removeReference(JavaElementImpl ref, JavaImplementation type) { - if (ref == null) { - return false; - } - List<org.apache.tuscany.sca.assembly.Reference> refs = type.getReferences(); - for (int i = 0; i < refs.size(); i++) { - if (refs.get(i).getName().equals(ref.getName())) { - refs.remove(i); - return true; - } - } - return false; - } @Override public void visitField(Field field, JavaImplementation type) throws IntrospectionException { @@ -145,20 +132,13 @@ public class ReferenceProcessor extends BaseJavaClassVisitor { 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; - } - } - + /** + * Create a SCA reference for a java Element + * @param element + * @param name + * @return + * @throws IntrospectionException + */ private org.apache.tuscany.sca.assembly.Reference createReference(JavaElementImpl element, String name) throws IntrospectionException { org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory.createReference(); @@ -207,4 +187,53 @@ public class ReferenceProcessor extends BaseJavaClassVisitor { } return reference; } + + + /** + * Utility methods + */ + + /** + * + * @param paramName + * @param pos + * @param name + * @return + * @throws InvalidConstructorException + */ + private static 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; + } + } + + /** + * + * @param ref + * @param type + * @return + */ + private static boolean removeReference(JavaElementImpl ref, JavaImplementation type) { + if (ref == null) { + return false; + } + List<org.apache.tuscany.sca.assembly.Reference> refs = type.getReferences(); + for (int i = 0; i < refs.size(); i++) { + if (refs.get(i).getName().equals(ref.getName())) { + refs.remove(i); + return true; + } + } + return false; + } + + } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java index 5498a792a3..49320579d0 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java @@ -96,12 +96,6 @@ public class ResourceProcessor extends BaseJavaClassVisitor { type.getResources().put(resource.getName(), resource); } - public JavaResourceImpl createResource(String name, JavaElementImpl element) { - element.setClassifer(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); - element.setName(name); - return new JavaResourceImpl(element); - } - @Override public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) throws IntrospectionException { @@ -135,4 +129,20 @@ public class ResourceProcessor extends BaseJavaClassVisitor { } } + + /** + * Utility methods + */ + + /** + * + * @param name + * @param element + * @return + */ + private static JavaResourceImpl createResource(String name, JavaElementImpl element) { + element.setClassifer(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + element.setName(name); + return new JavaResourceImpl(element); + } } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java index 7a5dbb01f9..52c68c1976 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java @@ -135,12 +135,34 @@ public class ServiceProcessor extends BaseJavaClassVisitor { createCallback(type, element); } + public Service createService(Class<?> interfaze) throws InvalidInterfaceException { + Service service = assemblyFactory.createService(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + // create a relative URI + service.setName(interfaze.getSimpleName()); + + JavaInterface callInterface = javaFactory.createJavaInterface(interfaze); + service.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } + return service; + } + + /** + * Utility methods + */ + + /** * @param type * @param element * @throws IllegalCallbackReferenceException */ - private void createCallback(JavaImplementation type, JavaElementImpl element) + private static void createCallback(JavaImplementation type, JavaElementImpl element) throws IllegalCallbackReferenceException { Service callbackService = null; Class<?> callbackClass = element.getType(); @@ -165,22 +187,4 @@ public class ServiceProcessor extends BaseJavaClassVisitor { } type.getCallbackMembers().get(baseType.getName()).add(element); } - - public Service createService(Class<?> interfaze) throws InvalidInterfaceException { - Service service = assemblyFactory.createService(); - JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); - service.setInterfaceContract(interfaceContract); - - // create a relative URI - service.setName(interfaze.getSimpleName()); - - JavaInterface callInterface = javaFactory.createJavaInterface(interfaze); - service.getInterfaceContract().setInterface(callInterface); - if (callInterface.getCallbackClass() != null) { - JavaInterface callbackInterface = javaFactory.createJavaInterface(callInterface.getCallbackClass()); - service.getInterfaceContract().setCallbackInterface(callbackInterface); - } - return service; - } - } diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConvertTimeMillisTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConvertTimeMillisTestCase.java index 728c6c89b8..4e5a2dfee6 100644 --- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConvertTimeMillisTestCase.java +++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConvertTimeMillisTestCase.java @@ -22,21 +22,19 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; -import org.junit.Before; import org.junit.Test; /** * @version $Rev$ $Date$ */ public class ConvertTimeMillisTestCase { - private MockProcessor registy; - @Test + @Test public void testConvertSeconds() throws Exception { - assertEquals(10000L, registy.convertTimeMillis("10 seconds")); - assertEquals(10000L, registy.convertTimeMillis("10 SECONDS")); + assertEquals(10000L, MockProcessor.convertTimeMillis("10 seconds")); + assertEquals(10000L, MockProcessor.convertTimeMillis("10 SECONDS")); try { - registy.convertTimeMillis("10seconds"); + MockProcessor.convertTimeMillis("10seconds"); fail(); } catch (NumberFormatException e) { // expected @@ -45,10 +43,10 @@ public class ConvertTimeMillisTestCase { @Test public void testConvertMinutes() throws Exception { - assertEquals(600000L, registy.convertTimeMillis("10 minutes")); - assertEquals(600000L, registy.convertTimeMillis("10 MINUTES")); + assertEquals(600000L, MockProcessor.convertTimeMillis("10 minutes")); + assertEquals(600000L, MockProcessor.convertTimeMillis("10 MINUTES")); try { - registy.convertTimeMillis("10minutes"); + MockProcessor.convertTimeMillis("10minutes"); fail(); } catch (NumberFormatException e) { // expected @@ -57,10 +55,10 @@ public class ConvertTimeMillisTestCase { @Test public void testConvertHours() throws Exception { - assertEquals(36000000L, registy.convertTimeMillis("10 hours")); - assertEquals(36000000L, registy.convertTimeMillis("10 HOURS")); + assertEquals(36000000L, MockProcessor.convertTimeMillis("10 hours")); + assertEquals(36000000L, MockProcessor.convertTimeMillis("10 HOURS")); try { - registy.convertTimeMillis("10hours"); + MockProcessor.convertTimeMillis("10hours"); fail(); } catch (NumberFormatException e) { // expected @@ -69,10 +67,10 @@ public class ConvertTimeMillisTestCase { @Test public void testConvertDays() throws Exception { - assertEquals(864000000L, registy.convertTimeMillis("10 days")); - assertEquals(864000000L, registy.convertTimeMillis("10 DAYS")); + assertEquals(864000000L, MockProcessor.convertTimeMillis("10 days")); + assertEquals(864000000L, MockProcessor.convertTimeMillis("10 DAYS")); try { - registy.convertTimeMillis("10days"); + MockProcessor.convertTimeMillis("10days"); fail(); } catch (NumberFormatException e) { // expected @@ -81,10 +79,10 @@ public class ConvertTimeMillisTestCase { @Test public void testConvertYears() throws Exception { - assertEquals(315569260000L, registy.convertTimeMillis("10 years")); - assertEquals(315569260000L, registy.convertTimeMillis("10 YEARS")); + assertEquals(315569260000L, MockProcessor.convertTimeMillis("10 years")); + assertEquals(315569260000L, MockProcessor.convertTimeMillis("10 YEARS")); try { - registy.convertTimeMillis("10years"); + MockProcessor.convertTimeMillis("10years"); fail(); } catch (NumberFormatException e) { // expected @@ -93,34 +91,24 @@ public class ConvertTimeMillisTestCase { @Test public void testConvertDefault() throws Exception { - assertEquals(10000L, registy.convertTimeMillis("10 ")); - assertEquals(10000L, registy.convertTimeMillis("10")); + assertEquals(10000L, MockProcessor.convertTimeMillis("10 ")); + assertEquals(10000L, MockProcessor.convertTimeMillis("10")); } @Test public void testInvalid() throws Exception { try { - registy.convertTimeMillis("foo"); + MockProcessor.convertTimeMillis("foo"); fail(); } catch (NumberFormatException e) { // expected } } - @Before - public void setUp() throws Exception { - registy = new MockProcessor(); - } - private class MockProcessor extends ConversationProcessor { public MockProcessor() { super(new DefaultAssemblyFactory()); } - - @Override - protected long convertTimeMillis(String expr) throws NumberFormatException { - return super.convertTimeMillis(expr); - } } } |