From c78cbecc4c028236a323c0841490ff537698352b Mon Sep 17 00:00:00 2001 From: ramkumar Date: Tue, 3 Mar 2009 06:51:59 +0000 Subject: Fix for TUSCANY-2875 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@749547 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/SpringImplementation.java | 97 +++++++++++++++++++++- .../spring/xml/SpringXMLComponentTypeLoader.java | 30 +++++-- 2 files changed, 118 insertions(+), 9 deletions(-) (limited to 'branches') diff --git a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java index e4f62d3bb2..165dfae8be 100644 --- a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java +++ b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/SpringImplementation.java @@ -20,8 +20,10 @@ package org.apache.tuscany.sca.implementation.spring; 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.Property; 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 @@ import org.springframework.core.io.Resource; * * @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 @@ public class SpringImplementation extends ImplementationImpl implements Implemen // Mapping of property names to Java class private Hashtable propertyMap; private List policyHandlerClassNames = null; + // List of unresolved bean property references + private Hashtable unresolvedBeanRef; // Method marked with @Init annotation private Method initMethod = null; @@ -65,6 +72,7 @@ public class SpringImplementation extends ImplementationImpl implements Implemen setUnresolved(true); serviceMap = new Hashtable(); propertyMap = new Hashtable(); + unresolvedBeanRef = new Hashtable(); } // end method SpringImplementation /* Returns the location attribute for this Spring implementation */ @@ -182,6 +190,17 @@ public class SpringImplementation extends ImplementationImpl implements Implemen 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 getPolicyHandlerClassNames() { return policyHandlerClassNames; } @@ -189,4 +208,80 @@ public class SpringImplementation extends ImplementationImpl implements Implemen public void setPolicyHandlerClassNames(List 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 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; + } } diff --git a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java index 9738cf22ef..d584329e88 100644 --- a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java +++ b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringXMLComponentTypeLoader.java @@ -325,12 +325,12 @@ public class SpringXMLComponentTypeLoader { List references, List 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 @@ public class SpringXMLComponentTypeLoader { } // 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 @@ public class SpringXMLComponentTypeLoader { 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 -- cgit v1.2.3