diff options
author | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-02 22:49:01 +0000 |
---|---|---|
committer | wjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-02 22:49:01 +0000 |
commit | 65c9710b644bd3ef0f5dd21aaa0049f984394c57 (patch) | |
tree | e67b40fea3ffe03bc0d28f0fbca5e020ef49fcc9 /sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java | |
parent | 6c6ad2fbdd72a4aabc4025339a3d6aa090668444 (diff) |
Epmd starts automatically - if epmd is not available tests are beeing ignored. Normalized service/reference RPC, added some error handling.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@749461 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java')
-rw-r--r-- | sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java | 224 |
1 files changed, 127 insertions, 97 deletions
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 3940c34b7b..6707496194 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 @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.binding.erlang.impl; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.List; import org.apache.tuscany.sca.binding.erlang.ErlangBinding; @@ -39,105 +40,134 @@ import com.ericsson.otp.erlang.OtpErlangTuple; public class RpcExecutor implements Runnable { - private RuntimeComponentService service; - private ErlangBinding binding; - private OtpConnection connection; + private RuntimeComponentService service; + private ErlangBinding binding; + private OtpConnection connection; - private static final OtpErlangAtom OK = new OtpErlangAtom("ok"); - private static final OtpErlangAtom ERROR = new OtpErlangAtom("error"); + 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, OtpConnection connection) { - this.service = service; - this.binding = binding; - this.connection = connection; - } + public RpcExecutor(RuntimeComponentService service, ErlangBinding binding, + OtpConnection connection) { + this.service = service; + this.binding = binding; + this.connection = connection; + } - private void sendMessage(OtpConnection connection, - OtpErlangPid pid, - OtpErlangRef ref, - OtpErlangAtom head, - OtpErlangObject message) throws IOException { - OtpErlangTuple tResult = new OtpErlangTuple(new OtpErlangObject[] {head, message}); - OtpErlangObject msg = null; - if (ref != null) { - msg = new OtpErlangTuple(new OtpErlangObject[] {ref, tResult}); - } else { - msg = tResult; - } - connection.send(pid, msg); - } - - public void run() { - OtpErlangTuple request = null; - OtpErlangPid senderPid = null; - OtpErlangRef senderRef = null; - try { - OtpErlangTuple call = (OtpErlangTuple)connection.receive(); - if (call.arity() == 2) { - // is call from jinterface library - senderPid = (OtpErlangPid)call.elementAt(0); - request = (OtpErlangTuple)call.elementAt(1); - } else { - // is call from native *erl* console - OtpErlangTuple from = (OtpErlangTuple)call.elementAt(1); - request = (OtpErlangTuple)call.elementAt(2); - senderPid = (OtpErlangPid)from.elementAt(0); - senderRef = (OtpErlangRef)from.elementAt(1); - } - String module = ((OtpErlangAtom)request.elementAt(1)).atomValue(); - String function = ((OtpErlangAtom)request.elementAt(2)).atomValue(); - OtpErlangObject args = request.elementAt(3); - OtpErlangList argsList = null; - if (args instanceof OtpErlangList) { - argsList = (OtpErlangList)args; - } else { - argsList = new OtpErlangList(args); - } - if (!binding.getModule().equals(module)) { - sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("No such module")); - } else { - List<Operation> operations = service.getInterfaceContract().getInterface().getOperations(); - Operation operation = null; - for (Operation o : operations) { - if (o.getName().equals(function)) { - operation = o; - break; - } - } - if (operation != null) { - List<DataType> iTypes = operation.getInputType().getLogical(); - Class<?>[] forClasses = new Class<?>[iTypes.size()]; - for (int i = 0; i < iTypes.size(); i++) { - forClasses[i] = iTypes.get(i).getPhysical(); - } - Object result = - service.getRuntimeWire(binding).invoke(operation, - TypeHelpersProxy.toJavaFromList(argsList, forClasses)); - OtpErlangObject response = null; - if (operation.getOutputType() != null && operation.getOutputType().getPhysical().isArray()) { - response = TypeHelpersProxy.toErlangAsList(result); - } else if (operation.getOutputType() == null) { - Object[] arrArg = new Object[] {}; - response = TypeHelpersProxy.toErlang(arrArg); - } else { - Object[] arrArg = new Object[] {result}; - response = TypeHelpersProxy.toErlang(arrArg); - } - sendMessage(connection, senderPid, senderRef, OK, response); - } else { - sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("No such operation")); - } - } - } catch (Exception e) { - // TODO: distinguish and describe errors! - try { - sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("Error while processing request")); - } catch (IOException e1) { - // error while sending error message. Can't do anything now - } - } finally { - connection.close(); - } - } + private void sendMessage(OtpConnection connection, OtpErlangPid pid, + OtpErlangRef ref, OtpErlangAtom head, OtpErlangObject message) + throws IOException { + OtpErlangTuple tResult = new OtpErlangTuple(new OtpErlangObject[] { + head, message }); + OtpErlangObject msg = null; + msg = new OtpErlangTuple(new OtpErlangObject[] { ref, tResult }); + connection.send(pid, msg); + } + public void run() { + OtpErlangTuple request = null; + OtpErlangPid senderPid = null; + OtpErlangRef senderRef = null; + try { + OtpErlangTuple call = (OtpErlangTuple) connection.receive(); + OtpErlangTuple from = (OtpErlangTuple) call.elementAt(1); + request = (OtpErlangTuple) call.elementAt(2); + senderPid = (OtpErlangPid) from.elementAt(0); + senderRef = (OtpErlangRef) from.elementAt(1); + String module = ((OtpErlangAtom) request.elementAt(1)).atomValue(); + String function = ((OtpErlangAtom) request.elementAt(2)) + .atomValue(); + OtpErlangObject args = request.elementAt(3); + OtpErlangList argsList = null; + if (args instanceof OtpErlangList) { + argsList = (OtpErlangList) args; + } else { + argsList = new OtpErlangList(args); + } + if (!binding.getModule().equals(module)) { + // TODO: externalize message? + OtpErlangObject errorMsg = MessageHelper.functionUndefMessage( + module, function, argsList, + "Module not found in SCA component."); + sendMessage(connection, senderPid, senderRef, BADRPC, errorMsg); + } else { + List<Operation> operations = service.getInterfaceContract() + .getInterface().getOperations(); + Operation operation = null; + for (Operation o : operations) { + if (o.getName().equals(function)) { + operation = o; + break; + } + } + if (operation != null) { + List<DataType> iTypes = operation.getInputType() + .getLogical(); + Class<?>[] forClasses = new Class<?>[iTypes.size()]; + for (int i = 0; i < iTypes.size(); i++) { + forClasses[i] = iTypes.get(i).getPhysical(); + } + try { + Object result = service.getRuntimeWire(binding).invoke( + operation, + TypeHelpersProxy.toJavaFromList(argsList, + forClasses)); + OtpErlangObject response = null; + if (operation.getOutputType() != null + && operation.getOutputType().getPhysical() + .isArray()) { + response = TypeHelpersProxy.toErlangAsList(result); + } else if (operation.getOutputType() == null) { + Object[] arrArg = new Object[] {}; + response = TypeHelpersProxy.toErlang(arrArg); + } else { + Object[] arrArg = new Object[] { result }; + response = TypeHelpersProxy.toErlang(arrArg); + } + sendMessage(connection, senderPid, senderRef, OK, + response); + } catch (Exception e) { + if ((e.getClass().equals( + InvocationTargetException.class) && e + .getCause().getClass().equals( + IllegalArgumentException.class)) + || e.getClass().equals( + TypeMismatchException.class)) { + // TODO: externalize message? + OtpErlangObject errorMsg = MessageHelper + .functionUndefMessage(module, function, + argsList, + "Operation name found in SCA component, but parameters types didn't match."); + sendMessage(connection, senderPid, senderRef, + BADRPC, errorMsg); + } else { + throw e; + } + } + } else { + // TODO: externalize message? + OtpErlangObject errorMsg = MessageHelper + .functionUndefMessage(module, function, argsList, + "Operation name not found in SCA component."); + sendMessage(connection, senderPid, senderRef, BADRPC, + errorMsg); + } + } + } catch (Exception e) { + // TODO: distinguish and describe errors! + try { + e.printStackTrace(); + sendMessage(connection, senderPid, senderRef, ERROR, + new OtpErlangString( + "Unhandled error while processing request: " + + e.getClass().getCanonicalName() + + ", message: " + e.getMessage())); + } catch (IOException e1) { + // error while sending error message. Can't do anything now + } + } finally { + connection.close(); + } + } } |