diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules')
8 files changed, 165 insertions, 8 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaNamespaceHandler.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaNamespaceHandler.java index 89c87ad1d1..66cb3c76fc 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaNamespaceHandler.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaNamespaceHandler.java @@ -16,7 +16,15 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.namespace.QName; + import org.springframework.beans.factory.xml.NamespaceHandlerSupport; +import org.w3c.dom.Element; +import org.w3c.dom.Node; /** * Handler for the <sca:> namespace in an application context @@ -35,4 +43,51 @@ public class ScaNamespaceHandler extends NamespaceHandlerSupport { registerBeanDefinitionParser("property", new ScaPropertyBeanDefinitionParser()); } + private static String getNamespaceURI(Element element, String prefix) { + if (element == null) { + return null; + } + String name = ("".equals(prefix)) ? "xmlns" : "xmlns:" + prefix; + String ns = element.getAttribute(name); + if (ns != null && !"".equals(ns)) { + return ns; + } + Node parent = element.getParentNode(); + if (parent instanceof Element) { + return getNamespaceURI((Element)parent, prefix); + } else { + return null; + } + } + + public static List<QName> resolve(Element element, String listOfNames) { + List<QName> qnames = new ArrayList<QName>(); + StringTokenizer tokenizer = new StringTokenizer(listOfNames); + while (tokenizer.hasMoreTokens()) { + String qname = tokenizer.nextToken(); + String prefix = ""; + String local = qname; + int index = qname.indexOf(':'); + if (index != -1) { + local = qname.substring(index + 1); + prefix = qname.substring(0, index); + } + String ns = getNamespaceURI(element, prefix); + if (ns != null) { + qnames.add(new QName(ns, local, prefix)); + } else { + throw new IllegalArgumentException("Prefix " + prefix + "is not bound to a namespace"); + } + } + return qnames; + } + + public static String getAttribute(Element element, String name) { + String attr = element.getAttributeNS(null, name); + if ("".equals(attr)) { + return null; + } else { + return attr; + } + } } diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaReferenceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaReferenceBeanDefinitionParser.java index 6cb4978a47..ab797c1e52 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaReferenceBeanDefinitionParser.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaReferenceBeanDefinitionParser.java @@ -16,6 +16,12 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace; +import static org.apache.tuscany.sca.implementation.spring.namespace.ScaNamespaceHandler.getAttribute; + +import java.util.List; + +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.implementation.spring.SpringSCAReferenceElement; import org.apache.tuscany.sca.implementation.spring.context.SCAGenericApplicationContext; import org.springframework.beans.factory.config.BeanDefinition; @@ -36,9 +42,21 @@ public class ScaReferenceBeanDefinitionParser implements BeanDefinitionParser { if (registry instanceof SCAGenericApplicationContext) { SCAGenericApplicationContext context = (SCAGenericApplicationContext)registry; SpringSCAReferenceElement referenceElement = - new SpringSCAReferenceElement(element.getAttributeNS(null, "name"), - element.getAttributeNS(null, "type")); - referenceElement.setDefaultBean(element.getAttributeNS(null, "default")); + new SpringSCAReferenceElement(getAttribute(element, "name"), getAttribute(element, "type")); + referenceElement.setDefaultBean(getAttribute(element, "default")); + + String requires = getAttribute(element, "requires"); + if (requires != null) { + List<QName> qnames = ScaNamespaceHandler.resolve(element, requires); + referenceElement.getIntentNames().addAll(qnames); + } + + String policySets = getAttribute(element, "policySets"); + if (policySets != null) { + List<QName> qnames = ScaNamespaceHandler.resolve(element, policySets); + referenceElement.getPolicySetNames().addAll(qnames); + } + context.addSCAReferenceElement(referenceElement); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaServiceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaServiceBeanDefinitionParser.java index e5eabd2a4b..6a3beebcff 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaServiceBeanDefinitionParser.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/ScaServiceBeanDefinitionParser.java @@ -16,6 +16,12 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace; +import static org.apache.tuscany.sca.implementation.spring.namespace.ScaNamespaceHandler.getAttribute; + +import java.util.List; + +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.implementation.spring.SpringSCAServiceElement; import org.apache.tuscany.sca.implementation.spring.context.SCAGenericApplicationContext; import org.springframework.beans.factory.config.BeanDefinition; @@ -36,9 +42,21 @@ public class ScaServiceBeanDefinitionParser implements BeanDefinitionParser { if (registry instanceof SCAGenericApplicationContext) { SCAGenericApplicationContext context = (SCAGenericApplicationContext)registry; SpringSCAServiceElement serviceElement = - new SpringSCAServiceElement(element.getAttributeNS(null, "name"), - element.getAttributeNS(null, "target")); - serviceElement.setType(element.getAttributeNS(null, "type")); + new SpringSCAServiceElement(getAttribute(element, "name"), getAttribute(element, "target")); + serviceElement.setType(getAttribute(element, "type")); + + String requires = getAttribute(element, "requires"); + if (requires != null) { + List<QName> qnames = ScaNamespaceHandler.resolve(element, requires); + serviceElement.getIntentNames().addAll(qnames); + } + + String policySets = getAttribute(element, "policySets"); + if (policySets != null) { + List<QName> qnames = ScaNamespaceHandler.resolve(element, policySets); + serviceElement.getPolicySetNames().addAll(qnames); + } + context.addSCAServiceElement(serviceElement); } // do nothing, handled by Tuscany diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLBeanDefinitionLoaderImpl.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLBeanDefinitionLoaderImpl.java index 71feddb9f5..6ba2c65ef0 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLBeanDefinitionLoaderImpl.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLBeanDefinitionLoaderImpl.java @@ -67,6 +67,7 @@ public class SpringXMLBeanDefinitionLoaderImpl implements SpringXMLBeanDefinitio List<SpringSCAPropertyElement> propertyElements, List<SpringBeanElement> beanElements, ProcessorContext context) { + // FIXME: [rfeng] We should try to avoid parsing the Spring xml twice for the deployment and runtime SCAGenericApplicationContext applicationContext = createApplicationContext(null, null, resources); serviceElements.addAll(applicationContext.getServiceElements()); referenceElements.addAll(applicationContext.getReferenceElements()); diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/resources/META-INF/spring.handlers b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/resources/META-INF/spring.handlers index 6d29e21024..1e1f558382 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/resources/META-INF/spring.handlers +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/resources/META-INF/spring.handlers @@ -1 +1,2 @@ http\://www.springframework.org/schema/sca=org.apache.tuscany.sca.implementation.spring.namespace.ScaNamespaceHandler
+http\://docs.oasis-open.org/ns/opencsa/sca-j/spring/200810=org.apache.tuscany.sca.implementation.spring.namespace.ScaNamespaceHandler
diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAReferenceElement.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAReferenceElement.java index 74cd3f4290..f06f05a46a 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAReferenceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAReferenceElement.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.implementation.spring; import java.util.ArrayList; import java.util.List; +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.policy.Intent; import org.apache.tuscany.sca.policy.PolicySet; @@ -39,6 +41,9 @@ public class SpringSCAReferenceElement { private List<Intent> intents = new ArrayList<Intent>(); private List<PolicySet> policySets = new ArrayList<PolicySet>(); + private List<QName> intentNames = new ArrayList<QName>(); + private List<QName> policySetNames = new ArrayList<QName>(); + public SpringSCAReferenceElement() { } @@ -80,12 +85,21 @@ public class SpringSCAReferenceElement { return policySets; } + public List<QName> getIntentNames() { + return intentNames; + } + + public List<QName> getPolicySetNames() { + return policySetNames; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("SpringSCAReferenceElement [name=").append(name).append(", type=").append(type) .append(", defaultBean=").append(defaultBean).append(", intents=").append(intents).append(", policySets=") - .append(policySets).append("]"); + .append(policySets).append(", intentNames=").append(intentNames).append(", policySetNames=") + .append(policySetNames).append("]"); return builder.toString(); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAServiceElement.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAServiceElement.java index 181bd7a787..38c124c3b4 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAServiceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringSCAServiceElement.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.implementation.spring; import java.util.ArrayList; import java.util.List; +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.policy.Intent; import org.apache.tuscany.sca.policy.PolicySet; @@ -38,6 +40,8 @@ public class SpringSCAServiceElement { private String target; private List<Intent> intents = new ArrayList<Intent>(); private List<PolicySet> policySets = new ArrayList<PolicySet>(); + private List<QName> intentNames = new ArrayList<QName>(); + private List<QName> policySetNames = new ArrayList<QName>(); public SpringSCAServiceElement() { @@ -80,12 +84,21 @@ public class SpringSCAServiceElement { return policySets; } + public List<QName> getIntentNames() { + return intentNames; + } + + public List<QName> getPolicySetNames() { + return policySetNames; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("SpringSCAServiceElement [name=").append(name).append(", type=").append(type) .append(", target=").append(target).append(", intents=").append(intents).append(", policySets=") - .append(policySets).append("]"); + .append(policySets).append(", intentNames=").append(intentNames).append(", policySetNames=") + .append(policySetNames).append("]"); return builder.toString(); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java index 9d360ebc2b..cca7586734 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java +++ b/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java @@ -41,6 +41,8 @@ import java.util.jar.Manifest; import java.util.logging.Level; import java.util.logging.Logger; +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.ComponentType; import org.apache.tuscany.sca.assembly.Multiplicity; @@ -76,6 +78,9 @@ import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; /** * Introspects a Spring XML application-context configuration file to create <implementation-spring../> @@ -89,6 +94,7 @@ public class SpringXMLComponentTypeLoader { private ExtensionPointRegistry registry; private ContributionFactory contributionFactory; private AssemblyFactory assemblyFactory; + private PolicyFactory policyFactory; private JavaInterfaceFactory javaFactory; private SpringBeanIntrospector beanIntrospector; @@ -99,6 +105,7 @@ public class SpringXMLComponentTypeLoader { this.registry = registry; FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + this.policyFactory = factories.getFactory(PolicyFactory.class); this.javaFactory = factories.getFactory(JavaInterfaceFactory.class); this.contributionFactory = factories.getFactory(ContributionFactory.class); this.xmlBeanDefinitionLoader = @@ -219,6 +226,7 @@ public class SpringXMLComponentTypeLoader { appCxtProperties, appCxtBeans, context); + populatePolicies(appCxtServices, appCxtReferences); } // Validate the beans from individual application context for uniqueness validateBeans(appCxtBeans, appCxtServices, appCxtReferences, appCxtProperties, context.getMonitor()); @@ -239,6 +247,35 @@ public class SpringXMLComponentTypeLoader { return; } // end method loadFromXML + public void populatePolicies(List<SpringSCAServiceElement> appCxtServices, + List<SpringSCAReferenceElement> appCxtReferences) { + for (SpringSCAReferenceElement e : appCxtReferences) { + for (QName qn : e.getIntentNames()) { + Intent intent = policyFactory.createIntent(); + intent.setName(qn); + e.getRequiredIntents().add(intent); + } + for (QName qn : e.getPolicySetNames()) { + PolicySet ps = policyFactory.createPolicySet(); + ps.setName(qn); + e.getPolicySets().add(ps); + } + } + + for (SpringSCAServiceElement e : appCxtServices) { + for (QName qn : e.getIntentNames()) { + Intent intent = policyFactory.createIntent(); + intent.setName(qn); + e.getRequiredIntents().add(intent); + } + for (QName qn : e.getPolicySetNames()) { + PolicySet ps = policyFactory.createPolicySet(); + ps.setName(qn); + e.getPolicySets().add(ps); + } + } + } + private URL resolveLocation(ModelResolver resolver, String contextPath, ProcessorContext context) throws MalformedURLException, ContributionReadException { URL resource = null; |