Author: ramkumar
Date: Tue Mar 3 06:51:59 2009
New Revision: 749547
URL: http://svn.apache.org/viewvc?rev=749547&view=rev
Log:
Fix for TUSCANY-2875
Modified:
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
Modified:
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java
URL:
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java?rev=749547&r1=749546&r2=749547&view=diff
==============================================================================
---
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java
(original)
+++
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java
Tue Mar 3 06:51:59 2009
@@ -20,8 +20,10 @@
import java.lang.reflect.Method;
import java.util.Hashtable;
+import java.util.Enumeration;
import java.util.List;
+import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentType;
import org.apache.tuscany.sca.assembly.Extensible;
import org.apache.tuscany.sca.assembly.Implementation;
@@ -29,9 +31,12 @@
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.assembly.impl.ImplementationImpl;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.assembly.builder.ComponentPreProcessor;
import org.apache.tuscany.sca.implementation.java.impl.JavaConstructorImpl;
import org.apache.tuscany.sca.implementation.spring.xml.SpringBeanElement;
import org.apache.tuscany.sca.policy.util.PolicyHandlerTuple;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.springframework.core.io.Resource;
/**
@@ -39,7 +44,7 @@
*
* @version $Rev: 511195 $ $Date: 2007-02-24 02:29:46 +0000 (Sat, 24 Feb 2007)
$
*/
-public class SpringImplementation extends ImplementationImpl implements
Implementation, Extensible {
+public class SpringImplementation extends ImplementationImpl implements
Implementation, ComponentPreProcessor, Extensible {
// The location attribute which points to the Spring application-context
XML file
private String location;
@@ -51,6 +56,8 @@
// Mapping of property names to Java class
private Hashtable<String, Class> propertyMap;
private List<PolicyHandlerTuple> policyHandlerClassNames = null;
+ // List of unresolved bean property references
+ private Hashtable<String, Reference> unresolvedBeanRef;
// Method marked with @Init annotation
private Method initMethod = null;
@@ -65,6 +72,7 @@
setUnresolved(true);
serviceMap = new Hashtable<String, SpringBeanElement>();
propertyMap = new Hashtable<String, Class>();
+ unresolvedBeanRef = new Hashtable<String, Reference>();
} // end method SpringImplementation
/* Returns the location attribute for this Spring implementation */
@@ -182,6 +190,17 @@
return propertyMap.get(propertyName);
} // end method getPropertyClass
+ public void setUnresolvedBeanRef(String refName, Reference reference) {
+ if (refName == null || reference == null)
+ return;
+ unresolvedBeanRef.put(refName, reference);
+ return;
+ } // end method setUnresolvedBeanRef
+
+ public Reference getUnresolvedBeanRef(String refName) {
+ return unresolvedBeanRef.get(refName);
+ } // end method getUnresolvedBeanRef
+
public List<PolicyHandlerTuple> getPolicyHandlerClassNames() {
return policyHandlerClassNames;
}
@@ -189,4 +208,80 @@
public void setPolicyHandlerClassNames(List<PolicyHandlerTuple>
policyHandlerClassNames) {
this.policyHandlerClassNames = policyHandlerClassNames;
} // end method setPolicyHandlerClassNames
+
+
+ /**
+ * Use preProcess to validate and map the references and properties
dynamically
+ */
+ public void preProcess(Component component) {
+ if (!(component instanceof RuntimeComponent))
+ return;
+
+ RuntimeComponent rtc = (RuntimeComponent) component;
+
+ // Check if the SCDL is properly configured for all the services
+ // exposed by Spring application context, otherwise report a error
+ /*Enumeration<SpringBeanElement> itr = serviceMap.elements();
+ while (itr.hasMoreElements()) {
+ SpringBeanElement beanElement = itr.nextElement();
+ if (!rtc.getServices().contains(beanElement.getId())) {
+ throw new AssertionError("Configuration Error:");
+ }
+ }*/
+
+ for (Reference reference : rtc.getReferences()) {
+ if (unresolvedBeanRef.containsKey(reference.getName())) {
+ Reference ref = unresolvedBeanRef.get(reference.getName());
+ componentType.getReferences().add(
+ createReference(reference,
ref.getInterfaceContract()));
+ unresolvedBeanRef.remove(reference.getName());
+ }
+ }
+
+ for (Property property : rtc.getProperties()) {
+ if (unresolvedBeanRef.containsKey(property.getName())) {
+
componentType.getProperties().add(createProperty(property));
+ this.setPropertyClass(property.getName(),
property.getClass());
+ unresolvedBeanRef.remove(property.getName());
+ }
+ }
+
+ // Check if the SCDL is properly configured for all the SCA references
+ // used in the Spring application context, otherwise report a error
+ /*for (Reference reference: componentType.getReferences()) {
+ if (!rtc.getReferences().contains(reference.getName())) {
+ throw new AssertionError("Configuration Error:");
+ }
+ }*/
+
+ // Check if the SCDL is properly configured for all the SCA property
+ // used in the Spring application context, otherwise report a error
+ /*for (Property property: componentType.getProperties()) {
+ if (!rtc.getProperties().contains(property.getName())) {
+ throw new AssertionError("Configuration Error:");
+ }
+ }*/
+ }
+
+ protected Reference createReference(Reference reference, InterfaceContract
interfaze) {
+ Reference newReference;
+ try {
+ newReference = (Reference)reference.clone();
+ if (newReference.getInterfaceContract() == null)
+ newReference.setInterfaceContract(interfaze);
+ } catch (CloneNotSupportedException e) {
+ throw new AssertionError(e); // should not ever happen
+ }
+ return newReference;
+ }
+
+ protected Property createProperty(Property property) {
+ Property newProperty;
+ try {
+ newProperty = (Property)property.clone();
+ } catch (CloneNotSupportedException e) {
+ throw new AssertionError(e); // should not ever happen
+ }
+ return newProperty;
+ }
}
Modified:
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
URL:
http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java?rev=749547&r1=749546&r2=749547&view=diff
==============================================================================
---
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
(original)
+++
tuscany/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java
Tue Mar 3 06:51:59 2009
@@ -325,12 +325,12 @@
List<SpringSCAReferenceElement>
references,
List<SpringSCAPropertyElement>
scaproperties) throws ContributionReadException {
/*
- * 1. Each service becomes a service in the component type
- * 2. Each reference becomes a reference in the component type
+ * 1. Each sca:service becomes a service in the component type
+ * 2. Each sca:reference becomes a reference in the component type
* 3. IF there are no explicit service elements, each bean becomes a
service
* 4. Each bean property which is a reference not pointing at another
bean in the
* application context becomes a reference unless it is pointing at
one of the references
- * 5. Each scaproperty becomes a property in the component type
+ * 5. Each sca:property becomes a property in the component type
* 6. Each bean property which is not a reference and which is not
pointing
* at another bean in the application context becomes a property in
the component type
*/
@@ -430,9 +430,22 @@
} // end if
} // end for
if (!resolved) {
- // If the bean property is not already
resolved as a reference
+ // If the bean property is not already
resolved as a reference
+ // then it may be an SCA reference OR a
SCA property, it really depends
+ // on how the SCDL has defined
references and properties for this component.
+ // So lets assume all unresolved bean
properties as references.
+ for (Property scaproperty :
beanProperties) {
+ if
(propertyElement.getName().equals(scaproperty.getName())) {
+ Class<?> interfaze =
cl.loadClass((propertyMap.get(propertyElement.getName()).getType()).getName());
+ Reference theReference =
createReference(interfaze, propertyElement.getRef());
+
implementation.setUnresolvedBeanRef(propertyElement.getRef(), theReference);
+ resolved = true;
+ }
+ }
+
+ /*// If the bean property is not already
resolved as a reference
// then it must be an SCA property...
- for (Property scaproperty :
beanProperties) {
+ for (Property scaproperty :
beanProperties) {
if
(propertyElement.getName().equals(scaproperty.getName())) {
// The name of the reference in
this case is the string in
// the @ref attribute of the
Spring property element, NOT the
@@ -440,11 +453,12 @@
scaproperty.setName(propertyElement.getRef());
componentType.getProperties().add(scaproperty);
// Fetch and store the type of the
property
-
implementation.setPropertyClass(scaproperty.getName(), propertyMap
-
.get(scaproperty.getName()).getType());
+
implementation.setPropertyClass(propertyElement.getRef(), propertyMap
+
.get(propertyElement.getName()).getType());
resolved = true;
} // end if
- } // end for
+ } // end for */
+
} // end if
} // end if
} // end while