diff options
Diffstat (limited to 'sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript')
4 files changed, 451 insertions, 0 deletions
diff --git a/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubySCAConfig.java b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubySCAConfig.java new file mode 100644 index 0000000000..ce1dfc0323 --- /dev/null +++ b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubySCAConfig.java @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.ruby.rubyscript; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.model.Scope; +import org.jruby.RubyHash; +import org.jruby.RubyObject; +import org.jruby.internal.runtime.GlobalVariables; +import org.jruby.runtime.builtin.IRubyObject; + +/** + * Represents the variable defining the SCA aspects of the script + * <code> + * SCA = { + * javaInterface : "my.pkg.ClassName", + * wsdlPortType : "wsdlPortTypeName", + * wsdlNameSpace : "http://my.namespace.com", + * wsdlLocation : "\wsdl\mywsdl.txt", + * properties : { "foo" : ["java.lang.String", "defaultValue"],}, + * references : {}, + * scope : 'stateless'|'request'|'conversational'|'composite', + * } + * </code> + * The config must define the service with either javaInterface or wsdl. When + * using wsdl the three parameters are optional. If wsdlLocation is used that is the + * WSDL document used, and the namespace and portType parameters are only required if + * the WSDL definition defines multiple portTypes. + */ +public class RubySCAConfig { + + private boolean hasSCAConfig; + + private String javaInterface; + + private String wsdlLocation; + + private String wsdlNamespace; + + private String wsdlPortType; + + private Map properties; + + private Map references; + + private Scope scope; + + public RubySCAConfig(GlobalVariables globalVariables) { + IRubyObject rubyObject = globalVariables.get("$SCA"); + if (rubyObject != null && !rubyObject.isNil()) { + hasSCAConfig = true; + RubyHash scaVar = (RubyHash) rubyObject; + Object o = scaVar.get("javaInterface"); + if ( o != null ) { + this.javaInterface = o.toString(); + } + o = scaVar.get("wsdlLocation"); + if (o != null ) { + this.wsdlLocation = o.toString(); + } + o = scaVar.get("wsdlPortType"); + if (o != null ) { + this.wsdlPortType = o.toString(); + } + o = scaVar.get("wsdlNamespace"); + if (o != null ) { + this.wsdlNamespace = o.toString(); + } + if (javaInterface != null) { + if (wsdlLocation != null || wsdlPortType != null || wsdlNamespace != null) { + throw new IllegalArgumentException("script SCA config defines both Java and WSDL service interface"); + } + } else { + if (wsdlLocation == null && wsdlPortType == null && wsdlNamespace == null) { + throw new IllegalArgumentException("script SCA config must define either Java or WSDL service interface"); + } + } + + this.properties = new HashMap(); + o = scaVar.get("properties"); + if (o != null ) { + // TODO parse properties + } + + this.references = new HashMap(); + o = scaVar.get("references"); + if (o != null ) { + // TODO parse references + } + + o = scaVar.get("scope"); + if (o != null ) { + if ("stateless".equalsIgnoreCase(String.valueOf(o))) { + this.scope = Scope.STATELESS; + } else if ("request".equalsIgnoreCase(String.valueOf(o))) { + this.scope = Scope.REQUEST; + } else if ("conversational".equalsIgnoreCase(String.valueOf(o))) { + this.scope = Scope.SESSION; // TODO: where's CONVERSATIONAL? + } else if ("composite".equalsIgnoreCase(String.valueOf(o))) { + this.scope = Scope.COMPOSITE; // TODO: composite = MODULE for now? + } else { + throw new IllegalArgumentException("invalid scope value: " + o); + } + } + + } + } + + public boolean hasSCAConfig() { + return hasSCAConfig; + } + + public String getJavaInterface() { + return javaInterface; + } + + public Map getProperties() { + return properties; + } + + public Map getReferences() { + return references; + } + + public String getWSDLLocation() { + return wsdlLocation; + } + + public String getWSDLNamespace() { + return wsdlNamespace; + } + + public String getWSDLPortType() { + return wsdlPortType; + } + + public Scope getScope() { + return scope; + } + +} diff --git a/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScript.java b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScript.java new file mode 100644 index 0000000000..87d995eabd --- /dev/null +++ b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScript.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.ruby.rubyscript; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import org.jruby.IRuby; +import org.jruby.RubyString; +import org.jruby.javasupport.JavaEmbedUtils; +import org.jruby.javasupport.JavaUtil; +import org.jruby.runtime.builtin.IRubyObject; + +/** + * A RhinoScript represents a compiled JavaScript script + */ +public class RubyScript { + protected final String NEW = ".new"; + protected final String EQUAL = "="; + + protected String scriptName; + + protected String script; + + protected Map<String, Class> responseClasses; + + protected ClassLoader classLoader; + + private IRuby rubyEngine = JavaEmbedUtils.initialize(new Vector()); + + /** + * Create a new RubyScript. + * + * @param scriptName + * the name of the script. Can be anything, only used in messages to identify the script + * @param script + * the complete script + */ + public RubyScript(String scriptName, String script) { + this(scriptName, script, (Map) null, null); + } + + /** + * Create a new RubyScript. + * + * @param scriptName + * the name of the script. Can be anything, only used in messages to identify the script + * @param script + * the complete script + * @param context + * name-value pairs that are added in to the scope where the script is compiled. May be null. The value objects are made available to + * the script by using a variable with the name. + * @param classLoader + * the ClassLoader to be used to locate any user Java classes used in the script + */ + public RubyScript(String scriptName, String script, Map context, ClassLoader classLoader) { + this.scriptName = scriptName; + this.script = script; + this.responseClasses = new HashMap<String, Class>(); + this.classLoader = classLoader; + rubyEngine.loadScript((RubyString) JavaUtil.convertJavaToRuby(rubyEngine, + "MyScript.rb", + String.class), + (RubyString) JavaUtil.convertJavaToRuby(rubyEngine, + this.script, + String.class), + false); + } + + /** + * Create a new invokeable instance of the script + * + * @return a IRubyObject + */ +public RubyScriptInstance createScriptInstance(Map<String, Object> context, String rubyClassName) { + if ( rubyClassName == null ) { + return new RubyScriptInstance(rubyEngine.evalScript(script), responseClasses); + } + else { + IRubyObject rubyObject = rubyEngine.evalScript(rubyClassName + NEW); + + Iterator<String> keyIterator = context.keySet().iterator(); + String key = null; + Object value = null; + while ( keyIterator.hasNext()) { + key = keyIterator.next(); + value = JavaUtil.convertJavaToRuby(rubyEngine, + context.get(key), + context.get(key).getClass()); + + JavaEmbedUtils.invokeMethod(rubyEngine, + rubyObject, + key + EQUAL, + new Object[]{value}, null); + } + + return new RubyScriptInstance(rubyObject, responseClasses); + } + } + public String getScript() { + return script; + } + + public String getScriptName() { + return scriptName; + } + + public Map<String, Class> getResponseClasses() { + return responseClasses; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + /** + * Set the Java type of a response value. JavaScript is dynamically typed so Rhino cannot always work out what the intended Java type of a + * response should be, for example should the statement "return 42" be a Java int, or Integer or Double etc. When Rhino can't determine the type + * it will default to returning a String, using this method enables overriding the Rhino default to use a specific Java type. + */ + public void setResponseClass(String functionName, Class responseClasses) { + this.responseClasses.put(functionName, + responseClasses); + } + + public RubySCAConfig getSCAConfig() { + return new RubySCAConfig(rubyEngine.getGlobalVariables()); + } + + public void setScript(String script) { + this.script = script; + } + + public IRuby getRubyEngine() { + return rubyEngine; + } + + public void setRubyEngine(IRuby rubyEngine) { + this.rubyEngine = rubyEngine; + } + +} diff --git a/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScriptInstance.java b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScriptInstance.java new file mode 100644 index 0000000000..2d93c57fc9 --- /dev/null +++ b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyScriptInstance.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.ruby.rubyscript; + +import java.util.HashMap; +import java.util.Map; + +import org.jruby.javasupport.JavaEmbedUtils; +import org.jruby.runtime.builtin.IRubyObject; + +/** + * An invokeable instance of a JavaScript script. + */ +public class RubyScriptInstance { + + private IRubyObject rubyInstance; + + private Map<String, Class> responseClasses; + + public RubyScriptInstance(IRubyObject rubyInstance, Map<String, Class> responseClasses) { + this.rubyInstance = rubyInstance; + this.responseClasses = responseClasses; + if (this.responseClasses == null) { + this.responseClasses = new HashMap<String, Class>(); + } + } + + public Object invokeFunction(String functionName, Object[] args, Class returnType) { + Object[] rubyArgs = RubyUtils.fromJavaToRuby(rubyInstance.getRuntime(), args); + + Object rubyResponse = JavaEmbedUtils.invokeMethod(rubyInstance.getRuntime(), + rubyInstance, + functionName, + rubyArgs, + returnType); + Object response = RubyUtils.fromRubyToJava(rubyInstance.getRuntime(), + returnType, + rubyResponse); + return response; + } + + + + +} diff --git a/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyUtils.java b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyUtils.java new file mode 100644 index 0000000000..c19f233b94 --- /dev/null +++ b/sandbox/old/contrib/implementation-ruby/container/src/main/java/org/apache/tuscany/container/ruby/rubyscript/RubyUtils.java @@ -0,0 +1,71 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.ruby.rubyscript;
+
+import java.io.ByteArrayInputStream;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.util.StAXUtils;
+import org.jruby.IRuby;
+import org.jruby.RubyObject;
+import org.jruby.javasupport.JavaUtil;
+
+/**
+ * @author administrator
+ *
+ */
+public class RubyUtils {
+ public static Object fromRubyToJava(IRuby rubyEngine, Class reqArgType, Object rubyArg) {
+ Object javaArg = null;
+
+ //for known cases the JRuby runtime handles the conversion before calling the Java objects
+ //so nothing to do. When it cannot convert it simply passed the instance of RubyObject
+ if ( rubyArg instanceof RubyObject ) {
+ //need to deal with this
+ } else {
+ javaArg = rubyArg;
+ }
+
+ return javaArg;
+ }
+
+ public static Object[] fromJavaToRuby(IRuby rubyEngine, Object[] arg) {
+ Object[] jsArgs;
+ if (arg == null) {
+ jsArgs = new Object[0];
+ } else {
+ jsArgs = new Object[arg.length];
+ for (int i = 0; i < jsArgs.length; i++) {
+ jsArgs[i] = fromJavaToRuby(rubyEngine, arg[i]);
+ }
+ }
+
+ return jsArgs;
+ }
+
+ public static Object fromJavaToRuby(IRuby rubyEngine, Object javaObj) {
+ Object rubyObj = JavaUtil.convertJavaToRuby(rubyEngine, javaObj, javaObj.getClass());
+ return rubyObj;
+ }
+
+}
|