From a6cb76239fe7e25565515fd6621ff82dc8c603f8 Mon Sep 17 00:00:00 2001 From: ramkumar Date: Thu, 26 Mar 2009 10:47:36 +0000 Subject: Fixes for TUSCANY-2941 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@758591 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/xml/SpringBeanIntrospector.java | 13 +-- .../spring/xml/SpringBeanPojoProcessor.java | 38 +++++--- .../spring/xml/SpringConstructorArgElement.java | 11 ++- .../spring/xml/SpringXMLComponentTypeLoader.java | 108 +++++---------------- 4 files changed, 59 insertions(+), 111 deletions(-) (limited to 'branches/sca-java-1.x/modules/implementation-spring/src/main') diff --git a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanIntrospector.java b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanIntrospector.java index df85e4f302..5b6cfcab5b 100644 --- a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanIntrospector.java +++ b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanIntrospector.java @@ -18,6 +18,7 @@ */ package org.apache.tuscany.sca.implementation.spring.xml; +import java.util.List; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.ComponentType; import org.apache.tuscany.sca.contribution.service.ContributionResolveException; @@ -26,21 +27,17 @@ import org.apache.tuscany.sca.implementation.java.IntrospectionException; import org.apache.tuscany.sca.implementation.java.JavaImplementation; import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.AllowsPassByReferenceProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.BaseJavaClassVisitor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ComponentNameProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ConstructorProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ContextProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ConversationProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.DestroyProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.EagerInitProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.InitProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.PropertyProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ReferenceProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ResourceProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ScopeProcessor; import org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.policy.PolicyFactory; @@ -65,7 +62,8 @@ public class SpringBeanIntrospector { */ public SpringBeanIntrospector(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory, - PolicyFactory policyFactory) { + PolicyFactory policyFactory, + List conArgs) { javaImplementationFactory = new DefaultJavaImplementationFactory(); @@ -73,19 +71,16 @@ public class SpringBeanIntrospector { BaseJavaClassVisitor[] extensions = new BaseJavaClassVisitor[] { new ConstructorProcessor(assemblyFactory), - new AllowsPassByReferenceProcessor(assemblyFactory), new ComponentNameProcessor(assemblyFactory), new ContextProcessor(assemblyFactory), - new ConversationProcessor(assemblyFactory), new DestroyProcessor(assemblyFactory), new EagerInitProcessor(assemblyFactory), new InitProcessor(assemblyFactory), new PropertyProcessor(assemblyFactory), new ReferenceProcessor(assemblyFactory, javaFactory), new ResourceProcessor(assemblyFactory), - new ScopeProcessor(assemblyFactory), new ServiceProcessor(assemblyFactory, javaFactory), - new SpringBeanPojoProcessor(assemblyFactory, javaFactory), + new SpringBeanPojoProcessor(assemblyFactory, javaFactory, conArgs), new PolicyProcessor(assemblyFactory, policyFactory)}; for (JavaClassVisitor extension : extensions) { javaImplementationFactory.addClassVisitor(extension); diff --git a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanPojoProcessor.java b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanPojoProcessor.java index 0510a6d338..fe632190ee 100644 --- a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanPojoProcessor.java +++ b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringBeanPojoProcessor.java @@ -81,10 +81,12 @@ import org.apache.tuscany.sca.implementation.java.introspect.impl.Resource; */ public class SpringBeanPojoProcessor extends BaseJavaClassVisitor { private JavaInterfaceFactory javaFactory; + private List conArgs; - public SpringBeanPojoProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + public SpringBeanPojoProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory, List conArgs) { super(assemblyFactory); this.javaFactory = javaFactory; + this.conArgs = conArgs; } @Override @@ -280,6 +282,8 @@ public class SpringBeanPojoProcessor extends BaseJavaClassVisitor { private void evaluateConstructor(JavaImplementation type, Class clazz) throws IntrospectionException { // determine constructor if one is not annotated JavaConstructorImpl definition = type.getConstructor(); + Map props = type.getPropertyMembers(); + Map refs = type.getReferenceMembers(); Constructor constructor; boolean explict = false; if (definition != null && definition.getConstructor() @@ -298,37 +302,41 @@ public class SpringBeanPojoProcessor extends BaseJavaClassVisitor { // Only one constructor, take it constructor = constructors[0]; } else { - // FIXME multiple constructors, none yet done - Constructor selected = null; - int sites = type.getPropertyMembers().size() + type.getReferenceMembers().size(); + // multiple constructors scenario + Constructor selected = null; for (Constructor ctor : constructors) { if (ctor.getParameterTypes().length == 0) { selected = ctor; - } - if (ctor.getParameterTypes().length == sites) { - // TODO finish - // selected = constructor; - // select constructor - // break; + } else if (ctor.getParameterTypes().length == conArgs.size()) { + // we will find a constructor which has atleast one + // reference or property as its parameter types. + Class[] parametersTypes = ctor.getParameterTypes(); + for (Class pType: parametersTypes) { + for (JavaElementImpl property : props.values()) { + if (pType.equals(property.getType())) + selected = ctor; + } + for (JavaElementImpl reference : refs.values()) { + if (pType.equals(reference.getType())) + selected = ctor; + } + } } } if (selected == null) { throw new NoConstructorException(); } constructor = selected; - definition = type.getConstructors().get(selected); - type.setConstructor(definition); - // return; } definition = type.getConstructors().get(constructor); type.setConstructor(definition); } + JavaParameterImpl[] parameters = definition.getParameters(); if (parameters.length == 0) { return; } - Map props = type.getPropertyMembers(); - Map refs = type.getReferenceMembers(); + Annotation[][] annotations = constructor.getParameterAnnotations(); if (!explict) { // the constructor wasn't defined by an annotation, so check to see diff --git a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java index ce83baee05..553b3d33dd 100644 --- a/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java +++ b/branches/sca-java-1.x/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringConstructorArgElement.java @@ -31,7 +31,8 @@ public class SpringConstructorArgElement { private String ref; private String type; - private int index; + private int autoIndex = -1; + private int index = -1; private List values = new ArrayList(); public SpringConstructorArgElement(String ref, String type) { @@ -59,6 +60,14 @@ public class SpringConstructorArgElement { this.index = index; } + public int getAutoIndex() { + return this.autoIndex; + } + + public void setAutoIndex(int index) { + this.autoIndex = index; + } + public List getValues() { return this.values; } 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 0100bb8fc5..ef42e8c610 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 @@ -72,6 +72,7 @@ public class SpringXMLComponentTypeLoader { private AssemblyFactory assemblyFactory; private JavaInterfaceFactory javaFactory; + private PolicyFactory policyFactory; private ClassLoader cl; private SpringBeanIntrospector beanIntrospector; @@ -83,8 +84,7 @@ public class SpringXMLComponentTypeLoader { super(); this.assemblyFactory = assemblyFactory; this.javaFactory = javaFactory; - beanIntrospector = - new SpringBeanIntrospector(assemblyFactory, javaFactory, policyFactory); + this.policyFactory = policyFactory; } protected Class getImplementationClass() { @@ -260,6 +260,7 @@ public class SpringXMLComponentTypeLoader { SpringBeanElement innerbean = null; SpringPropertyElement property = null; SpringConstructorArgElement constructorArg = null; + int autoConstructorIndex = -1; try { boolean completed = false; @@ -280,6 +281,8 @@ public class SpringXMLComponentTypeLoader { } else if (Constants.CONSTRUCTORARG_ELEMENT.equals(qname)) { constructorArg = new SpringConstructorArgElement(reader.getAttributeValue(null, "ref"), reader.getAttributeValue(null, "type")); + autoConstructorIndex++; + constructorArg.setAutoIndex(autoConstructorIndex); if (reader.getAttributeValue(null, "index") != null) constructorArg.setIndex((new Integer(reader.getAttributeValue(null, "index"))).intValue()); if (reader.getAttributeValue(null, "value") != null) { @@ -413,7 +416,9 @@ public class SpringXMLComponentTypeLoader { if (beanElement.isInnerBean()) continue; // Load the Spring bean class Class beanClass = cl.loadClass(beanElement.getClassName()); - // Introspect the bean + // Introspect the bean + beanIntrospector = + new SpringBeanIntrospector(assemblyFactory, javaFactory, policyFactory, beanElement.getCustructorArgs()); ComponentType beanComponentType = assemblyFactory.createComponentType(); javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType); // Get the service interface defined by this Spring Bean and add to @@ -435,7 +440,9 @@ public class SpringXMLComponentTypeLoader { continue; Class beanClass = cl.loadClass(beanElement.getClassName()); - // Introspect the bean + // Introspect the bean + beanIntrospector = + new SpringBeanIntrospector(assemblyFactory, javaFactory, policyFactory, beanElement.getCustructorArgs()); ComponentType beanComponentType = assemblyFactory.createComponentType(); javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType); Map propertyMap = javaImplementation.getPropertyMembers(); @@ -482,98 +489,27 @@ public class SpringXMLComponentTypeLoader { String paramType = parameter.getType().getName(); Class interfaze = cl.loadClass(paramType); // Create a component type reference/property if the constructor-arg element has a - // type attribute declared... - if (conArgElement.getType() != null && paramType.equals(conArgElement.getType())) { + // type attribute OR index attribute declared... + if ((conArgElement.getType() != null && paramType.equals(conArgElement.getType())) || + (conArgElement.getIndex() != -1 && (conArgElement.getIndex() == parameter.getIndex())) || + (conArgElement.getAutoIndex() == parameter.getIndex())) + { if (parameter.getClassifer().getName().equals("org.osoa.sca.annotations.Reference")) { Reference theReference = createReference(interfaze, conArgElement.getRef()); componentType.getReferences().add(theReference); } if (parameter.getClassifer().getName().equals("org.osoa.sca.annotations.Property")) { - Property theProperty = assemblyFactory.createProperty(); - theProperty.setName(conArgElement.getRef()); - theProperty.setXSDType(JavaXMLMapper.getXMLType(interfaze)); - componentType.getProperties().add(theProperty); - // Remember the Java Class (ie the type) for this property - implementation.setPropertyClass(theProperty.getName(), interfaze); + // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type + // we might need to verify with the component definition later. + Reference theReference = createReference(interfaze, conArgElement.getRef()); + implementation.setUnresolvedBeanRef(conArgElement.getRef(), theReference); } - } - - // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type - // if the type attribute is absent in the contructor-arg element... - if (conArgElement.getType() == null) { - Reference theReference = createReference(interfaze, conArgElement.getRef()); - implementation.setUnresolvedBeanRef(conArgElement.getRef(), theReference); - } - } // end for + } + } // end for } // end if } // end while } // end while - - - // Now check to see if there are any more references from beans that are not satisfied - /*itb = beans.iterator(); - while (itb.hasNext()) { - SpringBeanElement beanElement = itb.next(); - boolean unresolvedProperties = false; - if (!beanElement.getProperties().isEmpty()) { - // Scan through the properties - Iterator itp = beanElement.getProperties().iterator(); - while (itp.hasNext()) { - SpringPropertyElement propertyElement = itp.next(); - if (propertyRefUnresolved(propertyElement.getRef(), beans, references, scaproperties)) { - // This means an unresolved reference from the spring bean... - unresolvedProperties = true; - } // end if - } // end while - // If there are unresolved properties, then find which ones are references - if (unresolvedProperties) { - Class beanClass = cl.loadClass(beanElement.getClassName()); - // Introspect the bean - ComponentType beanComponentType = assemblyFactory.createComponentType(); - javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType); - Map propertyMap = javaImplementation.getPropertyMembers(); - // Get the references by this Spring Bean and add the unresolved ones to - // the component type of the Spring Assembly - List beanReferences = beanComponentType.getReferences(); - List beanProperties = beanComponentType.getProperties(); - itp = beanElement.getProperties().iterator(); - while (itp.hasNext()) { - SpringPropertyElement propertyElement = itp.next(); - if (propertyRefUnresolved(propertyElement.getRef(), beans, references, scaproperties)) { - boolean resolved = false; - // This means an unresolved reference from the spring bean...add it to - // the references for the Spring application context - for (Reference reference : beanReferences) { - if (propertyElement.getName().equals(reference.getName())) { - // The name of the reference in this case is the string in - // the @ref attribute of the Spring property element, NOT the - // name of the field in the Spring bean.... - reference.setName(propertyElement.getRef()); - componentType.getReferences().add(reference); - resolved = true; - } // end if - } // end for - if (!resolved) { - // 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; - } - } - - } // end if - } // end if - } // end while - } // end if - } // end if - } // end while*/ } catch (ClassNotFoundException e) { // Means that either an interface class, property class or a bean was not found -- cgit v1.2.3