From c53bddb7cd7eb1f06a3a8a4f43e590b5cea28aa1 Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 20 Oct 2009 23:08:05 +0000 Subject: Fix for otest JCA_800? ... git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@827831 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/impl/CompositePolicyBuilderImpl.java | 31 ++++- .../java/introspect/impl/PolicyProcessor.java | 146 +++++++++++---------- .../java/xml/JavaImplementationProcessor.java | 8 +- .../introspect/impl/PolicyProcessorTestCase.java | 4 +- 4 files changed, 117 insertions(+), 72 deletions(-) (limited to 'java/sca/modules') diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java index 3db76a81e3..ac5e5f9fcc 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java @@ -112,6 +112,23 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo } return false; } + + private boolean checkMutualExclusion(PolicySubject subject, BuilderContext context) { + if (subject == null) { + return false; + } + // FIXME: [rfeng] When should the intents be resolved? + resolveAndNormalize(subject, context.getDefinitions(), context); + for (Intent i1 : subject.getRequiredIntents()) { + for (Intent i2 : subject.getRequiredIntents()) { + if (i1 != i2 && i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) { + error(context.getMonitor(), "MutuallyExclusiveIntents", subject, i1, i2); + return true; + } + } + } + return false; + } /** * Inherit the policySets and intents from the implementation hierarchy @@ -298,7 +315,8 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo } protected void computePolicies(Composite composite, Definitions definitions, BuilderContext context) { - + checkMutualExclusion(composite, context); + // compute policies recursively for (Component component : composite.getComponents()) { Implementation implementation = component.getImplementation(); @@ -308,10 +326,16 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo } for (Component component : composite.getComponents()) { + checkMutualExclusion(component, context); + checkMutualExclusion(component.getImplementation(), context); + // Check component against implementation checkMutualExclusion(component, component.getImplementation(), context); for (ComponentService componentService : component.getServices()) { + checkMutualExclusion(componentService, context); + checkMutualExclusion(componentService.getService(), context); + // Check component/service against componentType/service checkMutualExclusion(componentService, componentService.getService(), context); @@ -337,6 +361,7 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo // Find the corresponding binding in the componentType and inherit the intents/policySets if (componentService.getService() != null) { for (Binding binding : componentService.getService().getBindings()) { + checkMutualExclusion((PolicySubject) binding, context); if (isEqual(ep.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) { checkMutualExclusion((PolicySubject)ep.getBinding(), (PolicySubject)binding, context); // Inherit from componentType.service.binding @@ -359,6 +384,9 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo } for (ComponentReference componentReference : component.getReferences()) { + checkMutualExclusion(componentReference, context); + checkMutualExclusion(componentReference.getReference(), context); + // Check component/reference against componentType/reference checkMutualExclusion(componentReference, componentReference.getReference(), context); @@ -388,6 +416,7 @@ public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements Compo for (Binding binding : componentReference.getReference().getBindings()) { if (epr.getBinding() != null && isEqual(epr.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) { + checkMutualExclusion((PolicySubject) binding, context); checkMutualExclusion((PolicySubject)epr.getBinding(), (PolicySubject)binding, context); // Inherit from componentType.reference.binding inherit(epr, binding); 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 1b75ff938c..79a0adac88 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 @@ -19,6 +19,8 @@ package org.apache.tuscany.sca.implementation.java.introspect.impl; import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; @@ -31,21 +33,19 @@ import javax.xml.namespace.QName; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Reference; -import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.implementation.java.IntrospectionException; import org.apache.tuscany.sca.implementation.java.JavaElementImpl; import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; -import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; -import org.apache.tuscany.sca.interfacedef.java.JavaOperation; -import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; import org.apache.tuscany.sca.policy.Intent; import org.apache.tuscany.sca.policy.PolicyFactory; import org.apache.tuscany.sca.policy.PolicySet; import org.apache.tuscany.sca.policy.PolicySubject; +import org.oasisopen.sca.ServiceRuntimeException; import org.oasisopen.sca.annotation.PolicySets; import org.oasisopen.sca.annotation.Qualifier; import org.oasisopen.sca.annotation.Requires; @@ -56,15 +56,17 @@ import org.oasisopen.sca.annotation.Requires; * @version $Rev$ $Date$ */ public class PolicyProcessor extends BaseJavaClassVisitor { - + private PolicyFactory policyFactory; - public PolicyProcessor(AssemblyFactory assemblyFactory, PolicyFactory policyFactory, JavaInterfaceFactory javaInterfaceFactory) { + public PolicyProcessor(AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + JavaInterfaceFactory javaInterfaceFactory) { super(assemblyFactory); this.policyFactory = policyFactory; this.javaInterfaceFactory = javaInterfaceFactory; } - + public PolicyProcessor(ExtensionPointRegistry registry) { super(registry); FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); @@ -73,14 +75,11 @@ public class PolicyProcessor extends BaseJavaClassVisitor { @Override public void visitClass(Class clazz, JavaImplementation type) throws IntrospectionException { - + // Read intents on the Java implementation class - if ( type instanceof PolicySubject ) { - readIntentsAndPolicySets(clazz, - ((PolicySubject)type).getRequiredIntents(), - ((PolicySubject)type).getPolicySets()); - } + readPolicySetAndIntents((PolicySubject)type, clazz); + /* // FIXME: [rfeng] We might want to refactor this out // Find the business methods in the implementation class for all services Set methods = new HashSet(); @@ -108,27 +107,36 @@ public class PolicyProcessor extends BaseJavaClassVisitor { PolicySubject subject = op; Method method = operation.getJavaMethod(); if (subject != null) { - readIntents(method.getAnnotation(Requires.class), subject.getRequiredIntents()); - readSpecificIntents(method.getAnnotations(), subject.getRequiredIntents()); - readPolicySets(method.getAnnotation(PolicySets.class), subject.getPolicySets()); + readPolicySetAndIntents(subject, method); } } + */ // Start to process annotations on the reference members Map referenceMap = new HashMap(); - for(Reference ref: type.getReferences()) { + for (Reference ref : type.getReferences()) { referenceMap.put(ref.getName(), ref); } Map members = type.getReferenceMembers(); - for(Map.Entry e: members.entrySet()) { + for (Map.Entry e : members.entrySet()) { Reference reference = referenceMap.get(e.getKey()); - readIntents(e.getValue().getAnnotation(Requires.class), reference.getRequiredIntents()); - readSpecificIntents(e.getValue().getAnnotations(), reference.getRequiredIntents()); - readPolicySets(e.getValue().getAnnotation(PolicySets.class), reference.getPolicySets()); + readPolicySetAndIntents(reference, e.getValue().getAnchor()); } } + + private void readPolicySetAndIntents(PolicySubject subject, AnnotatedElement element) { + readIntents(element.getAnnotation(Requires.class), subject.getRequiredIntents()); + readSpecificIntents(element.getAnnotations(), subject.getRequiredIntents()); + readPolicySets(element.getAnnotation(PolicySets.class), subject.getPolicySets()); + } + private void readPolicySetAndIntents(PolicySubject subject, JavaElementImpl element) { + readIntents(element.getAnnotation(Requires.class), subject.getRequiredIntents()); + readSpecificIntents(element.getAnnotations(), subject.getRequiredIntents()); + readPolicySets(element.getAnnotation(PolicySets.class), subject.getPolicySets()); + } + private void readSpecificIntents(Annotation[] annotations, List requiredIntents) { for (Annotation a : annotations) { org.oasisopen.sca.annotation.Intent intentAnnotation = @@ -144,14 +152,14 @@ public class PolicyProcessor extends BaseJavaClassVisitor { qname = new QName(intentAnnotation.targetNamespace(), intentAnnotation.localPart()); } Set qualifiers = new HashSet(); - for(Method m: a.annotationType().getMethods()) { + for (Method m : a.annotationType().getMethods()) { Qualifier qualifier = m.getAnnotation(Qualifier.class); if (qualifier != null && m.getReturnType() == String[].class) { try { - qualifiers.addAll(Arrays.asList((String[]) m.invoke(a))); + qualifiers.addAll(Arrays.asList((String[])m.invoke(a))); } catch (Throwable e) { e.printStackTrace(); - } + } } } qualifiers.remove(""); @@ -172,45 +180,6 @@ public class PolicyProcessor extends BaseJavaClassVisitor { } } - /** - * Read policy intents on the given interface or class - * @param clazz - * @param requiredIntents - */ - private void readIntentsAndPolicySets(Class clazz, - List requiredIntents, - List 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); - } - } - } - - readSpecificIntents(clazz.getAnnotations(), requiredIntents); - - 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); - } - } - } - } - /** * Read intent annotations on the given interface or class * @param intentAnnotation @@ -237,7 +206,6 @@ public class PolicyProcessor extends BaseJavaClassVisitor { } } - /** * Read policy set annotations on a given interface or class * @param policySetAnnotation @@ -265,7 +233,7 @@ public class PolicyProcessor extends BaseJavaClassVisitor { /** * Utility methods */ - + /** * * @param intentName @@ -285,5 +253,51 @@ public class PolicyProcessor extends BaseJavaClassVisitor { } return qname; } - + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + // Check if a field that is not an SCA reference has any policySet/intent annotations + JavaElementImpl element = new JavaElementImpl(field); + if (!type.getReferenceMembers().values().contains(element)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, field); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "Field that is not an SCA reference cannot have policySet/intent annotations: " + field); + } + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) { + if (!type.getReferenceMembers().values().contains(parameter)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, parameter); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "Constructor parameter that is not an SCA reference cannot have policySet/intent annotations: " + parameter); + } + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Set annotatedElements = new HashSet(); + for (JavaElementImpl element : type.getReferenceMembers().values()) { + annotatedElements.add(element.getAnchor()); + } + // Check if a field that is not an SCA reference has any policySet/intent annotations + if (!annotatedElements.contains(method)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, method); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "Method that is not an SCA reference cannot have policySet/intent annotations: " + method); + } + } + } diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java index df67814bd4..74b07bc4cc 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java @@ -197,9 +197,11 @@ public class JavaImplementationProcessor implements StAXArtifactProcessor