You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2010/07/31 05:02:15 UTC

svn commit: r980999 - /tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java

Author: rfeng
Date: Sat Jul 31 03:02:14 2010
New Revision: 980999

URL: http://svn.apache.org/viewvc?rev=980999&view=rev
Log:
Work around the issue that Tuscany java introspector doesn't handle Spring beans with constructor injections

Modified:
    tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java

Modified: tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java?rev=980999&r1=980998&r2=980999&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java Sat Jul 31 03:02:14 2010
@@ -41,6 +41,8 @@ import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
@@ -57,7 +59,6 @@ import org.apache.tuscany.sca.assembly.x
 import org.apache.tuscany.sca.contribution.Artifact;
 import org.apache.tuscany.sca.contribution.ContributionFactory;
 import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
-import org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
 import org.apache.tuscany.sca.contribution.processor.ProcessorContext;
 import org.apache.tuscany.sca.contribution.resolver.ClassReference;
 import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
@@ -92,6 +93,8 @@ import org.apache.tuscany.sca.policy.Pol
  * @version $Rev$ $Date$
  */
 public class SpringXMLComponentTypeLoader {
+    private final static Logger log = Logger.getLogger(SpringXMLComponentTypeLoader.class.getName());
+
     private ExtensionPointRegistry registry;
     private XMLInputFactory xmlInputFactory;
     private ContributionFactory contributionFactory;
@@ -114,26 +117,6 @@ public class SpringXMLComponentTypeLoade
     }
 
     /**
-     * Report a exception.
-     *
-     * @param problems
-     * @param message
-     * @param model
-     */
-    private void error(Monitor monitor, String message, Object model, Exception ex) {
-        if (monitor != null) {
-            Problem problem =
-                monitor.createProblem(this.getClass().getName(),
-                                      "impl-spring-validation-messages",
-                                      Severity.ERROR,
-                                      model,
-                                      message,
-                                      ex);
-            monitor.problem(problem);
-        }
-    }
-
-    /**
      * Report a error.
      *
      * @param problems
@@ -336,9 +319,7 @@ public class SpringXMLComponentTypeLoade
                                     contextPath.substring(0, contextPath.lastIndexOf("/") + 1) + location;
                                 XMLStreamReader ireader = getApplicationContextReader(resolver, resourcePath, context);
                                 // Read the context definition for the identified imported resource
-                                readContextDefinition(resolver,
-                                                      ireader,
-                                                      resourcePath, // The new context path
+                                readContextDefinition(resolver, ireader, resourcePath, // The new context path
                                                       beans,
                                                       services,
                                                       references,
@@ -408,6 +389,9 @@ public class SpringXMLComponentTypeLoade
                                 }
                             }
                             beans.add(bean);
+                            if (log.isLoggable(Level.FINE)) {
+                                log.log(Level.FINE, "Adding Spring bean ..." + bean.getId() + " from " + contextPath);
+                            }
                             // Read the <bean> element and its child elements
                             readBeanDefinition(reader, bean, beans);
                         } // end if
@@ -698,25 +682,34 @@ public class SpringXMLComponentTypeLoade
                 // Loop through all the beans found
                 while (itb.hasNext()) {
                     SpringBeanElement beanElement = itb.next();
+
                     // If its not a valid bean for service, ignore it
-                    if (!isvalidBeanForService(beanElement))
+                    if (!isvalidBeanForService(beanElement)) {
                         continue;
-                    // Load the Spring bean class
-                    Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
-                    // Introspect the bean
-                    beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
-                    ComponentType beanComponentType = assemblyFactory.createComponentType();
-                    javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
-                    // Set the service name as bean name
-                    for (Service componentService : beanComponentType.getServices())
-                        componentService.setName(beanElement.getId());
-                    // Get the service interface defined by this Spring Bean and add to
-                    // the component type of the Spring Assembly
-                    List<Service> beanServices = beanComponentType.getServices();
-                    componentType.getServices().addAll(beanServices);
-                    // Add these services to the Service / Bean map
-                    for (Service beanService : beanServices) {
-                        implementation.setBeanForService(beanService, beanElement);
+                    }
+                    try {
+                        // Load the Spring bean class
+                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
+                        // Introspect the bean
+                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
+                        ComponentType beanComponentType = assemblyFactory.createComponentType();
+                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
+                        // Set the service name as bean name
+                        for (Service componentService : beanComponentType.getServices()) {
+                            componentService.setName(beanElement.getId());
+                        }
+                        // Get the service interface defined by this Spring Bean and add to
+                        // the component type of the Spring Assembly
+                        List<Service> beanServices = beanComponentType.getServices();
+                        componentType.getServices().addAll(beanServices);
+                        // Add these services to the Service / Bean map
+                        for (Service beanService : beanServices) {
+                            implementation.setBeanForService(beanService, beanElement);
+                        }
+                    } catch (Throwable e) {
+                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
+                        // Tuscany is not happy with that during the introspection
+                        log.log(Level.SEVERE, e.getMessage(), e);
                     }
                 } // end while
             } // end if
@@ -728,11 +721,19 @@ public class SpringXMLComponentTypeLoade
                 if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
                     continue;
 
-                Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
-                // Introspect the bean
-                beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                 ComponentType beanComponentType = assemblyFactory.createComponentType();
-                javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
+
+                try {
+                    Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
+                    // Introspect the bean
+                    beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
+                    javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
+                } catch (Exception e) {
+                    // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
+                    // Tuscany is not happy with that during the introspection
+                    log.log(Level.SEVERE, e.getMessage(), e);
+                    continue;
+                }
                 Map<String, JavaElementImpl> propertyMap = javaImplementation.getPropertyMembers();
                 JavaConstructorImpl constructor = javaImplementation.getConstructor();
                 // Get the references by this Spring Bean and add the unresolved ones to
@@ -745,11 +746,11 @@ public class SpringXMLComponentTypeLoade
                 while (itp.hasNext()) {
                     SpringPropertyElement propertyElement = itp.next();
                     // Exclude the reference that is also known as a spring property
-                    excludedNames.add(propertyElement.getName()); 
+                    excludedNames.add(propertyElement.getName());
                     for (String propertyRef : propertyElement.getRefs()) {
                         if (propertyRefUnresolved(propertyRef, beans, references, scaproperties)) {
                             // This means an unresolved reference from the spring bean...
-                            for (Reference reference: beanReferences) {
+                            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
@@ -808,7 +809,7 @@ public class SpringXMLComponentTypeLoade
                         } // end if
                     } // end for
                 } // end while
-                
+
                 // [rfeng] Add the remaining introspected references (w/ @Reference but without Spring property ref)
                 for (Reference ref : beanReferences) {
                     if (!excludedNames.contains(ref.getName()) && componentType.getReference(ref.getName()) == null) {
@@ -816,7 +817,7 @@ public class SpringXMLComponentTypeLoade
                         componentType.getReferences().add(ref);
                     }
                 }
-                
+
             } // end while
 
         } catch (ClassNotFoundException e) {
@@ -824,8 +825,6 @@ public class SpringXMLComponentTypeLoade
             throw new ContributionReadException(e);
         } catch (InvalidInterfaceException e) {
             throw new ContributionReadException(e);
-        } catch (ContributionResolveException e) {
-
         } // end try
 
         // If we get here, the Spring assembly component type is resolved