Author: antelder
Date: Tue Mar 28 04:24:10 2006
New Revision: 389487

URL: http://svn.apache.org/viewcvs?rev=389487&view=rev
Log:
Part 1 of first pass at E4X support  

Added:
    
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/E4XAXIOMUtils.java
    
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoE4XScript.java
    
incubator/tuscany/java/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.java
    
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.js
    
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/helloworld.wsdl
Modified:
    incubator/tuscany/java/sca/container.js/pom.xml
    
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoScript.java

Modified: incubator/tuscany/java/sca/container.js/pom.xml
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/pom.xml?rev=389487&r1=389486&r2=389487&view=diff
==============================================================================
--- incubator/tuscany/java/sca/container.js/pom.xml (original)
+++ incubator/tuscany/java/sca/container.js/pom.xml Tue Mar 28 04:24:10 2006
@@ -42,6 +42,20 @@
         </dependency>
 
         <dependency>
+            <groupId>org.apache.tuscany</groupId>
+            <artifactId>tuscany-binding-axis2</artifactId> <!-- Reqd for E4X 
-->
+            <version>${pom.version}</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>xmlbeans</groupId>
+            <artifactId>xbean</artifactId>  <!-- Reqd for E4X -->
+            <version>2.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>

Added: 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/E4XAXIOMUtils.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/E4XAXIOMUtils.java?rev=389487&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/E4XAXIOMUtils.java
 (added)
+++ 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/E4XAXIOMUtils.java
 Tue Mar 28 04:24:10 2006
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.js.rhino;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axis2.util.StreamWrapper;
+import org.apache.ws.commons.om.OMAbstractFactory;
+import org.apache.ws.commons.om.OMElement;
+import org.apache.ws.commons.om.OMFactory;
+import org.apache.ws.commons.om.impl.builder.StAXOMBuilder;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Wrapper;
+
+/**
+ * Utility methods for converting between AXIOM OMElements and E4X XML objects
+ */
+public class E4XAXIOMUtils {
+
+    /**
+     * Get an AXIOM OMElement from E4X XML
+     */
+    public static OMElement toOMElement(Scriptable jsXML) {
+
+        XmlObject xmlObject = toXmlObject(jsXML);
+
+        XMLStreamReader reader = xmlObject.newXMLStreamReader();
+        StreamWrapper stream = new StreamWrapper(reader);
+        OMFactory factory = OMAbstractFactory.getOMFactory();
+        StAXOMBuilder builder = new StAXOMBuilder(factory, stream);
+        OMElement omElement = builder.getDocumentElement();
+
+        // TODO: is this required anymore?
+        // // TODO: remove when AXIS2 fixed (its in the gen'd stub code, JIRA?)
+        // omElement.build();
+        // OptimizeExposer.optimizeContent(omElement, new QName[0]);
+
+        return omElement;
+    }
+
+    /**
+     * Get an E4X XML object from an AXIOM OMElement
+     */
+    public static Scriptable toScriptableObject(OMElement omElement, 
Scriptable scope) throws XmlException {
+
+        // TODO: is this required anymore?
+        // // TODO: remove when AXIS2 fixed (its in the gen'd stub code, JIRA?)
+        // omElement.build();
+
+        // XMLStreamReader reader = 
omElement.getXMLStreamReaderWithoutCaching();
+        XMLStreamReader reader = omElement.getXMLStreamReader(); // TODO: 
cache?
+        XmlObject xmlObject = XmlObject.Factory.parse(reader);
+
+        Context cx = Context.enter();
+        try {
+
+            Object jsSOAPEnvelope = cx.getWrapFactory().wrap(cx, scope, 
xmlObject, XmlObject.class);
+            Scriptable jsXML = cx.newObject(scope, "XML", new Object[] { 
jsSOAPEnvelope });
+
+            return jsXML;
+
+        } finally {
+            Context.exit();
+        }
+    }
+
+    /**
+     * Get an XMLBeans XmlObject from E4X XML
+     */
+    private static XmlObject toXmlObject(Scriptable jsXML) {
+        // TODO: E4X Bug? Shouldn't need this copy, but without it the outer 
element gets lost???
+        jsXML = (Scriptable) ScriptableObject.callMethod(jsXML, "copy", new 
Object[0]);
+
+        Wrapper wrapper = (Wrapper) ScriptableObject.callMethod(jsXML, 
"getXmlObject", new Object[0]);
+        XmlObject xmlObject = (XmlObject) wrapper.unwrap();
+        return xmlObject;
+    }
+
+}

Added: 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoE4XScript.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoE4XScript.java?rev=389487&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoE4XScript.java
 (added)
+++ 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoE4XScript.java
 Tue Mar 28 04:24:10 2006
@@ -0,0 +1,70 @@
+package org.apache.tuscany.container.js.rhino;
+
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.binding.axis2.util.AxiomHelper;
+import org.apache.ws.commons.om.OMElement;
+import org.apache.xmlbeans.XmlException;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.xml.XMLObject;
+import org.osoa.sca.ServiceRuntimeException;
+
+import commonj.sdo.helper.TypeHelper;
+
+/**
+ * Invokes a JavaScript/E4X function with argument and return values that may 
be E4X XML objects. When calling the script from Java request arguments
+ * that are AXIOM OMElements are converted to E4X XML objects. If the response 
from the script is an E4X XML object it is converted to an AXIOM
+ * OMElement.
+ */
+public class RhinoE4XScript extends RhinoScript {
+
+    private TypeHelper typeHelper;
+
+    private String serviceNS = "http://helloworld.samples.tuscany.apache.org";; 
// TODO can't hardcode this!
+
+    public RhinoE4XScript(String scriptName, String script, Map context, 
ClassLoader cl, TypeHelper typeHelper) {
+        super(scriptName, script, context, cl);
+        this.typeHelper = typeHelper;
+    }
+
+    protected RhinoE4XScript(String scriptName, String script, Scriptable 
scriptScope, TypeHelper typeHelper) {
+        super(scriptName, script, scriptScope);
+        this.typeHelper = typeHelper;
+    }
+
+    /**
+     * Turn args to JS objects and convert any OMElement to E4X XML
+     */
+    @Override
+    protected Object[] processArgs(String functionName, Object arg, Scriptable 
scope) {
+        QName operationQN = new QName(serviceNS, functionName);
+        OMElement om = AxiomHelper.toOMElement(typeHelper, (Object[]) arg, 
operationQN);
+        try {
+            return new Object[] { E4XAXIOMUtils.toScriptableObject(om, scope) 
};
+        } catch (XmlException e) {
+            throw new ServiceRuntimeException(e);
+        }
+    }
+
+    /**
+     * Unwrap and convert response
+     */
+    @Override
+    protected Object processResponse(Object response, Class responseClass) {
+        if (response instanceof XMLObject) {
+            OMElement om = E4XAXIOMUtils.toOMElement((XMLObject) response);
+            Object[] resp = AxiomHelper.toObjects(typeHelper, om);
+            return resp[0];
+        } else {
+            return super.processResponse(response, responseClass);
+        }
+    }
+
+    @Override
+    public RhinoE4XScript copy() {
+        return new RhinoE4XScript(scriptName, script, scriptScope, typeHelper);
+    }
+
+}

Modified: 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoScript.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoScript.java?rev=389487&r1=389486&r2=389487&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoScript.java
 (original)
+++ 
incubator/tuscany/java/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoScript.java
 Tue Mar 28 04:24:10 2006
@@ -32,11 +32,11 @@
  */
 public class RhinoScript {
 
-    private String scriptName;
+    protected String scriptName;
 
-    private String script;
+    protected String script;
 
-    private Scriptable scriptScope;
+    protected Scriptable scriptScope;
 
     private Scriptable sharedScope;
 
@@ -132,7 +132,7 @@
         try {
             Function function = getFunction(scriptScope, functionName);
             Scriptable invocationScope = getInvocationScope(cx, contexts);
-            Object[] args = processArgs(arg, invocationScope);
+            Object[] args = processArgs(functionName, arg, invocationScope);
             Object jsResponse = function.call(cx, invocationScope, 
invocationScope, args);
             Object response = processResponse(jsResponse, responseClass);
             return response;
@@ -144,17 +144,7 @@
     /**
      * Turn args to JS objects and convert any OMElement to E4X XML
      */
-    protected Object[] processArgs(Object arg, Scriptable scope) {
-        // TODO: implement pluggable way to transform objects (eg SDO or 
AXIOM) to E4X XML objects
-        // if (arg instanceof OMElement) {
-        // try {
-        // arg = E4XAXIOMUtils.toScriptableObject((OMElement) arg, scope);
-        // } catch (XmlException e) {
-        // throw new RuntimeException(e);
-        // }
-        // } else if (arg instanceof MessageContext) {
-        // arg = new E4XMessageContext((MessageContext) arg, scope);
-        // }
+    protected Object[] processArgs(String functionName, Object arg, Scriptable 
scope) {
         Object[] args;
         if (arg == null) {
             args = new Object[] { null };
@@ -173,18 +163,15 @@
      * Unwrap and convert response
      */
     protected Object processResponse(Object response, Class responseClass) {
-        // TODO: implement pluggable way to transform E4X XML into specific 
objects (eg SDO or AXIOM)
-        // } else if (response instanceof XMLObject) {
-        // response = E4XAXIOMUtils.toOMElement((XMLObject) response);
         if (Context.getUndefinedValue().equals(response)) {
             response = null;
         } else if (response instanceof Wrapper) {
             response = ((Wrapper) response).unwrap();
         } else {
             if (responseClass != null) {
-                response = Context.toType(response, responseClass);
+                response = Context.jsToJava(response, responseClass);
             } else {
-                response = Context.toType(response, String.class);
+                response = Context.jsToJava(response, String.class);
             }
         }
         return response;

Added: 
incubator/tuscany/java/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.java?rev=389487&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.java
 (added)
+++ 
incubator/tuscany/java/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.java
 Tue Mar 28 04:24:10 2006
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.js.rhino;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.helper.XSDHelperImpl;
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.XSDHelper;
+
+/**
+ * Tests for the RhinoE4XScript
+ */
+public class RhinoE4XScriptTestCase extends TestCase {
+
+    private static final String scriptName = "RhinoE4XScriptTestCase.js";
+
+    private String script;
+
+    private TypeHelper th;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        this.script = readResource(scriptName);
+        this.th = SDOUtil.createTypeHelper();
+        XSDHelper xsdHelper = new XSDHelperImpl(th);
+        URL url = getClass().getResource("helloworld.wsdl");
+        xsdHelper.define(url.openStream(), null);
+    }
+
+    public void testSimpleInvocation() throws IOException {
+        RhinoE4XScript ri = new RhinoE4XScript(scriptName, script, null, null, 
th);
+        Object x = ri.invoke("getGreetings", new Object[] { "petra" }, null);
+        assertEquals(x, "hello petra");
+    }
+
+    /**
+     * Read a resource into a String
+     */
+    private String readResource(String name) {
+        try {
+            URL url = getClass().getResource(name);
+            if (url == null) {
+                throw new RuntimeException("resource not found: " + name);
+            }
+            InputStream inputStream = url.openStream();
+
+            StringBuffer resource = new StringBuffer();
+            int n = 0;
+
+            while ((n = inputStream.read()) != -1) {
+                resource.append((char) n);
+            }
+
+            inputStream.close();
+
+            String s = resource.toString();
+            return s;
+
+        } catch (IOException e) {
+            throw new RuntimeException("IOException reading resource " + name, 
e);
+        }
+    }
+
+}
\ No newline at end of file

Added: 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.js
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.js?rev=389487&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.js
 (added)
+++ 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/RhinoE4XScriptTestCase.js
 Tue Mar 28 04:24:10 2006
@@ -0,0 +1,11 @@
+
+function getGreetings(inXML) {
+
+   var greeting = "hello " + inXML..*::in0;
+   var outXML = 
+      <helloworldaxis:getGreetingsResponse 
xmlns:helloworldaxis="http://helloworld.samples.tuscany.apache.org";>
+         <helloworldaxis:getGreetingsReturn>{ greeting 
}</helloworldaxis:getGreetingsReturn>
+      </helloworldaxis:getGreetingsResponse>;
+
+   return outXML;
+}

Added: 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/helloworld.wsdl
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/helloworld.wsdl?rev=389487&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/helloworld.wsdl
 (added)
+++ 
incubator/tuscany/java/sca/container.js/src/test/resources/org/apache/tuscany/container/js/rhino/helloworld.wsdl
 Tue Mar 28 04:24:10 2006
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright (c) 2005 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.
+ -->
+<wsdl:definitions 
targetNamespace="http://helloworld.samples.tuscany.apache.org"; 
xmlns:apachesoap="http://xml.apache.org/xml-soap"; 
xmlns:impl="http://helloworld.samples.tuscany.apache.org"; 
xmlns:intf="http://helloworld.samples.tuscany.apache.org"; 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"; 
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"; 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"; name="helloworld">
+    <!--WSDL created by Apache Axis version: 1.2.1
+Built on Jun 14, 2005 (09:15:57 EDT)-->
+    <wsdl:types>
+        <schema elementFormDefault="qualified" 
targetNamespace="http://helloworld.samples.tuscany.apache.org"; 
xmlns="http://www.w3.org/2001/XMLSchema";>
+            <element name="getGreetings">
+                <complexType>
+                    <sequence>
+                        <element name="in0" type="xsd:string"/>
+                    </sequence>
+                </complexType>
+            </element>
+            <element name="getGreetingsResponse">
+                <complexType>
+                    <sequence>
+                        <element name="getGreetingsReturn" type="xsd:string"/>
+                    </sequence>
+                </complexType>
+            </element>
+        </schema>
+    </wsdl:types>
+
+    <wsdl:message name="getGreetingsRequest">
+
+        <wsdl:part element="impl:getGreetings" name="parameters"/>
+
+    </wsdl:message>
+
+    <wsdl:message name="getGreetingsResponse">
+
+        <wsdl:part element="impl:getGreetingsResponse" name="parameters"/>
+
+    </wsdl:message>
+
+    <wsdl:portType name="HelloWorldServiceImpl">
+
+        <wsdl:operation name="getGreetings">
+
+            <wsdl:input message="impl:getGreetingsRequest" 
name="getGreetingsRequest"/>
+
+            <wsdl:output message="impl:getGreetingsResponse" 
name="getGreetingsResponse"/>
+
+        </wsdl:operation>
+
+    </wsdl:portType>
+
+    <wsdl:binding name="helloworldSoapBinding" 
type="impl:HelloWorldServiceImpl">
+
+        <wsdlsoap:binding style="document" 
transport="http://schemas.xmlsoap.org/soap/http"/>
+
+        <wsdl:operation name="getGreetings">
+
+            <wsdlsoap:operation soapAction=""/>
+
+            <wsdl:input name="getGreetingsRequest">
+
+                <wsdlsoap:body use="literal"/>
+
+            </wsdl:input>
+
+            <wsdl:output name="getGreetingsResponse">
+
+                <wsdlsoap:body use="literal"/>
+
+            </wsdl:output>
+
+        </wsdl:operation>
+
+    </wsdl:binding>
+
+    <wsdl:service name="HelloWorldServiceImplService">
+
+        <wsdl:port binding="impl:helloworldSoapBinding" name="helloworld">
+
+            <wsdlsoap:address 
location="http://localhost:8080/helloworldws-SNAPSHOT/services/HelloWorldService"/>
+
+        </wsdl:port>
+
+    </wsdl:service>
+
+</wsdl:definitions>


Reply via email to