diff options
author | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-06 21:57:58 +0000 |
---|---|---|
committer | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-06 21:57:58 +0000 |
commit | 9320e6220c2a7135e7a2ec16e0b90bb00d65cdc5 (patch) | |
tree | 46625ffb6b7a18e691b0a4833cd8a5250856ee67 /sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org | |
parent | e526f246a6d2ff674975bbdd2a6a36d4cba71d57 (diff) |
Service RPC updated by allowing to have more than one SCA-Erlang module on single SCA-Erlang node
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@751092 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
6 files changed, 160 insertions, 108 deletions
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java index 8953112aaa..f823be2d8f 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java @@ -19,6 +19,9 @@ package org.apache.tuscany.sca.binding.erlang.impl;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.provider.BindingProviderFactory;
@@ -31,34 +34,61 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentService; /**
* @version $Rev: $ $Date: $
*/
-public class ErlangBindingProviderFactory implements BindingProviderFactory<ErlangBinding> {
-
- public ErlangBindingProviderFactory(ExtensionPointRegistry registry) {
-
- }
- /**
- * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createReferenceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentReference, org.apache.tuscany.sca.assembly.Binding)
- */
- public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component,
- RuntimeComponentReference reference,
- ErlangBinding binding) {
- return new ErlangReferenceBindingProvider(binding, reference);
- }
+public class ErlangBindingProviderFactory implements
+ BindingProviderFactory<ErlangBinding> {
+
+ private Map<String, ErlangNode> nodes = new HashMap<String, ErlangNode>();
+
+ public ErlangBindingProviderFactory(ExtensionPointRegistry registry) {
+
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createReferenceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent,
+ * org.apache.tuscany.sca.runtime.RuntimeComponentReference,
+ * org.apache.tuscany.sca.assembly.Binding)
+ */
+ public ReferenceBindingProvider createReferenceBindingProvider(
+ RuntimeComponent component, RuntimeComponentReference reference,
+ ErlangBinding binding) {
+ return new ErlangReferenceBindingProvider(binding, reference);
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createServiceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent,
+ * org.apache.tuscany.sca.runtime.RuntimeComponentService,
+ * org.apache.tuscany.sca.assembly.Binding)
+ */
+ public ServiceBindingProvider createServiceBindingProvider(
+ RuntimeComponent component, RuntimeComponentService service,
+ ErlangBinding binding) {
+ ServiceBindingProvider provider = null;
+ try {
+ provider = new ErlangServiceBindingProvider(getErlangNode(binding
+ .getNode()), binding, service);
+ } catch (Exception e) {
+ // TODO: log, throw, do something with this error
+ e.printStackTrace();
+ }
+ return provider;
+ }
- /**
- * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createServiceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentService, org.apache.tuscany.sca.assembly.Binding)
- */
- public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component,
- RuntimeComponentService service,
- ErlangBinding binding) {
- return new ErlangServiceBindingProvider(binding, service);
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType()
+ */
+ public Class<ErlangBinding> getModelType() {
+ return ErlangBinding.class;
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType()
- */
- public Class<ErlangBinding> getModelType() {
- return ErlangBinding.class;
- }
+ private ErlangNode getErlangNode(String name) throws Exception {
+ ErlangNode result = null;
+ if (nodes.containsKey(name)) {
+ result = nodes.get(name);
+ } else {
+ result = new ErlangNode(name);
+ nodes.put(name, result);
+ }
+ return result;
+ }
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java new file mode 100644 index 0000000000..a5cfbe6d90 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java @@ -0,0 +1,83 @@ +package org.apache.tuscany.sca.binding.erlang.impl; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +import com.ericsson.otp.erlang.OtpConnection; +import com.ericsson.otp.erlang.OtpSelf; + +public class ErlangNode implements Runnable { + + private Map<String, RuntimeComponentService> services = new HashMap<String, RuntimeComponentService>(); + private Map<String, ErlangBinding> bindings = new HashMap<String, ErlangBinding>(); + private String name; + private OtpSelf self; + private ExecutorService executors; + private boolean stopRequested; + + public ErlangNode(String name) throws Exception { + this.name = name; + self = new OtpSelf(name); + boolean registered = self.publishPort(); + if (!registered) { + // TODO: externalize messages? + throw new ErlangException( + "Problem with publishing service under epmd server."); + } + } + + private void stop() { + stopRequested = true; + executors.shutdownNow(); + } + + public void run() { + executors = Executors.newFixedThreadPool(10); + while (!stopRequested) { + try { + OtpConnection connection = self.accept(); + executors.execute(new RpcExecutor(services, bindings, + connection)); + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + } + + public void registerModule(ErlangBinding binding, + RuntimeComponentService service) throws ErlangException { + if (services.containsKey(binding.getModule())) { + // TODO: externalize message + // TODO: really want to throw exception? Log only? + throw new ErlangException("Module " + binding.getModule() + + " already defined under " + name + + " node. Duplicate module won't be started"); + } else { + if (services.size() == 0) { + // TODO: should ErlangNode manage its thread? + Thread selfThread = new Thread(this); + selfThread.start(); + } + services.put(binding.getModule(), service); + bindings.put(binding.getModule(), binding); + } + } + + public void unregisterModule(ErlangBinding binding) throws ErlangException { + if (services.containsKey(binding.getModule())) { + services.remove(binding.getModule()); + bindings.remove(binding.getModule()); + if (services.size() == 0) { + stop(); + } + } + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java index df82c5813c..651067f4e7 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java @@ -31,12 +31,13 @@ import org.osoa.sca.ServiceRuntimeException; public class ErlangServiceBindingProvider implements ServiceBindingProvider {
private RuntimeComponentService service;
+ private ErlangNode node;
private ErlangBinding binding;
- private RpcServer rpcServer;
- public ErlangServiceBindingProvider(ErlangBinding binding, RuntimeComponentService service) {
+ public ErlangServiceBindingProvider(ErlangNode node, ErlangBinding binding, RuntimeComponentService service) {
this.service = service;
this.binding = binding;
+ this.node = node;
}
/**
@@ -51,9 +52,7 @@ public class ErlangServiceBindingProvider implements ServiceBindingProvider { */
public void start() {
try {
- rpcServer = new RpcServer(service, binding);
- Thread thread = new Thread(rpcServer);
- thread.start();
+ node.registerModule(binding, service);
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
@@ -65,7 +64,7 @@ public class ErlangServiceBindingProvider implements ServiceBindingProvider { */
public void stop() {
try {
- rpcServer.stop();
+ node.unregisterModule(binding);
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java index 6707496194..03e83cec61 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java @@ -22,6 +22,7 @@ package org.apache.tuscany.sca.binding.erlang.impl; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.binding.erlang.ErlangBinding; import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy; @@ -40,18 +41,18 @@ import com.ericsson.otp.erlang.OtpErlangTuple; public class RpcExecutor implements Runnable { - private RuntimeComponentService service; - private ErlangBinding binding; + private Map<String, RuntimeComponentService> services; + private Map<String, ErlangBinding> bindings; private OtpConnection connection; private static final OtpErlangAtom OK = new OtpErlangAtom("ok"); private static final OtpErlangAtom ERROR = new OtpErlangAtom("error"); private static final OtpErlangAtom BADRPC = new OtpErlangAtom("badrpc"); - public RpcExecutor(RuntimeComponentService service, ErlangBinding binding, + public RpcExecutor(Map<String, RuntimeComponentService> services, Map<String, ErlangBinding> bindings, OtpConnection connection) { - this.service = service; - this.binding = binding; + this.bindings = bindings; + this.services = services; this.connection = connection; } @@ -85,13 +86,15 @@ public class RpcExecutor implements Runnable { } else { argsList = new OtpErlangList(args); } - if (!binding.getModule().equals(module)) { + if (!services.containsKey(module)) { // TODO: externalize message? OtpErlangObject errorMsg = MessageHelper.functionUndefMessage( module, function, argsList, "Module not found in SCA component."); sendMessage(connection, senderPid, senderRef, BADRPC, errorMsg); } else { + RuntimeComponentService service = services.get(module); + ErlangBinding binding = bindings.get(module); List<Operation> operations = service.getInterfaceContract() .getInterface().getOperations(); Operation operation = null; diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java deleted file mode 100644 index ab10865c83..0000000000 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.sca.binding.erlang.impl; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.apache.tuscany.sca.binding.erlang.ErlangBinding; -import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; - -import com.ericsson.otp.erlang.OtpConnection; -import com.ericsson.otp.erlang.OtpSelf; - -public class RpcServer implements Runnable { - - private RuntimeComponentService service; - private ErlangBinding binding; - private OtpSelf self; - ExecutorService executors; - private boolean stopRequested; - - public RpcServer(RuntimeComponentService service, ErlangBinding binding) throws Exception { - this.service = service; - this.binding = binding; - self = new OtpSelf(binding.getNode()); - boolean registered = self.publishPort(); - if (!registered) { - //TODO: externalize messages? - throw new ErlangException("Problem with publishing service under epmd server."); - } - executors = Executors.newFixedThreadPool(10); - } - - public void stop() { - stopRequested = true; - executors.shutdownNow(); - } - - public void run() { - while (!stopRequested) { - try { - OtpConnection connection = self.accept(); - executors.execute(new RpcExecutor(service, binding, connection)); - } catch (Exception e) { - // TODO: handle exception - e.printStackTrace(); - } - } - } - -} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java index 6ae919b969..cf573def95 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java @@ -2,8 +2,14 @@ package org.apache.tuscany.sca.binding.erlang.impl.exceptions; public class ErlangException extends Exception { + private static final long serialVersionUID = 1L; + public ErlangException(String message) { super(message); } + public ErlangException(String message, Throwable cause) { + super(message, cause); + } + } |