Author: jboynes
Date: Thu Jul 20 08:26:59 2006
New Revision: 423955

URL: http://svn.apache.org/viewvc?rev=423955&view=rev
Log:
refactor groovy component to create classloader at build time rather than with 
each instance

Modified:
    
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java
    
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java
    
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/PropertyTestCase.java
    
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ScriptInvokeTestCase.java
    
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/WireTestCase.java

Modified: 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java?rev=423955&r1=423954&r2=423955&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java
 (original)
+++ 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java
 Thu Jul 20 08:26:59 2006
@@ -33,19 +33,18 @@
 import org.apache.tuscany.spi.wire.OutboundWire;
 import org.apache.tuscany.spi.wire.TargetInvoker;
 import org.apache.tuscany.spi.wire.WireService;
-import org.codehaus.groovy.control.CompilationFailedException;
 
 /**
  * The Groovy atomic component implementation. Groovy implementations may be 
"scripts" or classes.
  */
 public class GroovyAtomicComponent<T> extends AtomicComponentExtension<T> {
+    private final Class<? extends GroovyObject> groovyClass;
 
-    private String script;
     private List<Class<?>> services;
     private List<PropertyInjector> injectors;
 
     public GroovyAtomicComponent(String name,
-                                 String script,
+                                 Class<? extends GroovyObject> groovyClass,
                                  List<Class<?>>services,
                                  Scope scope,
                                  List<PropertyInjector> injectors,
@@ -53,18 +52,15 @@
                                  ScopeContainer scopeContainer,
                                  WireService wireService) {
         super(name, parent, scopeContainer, wireService, 0);
-        this.script = script;
-        this.services = services;
+        this.groovyClass = groovyClass;
+        this.services = Collections.unmodifiableList(services);
         this.scope = scope;
         this.injectors = (injectors != null) ? injectors : new 
ArrayList<PropertyInjector>();
-    }
 
-    public String getScript() {
-        return script;
     }
 
     public List<Class<?>> getServiceInterfaces() {
-        return Collections.unmodifiableList(services);
+        return services;
     }
 
     public TargetInvoker createTargetInvoker(String serviceName, Method 
method) {
@@ -72,29 +68,27 @@
     }
 
     public Object createInstance() throws ObjectCreationException {
+        GroovyObject instance;
         try {
-            ClassLoader parent = getClass().getClassLoader();
-            GroovyClassLoader loader = new GroovyClassLoader(parent);
-            Class groovyClass = loader.parseClass(script);
-            GroovyObject object = (GroovyObject) groovyClass.newInstance();
-            // inject properties
-            for (PropertyInjector injector : injectors) {
-                injector.inject(object);
-            }
-            // inject wires
-            for (List<OutboundWire> referenceWires : 
getOutboundWires().values()) {
-                for (OutboundWire<?> wire : referenceWires) {
-                    object.setProperty(wire.getReferenceName(), 
wireService.createProxy(wire));
-                }
-            }
-            return object;
-        } catch (CompilationFailedException e) {
-            throw new ObjectCreationException(e);
+            instance = groovyClass.newInstance();
         } catch (IllegalAccessException e) {
             throw new ObjectCreationException(e);
         } catch (InstantiationException e) {
             throw new ObjectCreationException(e);
         }
+
+        // inject properties
+        for (PropertyInjector injector : injectors) {
+            injector.inject(instance);
+        }
+
+        // inject wires
+        for (List<OutboundWire> referenceWires : getOutboundWires().values()) {
+            for (OutboundWire<?> wire : referenceWires) {
+                instance.setProperty(wire.getReferenceName(), 
wireService.createProxy(wire));
+            }
+        }
+        return instance;
     }
 
     @SuppressWarnings("unchecked")
@@ -116,13 +110,5 @@
             throw e;
         }
         return wireService.createProxy(wire);
-    }
-
-    public void init(Object instance) throws TargetException {
-        //TODO implement - this should call some kind of init method
-    }
-
-    public void destroy(Object instance) throws TargetException {
-        //TODO implement - this should call some kind of destroy method
     }
 }

Modified: 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java?rev=423955&r1=423954&r2=423955&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java
 (original)
+++ 
incubator/tuscany/java/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java
 Thu Jul 20 08:26:59 2006
@@ -4,6 +4,9 @@
 import java.util.Collection;
 import java.util.List;
 
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyObject;
+
 import org.apache.tuscany.spi.builder.BuilderConfigException;
 import org.apache.tuscany.spi.component.Component;
 import org.apache.tuscany.spi.component.CompositeComponent;
@@ -13,6 +16,8 @@
 import org.apache.tuscany.spi.model.Scope;
 import org.apache.tuscany.spi.model.ServiceDefinition;
 
+import org.codehaus.groovy.control.CompilationFailedException;
+
 /**
  * Extension point for creating [EMAIL PROTECTED] GroovyAtomicComponent}s from 
an assembly configuration
  *
@@ -37,14 +42,26 @@
         String script = implementation.getScript();
         String name = componentDefinition.getName();
         Scope scope = implementation.getComponentType().getLifecycleScope();
+
+        ClassLoader parentCL = deploymentContext.getClassLoader();
+        GroovyClassLoader loader = new GroovyClassLoader(parentCL);
+        Class<? extends GroovyObject> groovyClass;
+        try {
+            groovyClass = loader.parseClass(script);
+        } catch (CompilationFailedException e) {
+            BuilderConfigException bce = new BuilderConfigException(e);
+            bce.setIdentifier(name);
+            throw bce;
+        }
+
         return new GroovyAtomicComponent(name,
-                script,
-                services,
-                scope,
-                null,
-                parent,
-                deploymentContext.getModuleScope(),
-                wireService);
+                                         groovyClass,
+                                         services,
+                                         scope,
+                                         null,
+                                         parent,
+                                         deploymentContext.getModuleScope(),
+                                         wireService);
     }
 
 }

Modified: 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/PropertyTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/PropertyTestCase.java?rev=423955&r1=423954&r2=423955&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/PropertyTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/PropertyTestCase.java
 Thu Jul 20 08:26:59 2006
@@ -12,6 +12,9 @@
 import org.apache.tuscany.spi.wire.WireService;
 import org.apache.tuscany.test.ArtifactFactory;
 
+import groovy.lang.GroovyObject;
+import groovy.lang.GroovyClassLoader;
+
 /**
  * @version $$Rev$$ $$Date$$
  */
@@ -25,6 +28,8 @@
             + "   }"
             + "}";
 
+    private Class<? extends GroovyObject> implClass;
+
     /**
      * Tests injecting a simple property type on a Groovy implementation 
instance
      */
@@ -37,7 +42,7 @@
         injectors.add(new SingletonInjector("property", "bar"));
         WireService wireService = ArtifactFactory.createWireService();
         GroovyAtomicComponent<Greeting> context = new 
GroovyAtomicComponent<Greeting>("source",
-                                                                               
       PropertyTestCase.SCRIPT,
+                                                                               
       implClass,
                                                                                
       services,
                                                                                
       Scope.MODULE,
                                                                                
       injectors,
@@ -50,5 +55,9 @@
         scope.stop();
     }
 
-
+    protected void setUp() throws Exception {
+        super.setUp();
+        GroovyClassLoader cl = new 
GroovyClassLoader(getClass().getClassLoader());
+        implClass = cl.parseClass(SCRIPT);
+    }
 }

Modified: 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ScriptInvokeTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ScriptInvokeTestCase.java?rev=423955&r1=423954&r2=423955&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ScriptInvokeTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ScriptInvokeTestCase.java
 Thu Jul 20 08:26:59 2006
@@ -4,6 +4,7 @@
 import java.util.List;
 
 import groovy.lang.GroovyObject;
+import groovy.lang.GroovyClassLoader;
 import junit.framework.TestCase;
 
 import org.apache.tuscany.container.groovy.mock.Greeting;
@@ -16,7 +17,8 @@
  */
 public class ScriptInvokeTestCase extends TestCase {
 
-    private String script2 = "def greet(name) { return name }";
+    private static final String SCRIPT = "def greet(name) { return name }";
+    private Class<? extends GroovyObject> implClass;
 
     /**
      * Tests the invocation of a Groovy "script" as opposed to a class
@@ -26,12 +28,24 @@
         scope.start();
         List<Class<?>> services = new ArrayList<Class<?>>();
         services.add(Greeting.class);
-        GroovyAtomicComponent<GroovyObject> context = new 
GroovyAtomicComponent<GroovyObject>("source", script2,
-                                                                               
               services, Scope.MODULE, null, null, scope, 
ArtifactFactory.createWireService());
+        GroovyAtomicComponent<GroovyObject> context =
+                new GroovyAtomicComponent<GroovyObject>("source",
+                                                        implClass,
+                                                        services,
+                                                        Scope.MODULE,
+                                                        null,
+                                                        null,
+                                                        scope,
+                                                        
ArtifactFactory.createWireService());
         scope.register(context);
         GroovyObject object = context.getServiceInstance();
         assertEquals("foo", object.invokeMethod("greet", "foo"));
         scope.stop();
     }
 
+    protected void setUp() throws Exception {
+        super.setUp();
+        GroovyClassLoader cl = new 
GroovyClassLoader(getClass().getClassLoader());
+        implClass = cl.parseClass(SCRIPT);
+    }
 }

Modified: 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/WireTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/WireTestCase.java?rev=423955&r1=423954&r2=423955&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/WireTestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/WireTestCase.java
 Thu Jul 20 08:26:59 2006
@@ -19,6 +19,9 @@
 import org.apache.tuscany.spi.wire.TargetInvoker;
 import org.apache.tuscany.test.ArtifactFactory;
 
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyObject;
+
 /**
  * @version $$Rev$$ $$Date$$
  */
@@ -44,6 +47,9 @@
             + "   }"
             + "}";
 
+    private Class<? extends GroovyObject> implClass1;
+    private Class<? extends GroovyObject> implClass2;
+
     /**
      * Tests a basic invocation down a source wire
      */
@@ -53,8 +59,15 @@
 
         List<Class<?>> services = new ArrayList<Class<?>>();
         services.add(Greeting.class);
-        GroovyAtomicComponent<Greeting> context = new 
GroovyAtomicComponent<Greeting>("source", SCRIPT,
-                                                                               
       services, Scope.MODULE, null, null, scope, 
ArtifactFactory.createWireService());
+        GroovyAtomicComponent<Greeting> context =
+                new GroovyAtomicComponent<Greeting>("source",
+                                                    implClass1,
+                                                    services,
+                                                    Scope.MODULE,
+                                                    null,
+                                                    null,
+                                                    scope,
+                                                    
ArtifactFactory.createWireService());
         OutboundWire<?> wire = ArtifactFactory.createOutboundWire("wire", 
Greeting.class);
         ArtifactFactory.terminateWire(wire);
 
@@ -104,8 +117,15 @@
         scope.start();
         List<Class<?>> services = new ArrayList<Class<?>>();
         services.add(Greeting.class);
-        GroovyAtomicComponent<Greeting> context = new 
GroovyAtomicComponent<Greeting>("source", SCRIPT2, services,
-                                                                               
       Scope.MODULE, null, null, scope, ArtifactFactory.createWireService());
+        GroovyAtomicComponent<Greeting> context =
+                new GroovyAtomicComponent<Greeting>("source",
+                                                    implClass2,
+                                                    services,
+                                                    Scope.MODULE,
+                                                    null,
+                                                    null,
+                                                    scope,
+                                                    
ArtifactFactory.createWireService());
         scope.register(context);
         TargetInvoker invoker =
                 context.createTargetInvoker("greeting", 
Greeting.class.getMethod("greet", String.class));
@@ -122,8 +142,15 @@
         scope.start();
         List<Class<?>> services = new ArrayList<Class<?>>();
         services.add(Greeting.class);
-        GroovyAtomicComponent<Greeting> context = new 
GroovyAtomicComponent<Greeting>("source", SCRIPT2,
-                                                                               
       services, Scope.MODULE, null, null, scope, 
ArtifactFactory.createWireService());
+        GroovyAtomicComponent<Greeting> context =
+                new GroovyAtomicComponent<Greeting>("source",
+                                                    implClass2,
+                                                    services,
+                                                    Scope.MODULE,
+                                                    null,
+                                                    null,
+                                                    scope,
+                                                    
ArtifactFactory.createWireService());
         scope.register(context);
 
         InboundWire<?> wire = ArtifactFactory.createInboundWire("Greeting", 
Greeting.class);
@@ -137,4 +164,10 @@
         scope.stop();
     }
 
+    protected void setUp() throws Exception {
+        super.setUp();
+        GroovyClassLoader cl = new 
GroovyClassLoader(getClass().getClassLoader());
+        implClass1 = cl.parseClass(SCRIPT);
+        implClass2 = cl.parseClass(SCRIPT2);
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to