Author: jboynes
Date: Wed Jul 19 08:15:37 2006
New Revision: 423486

URL: http://svn.apache.org/viewvc?rev=423486&view=rev
Log:
add loader for groovy implementation

Added:
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
   (with props)
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
   (with props)
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/
    
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/TestScript.groovy
Modified:
    incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/pom.xml

Modified: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/pom.xml?rev=423486&r1=423485&r2=423486&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/pom.xml 
(original)
+++ incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/pom.xml 
Wed Jul 19 08:15:37 2006
@@ -64,6 +64,10 @@
             <groupId>org.easymock</groupId>
             <artifactId>easymock</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymockclassextension</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

Added: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java?rev=423486&view=auto
==============================================================================
--- 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
 (added)
+++ 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
 Wed Jul 19 08:15:37 2006
@@ -0,0 +1,106 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.container.groovy;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.extension.LoaderExtension;
+import org.apache.tuscany.spi.loader.LoaderException;
+import org.apache.tuscany.spi.loader.LoaderRegistry;
+import org.apache.tuscany.spi.loader.LoaderUtil;
+import org.apache.tuscany.spi.loader.MissingResourceException;
+
+/**
+ * Loader for handling <groovy:implementation> elements.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ImplementationLoader extends 
LoaderExtension<GroovyImplementation> {
+    private static final QName IMPLEMENTATION_GROOVY =
+            new QName("http://tuscany.apache.org/xmlns/groovy/1.0";, 
"implementation");
+
+    public ImplementationLoader(LoaderRegistry registry) {
+        super(registry);
+    }
+
+    public QName getXMLType() {
+        return IMPLEMENTATION_GROOVY;
+    }
+
+    public GroovyImplementation load(CompositeComponent parent,
+                                     XMLStreamReader reader,
+                                     DeploymentContext deploymentContext)
+        throws XMLStreamException, LoaderException {
+
+        String script = reader.getAttributeValue(null, "script");
+        if (script == null) {
+            throw new MissingResourceException("No script supplied");
+        }
+        String source = loadSource(deploymentContext.getClassLoader(), script);
+
+        LoaderUtil.skipToEndElement(reader);
+
+        GroovyImplementation implementation = new GroovyImplementation();
+        implementation.setScript(source);
+        registry.loadComponentType(parent, implementation, deploymentContext);
+        return implementation;
+    }
+
+    protected String loadSource(ClassLoader cl, String resource) throws 
LoaderException {
+        URL url = cl.getResource(resource);
+        if (url == null) {
+            throw new MissingResourceException(resource);
+        }
+        InputStream is;
+        try {
+            is = url.openStream();
+        } catch (IOException e) {
+            MissingResourceException mre = new 
MissingResourceException(resource, e);
+            mre.setIdentifier(resource);
+            throw mre;
+        }
+        try {
+            Reader reader = new InputStreamReader(is, "UTF-8");
+            char[] buffer = new char[1024];
+            StringBuilder source = new StringBuilder();
+            int count;
+            while ((count = reader.read(buffer)) > 0) {
+                source.append(buffer, 0, count);
+            }
+            return source.toString();
+        } catch (IOException e) {
+            LoaderException le = new LoaderException(e);
+            le.setIdentifier(resource);
+            throw le;
+        } finally {
+            try {
+                is.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+}

Propchange: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java?rev=423486&view=auto
==============================================================================
--- 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
 (added)
+++ 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
 Wed Jul 19 08:15:37 2006
@@ -0,0 +1,97 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.container.groovy;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+
+import junit.framework.TestCase;
+import static org.easymock.classextension.EasyMock.*;
+
+import org.apache.tuscany.spi.loader.LoaderRegistry;
+import org.apache.tuscany.spi.loader.LoaderException;
+import org.apache.tuscany.spi.loader.MissingResourceException;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.component.CompositeComponent;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ImplementationLoaderTestCase extends TestCase {
+    private CompositeComponent parent;
+    private XMLStreamReader reader;
+    private DeploymentContext deploymentContext;
+    private ClassLoader classLoader;
+    private LoaderRegistry registry;
+    private ImplementationLoader loader;
+
+    public void testNoScriptAttribute() throws LoaderException, 
XMLStreamException {
+        expect(reader.getAttributeValue(null, "script")).andReturn(null);
+        replay(reader);
+        replay(deploymentContext);
+
+        try {
+            loader.load(parent, reader, deploymentContext);
+            fail();
+        } catch (MissingResourceException e) {
+            // ok
+        }
+        verify(reader);
+        verify(deploymentContext);
+    }
+
+    public void testNoScriptPresent() throws LoaderException, 
XMLStreamException {
+        expect(reader.getAttributeValue(null, 
"script")).andReturn("foo.groovy");
+        expect(deploymentContext.getClassLoader()).andReturn(classLoader);
+
+        replay(reader);
+        replay(deploymentContext);
+
+        ImplementationLoader mockLoader = new ImplementationLoader(registry) {
+            protected String loadSource(ClassLoader cl, String resource) 
throws LoaderException {
+                assertSame(classLoader, cl);
+                assertEquals("foo.groovy", resource);
+                throw new MissingResourceException(resource);
+            }
+        };
+        try {
+            mockLoader.load(parent, reader, deploymentContext);
+            fail();
+        } catch (MissingResourceException e) {
+            assertEquals("foo.groovy", e.getMessage());
+        }
+        verify(reader);
+        verify(deploymentContext);
+    }
+
+    public void testLoadScript() throws LoaderException {
+        String script = loader.loadSource(getClass().getClassLoader(),
+                                          
"org/apache/tuscany/container/groovy/mock/TestScript.groovy");
+        assertEquals("Test Script", script);
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        registry = createMock(LoaderRegistry.class);
+        loader = new ImplementationLoader(registry);
+
+        parent = createMock(CompositeComponent.class);
+        reader = createMock(XMLStreamReader.class);
+        deploymentContext = createMock(DeploymentContext.class);
+        classLoader = createMock(ClassLoader.class);
+    }
+}

Propchange: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/java/org/apache/tuscany/container/groovy/ImplementationLoaderTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/TestScript.groovy
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/TestScript.groovy?rev=423486&view=auto
==============================================================================
--- 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/TestScript.groovy
 (added)
+++ 
incubator/tuscany/sandbox/chianti/sca/containers/container.groovy/src/test/resources/org/apache/tuscany/container/groovy/mock/TestScript.groovy
 Wed Jul 19 08:15:37 2006
@@ -0,0 +1 @@
+Test Script
\ No newline at end of file



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

Reply via email to