From 81358f327591d183cb2ea2efade790a282307d67 Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 24 Aug 2010 22:50:49 +0000 Subject: Port the policy changes git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@988752 13f79535-47bb-0310-9956-ffa450edef68 --- .../elements/tie/SpringSCAReferenceElement.java | 20 +++++++- .../elements/tie/SpringSCAServiceElement.java | 20 +++++++- .../spring/namespace/tie/ScaNamespaceHandler.java | 55 ++++++++++++++++++++++ .../tie/ScaReferenceBeanDefinitionParser.java | 24 ++++++++-- .../tie/ScaServiceBeanDefinitionParser.java | 24 ++++++++-- 5 files changed, 135 insertions(+), 8 deletions(-) (limited to 'sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java') diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java index 7569d2996f..0bcbb73583 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAReferenceElement.java @@ -18,6 +18,11 @@ */ package org.apache.tuscany.sca.implementation.spring.elements.tie; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + /** * Represents a element in a Spring application-context @@ -31,6 +36,10 @@ public class SpringSCAReferenceElement { private String name; private String type; private String defaultBean; + + private List intentNames = new ArrayList(); + private List policySetNames = new ArrayList(); + public SpringSCAReferenceElement(String name, String type) { this.name = name; @@ -60,12 +69,21 @@ public class SpringSCAReferenceElement { public String getDefaultBean() { return defaultBean; } + + public List getIntentNames() { + return intentNames; + } + + public List 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("]"); + .append(", defaultBean=").append(defaultBean).append(", intentNames=").append(intentNames) + .append(", policySetNames=").append(policySetNames).append("]"); return builder.toString(); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java index 737abe7c04..2322d19e1f 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/elements/tie/SpringSCAServiceElement.java @@ -18,6 +18,11 @@ */ package org.apache.tuscany.sca.implementation.spring.elements.tie; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + /** * Represents a element in a Spring application-context @@ -32,6 +37,9 @@ public class SpringSCAServiceElement { private String type; private String target; + private List intentNames = new ArrayList(); + private List policySetNames = new ArrayList(); + public SpringSCAServiceElement(String name, String target) { this.name = name; @@ -61,12 +69,22 @@ public class SpringSCAServiceElement { public String getTarget() { return target; } + + public List getIntentNames() { + return intentNames; + } + + public List 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("]"); + .append(", target=").append(target).append(", intentNames=").append(intentNames) + .append(", policySetNames=").append(policySetNames).append("]"); return builder.toString(); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java index 6ed49abb6f..27b1811b86 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaNamespaceHandler.java @@ -16,7 +16,15 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace.tie; +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 resolve(Element element, String listOfNames) { + List qnames = new ArrayList(); + 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-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java index a4fefa70f1..8470d2ffd5 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaReferenceBeanDefinitionParser.java @@ -16,6 +16,12 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace.tie; +import static org.apache.tuscany.sca.implementation.spring.namespace.tie.ScaNamespaceHandler.getAttribute; + +import java.util.List; + +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAReferenceElement; 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 qnames = ScaNamespaceHandler.resolve(element, requires); + referenceElement.getIntentNames().addAll(qnames); + } + + String policySets = getAttribute(element, "policySets"); + if (policySets != null) { + List qnames = ScaNamespaceHandler.resolve(element, policySets); + referenceElement.getPolicySetNames().addAll(qnames); + } + context.addSCAReferenceElement(referenceElement); } diff --git a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java index 64f4ce20dc..900356698d 100644 --- a/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java +++ b/sca-java-2.x/trunk/modules/implementation-spring-tie/src/main/java/org/apache/tuscany/sca/implementation/spring/namespace/tie/ScaServiceBeanDefinitionParser.java @@ -16,6 +16,12 @@ */ package org.apache.tuscany.sca.implementation.spring.namespace.tie; +import static org.apache.tuscany.sca.implementation.spring.namespace.tie.ScaNamespaceHandler.getAttribute; + +import java.util.List; + +import javax.xml.namespace.QName; + import org.apache.tuscany.sca.implementation.spring.context.tie.SCAGenericApplicationContext; import org.apache.tuscany.sca.implementation.spring.elements.tie.SpringSCAServiceElement; 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 qnames = ScaNamespaceHandler.resolve(element, requires); + serviceElement.getIntentNames().addAll(qnames); + } + + String policySets = getAttribute(element, "policySets"); + if (policySets != null) { + List qnames = ScaNamespaceHandler.resolve(element, policySets); + serviceElement.getPolicySetNames().addAll(qnames); + } + context.addSCAServiceElement(serviceElement); } // do nothing, handled by Tuscany -- cgit v1.2.3