summaryrefslogtreecommitdiffstats
path: root/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby')
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/assembly/RubyImplementation.java64
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyContextFactoryBuilder.java39
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyTargetWireBuilder.java47
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/config/RubyContextFactory.java55
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/context/RubyComponentContext.java56
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyInvoker.java108
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java101
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/loader/RubyImplementationLoader.java97
8 files changed, 567 insertions, 0 deletions
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/assembly/RubyImplementation.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/assembly/RubyImplementation.java
new file mode 100644
index 0000000000..4fd6652495
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/assembly/RubyImplementation.java
@@ -0,0 +1,64 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.assembly;
+
+import org.apache.tuscany.common.resource.ResourceLoader;
+import org.apache.tuscany.model.assembly.impl.AtomicImplementationImpl;
+
+/**
+ * Meta-information for the Ruby implementation.
+ *
+ */
+public class RubyImplementation extends AtomicImplementationImpl {
+
+ // Full path of the script file to be executed
+ private String script;
+
+ // Resource loader for accessing classpath resources
+ private ResourceLoader resourceLoader;
+
+ /**
+ * Gets the full path of the script file to be executed.
+ * @return Full path of the script file to be executed.
+ */
+ public String getScript() {
+ return script;
+ }
+
+ /**
+ * Sets the full path of the script file to be executed.
+ * @param script Full path of the script file to be executed.
+ */
+ public void setScript(String script) {
+ this.script = script;
+ }
+
+ /**
+ * Gets the resource loader for accessing classpath resources.
+ * @return Resource loader.
+ */
+ public ResourceLoader getResourceLoader() { return resourceLoader; }
+
+ /**
+ * Sets the resource loader for accessing classpath resources.
+ * @param resourceLoader Resource loader.
+ */
+ public void setResourceLoader(ResourceLoader resourceLoader) {
+ this.resourceLoader = resourceLoader;
+ }
+
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyContextFactoryBuilder.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyContextFactoryBuilder.java
new file mode 100644
index 0000000000..77f202f5d3
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyContextFactoryBuilder.java
@@ -0,0 +1,39 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.builder;
+
+import org.apache.tuscany.container.ruby.assembly.RubyImplementation;
+import org.apache.tuscany.container.ruby.config.RubyContextFactory;
+import org.apache.tuscany.container.ruby.invoker.RubyScript;
+import org.apache.tuscany.core.builder.ContextFactory;
+import org.apache.tuscany.core.extension.ContextFactoryBuilderSupport;
+import org.apache.tuscany.model.assembly.Scope;
+
+/**
+ * Ruby context factory builder.
+ *
+ */
+@org.osoa.sca.annotations.Scope("MODULE")
+public class RubyContextFactoryBuilder extends ContextFactoryBuilderSupport<RubyImplementation> {
+
+ @Override
+ protected ContextFactory createContextFactory(String componentName, RubyImplementation implementation, Scope scope) {
+ RubyScript rubyScript = new RubyScript(implementation.getScript());
+ return new RubyContextFactory(componentName, scope, rubyScript);
+ }
+
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyTargetWireBuilder.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyTargetWireBuilder.java
new file mode 100644
index 0000000000..4f7fa6cb03
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/builder/RubyTargetWireBuilder.java
@@ -0,0 +1,47 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.builder;
+
+import org.apache.tuscany.container.ruby.config.RubyContextFactory;
+import org.apache.tuscany.container.ruby.invoker.RubyInvoker;
+import org.apache.tuscany.core.context.QualifiedName;
+import org.apache.tuscany.core.context.ScopeContext;
+import org.apache.tuscany.core.extension.WireBuilderSupport;
+import org.apache.tuscany.core.wire.TargetInvoker;
+import org.osoa.sca.annotations.Scope;
+
+import java.lang.reflect.Method;
+
+/**
+ * Ruby target wire builder.
+ */
+@Scope("MODULE")
+public class RubyTargetWireBuilder extends WireBuilderSupport<RubyContextFactory> {
+
+ /**
+ * Created the invoker for Ruby.
+ *
+ * @param targetName Target component name.
+ * @param method Method invoked on the proxy.
+ * @param context Scope context.
+ * @param downScope Flag for down scope.
+ */
+ protected TargetInvoker createInvoker(QualifiedName targetName, Method method, ScopeContext context, boolean downScope) {
+ String serviceName = targetName.getPartName();
+ return new RubyInvoker(context, serviceName, method.getName());
+ }
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/config/RubyContextFactory.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/config/RubyContextFactory.java
new file mode 100644
index 0000000000..e39faca6a3
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/config/RubyContextFactory.java
@@ -0,0 +1,55 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.config;
+
+import org.apache.tuscany.container.extension.config.AbstractContextFactory;
+import org.apache.tuscany.container.ruby.context.RubyComponentContext;
+import org.apache.tuscany.container.ruby.invoker.RubyScript;
+import org.apache.tuscany.core.builder.ContextCreationException;
+import org.apache.tuscany.model.assembly.Scope;
+
+/**
+ * Ruby context factory.
+ */
+public class RubyContextFactory extends AbstractContextFactory<RubyComponentContext> {
+
+ // Ruby script to run
+ private RubyScript rubyScript;
+
+ /**
+ * Initializes the context factory.
+ *
+ * @param name Context name.
+ * @param scope Context scope.
+ * @param rubyScript Ruby script to run.
+ */
+ public RubyContextFactory(String name, Scope scope, RubyScript rubyScript) {
+ super(name, scope);
+ this.rubyScript = rubyScript;
+ }
+
+ /**
+ * Creates the Ruby component context.
+ */
+ public RubyComponentContext createContext() throws ContextCreationException {
+ return new RubyComponentContext(
+ getName(),
+ getTargetWireFactories(),
+ rubyScript);
+ }
+
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/context/RubyComponentContext.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/context/RubyComponentContext.java
new file mode 100644
index 0000000000..232f184c95
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/context/RubyComponentContext.java
@@ -0,0 +1,56 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.context;
+
+import java.util.Map;
+
+import org.apache.tuscany.container.extension.context.AbstractComponentContext;
+import org.apache.tuscany.container.ruby.invoker.RubyScript;
+import org.apache.tuscany.core.context.TargetException;
+import org.apache.tuscany.core.wire.TargetWireFactory;
+
+/**
+ * Ruby component context.
+ *
+ */
+public class RubyComponentContext extends AbstractComponentContext {
+
+ // Ruby script.
+ private RubyScript rubyScript;
+
+ /**
+ * Initializes the context.
+ *
+ * @param name Component name.
+ * @param targetProxyFactories Target wire factories.
+ * @param rubyScript Ruby script.
+ */
+ public RubyComponentContext(String name,
+ Map<String, TargetWireFactory> targetProxyFactories,
+ RubyScript rubyScript) {
+ super(name, targetProxyFactories);
+ this.rubyScript = rubyScript;
+ }
+
+ /**
+ * Gets the target invocation instance.
+ */
+ public Object getTargetInstance() throws TargetException {
+ return rubyScript;
+ }
+
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyInvoker.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyInvoker.java
new file mode 100644
index 0000000000..6e1ba28c5d
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyInvoker.java
@@ -0,0 +1,108 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.invoker;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.tuscany.core.context.AtomicContext;
+import org.apache.tuscany.core.context.ScopeContext;
+import org.apache.tuscany.core.message.Message;
+import org.apache.tuscany.core.wire.Interceptor;
+import org.apache.tuscany.core.wire.TargetInvoker;
+
+/**
+ * Ruby target invoker.
+ *
+ */
+public class RubyInvoker implements TargetInvoker, Cloneable {
+
+ // SCope context
+ private ScopeContext scopeContext;
+
+ // Srevice name
+ private String serviceName;
+
+ // Method name
+ private String methodName;
+
+ /**
+ * Initializes the invoker.
+ *
+ * @param scopeContext SCope context.
+ * @param serviceName Service name.
+ * @param methodName Method name.
+ */
+ public RubyInvoker(ScopeContext scopeContext, String serviceName, String methodName) {
+ this.scopeContext = scopeContext;
+ this.serviceName = serviceName;
+ this.methodName = methodName;
+ }
+
+ /**
+ * Invokes the target.
+ */
+ public Object invokeTarget(Object payload) throws InvocationTargetException {
+
+ AtomicContext atomicContext = (AtomicContext)scopeContext.getContext(serviceName);
+ RubyScript rubyScript = (RubyScript)atomicContext.getTargetInstance();
+
+ Object[] args = (Object[])payload;
+
+ try {
+ return rubyScript.runScript(methodName, args);
+ } catch(Exception ex) {
+ throw new InvocationTargetException(ex);
+ }
+
+ }
+
+ /**
+ * Whether the invoker is cacheable.
+ */
+ public boolean isCacheable() {
+ return false;
+ }
+
+ /**
+ * Invokes the servce.
+ */
+ public Message invoke(Message msg) {
+ try {
+ Object resp = invokeTarget(msg.getBody());
+ msg.setBody(resp);
+ } catch (InvocationTargetException e) {
+ msg.setBody(e.getCause());
+ } catch (Throwable e) {
+ msg.setBody(e);
+ }
+ return msg;
+ }
+
+ /**
+ * Sets interceptor.
+ */
+ public void setNext(Interceptor next) {
+ }
+
+ /**
+ * Implementations must support deep cloning
+ */
+ public Object clone() {
+ return new RubyInvoker(scopeContext, serviceName, methodName);
+ }
+
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
new file mode 100644
index 0000000000..ac66a13806
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
@@ -0,0 +1,101 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.invoker;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import org.eclipse.emf.common.util.URI;
+
+import org.apache.bsf.BSFManager;
+import org.apache.bsf.util.IOUtils;
+import org.jruby.RubyObject;
+
+
+/**
+ * Models a Ruby script.
+ */
+public class RubyScript {
+
+ // Script to run.
+ private String script;
+ private BSFManager bsfManager;
+
+ /**
+ * Initializes the script name.
+ * @param script Full path to the script.
+ */
+ public RubyScript(String script) {
+ this.script = script;
+
+ // Register the BSF Engine
+ BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine",
+ new String[] { "rb" });
+ bsfManager = new BSFManager();
+ }
+
+ /**
+ * Executes the script.
+ * @param methodName Name of the method to be executed.
+ * @param args Arguments to the method.
+ * @return Result of invocation.
+ * @throws CompilationFailedException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public Object runScript(String methodName, Object[] args) throws InstantiationException, IllegalAccessException {
+
+ ClassLoader classLoader = getClass().getClassLoader();
+
+ try {
+ // Get the Ruby Class
+ Object rubyClass = getRubyClass(classLoader, script);
+ Method rubyMethod = findFunction(rubyClass, methodName);
+ Object result = null;
+ if(rubyMethod != null) {
+ result = rubyMethod.invoke(rubyClass, args);
+ }
+ return result;
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ }
+
+ return null;
+
+ }
+
+ // Gives the Ruby class for the corresponding script
+ // Can optimize in future by caching the instance
+ private Object getRubyClass(ClassLoader classLoader, String rubyScriptName) throws Exception {
+ // Load the ruby script
+ InputStream inputStream = classLoader.getResourceAsStream(rubyScriptName);
+ String script = IOUtils.getStringFromReader(new InputStreamReader(inputStream));
+ bsfManager.exec("ruby", "(java)", 1, 1, script);
+
+ // Get the Ruby class
+ String rubyClassName=URI.createURI(rubyScriptName).trimFileExtension().lastSegment();
+ Object rubyClass = bsfManager.eval("ruby", "(java)", 1, 1, rubyClassName+".new");
+ return rubyClass;
+ }
+
+ // Find whether a particular method exists
+ private Method findFunction(Object rubyClass, String functionName) throws Exception {
+ Class[] parameterTypes = new Class[]{String.class};
+ Method method = rubyClass.getClass().getMethod(functionName, parameterTypes);
+ return method;
+ }
+}
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/loader/RubyImplementationLoader.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/loader/RubyImplementationLoader.java
new file mode 100644
index 0000000000..647a57507d
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/loader/RubyImplementationLoader.java
@@ -0,0 +1,97 @@
+/**
+ *
+ * Copyright 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.
+ */
+package org.apache.tuscany.container.ruby.loader;
+
+import java.net.URL;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.container.extension.loader.AbstractImplementationLoader;
+import org.apache.tuscany.container.ruby.assembly.RubyImplementation;
+import org.apache.tuscany.core.config.MissingResourceException;
+import org.apache.tuscany.core.loader.LoaderContext;
+import org.osoa.sca.annotations.Destroy;
+import org.osoa.sca.annotations.Init;
+import org.osoa.sca.annotations.Scope;
+
+/**
+ * Ruby implementation loader.
+ *
+ */
+@Scope("MODULE")
+public class RubyImplementationLoader extends AbstractImplementationLoader<RubyImplementation> {
+
+ // Qualified name for the Ruby implementation.
+ public static final QName NAME = new QName("http://org.apache.tuscany/xmlns/ruby/0.9", "implementation.ruby");
+
+ /**
+ * Lifecycle method registers the implementation loader.
+ *
+ */
+ @Init(eager = true)
+ public void start() {
+ registry.registerLoader(NAME, this);
+ }
+
+ /**
+ * Lifecycle method deregisters the implementation loader.
+ *
+ */
+ @Destroy
+ public void stop() {
+ registry.unregisterLoader(NAME, this);
+ }
+
+ /**
+ * Required to be implemented by the concrete classes.
+ * @return Implementation object.
+ */
+ protected RubyImplementation getAssemblyObject(XMLStreamReader reader, LoaderContext loaderContext) {
+
+ RubyImplementation rubyImplementation = new RubyImplementation();
+ String script = reader.getAttributeValue(null, "script");
+
+ rubyImplementation.setScript(script);
+ rubyImplementation.setResourceLoader(loaderContext.getResourceLoader());
+
+ return rubyImplementation;
+
+ }
+
+ /**
+ * Gets the side file.
+ *
+ * @param reader Reader for the module file.
+ * @param loaderContext Loader context.
+ * @return Side file Url.
+ * @throws MissingResourceException
+ */
+ protected URL getSideFile(XMLStreamReader reader, LoaderContext loaderContext)
+ throws MissingResourceException {
+
+ String script = reader.getAttributeValue(null, "script");
+ String sidefile = script.substring(0, script.lastIndexOf('.')) + ".componentType";
+ URL componentTypeFile = loaderContext.getResourceLoader().getResource(sidefile);
+ if (componentTypeFile == null) {
+ throw new MissingResourceException(sidefile);
+ }
+ return componentTypeFile;
+
+ }
+
+}