summaryrefslogtreecommitdiffstats
path: root/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java')
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java163
1 files changed, 94 insertions, 69 deletions
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java
index 7e963cc34a..fd9e89bc2d 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java
@@ -20,15 +20,16 @@
package org.apache.tuscany.sca.binding.erlang.impl;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
+import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException;
import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
-import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpEpmd;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangObject;
+import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpMsg;
import com.ericsson.otp.erlang.OtpNode;
@@ -40,78 +41,102 @@ import com.ericsson.otp.erlang.OtpSelf;
*/
public class ErlangInvoker implements Invoker {
- private RuntimeComponentReference reference;
- private ErlangBinding binding;
- private OtpNode node;
+ private ErlangBinding binding;
+ private OtpNode node;
- public ErlangInvoker(RuntimeComponentReference reference, ErlangBinding binding) {
- this.reference = reference;
- this.binding = binding;
- }
+ public ErlangInvoker(ErlangBinding binding) {
+ this.binding = binding;
+ }
- private Message sendMessage(Message msg) {
- OtpMbox tmpMbox = null;
- try {
- String nodeName = "_connector_to_" + binding.getNode();
- node = new OtpNode(nodeName);
- tmpMbox = node.createMbox();
- Object[] args = msg.getBody();
- OtpErlangObject msgPayload = TypeHelpersProxy.toErlang(args);
- tmpMbox.send(msg.getOperation().getName(), binding.getNode(), msgPayload);
- if (msg.getOperation().getOutputType() != null) {
- OtpMsg resultMsg = tmpMbox.receiveMsg();
- OtpErlangObject result = resultMsg.getMsg();
- msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation().getOutputType().getPhysical()));
- }
- } catch (Exception e) {
- msg.setFaultBody(e);
- } finally {
- if (tmpMbox != null) {
- tmpMbox.close();
- }
- if (node != null) {
- OtpEpmd.unPublishPort(node);
- node.close();
- }
- }
- return msg;
- }
+ private Message sendMessage(Message msg) {
+ OtpMbox tmpMbox = null;
+ try {
+ String nodeName = "_connector_to_" + binding.getNode();
+ node = new OtpNode(nodeName);
+ tmpMbox = node.createMbox();
+ Object[] args = msg.getBody();
+ OtpErlangObject msgPayload = TypeHelpersProxy.toErlang(args);
+ tmpMbox.send(msg.getOperation().getName(), binding.getNode(),
+ msgPayload);
+ if (msg.getOperation().getOutputType() != null) {
+ OtpMsg resultMsg = tmpMbox.receiveMsg();
+ OtpErlangObject result = resultMsg.getMsg();
+ msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation()
+ .getOutputType().getPhysical()));
+ }
+ } catch (Exception e) {
+ msg.setFaultBody(e);
+ } finally {
+ if (tmpMbox != null) {
+ tmpMbox.close();
+ }
+ if (node != null) {
+ OtpEpmd.unPublishPort(node);
+ node.close();
+ }
+ }
+ return msg;
+ }
- private Message invokeOperation(Message msg) {
- OtpSelf self = null;
- OtpPeer other = null;
- OtpConnection connection = null;
- try {
- String nodeName = "_connector_to_" + binding.getNode();
- self = new OtpSelf(nodeName);
- other = new OtpPeer(binding.getNode());
- connection = self.connect(other);
- OtpErlangList params = TypeHelpersProxy.toErlangAsList((Object[])msg.getBody());
- connection.sendRPC(binding.getModule(), msg.getOperation().getName(), params);
- if (msg.getOperation().getOutputType() != null) {
- OtpErlangObject result = connection.receiveRPC();
- msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation().getOutputType().getPhysical()));
- }
- } catch (Exception e) {
- msg.setFaultBody(e);
- } finally {
- if (connection != null) {
- connection.close();
- }
- }
- return msg;
- }
+ private Message invokeOperation(Message msg) {
+ OtpSelf self = null;
+ OtpPeer other = null;
+ OtpConnection connection = null;
+ try {
+ String nodeName = "_connector_to_" + binding.getNode();
+ self = new OtpSelf(nodeName);
+ other = new OtpPeer(binding.getNode());
+ connection = self.connect(other);
+ OtpErlangList params = TypeHelpersProxy
+ .toErlangAsList((Object[]) msg.getBody());
+ OtpErlangTuple message = MessageHelper.rpcMessage(self.pid(), self
+ .createRef(), binding.getModule(), msg.getOperation()
+ .getName(), params);
+ connection.send("rex", message);
+ OtpErlangObject result = connection.receiveRPC();
+ if (MessageHelper.isfunctionUndefMessage(result)) {
+ // TODO: externalize message?
+ Exception e = new ErlangException(
+ "No such function in referenced Erlang node.");
+ if (msg.getOperation().getFaultTypes().size() == 0) {
+ // TODO: no way to throw exception, log it (temporary as System.out)
+ // TODO: do we really want not to throw any exception?
+ System.out.println("PROBLEM: " + e.getMessage());
+ // in this case we don't throw occured problem, so we need
+ // to reset message body (if body is not cleared then
+ // operation arguments would be set as operation output)
+ msg.setBody(null);
+ } else {
+ msg.setFaultBody(e);
+ }
+ } else if (msg.getOperation().getOutputType() != null) {
+ if (result.getClass().equals(OtpErlangTuple.class)) {
+ OtpErlangObject resultBody = ((OtpErlangTuple) result)
+ .elementAt(1);
+ msg.setBody(TypeHelpersProxy.toJava(resultBody, msg
+ .getOperation().getOutputType().getPhysical()));
+ }
+ }
+ } catch (Exception e) {
+ msg.setFaultBody(e);
+ } finally {
+ if (connection != null) {
+ connection.close();
+ }
+ }
+ return msg;
+ }
- /**
- * @see org.apache.tuscany.sca.invocation.Invoker#invoke(org.apache.tuscany.sca.invocation.Message)
- */
- public Message invoke(Message msg) {
- if (binding.isMbox()) {
- return sendMessage(msg);
- } else {
- return invokeOperation(msg);
- }
+ /**
+ * @see org.apache.tuscany.sca.invocation.Invoker#invoke(org.apache.tuscany.sca.invocation.Message)
+ */
+ public Message invoke(Message msg) {
+ if (binding.isMbox()) {
+ return sendMessage(msg);
+ } else {
+ return invokeOperation(msg);
+ }
- }
+ }
}