tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edward...@apache.org
Subject svn commit: r544394 - /incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/
Date Tue, 05 Jun 2007 05:58:59 GMT
Author: edwardsmj
Date: Mon Jun  4 22:58:58 2007
New Revision: 544394

URL: http://svn.apache.org/viewvc?view=rev&rev=544394
Log:
First working version of Spring implementation - services supported, but not references

Modified:
    incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringArtifactProcessor.java
    incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementation.java
    incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProvider.java
    incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProviderFactory.java
    incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringInvoker.java

Modified: incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringArtifactProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringArtifactProcessor.java?view=diff&rev=544394&r1=544393&r2=544394
==============================================================================
--- incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringArtifactProcessor.java
(original)
+++ incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringArtifactProcessor.java
Mon Jun  4 22:58:58 2007
@@ -39,6 +39,7 @@
 import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
 import org.apache.tuscany.implementation.spi.AbstractStAXArtifactProcessor;
 import org.apache.tuscany.implementation.spi.ResourceHelper;
+import org.apache.tuscany.implementation.spring.xml.SpringXMLComponentTypeLoader;
 import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
 
 import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;

Modified: incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementation.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementation.java?view=diff&rev=544394&r1=544393&r2=544394
==============================================================================
--- incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementation.java
(original)
+++ incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementation.java
Mon Jun  4 22:58:58 2007
@@ -19,6 +19,7 @@
 package org.apache.tuscany.implementation.spring;
 
 import java.util.List;
+import java.util.Hashtable;
 
 import org.apache.tuscany.sca.assembly.ComponentType;
 import org.apache.tuscany.sca.assembly.Implementation;
@@ -26,6 +27,10 @@
 import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.assembly.impl.ComponentTypeImpl;
 
+import org.springframework.core.io.Resource;
+
+import org.apache.tuscany.implementation.spring.xml.SpringBeanElement;
+
 /**
  * Represents a Spring implementation.
  * 
@@ -33,13 +38,19 @@
  */
 public class SpringImplementation  extends ComponentTypeImpl implements Implementation {
 
-	/* The location attribute which points to the Spring application-context XML file */
+	// The location attribute which points to the Spring application-context XML file
     private String springLocation;
+    // The application-context file as a Spring Resource
+    private Resource resource;
     private ComponentType componentType;
+    // Mapping of Services to Beans
+    private Hashtable<String,SpringBeanElement> serviceMap;
 
     protected SpringImplementation() {
         this.springLocation = null;
+        this.resource = null;
         setUnresolved(true);
+        serviceMap = new Hashtable<String,SpringBeanElement>();
     }
 
     /* Returns the location attribute for this Spring implementation */
@@ -55,6 +66,14 @@
     	this.springLocation = location;
         return;
     }
+    
+    public void setResource( Resource resource ) {
+    	this.resource = resource;
+    }
+    
+    public Resource getResource() {
+    	return resource;
+    }
 
     /* 
      * Returns the componentType for this Spring implementation 
@@ -76,5 +95,24 @@
 
     public List<Reference> getReferences() {
         return componentType.getReferences();
+    }
+    
+    /**
+     * Returns the Spring Bean which implements a particular service
+     * @param service the service
+     * @return the bean which implements the service, as a SpringBeanElement
+     */
+    public SpringBeanElement getBeanFromService( Service service ) {
+    	SpringBeanElement theBean = serviceMap.get( service.getName() );
+    	return theBean;
+    }
+    
+    /**
+     * Sets the mapping from a service to the Spring Bean that implements the service
+     * @param service the service
+     * @param theBean a SpringBeanElement for the Bean implementing the service
+     */
+    public void setBeanForService( Service service, SpringBeanElement theBean ) {
+    	serviceMap.put( service.getName(), theBean );
     }
 }

Modified: incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProvider.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProvider.java?view=diff&rev=544394&r1=544393&r2=544394
==============================================================================
--- incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProvider.java
(original)
+++ incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProvider.java
Mon Jun  4 22:58:58 2007
@@ -14,33 +14,58 @@
 import org.apache.tuscany.sca.provider.ImplementationProvider;
 import org.osoa.sca.ComponentContext;
 
-// TODO - create a working version of this invoker...
+import org.springframework.context.support.AbstractApplicationContext;
+
+// TODO - create a working version of this class...
+/**
+ * A provider class for runtime Spring implementation instances
+ */
 public class SpringImplementationProvider implements ImplementationProvider {
-    private SpringImplementation implementation;
-    private RuntimeComponent component;
+    private SpringImplementation 	implementation;
+    private RuntimeComponent 		component;
+    
+    // A Spring application context object
+    private AbstractApplicationContext springContext;
     
+    /**
+     * Constructor for the provider - takes a component definition and a Spring implementation
+     * description
+     * @param component - the component in the assembly
+     * @param implementation - the implementation
+     */
     public SpringImplementationProvider( RuntimeComponent component,
                                          SpringImplementation implementation ) {
         super();
         this.implementation = implementation;
-        this.component = component;
+        this.component 		= component;
+        SCAParentApplicationContext scaParentContext = 
+            	new SCAParentApplicationContext( implementation );
+        springContext = new SCAApplicationContext(scaParentContext, implementation.getResource()
);
     } // end constructor
 
     public Invoker createInvoker(RuntimeComponentService service, Operation operation) {
-        return new SpringInvoker(this, operation);
+        return new SpringInvoker( component, springContext, service, operation );
     }
 
     public Invoker createCallbackInvoker(Operation operation) {
-        return new SpringInvoker(this, operation);
+        return new SpringInvoker( component, springContext, null, operation );
     }
     
-
+    /**
+     * Start this Spring implementation instance
+     */
     public void start() {
-        // TODO
-    }
+        springContext.start();
+        System.out.println("SpringImplementationProvider: Spring context started");
+    } // end method start()
 
+    /**
+     * Stop this implementation instance
+     */
     public void stop() {
-        // TODO
-    }
+        // TODO - complete 
+    	springContext.stop();
+    	System.out.println("SpringImplementationProvider: Spring context stopped");
+    } // end method stop
 
 } // end class SpringImplementationProvider

Modified: incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProviderFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProviderFactory.java?view=diff&rev=544394&r1=544393&r2=544394
==============================================================================
--- incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProviderFactory.java
(original)
+++ incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringImplementationProviderFactory.java
Mon Jun  4 22:58:58 2007
@@ -11,15 +11,29 @@
  */
 public class SpringImplementationProviderFactory implements ImplementationProviderFactory<SpringImplementation>
{
 
+	/**
+	 * Simple constructor
+	 *
+	 */
     public SpringImplementationProviderFactory() {
         super();
-    }
+    } 
 
+    /**
+     * Returns a SpringImplementationProvider for a given component and Spring implementation
+     * @param component the component for which implementation instances are required
+     * @param implementation the Spring implementation with details of the component
+     * implementation
+     * @return the SpringImplementationProvider for the specified component
+     */
     public ImplementationProvider createImplementationProvider(RuntimeComponent component,
                                                                SpringImplementation implementation)
{
         return new SpringImplementationProvider( component, implementation );
     }
 
+    /**
+     * Returns the class of the Spring implementation
+     */
     public Class<SpringImplementation> getModelType() {
         return SpringImplementation.class;
     }

Modified: incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringInvoker.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringInvoker.java?view=diff&rev=544394&r1=544393&r2=544394
==============================================================================
--- incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringInvoker.java
(original)
+++ incubator/tuscany/sandbox/implementation-spring2/src/main/java/org/apache/tuscany/implementation/spring/SpringInvoker.java
Mon Jun  4 22:58:58 2007
@@ -1,38 +1,101 @@
 package org.apache.tuscany.implementation.spring;
 
 
-import javax.script.Invocable;
-import javax.script.ScriptException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 
-import org.apache.axiom.om.OMElement;
+import org.apache.tuscany.sca.core.invocation.ThreadMessageContext;
+import org.apache.tuscany.sca.interfacedef.ConversationSequence;
 import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil;
 import org.apache.tuscany.sca.invocation.Invoker;
 import org.apache.tuscany.sca.invocation.Message;
-
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.scope.InstanceWrapper;
+
+import org.apache.tuscany.implementation.spring.xml.SpringBeanElement;
+
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.beans.BeansException;
+
+/**
+ * Initial implementation of a Spring bean invoker
+ * @author MikeEdwards
+ *
+ */
 public class SpringInvoker implements Invoker {
 	
-	private SpringImplementationProvider provider;
-	private Operation operation;
+	private Method					theMethod;
+	private Object					bean;
+	private SpringBeanElement		beanElement;
+	private boolean					badInvoker = false;
 	
-    public SpringInvoker(SpringImplementationProvider provider, Operation operation) {
-        this.provider = provider;
+	/**
+	 * SpringInvoker constructor
+	 * @param component - the Spring component to invoke
+	 * @param service - the service to invoke
+	 * @param operation - the operation to invoke
+	 */
+    public SpringInvoker( RuntimeComponent component,
+    					  AbstractApplicationContext springContext,
+    					  RuntimeComponentService service, 
+    					  Operation operation) {
+
+        // From the component and the service, identify the Spring Bean which is the target
+        SpringImplementation theImplementation = (SpringImplementation) component.getImplementation();
+        beanElement = theImplementation.getBeanFromService( service.getService() );
         
-        // Use the Operation instead of the Operation name as it may be a dynamic
-        // operation so the name may change at runtime
-        this.operation = operation;
-    }
-
-    private Object doInvoke(Object[] objects) throws ScriptException {
-    	// TODO
-    	Object response = new Object();
-
-        return response;
-    }
-
+        if( beanElement == null ) {
+        	badInvoker = true;
+        	return;
+        }
+        // Now load the class for the bean and get the method relating to the operation....
+        try {
+        	bean = springContext.getBean( beanElement.getId() );
+            Class<?> beanClass = bean.getClass();
+            theMethod = JavaInterfaceUtil.findMethod( beanClass, operation );
+            System.out.println("SpringInvoker - found method " + theMethod.getName() );
+        } catch ( BeansException e ) {
+        	badInvoker = true;
+        } catch ( NoSuchMethodException e ) {
+        	badInvoker = true;
+        }
+    } // end constructor SpringInvoker
+
+    private Object doInvoke(Object payload) throws SpringInvocationException {
+    	if( badInvoker ) throw new SpringInvocationException("Spring invoker incorrectly configured");
+    	// Invoke the method on the Spring bean using the payload, returning the results
+        try {
+            Object ret;
+
+            if (payload != null && !payload.getClass().isArray()) {
+                ret = theMethod.invoke(bean, payload);
+            } else {
+                ret = theMethod.invoke(bean, (Object[])payload);
+            }
+            return ret;
+        } catch (InvocationTargetException e) {
+            throw new SpringInvocationException( e.getMessage() );
+        } catch (Exception e) {
+            throw new SpringInvocationException( e.getMessage() );
+        }
+
+    } // end method doInvoke
+
+    /**
+     * @param msg the message to invoke on the target bean
+     */
     public Message invoke(Message msg) {
-    	// TODO
+        try {
+            Object resp = doInvoke(msg.getBody());
+            msg.setBody(resp);
+        } catch (SpringInvocationException e) {
+            msg.setFaultBody(e.getCause());
+        }
+        System.out.println("Spring Invoker - invoke called");
         return msg;
-    }
+    } // end method invoke
 
 
-}
+} // end class SpringInvoker



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message