summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2008-09-12 07:53:45 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2008-09-12 07:53:45 +0000
commitcea87e003518f942a98c3a4586461a5e74597f53 (patch)
tree54d4c77e9e8d3410b5118f006fc10947f3fd328f
parent4c60c728cde15e2bc3878d5447641d775c53c196 (diff)
TUSCANY-2596: Apply patch from Ramkumar Ramalingam for Support for <constructor-arg> element in Spring Implementation
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@694636 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/Constants.java64
-rw-r--r--java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanElement.java9
-rw-r--r--java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java60
-rw-r--r--java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringPropertyElement.java16
-rw-r--r--java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java112
5 files changed, 224 insertions, 37 deletions
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/Constants.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/Constants.java
new file mode 100644
index 0000000000..92f980fdfe
--- /dev/null
+++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/Constants.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.implementation.spring.xml;
+
+import javax.xml.namespace.QName;
+
+/**
+ * Constants used in Spring Application Context XML files.
+ */
+public interface Constants {
+
+ String SCA_NS = "http://www.springframework.org/schema/sca";
+ String SPRING_NS = "http://www.springframework.org/schema/beans";
+
+ String PROPERTY = "property";
+ QName SCAPROPERTY_ELEMENT = new QName(SCA_NS, PROPERTY);
+ QName PROPERTY_ELEMENT = new QName(SPRING_NS, PROPERTY);
+
+ String SERVICE = "service";
+ QName SERVICE_ELEMENT = new QName(SCA_NS, SERVICE);
+
+ String REFERENCE = "reference";
+ QName REFERENCE_ELEMENT = new QName(SCA_NS, REFERENCE);
+
+ String BEANS = "beans";
+ QName BEANS_ELEMENT = new QName(SPRING_NS, BEANS);
+
+ String IMPORT = "import";
+ QName IMPORT_ELEMENT = new QName(SPRING_NS, IMPORT);
+
+ String BEAN = "bean";
+ QName BEAN_ELEMENT = new QName(SPRING_NS, BEAN);
+
+ String CONSTRUCTORARG = "constructor-arg";
+ QName CONSTRUCTORARG_ELEMENT = new QName(SPRING_NS, CONSTRUCTORARG);
+
+ String LIST = "list";
+ QName LIST_ELEMENT = new QName(SPRING_NS, LIST);
+
+ String VALUE = "value";
+ QName VALUE_ELEMENT = new QName(SPRING_NS, VALUE);
+
+ String REF = "ref";
+ QName REF_ELEMENT = new QName(SPRING_NS, REF);
+
+ String APPLICATION_CONTEXT = "application-context.xml";
+}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanElement.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanElement.java
index b39c5d674e..f9a9fee58f 100644
--- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanElement.java
+++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanElement.java
@@ -33,6 +33,7 @@ public class SpringBeanElement {
private String id;
private String className;
private List<SpringPropertyElement> properties = new ArrayList<SpringPropertyElement>();
+ private List<SpringConstructorArgElement> constructorargs = new ArrayList<SpringConstructorArgElement>();
public SpringBeanElement(String id, String className) {
this.id = id;
@@ -54,5 +55,13 @@ public class SpringBeanElement {
public void addProperty(SpringPropertyElement property) {
properties.add(property);
}
+
+ public List<SpringConstructorArgElement> getCustructorArgs() {
+ return constructorargs;
+ }
+
+ public void addCustructorArgs(SpringConstructorArgElement args) {
+ constructorargs.add(args);
+ }
} // end class SpringBeanElement
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java
new file mode 100644
index 0000000000..57f4a26ba3
--- /dev/null
+++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.implementation.spring.xml;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents a <constructor-arg> element in a Spring application-context
+ * - this has ref attribute
+ *
+ * @version $Rev: 512919 $ $Date: 2007-02-28 19:32:56 +0000 (Wed, 28 Feb 2007) $
+ */
+public class SpringConstructorArgElement {
+
+ private String ref;
+ private String type;
+ private List<String> values = new ArrayList<String>();
+
+ public SpringConstructorArgElement(String ref, String type) {
+ this.ref = ref;
+ this.type = type;
+ }
+
+ public String getType() {
+ return this.type;
+ }
+
+ public String getRef() {
+ return this.ref;
+ }
+
+ public void setRef(String ref) {
+ this.ref = ref;
+ }
+
+ public List<String> getValues() {
+ return this.values;
+ }
+
+ public void addValue(String value) {
+ this.values.add(value);
+ }
+}
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringPropertyElement.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringPropertyElement.java
index ceee459e3e..39e1286f32 100644
--- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringPropertyElement.java
+++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringPropertyElement.java
@@ -18,6 +18,9 @@
*/
package org.apache.tuscany.sca.implementation.spring.xml;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Represents a <property> element in a Spring application-context
* - this has name and ref attributes
@@ -28,6 +31,7 @@ public class SpringPropertyElement {
private String name;
private String ref;
+ private List<String> values = new ArrayList<String>();
public SpringPropertyElement(String name, String ref) {
this.name = name;
@@ -41,5 +45,17 @@ public class SpringPropertyElement {
public String getRef() {
return ref;
}
+
+ public void setRef(String ref) {
+ this.ref = ref;
+ }
+
+ public List<String> getProperties() {
+ return values;
+ }
+
+ public void addProperty(String value) {
+ values.add(value);
+ }
} // end class SpringPropertyElement
diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
index f8cc9ead80..a6dfe67a92 100644
--- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
+++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
@@ -42,11 +42,9 @@ import javax.xml.stream.XMLStreamReader;
import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.ComponentType;
import org.apache.tuscany.sca.assembly.Multiplicity;
-import org.apache.tuscany.sca.assembly.OperationsConfigurator;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
-import org.apache.tuscany.sca.assembly.xml.Constants;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
import org.apache.tuscany.sca.implementation.java.impl.JavaElementImpl;
@@ -68,16 +66,6 @@ import org.springframework.core.io.UrlResource;
* @version $Rev: 512919 $ $Date: 2007-02-28 19:32:56 +0000 (Wed, 28 Feb 2007) $
*/
public class SpringXMLComponentTypeLoader {
- private static final String SCA_NS = "http://www.springframework.org/schema/sca";
- private static final String SPRING_NS = "http://www.springframework.org/schema/beans";
- private static final QName SERVICE_ELEMENT = new QName(SCA_NS, "service");
- private static final QName REFERENCE_ELEMENT = new QName(SCA_NS, "reference");
- private static final QName SCAPROPERTY_ELEMENT = new QName(SCA_NS, "property");
- private static final QName IMPORT_ELEMENT = new QName(SPRING_NS, "import");
- private static final QName BEANS_ELEMENT = new QName(SPRING_NS, "beans");
- private static final QName BEAN_ELEMENT = new QName(SPRING_NS, "bean");
- private static final QName PROPERTY_ELEMENT = new QName(SPRING_NS, "property");
- private static final String APPLICATION_CONTEXT = "application-context.xml";
private AssemblyFactory assemblyFactory;
private JavaInterfaceFactory javaFactory;
@@ -165,8 +153,28 @@ public class SpringXMLComponentTypeLoader {
generateComponentType(implementation, beans, services, references, scaproperties);
return;
- } // end method loadFromXML
+ } // end method loadFromXML
+ /**
+ * Method which returns the XMLStreamReader for the Spring application-context.xml file
+ * specified in the location attribute
+ */
+ private XMLStreamReader getApplicationContextReader (String location) throws ContributionReadException {
+
+ try {
+ // FIXME - is the ContextClassLoader the right place to start the search?
+ cl = Thread.currentThread().getContextClassLoader();
+ Resource resource = getApplicationContextResource(location, cl);
+ // FIXME - need a better way to handle the XMLInputFactory than allocating a new one every time
+ XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
+ XMLStreamReader reader = xmlFactory.createXMLStreamReader(resource.getInputStream());
+ return reader;
+ } catch (IOException e) {
+ throw new ContributionReadException(e);
+ } catch (XMLStreamException e) {
+ throw new ContributionReadException(e);
+ }
+ }
/**
* Method which reads the bean definitions from Spring application-context.xml file and identifies
@@ -179,6 +187,8 @@ public class SpringXMLComponentTypeLoader {
List<SpringSCAPropertyElement> scaproperties) throws ContributionReadException {
SpringBeanElement bean = null;
+ SpringPropertyElement property = null;
+ SpringConstructorArgElement constructorArg = null;
try {
boolean completed = false;
@@ -187,51 +197,79 @@ public class SpringXMLComponentTypeLoader {
case START_ELEMENT:
QName qname = reader.getName();
//System.out.println("Spring TypeLoader - found element with name: " + qname.toString());
- if (IMPORT_ELEMENT.equals(qname)) {
+ if (Constants.IMPORT_ELEMENT.equals(qname)) {
String location = reader.getAttributeValue(null, "resource");
- // Create an input stream for the imported resource file
- cl = Thread.currentThread().getContextClassLoader();
- Resource resource = getApplicationContextResource(location, cl);
- XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
- XMLStreamReader ireader = xmlFactory.createXMLStreamReader(resource.getInputStream());
- // Call this method to read the bean definition for the imported resource
- readBeanDefinition(ireader, beans, services, references, scaproperties);
- } else if (SERVICE_ELEMENT.equals(qname)) {
+ if (location != null) {
+ XMLStreamReader ireader = getApplicationContextReader(location);
+ // Read the bean definition for the identified imported resource
+ readBeanDefinition(ireader, beans, services, references, scaproperties);
+ }
+ } else if (Constants.SERVICE_ELEMENT.equals(qname)) {
SpringSCAServiceElement service =
new SpringSCAServiceElement(reader.getAttributeValue(null, "name"), reader
.getAttributeValue(null, "type"), reader.getAttributeValue(null, "target"));
services.add(service);
- } else if (REFERENCE_ELEMENT.equals(qname)) {
+ } else if (Constants.REFERENCE_ELEMENT.equals(qname)) {
SpringSCAReferenceElement reference =
new SpringSCAReferenceElement(reader.getAttributeValue(null, "name"), reader
.getAttributeValue(null, "type"));
references.add(reference);
- } else if (SCAPROPERTY_ELEMENT.equals(qname)) {
+ } else if (Constants.SCAPROPERTY_ELEMENT.equals(qname)) {
SpringSCAPropertyElement scaproperty =
new SpringSCAPropertyElement(reader.getAttributeValue(null, "name"), reader
.getAttributeValue(null, "type"));
scaproperties.add(scaproperty);
- } else if (BEAN_ELEMENT.equals(qname)) {
+ } else if (Constants.BEAN_ELEMENT.equals(qname)) {
bean = new SpringBeanElement(reader.getAttributeValue(null, "id"), reader
.getAttributeValue(null, "class"));
- beans.add(bean);
- } else if (PROPERTY_ELEMENT.equals(qname)) {
- SpringPropertyElement property =
- new SpringPropertyElement(reader.getAttributeValue(null, "name"), reader
+ //beans.add(bean);
+ } else if (Constants.PROPERTY_ELEMENT.equals(qname)) {
+ property = new SpringPropertyElement(reader.getAttributeValue(null, "name"), reader
.getAttributeValue(null, "ref"));
- bean.addProperty(property);
+ //bean.addProperty(property);
+ } else if (Constants.CONSTRUCTORARG_ELEMENT.equals(qname)) {
+ constructorArg = new SpringConstructorArgElement(reader.getAttributeValue(null, "ref"),
+ reader.getAttributeValue(null, "type"));
+ } else if (Constants.REF_ELEMENT.equals(qname)) {
+ String ref = reader.getAttributeValue(null, "bean");
+ // Check if the parent element is a property
+ if (property != null) property.setRef(ref);
+ // Check if the parent element is a constructor-arg
+ if (constructorArg != null) constructorArg.setRef(ref);
+ } else if (Constants.VALUE_ELEMENT.equals(qname)) {
+ String value = reader.getElementText();
+ // Check if the parent element is a constructor-arg
+ if (constructorArg != null) {
+ constructorArg.addValue(value);
+ // Identify the XML resource specified for the constructor-arg element
+ if ((value.indexOf(".xml") != -1)) {
+ if ((bean.getClassName().indexOf(".ClassPathXmlApplicationContext") != -1) ||
+ (bean.getClassName().indexOf(".FileSystemXmlApplicationContext") != -1)) {
+ XMLStreamReader creader = getApplicationContextReader(value);
+ // Read the bean definition for the constructor-arg resources
+ readBeanDefinition(creader, beans, services, references, scaproperties);
+ }
+ }
+ }
} // end if
break;
case END_ELEMENT:
- if (BEANS_ELEMENT.equals(reader.getName())) {
+ if (Constants.BEANS_ELEMENT.equals(reader.getName())) {
//System.out.println("Spring TypeLoader - finished read of context file");
completed = true;
break;
+ } else if (Constants.BEAN_ELEMENT.equals(reader.getName())) {
+ beans.add(bean);
+ bean = null;
+ } else if (Constants.PROPERTY_ELEMENT.equals(reader.getName())) {
+ bean.addProperty(property);
+ property = null;
+ } else if (Constants.CONSTRUCTORARG_ELEMENT.equals(reader.getName())) {
+ bean.addCustructorArgs(constructorArg);
+ constructorArg = null;
} // end if
} // end switch
} // end while
- } catch (IOException e) {
- throw new ContributionReadException(e);
} catch (XMLStreamException e) {
throw new ContributionReadException(e);
}
@@ -502,7 +540,7 @@ public class SpringXMLComponentTypeLoader {
}
// no manifest-specified Spring context, use default
appXmlFile = new File(locationFile, "META-INF" + File.separator + "spring"
- + File.separator + APPLICATION_CONTEXT);
+ + File.separator + Constants.APPLICATION_CONTEXT);
if (appXmlFile.exists()) {
return new UrlResource(appXmlFile.toURL());
}
@@ -530,9 +568,9 @@ public class SpringXMLComponentTypeLoader {
}
}
je = jf.getJarEntry("META-INF" + File.separator + "spring"
- + File.separator + APPLICATION_CONTEXT);
+ + File.separator + Constants.APPLICATION_CONTEXT);
if (je != null) {
- return new UrlResource(new URL("jar:" + locationFile.toURI().toURL() + "!/" + APPLICATION_CONTEXT));
+ return new UrlResource(new URL("jar:" + locationFile.toURI().toURL() + "!/" + Constants.APPLICATION_CONTEXT));
}
} catch (IOException e) {
// bad archive
@@ -545,7 +583,7 @@ public class SpringXMLComponentTypeLoader {
}
throw new ContributionReadException("SpringXMLLoader getApplicationContextResource: "
- + "META-INF/spring/" + APPLICATION_CONTEXT + "not found");
+ + "META-INF/spring/" + Constants.APPLICATION_CONTEXT + "not found");
} // end method getApplicationContextResource
/**