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 2011/03/21 21:28:43 UTC

svn commit: r1083943 - in /tuscany/sca-java-2.x/trunk/modules: host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/ implementation-spring/src/main/java/org/a...

Author: rfeng
Date: Mon Mar 21 20:28:43 2011
New Revision: 1083943

URL: http://svn.apache.org/viewvc?rev=1083943&view=rev
Log:
Expose Node to the servlet

Modified:
    tuscany/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java
    tuscany/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/TuscanyDispatcherServlet.java
    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/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java?rev=1083943&r1=1083942&r2=1083943&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java Mon Mar 21 20:28:43 2011
@@ -26,6 +26,8 @@ import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 
+import org.apache.tuscany.sca.node.Node;
+
 
 /**
  * A Servlet that provides a hook to control the lifecycle of Tuscany node
@@ -37,6 +39,7 @@ public class TuscanyServlet extends Http
     private Logger logger = Logger.getLogger(TuscanyServlet.class.getName());
 
     private transient WebContextConfigurator configurator;
+    private transient Node node;
 
     public TuscanyServlet() {
         super();
@@ -47,7 +50,7 @@ public class TuscanyServlet extends Http
         try {
             super.init(config);
             configurator = WebAppHelper.getConfigurator(this);
-            WebAppHelper.init(configurator);
+            node = WebAppHelper.init(configurator);
         } catch (Throwable e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
             configurator.getServletContext().log(e.getMessage(), e);
@@ -57,6 +60,13 @@ public class TuscanyServlet extends Http
 
     public void destroy() {
         WebAppHelper.stop(configurator);
+        node = null;
+        configurator = null;
+        super.destroy();
+    }
+
+    public Node getNode() {
+        return node;
     }
 
 }

Modified: tuscany/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/TuscanyDispatcherServlet.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/TuscanyDispatcherServlet.java?rev=1083943&r1=1083942&r2=1083943&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/TuscanyDispatcherServlet.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/implementation-spring-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/spring/TuscanyDispatcherServlet.java Mon Mar 21 20:28:43 2011
@@ -27,6 +27,7 @@ import javax.servlet.ServletException;
 
 import org.apache.tuscany.sca.host.webapp.WebAppHelper;
 import org.apache.tuscany.sca.host.webapp.WebContextConfigurator;
+import org.apache.tuscany.sca.node.Node;
 import org.springframework.web.servlet.DispatcherServlet;
 
 /**
@@ -39,6 +40,7 @@ public class TuscanyDispatcherServlet ex
     private Logger logger = Logger.getLogger(TuscanyDispatcherServlet.class.getName());
 
     private transient WebContextConfigurator configurator;
+    private transient Node node;
 
     public TuscanyDispatcherServlet() {
         super();
@@ -49,7 +51,7 @@ public class TuscanyDispatcherServlet ex
         try {
             super.init(config);
             configurator = WebAppHelper.getConfigurator(this);
-            WebAppHelper.init(configurator);
+            node = WebAppHelper.init(configurator);
         } catch (Throwable e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
             configurator.getServletContext().log(e.getMessage(), e);
@@ -59,7 +61,13 @@ public class TuscanyDispatcherServlet ex
 
     public void destroy() {
         WebAppHelper.stop(configurator);
+        node = null;
+        configurator = null;
         super.destroy();
     }
 
+    public Node getNode() {
+        return node;
+    }
+
 }

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=1083943&r1=1083942&r2=1083943&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 Mon Mar 21 20:28:43 2011
@@ -101,7 +101,7 @@ public class SpringXMLComponentTypeLoade
     private SpringBeanIntrospector beanIntrospector;
 
     private SpringXMLBeanDefinitionLoader xmlBeanDefinitionLoader;
-    
+
     private JavaIntrospectionHelper javaIntrospectionHelper;
 
     public SpringXMLComponentTypeLoader(ExtensionPointRegistry registry) {
@@ -136,6 +136,7 @@ public class SpringXMLComponentTypeLoade
             monitor.problem(problem);
         }
     }
+
     /**
      * Report a error.
      *
@@ -155,6 +156,7 @@ public class SpringXMLComponentTypeLoade
             monitor.problem(problem);
         }
     }
+
     protected Class<SpringImplementation> getImplementationClass() {
         return SpringImplementation.class;
     }
@@ -226,11 +228,11 @@ public class SpringXMLComponentTypeLoade
 
             if (xmlBeanDefinitionLoader != null) {
                 xmlBeanDefinitionLoader.load(contextResources,
-                                                   appCxtServices,
-                                                   appCxtReferences,
-                                                   appCxtProperties,
-                                                   appCxtBeans,
-                                                   context);
+                                             appCxtServices,
+                                             appCxtReferences,
+                                             appCxtProperties,
+                                             appCxtBeans,
+                                             context);
                 populatePolicies(appCxtServices, appCxtReferences);
             }
             // Validate the beans from individual application context for uniqueness
@@ -266,7 +268,7 @@ public class SpringXMLComponentTypeLoade
                 e.getPolicySets().add(ps);
             }
         }
-        
+
         for (SpringSCAServiceElement e : appCxtServices) {
             for (QName qn : e.getIntentNames()) {
                 Intent intent = policyFactory.createIntent();
@@ -352,7 +354,7 @@ public class SpringXMLComponentTypeLoade
                 } else {
                     interfaze = getBeanInterface(resolver, serviceElement.getTarget(), beans, context);
                 }
-                
+
                 Service theService = createService(interfaze, serviceElement.getName());
                 // Spring allows duplication of bean definitions in multiple context scenario,
                 // in such cases, the latest bean definition overrides the older ones, hence 
@@ -381,7 +383,7 @@ public class SpringXMLComponentTypeLoade
                         }
                     }
                 } // end for
-                
+
                 if (!found) {
                     // REVIEW: Adding a SpringBeanElement "proxy" so that the bean id can be used at runtime to look
                     // up the bean instance from the parent context
@@ -480,118 +482,123 @@ public class SpringXMLComponentTypeLoade
                 } // end while
             } // end if
 
-            itb = beans.iterator();
-            while (itb.hasNext()) {
-                SpringBeanElement beanElement = itb.next();
-
-                // If its not a valid bean for service, ignore it
-                if (!isValidBeanForService(beanElement)) {
-                    continue;
-                }
-                // Ignore if the bean has no properties and constructor arguments
-                if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
-                    continue;
+            // [rfeng] We only try to implicitly map Spring beans if no sca:reference or sca:property is present
+            if (references.isEmpty() && scaproperties.isEmpty()) {
+                itb = beans.iterator();
+                while (itb.hasNext()) {
+                    SpringBeanElement beanElement = itb.next();
 
-                ComponentType beanComponentType = assemblyFactory.createComponentType();
+                    // If its not a valid bean for service, ignore it
+                    if (!isValidBeanForService(beanElement)) {
+                        continue;
+                    }
+                    // Ignore if the bean has no properties and constructor arguments
+                    if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
+                        continue;
 
-                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
-                // the component type of the Spring Assembly
-                List<Reference> beanReferences = beanComponentType.getReferences();
-                List<Property> beanProperties = beanComponentType.getProperties();
-
-                Set<String> excludedNames = new HashSet<String>();
-                Iterator<SpringPropertyElement> itp = beanElement.getProperties().iterator();
-                while (itp.hasNext()) {
-                    SpringPropertyElement propertyElement = itp.next();
-                    // Exclude the reference that is also known as a spring property
-                    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) {
-                                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(propertyRef);
-                                    componentType.getReferences().add(reference);
-                                    break;
-                                } // end if
-                            } // end for
-
-                            // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type
-                            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
-                                    // name of the field in the Spring bean....
-                                    Class<?> interfaze =
-                                        resolveClass(resolver,
-                                                     (propertyMap.get(propertyElement.getName()).getType()).getName(),
-                                                     context);
-                                    Reference theReference = createReference(interfaze, propertyRef);
-                                    implementation.setUnresolvedBeanRef(propertyRef, theReference);
-                                    break;
-                                } // end if
-                            } // end for
-                        } // end if 
-                    } // end for
-                } // end while
+                    ComponentType beanComponentType = assemblyFactory.createComponentType();
 
-                Iterator<SpringConstructorArgElement> itcr = beanElement.getCustructorArgs().iterator();
-                while (itcr.hasNext()) {
-                    SpringConstructorArgElement conArgElement = itcr.next();
-                    for (String constructorArgRef : conArgElement.getRefs()) {
-                        if (propertyRefUnresolved(constructorArgRef, beans, references, scaproperties)) {
-                            for (JavaParameterImpl parameter : constructor.getParameters()) {
-                                String paramType = parameter.getType().getName();
-                                Class<?> interfaze = resolveClass(resolver, paramType, context);
-                                // Create a component type reference/property if the constructor-arg element has a
-                                // type attribute OR index attribute declared...
-                                if ((conArgElement.getType() != null && paramType.equals(conArgElement.getType())) || (conArgElement
-                                    .getIndex() != -1 && (conArgElement.getIndex() == parameter.getIndex()))) {
-                                    // [rfeng] Commenting out the following code as the constructor parameter based SCA
-                                    // references are added already
-                                    /*
-                                    if (parameter.getClassifer() == org.oasisopen.sca.annotation.Reference.class) {
-                                        Reference theReference = createReference(interfaze, constructorArgRef);
-                                        componentType.getReferences().add(theReference);
-                                    }
-                                    */
-                                    if (parameter.getClassifer() == org.oasisopen.sca.annotation.Property.class) {
-                                        // 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, constructorArgRef);
-                                        implementation.setUnresolvedBeanRef(constructorArgRef, theReference);
+                    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
+                    // the component type of the Spring Assembly
+                    List<Reference> beanReferences = beanComponentType.getReferences();
+                    List<Property> beanProperties = beanComponentType.getProperties();
+
+                    Set<String> excludedNames = new HashSet<String>();
+                    Iterator<SpringPropertyElement> itp = beanElement.getProperties().iterator();
+                    while (itp.hasNext()) {
+                        SpringPropertyElement propertyElement = itp.next();
+                        // Exclude the reference that is also known as a spring property
+                        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) {
+                                    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(propertyRef);
+                                        // reference.setWiredByImpl(true);
+                                        componentType.getReferences().add(reference);
+                                        break;
+                                    } // end if
+                                } // end for
+
+                                // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type
+                                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
+                                        // name of the field in the Spring bean....
+                                        Class<?> interfaze =
+                                            resolveClass(resolver,
+                                                         (propertyMap.get(propertyElement.getName()).getType())
+                                                             .getName(),
+                                                         context);
+                                        Reference theReference = createReference(interfaze, propertyRef);
+                                        implementation.setUnresolvedBeanRef(propertyRef, theReference);
+                                        break;
+                                    } // end if
+                                } // end for
+                            } // end if 
+                        } // end for
+                    } // end while
+
+                    Iterator<SpringConstructorArgElement> itcr = beanElement.getCustructorArgs().iterator();
+                    while (itcr.hasNext()) {
+                        SpringConstructorArgElement conArgElement = itcr.next();
+                        for (String constructorArgRef : conArgElement.getRefs()) {
+                            if (propertyRefUnresolved(constructorArgRef, beans, references, scaproperties)) {
+                                for (JavaParameterImpl parameter : constructor.getParameters()) {
+                                    String paramType = parameter.getType().getName();
+                                    Class<?> interfaze = resolveClass(resolver, paramType, context);
+                                    // Create a component type reference/property if the constructor-arg element has a
+                                    // type attribute OR index attribute declared...
+                                    if ((conArgElement.getType() != null && paramType.equals(conArgElement.getType())) || (conArgElement
+                                        .getIndex() != -1 && (conArgElement.getIndex() == parameter.getIndex()))) {
+                                        // [rfeng] Commenting out the following code as the constructor parameter based SCA
+                                        // references are added already
+                                        /*
+                                        if (parameter.getClassifer() == org.oasisopen.sca.annotation.Reference.class) {
+                                            Reference theReference = createReference(interfaze, constructorArgRef);
+                                            componentType.getReferences().add(theReference);
+                                        }
+                                        */
+                                        if (parameter.getClassifer() == org.oasisopen.sca.annotation.Property.class) {
+                                            // 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, constructorArgRef);
+                                            implementation.setUnresolvedBeanRef(constructorArgRef, theReference);
+                                        }
                                     }
-                                }
-                            } // end for
-                        } // 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) {
-                        // Only add the ones that not listed by sca:reference
-                        componentType.getReferences().add(ref);
+                                } // end for
+                            } // 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) {
+                            // Only add the ones that not listed by sca:reference
+                            componentType.getReferences().add(ref);
+                        }
                     }
-                }
 
-            } // end while
+                } // end while
+            }
 
         } catch (ClassNotFoundException e) {
             // Means that either an interface class, property class or a bean was not found
@@ -606,31 +613,34 @@ public class SpringXMLComponentTypeLoade
         return;
     } // end method generateComponentType
 
-    private Class<?> getBeanInterface(ModelResolver resolver, String target, List<SpringBeanElement> beans, ProcessorContext context) throws ClassNotFoundException {
-    	SpringBeanElement bean = null;
-    	for (SpringBeanElement sbe : beans) {
-    		if (sbe.getId().equals(target)) {
-    			bean = sbe;
-    			break;
-    		}
-    	}
-    	if (bean == null) {
-    		error(context.getMonitor(), "TargetBeanDoesNotExist", null, target);
-    		return null;
-    	}
-    	
+    private Class<?> getBeanInterface(ModelResolver resolver,
+                                      String target,
+                                      List<SpringBeanElement> beans,
+                                      ProcessorContext context) throws ClassNotFoundException {
+        SpringBeanElement bean = null;
+        for (SpringBeanElement sbe : beans) {
+            if (sbe.getId().equals(target)) {
+                bean = sbe;
+                break;
+            }
+        }
+        if (bean == null) {
+            error(context.getMonitor(), "TargetBeanDoesNotExist", null, target);
+            return null;
+        }
+
         Class<?> beanClass = resolveClass(resolver, bean.getClassName(), context);
         Set<Class<?>> ifaces = javaIntrospectionHelper.getAllInterfaces(beanClass);
         for (Class<?> interfaze : ifaces) {
             if (interfaze.isAnnotationPresent(Remotable.class)) {
-               return interfaze;	
+                return interfaze;
             }
         }
 
         return beanClass;
-	}
+    }
 
-	/*
+    /*
      * Determines whether a reference attribute of a Spring property element is resolved either
      * by a bean in the application context or by an SCA reference element or by an SCA property
      * element