summaryrefslogtreecommitdiffstats
path: root/sandbox/wjaniszewski/binding-erlang-runtime
diff options
context:
space:
mode:
authorwjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68>2009-03-19 21:28:08 +0000
committerwjaniszewski <wjaniszewski@13f79535-47bb-0310-9956-ffa450edef68>2009-03-19 21:28:08 +0000
commit420526884c2571aa4b17c69e98128bfd0612046e (patch)
treebf9a9dc5514c9a677f671f92b64602cd37866a3b /sandbox/wjaniszewski/binding-erlang-runtime
parent33a374fb68af272fb1b130ed8e68538141028cef (diff)
Added timeout feature for reference bindings. Added reference cookies - need more testing. Added some comments to organize tasks.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@756212 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/wjaniszewski/binding-erlang-runtime')
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java7
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java91
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNode.java34
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java54
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BinaryTypeHelper.java39
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java13
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java8
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxInterface.java3
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxListener.java10
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java134
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceTestComponentImpl.java44
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceInterface.java1
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceTestComponentImplTimeout.java61
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangReference.composite16
-rw-r--r--sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangService.composite8
15 files changed, 460 insertions, 63 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 f4114132c1..d01d266332 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
@@ -21,6 +21,8 @@ package org.apache.tuscany.sca.binding.erlang.impl;
import java.util.HashMap;
import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
@@ -38,6 +40,8 @@ public class ErlangBindingProviderFactory implements
BindingProviderFactory<ErlangBinding> {
private Map<String, ErlangNode> nodes = new HashMap<String, ErlangNode>();
+ private static final Logger logger = Logger
+ .getLogger(ErlangBindingProviderFactory.class.getName());
public ErlangBindingProviderFactory(ExtensionPointRegistry registry) {
@@ -67,8 +71,7 @@ public class ErlangBindingProviderFactory implements
provider = new ErlangServiceBindingProvider(getErlangNode(binding
.getNode()), binding, service);
} catch (Exception e) {
- // TODO: log, throw, do something with this error
- e.printStackTrace();
+ logger.log(Level.WARNING, "Exception during creating ServiceBindingProvider", e);
}
return provider;
}
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 9ed3713db4..9a540fd5bb 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
@@ -19,12 +19,16 @@
package org.apache.tuscany.sca.binding.erlang.impl;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
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 com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpEpmd;
import com.ericsson.otp.erlang.OtpErlangList;
@@ -41,32 +45,63 @@ import com.ericsson.otp.erlang.OtpSelf;
*/
public class ErlangInvoker implements Invoker {
+ private static final Logger logger = Logger.getLogger(ErlangInvoker.class
+ .getName());
+
private ErlangBinding binding;
- private OtpNode node;
public ErlangInvoker(ErlangBinding binding) {
this.binding = binding;
}
+ private void reportProblem(Message msg, Exception e) {
+ if (msg.getOperation().getFaultTypes().size() > 0) {
+ msg.setFaultBody(e);
+ } else {
+ // NOTE: don't throw exception if not declared
+ // TODO: externalize message?
+ logger
+ .log(Level.WARNING, "Problem while sending/receiving data",
+ e);
+ }
+ }
+
+ private boolean isCookieProvided() throws Exception {
+ return binding.getCookie() != null && binding.getCookie().length() > 0;
+ }
+
+ private String getClientNodeName() {
+ return "_connector_to_" + binding.getNode()
+ + System.currentTimeMillis();
+ }
+
private Message sendMessage(Message msg) {
OtpMbox tmpMbox = null;
+ OtpNode node = null;
try {
- String nodeName = "_connector_to_" + binding.getNode();
- node = new OtpNode(nodeName);
+ node = new OtpNode(getClientNodeName());
+ if (isCookieProvided()) {
+ node.setCookie(binding.getCookie());
+ }
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) {
- // TODO: add timeouts, timeout declaration method?
- OtpMsg resultMsg = tmpMbox.receiveMsg();
+ OtpMsg resultMsg = tmpMbox.receiveMsg(binding.getTimeout());
OtpErlangObject result = resultMsg.getMsg();
msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation()
.getOutputType().getPhysical()));
}
+ } catch (InterruptedException e) {
+ // TODO: externalize message?
+ ErlangException ee = new ErlangException(
+ "Timeout while receiving message reply", e);
+ msg.setBody(null);
+ reportProblem(msg, ee);
} catch (Exception e) {
- msg.setFaultBody(e);
+ reportProblem(msg, e);
} finally {
if (tmpMbox != null) {
tmpMbox.close();
@@ -84,8 +119,10 @@ public class ErlangInvoker implements Invoker {
OtpPeer other = null;
OtpConnection connection = null;
try {
- String nodeName = "_connector_to_" + binding.getNode();
- self = new OtpSelf(nodeName);
+ self = new OtpSelf(getClientNodeName());
+ if (isCookieProvided()) {
+ self.setCookie(binding.getCookie());
+ }
other = new OtpPeer(binding.getNode());
connection = self.connect(other);
OtpErlangList params = TypeHelpersProxy
@@ -94,23 +131,18 @@ public class ErlangInvoker implements Invoker {
.createRef(), binding.getModule(), msg.getOperation()
.getName(), params);
connection.send(MessageHelper.RPC_MBOX, message);
- OtpErlangObject result = connection.receiveRPC();
+ OtpErlangObject rpcResponse = connection.receive(binding
+ .getTimeout());
+ OtpErlangObject result = ((OtpErlangTuple) rpcResponse)
+ .elementAt(1);
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);
- }
+ Exception e = new ErlangException("No '" + binding.getModule()
+ + ":" + msg.getOperation().getName()
+ + "' operation defined on remote '" + binding.getNode()
+ + "' node.");
+ reportProblem(msg, e);
+ msg.setBody(null);
} else if (msg.getOperation().getOutputType() != null) {
if (result.getClass().equals(OtpErlangTuple.class)) {
OtpErlangObject resultBody = ((OtpErlangTuple) result)
@@ -119,8 +151,19 @@ public class ErlangInvoker implements Invoker {
.getOperation().getOutputType().getPhysical()));
}
}
+ } catch (OtpAuthException e) {
+ // TODO: externalize message?
+ ErlangException ee = new ErlangException("Problem while authenticating client - check your cookie", e);
+ msg.setBody(null);
+ reportProblem(msg, ee);
+ } catch (InterruptedException e) {
+ // TODO: externalize message?
+ ErlangException ee = new ErlangException(
+ "Timeout while receiving RPC reply", e);
+ msg.setBody(null);
+ reportProblem(msg, ee);
} catch (Exception e) {
- msg.setFaultBody(e);
+ reportProblem(msg, e);
} finally {
if (connection != null) {
connection.close();
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
index 78ffdfaa49..beeec9fffb 100644
--- 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
@@ -19,18 +19,22 @@
package org.apache.tuscany.sca.binding.erlang.impl;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpSelf;
@@ -39,6 +43,9 @@ import com.ericsson.otp.erlang.OtpSelf;
*/
public class ErlangNode implements Runnable {
+ private static final Logger logger = Logger.getLogger(ErlangNode.class
+ .getName());
+
private Map<String, ErlangNodeElement> erlangModules = new HashMap<String, ErlangNodeElement>();
private ErlangNodeElement erlangMbox;
private boolean mboxNode;
@@ -53,7 +60,7 @@ public class ErlangNode implements Runnable {
self = new OtpSelf(name);
boolean registered = self.publishPort();
if (!registered) {
- // TODO: externalize messages?
+ // TODO: externalize message?
throw new ErlangException(
"Problem with publishing service under epmd server.");
}
@@ -65,15 +72,20 @@ public class ErlangNode implements Runnable {
}
public void run() {
+ // FIXME: add configurable thread pools
executors = Executors.newFixedThreadPool(10);
while (!stopRequested) {
try {
OtpConnection connection = self.accept();
executors.execute(new ServiceExecutor(connection,
- groupedOperations, erlangModules, erlangMbox));
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
+ groupedOperations, erlangModules, erlangMbox, name));
+ } catch (IOException e) {
+ // TODO: externalzie message?
+ logger.log(Level.WARNING,
+ "Error occured while accepting connection on '" + name
+ + "' node");
+ } catch (OtpAuthException e) {
+ // TODO: log bad authentication attempt
}
}
}
@@ -82,8 +94,9 @@ public class ErlangNode implements Runnable {
RuntimeComponentService service) throws ErlangException {
if (binding.isMbox()) {
if (mboxNode) {
- // TODO: externalize message
- // TODO: really want to throw exception? Log only?
+ // TODO: externalize message?
+ // NOTE: if mbox registered more than once for node then
+ // exception will be thrown
throw new ErlangException("Node " + binding.getNode()
+ " already defined as mbox node");
} else {
@@ -107,14 +120,15 @@ public class ErlangNode implements Runnable {
}
} else {
if (erlangModules.containsKey(binding.getModule())) {
- // TODO: externalize message
- // TODO: really want to throw exception? Log only?
+ // TODO: externalize message?
+ // NOTE: if the same module was registered more than once than
+ // exception will be thrown
throw new ErlangException("Module " + binding.getModule()
+ " already defined under " + name
+ " node. Duplicate module won't be started");
} else {
if (erlangModules.size() == 0) {
- // TODO: should ErlangNode manage its thread?
+ // NOTE: Erlang node is managing it's thread by itself. Just noticing.
Thread selfThread = new Thread(this);
selfThread.start();
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java
index 63c58cb696..a03d522860 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java
@@ -23,6 +23,8 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy;
@@ -30,8 +32,10 @@ import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpErlangAtom;
+import com.ericsson.otp.erlang.OtpErlangExit;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangPid;
@@ -47,21 +51,25 @@ import com.ericsson.otp.erlang.OtpNode;
*/
public class ServiceExecutor implements Runnable {
+ private static final Logger logger = Logger.getLogger(ServiceExecutor.class
+ .getName());
private static final long RECEIVE_TIMEOUT = 60000;
private Map<String, ErlangNodeElement> erlangModules;
private ErlangNodeElement erlangMbox;
private OtpConnection connection;
private Map<String, List<Operation>> groupedOperations;
+ private String name;
public ServiceExecutor(OtpConnection connection,
Map<String, List<Operation>> groupedOperations,
Map<String, ErlangNodeElement> erlangModules,
- ErlangNodeElement erlangMbox) {
+ ErlangNodeElement erlangMbox, String name) {
this.erlangModules = erlangModules;
this.connection = connection;
this.groupedOperations = groupedOperations;
this.erlangMbox = erlangMbox;
+ this.name = name;
}
private void sendMessage(OtpConnection connection, OtpErlangPid pid,
@@ -123,7 +131,8 @@ public class ServiceExecutor implements Runnable {
forClasses[i] = iTypes.get(i).getPhysical();
}
try {
- Object result = service.getRuntimeWire(binding).invoke(
+ Object result = service.getRuntimeWire(binding,
+ service.getInterfaceContract()).invoke(
operation,
TypeHelpersProxy.toJavaFromList(argsList,
forClasses));
@@ -169,9 +178,7 @@ public class ServiceExecutor implements Runnable {
}
}
} catch (Exception e) {
- // TODO: distinguish and describe errors!
try {
- e.printStackTrace();
sendMessage(connection, senderPid, senderRef,
MessageHelper.ATOM_ERROR, new OtpErlangString(
"Unhandled error while processing request: "
@@ -179,6 +186,9 @@ public class ServiceExecutor implements Runnable {
+ ", message: " + e.getMessage()));
} catch (IOException e1) {
// error while sending error message. Can't do anything now
+ logger.log(Level.WARNING, "Error during sending error message",
+ e);
+ e.printStackTrace();
}
}
}
@@ -189,7 +199,12 @@ public class ServiceExecutor implements Runnable {
List<Operation> operations = groupedOperations.get(msg
.getRecipientName());
if (operations == null) {
- // TODO: no such mbox, send error message?
+ // TODO: externalize message?
+ // NOTE: I assume in Erlang sender doesn't get confirmation so
+ // message will be send
+ logger.log(Level.WARNING, "Node '" + name
+ + "' received message addressed to non exising mbox: "
+ + msg.getRecipientName());
} else {
for (Operation operation : operations) {
List<DataType> iTypes = operation.getInputType().getLogical();
@@ -203,8 +218,8 @@ public class ServiceExecutor implements Runnable {
matchedOperation = operation;
break;
} catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ // this exception is expected while processing operation
+ // version with mismatched arguments
}
}
if (matchedOperation != null) {
@@ -228,22 +243,25 @@ public class ServiceExecutor implements Runnable {
mbox.send(msg.getSenderPid(), response);
}
} catch (InvocationTargetException e) {
- // TODO send some error?
+ // FIXME: use linking feature? send some error?
e.printStackTrace();
} catch (IOException e) {
- // TODO Auto-generated catch block
+ // FIXME: log this problem? use linking feature? send error?
e.printStackTrace();
}
} else {
- // TODO: send some error - no mapping for such arguments
- System.out
- .println("TODO: send some error - no mapping for such arguments");
+ // TODO: externalize message?
+ // NOTE: don't send error message if mapping not found
+ logger.log(Level.WARNING, "No mapping for such arguments in '"
+ + msg.getRecipientName() + "' operation in '" + name
+ + "' node.");
}
}
}
public void run() {
try {
+ // TODO: should receive timeout be configured in .composite file?
OtpMsg msg = connection.receiveMsg(RECEIVE_TIMEOUT);
if (msg.getRecipientName().equals(MessageHelper.RPC_MBOX)) {
handleRpc(msg);
@@ -252,8 +270,16 @@ public class ServiceExecutor implements Runnable {
} else {
// message receive timeout
}
- } catch (Exception e) {
- // TODO: log, send error?
+ } catch (IOException e) {
+ // TODO: externalize message?
+ logger.log(Level.WARNING, "Problem while receiving message", e);
+ } catch (OtpErlangExit e) {
+ // TODO: linking?
+ e.printStackTrace();
+ } catch (OtpAuthException e) {
+ // TODO: cookies?
+ } catch (InterruptedException e) {
+ // TODO: when it could happen?
e.printStackTrace();
} finally {
connection.close();
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BinaryTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BinaryTypeHelper.java
new file mode 100644
index 0000000000..7385fe64e4
--- /dev/null
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BinaryTypeHelper.java
@@ -0,0 +1,39 @@
+/*
+ * 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.types;
+
+import com.ericsson.otp.erlang.OtpErlangBinary;
+import com.ericsson.otp.erlang.OtpErlangObject;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BinaryTypeHelper implements TypeHelper {
+
+ public OtpErlangObject toErlang(Object object) {
+ return new OtpErlangBinary((byte[])object);
+ }
+
+ public Object toJava(OtpErlangObject object, Class<?> forClass)
+ throws Exception {
+ return ((OtpErlangBinary)object).binaryValue();
+ }
+
+}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java
index 3b5b30ac4d..324cd736ab 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java
@@ -23,6 +23,8 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
+import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException;
+
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;
@@ -42,7 +44,8 @@ public class TupleTypeHelper implements TypeHelper {
OtpErlangObject member = TypeHelpersProxy.toErlang(args);
tupleMembers.add(member);
} catch (Exception e) {
- // TODO Auto-generated catch block
+ // TODO: declaring toErlang method with Exception and throwing
+ // this?
e.printStackTrace();
}
}
@@ -57,8 +60,12 @@ public class TupleTypeHelper implements TypeHelper {
OtpErlangTuple tuple = (OtpErlangTuple) object;
Field[] fields = forClass.getFields();
if (fields.length != tuple.arity()) {
- // TODO: received tuple with different element count - wrong
- // message, exception!
+ throw new ErlangException(
+ "Received tuple with different element count ("
+ + tuple.arity() + ") than expected ("
+ + fields.length + ")");
+ // FIXME: JUnit this - received tuple with different element count -
+ // wrong message, exception!
}
result = forClass.newInstance();
for (int i = 0; i < tuple.arity(); i++) {
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java
index 0d0dd25a41..cbfd93796f 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java
@@ -59,15 +59,13 @@ public class TypeHelpersProxy {
primitiveTypes.put(Long.class, primitiveTypes.get(long.class));
primitiveTypes.put(Float.class, primitiveTypes.get(float.class));
primitiveTypes.put(Double.class, primitiveTypes.get(double.class));
- primitiveTypes.put(String.class, primitiveTypes.get(String.class));
+ primitiveTypes.put(byte[].class, new BinaryTypeHelper());
}
private static TypeHelper getTypeHelper(Class<?> forClass) {
- TypeHelper typeHelper = null;
- if (forClass.isArray()) {
+ TypeHelper typeHelper = primitiveTypes.get(forClass);
+ if (typeHelper == null && forClass.isArray()) {
typeHelper = new ListTypeHelper();
- } else {
- typeHelper = primitiveTypes.get(forClass);
}
if (typeHelper == null) {
typeHelper = new TupleTypeHelper();
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxInterface.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxInterface.java
index 0f477fe660..74ec613018 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxInterface.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxInterface.java
@@ -49,4 +49,7 @@ public interface MboxInterface {
String[] sendArgs(String[] arg) throws Exception;
String[][] sendArgs(String[][] arg);
+
+ byte[] sendArgs(byte[] arg);
+
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxListener.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxListener.java
index e64728b01e..0460db2c1e 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxListener.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/MboxListener.java
@@ -21,7 +21,6 @@ package org.apache.tuscany.sca.binding.erlang.testing;
import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy;
-import com.ericsson.otp.erlang.OtpErlangExit;
import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpMsg;
@@ -33,21 +32,28 @@ public class MboxListener implements Runnable {
private OtpMbox mbox;
private OtpMsg msg;
private Object response;
+ private long duration;
public MboxListener(OtpMbox mbox, Object response) {
+ this(mbox, response, 0);
+ }
+
+ public MboxListener(OtpMbox mbox, Object response, long duration) {
this.mbox = mbox;
this.response = response;
+ this.duration = duration;
}
public void run() {
try {
msg = mbox.receiveMsg();
+ Thread.sleep(duration);
if (response != null) {
Object[] args = new Object[1];
args[0] = response;
mbox.send(msg.getSenderPid(), TypeHelpersProxy.toErlang(args));
}
- } catch (OtpErlangExit e) {
+ } catch (Exception e) {
e.printStackTrace();
}
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
index 2312c5ebea..636cf124d8 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
@@ -20,6 +20,7 @@
package org.apache.tuscany.sca.binding.erlang.testing;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import java.io.IOException;
@@ -35,7 +36,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.osoa.sca.ServiceRuntimeException;
+import com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpErlangAtom;
+import com.ericsson.otp.erlang.OtpErlangBinary;
import com.ericsson.otp.erlang.OtpErlangBoolean;
import com.ericsson.otp.erlang.OtpErlangDouble;
import com.ericsson.otp.erlang.OtpErlangInt;
@@ -48,7 +51,9 @@ import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpNode;
/**
- * Test is annotated with test runner, which will ignore tests if epmd is not available
+ * Test is annotated with test runner, which will ignore tests if epmd is not
+ * available
+ *
* @version $Rev$ $Date$
*/
@RunWith(IgnorableRunner.class)
@@ -57,10 +62,16 @@ public class ReferenceServiceTestCase {
private static final String EPMD_COMMAND = "epmd";
private static MboxInterface mboxReference;
+ private static MboxInterface timeoutMboxReference;
+ private static MboxInterface cookieMboxReference;
private static ServiceInterface moduleReference;
+ private static ServiceInterface cookieModuleReference;
+ private static ServiceInterface timeoutModuleReference;
private static ServiceInterface clonedModuleReference;
private static OtpNode serNode;
+ private static OtpNode serCookieNode;
private static OtpMbox serMbox;
+ private static OtpMbox serCookieMbox;
private static OtpNode refNode;
private static OtpMbox refMbox;
private static Process epmdProcess;
@@ -74,11 +85,20 @@ public class ReferenceServiceTestCase {
SCADomain.newInstance("ErlangService.composite");
ReferenceTestComponentImpl component = domain.getService(
ReferenceTestComponentImpl.class, "ReferenceTest");
+
mboxReference = component.getMboxReference();
+ timeoutMboxReference = component.getTimeoutMboxReference();
+ cookieMboxReference = component.getCookieMboxReference();
moduleReference = component.getModuleReference();
+ cookieModuleReference = component.getCookieModuleReference();
+ timeoutModuleReference = component.getTimeoutModuleReference();
clonedModuleReference = component.getClonedModuleReference();
+
serNode = new OtpNode("MboxServer");
+ serCookieNode = new OtpNode("MboxServer");
+ serCookieNode.setCookie("cookie");
serMbox = serNode.createMbox("sendArgs");
+ serCookieMbox = serCookieNode.createMbox("sendArgs");
refNode = new OtpNode("MboxClient");
refMbox = refNode.createMbox("connector_to_SCA_mbox");
} catch (IOException e) {
@@ -268,6 +288,7 @@ public class ReferenceServiceTestCase {
*
* @throws Exception
*/
+ // TODO: this test fails sometime
@Test(timeout = 1000)
public void testMultipleArguments() throws Exception {
MboxListener mboxListener = new MboxListener(serMbox, true);
@@ -320,6 +341,30 @@ public class ReferenceServiceTestCase {
}
/**
+ * Test passing Erlang binaries
+ *
+ * @throws Exception
+ */
+ @Test(timeout = 1000)
+ public void testBinaries() throws Exception {
+ byte[] testArg = { 0, 1 };
+ MboxListener mboxListener = new MboxListener(serMbox, testArg);
+ Thread mboxThread = new Thread(mboxListener);
+ mboxThread.start();
+ byte[] testResult = mboxReference.sendArgs(testArg);
+ assertEquals(testArg.length, testResult.length);
+ for (int i = 0; i < testArg.length; i++) {
+ assertEquals(testArg[i], testResult[i]);
+ }
+ OtpErlangBinary received = (OtpErlangBinary) mboxListener.getMsg()
+ .getMsg();
+ assertEquals(testArg.length, received.size());
+ for (int i = 0; i < testArg.length; i++) {
+ assertEquals(testArg[i], received.binaryValue()[i]);
+ }
+ }
+
+ /**
* Tests passing lists
*
* @throws Exception
@@ -684,4 +729,91 @@ public class ReferenceServiceTestCase {
.elementAt(2)).booleanValue());
}
+ /**
+ * Tests timeout feature for reference binding messaging
+ *
+ * @throws Exception
+ */
+ @Test(timeout = 4000)
+ public void testMboxReferenceTimeouts() throws Exception {
+ long timeBiggerThanTimeout = 1000;
+ String stringResult = "result";
+
+ // doing test for response time bigger than declared timeout (500)
+ MboxListener mboxListener = new MboxListener(serMbox, stringResult,
+ timeBiggerThanTimeout);
+ Thread mboxThread = new Thread(mboxListener);
+ mboxThread.start();
+ try {
+ // timeout exception expected
+ timeoutMboxReference.sendArgs("");
+ fail();
+ } catch (Exception e) {
+ assertEquals(ErlangException.class, e.getClass());
+ assertEquals(e.getCause().getClass(), InterruptedException.class);
+ }
+
+ // doing test for response time smaller than declared timeout (500)
+ mboxListener = new MboxListener(serMbox, stringResult, 0);
+ mboxThread = new Thread(mboxListener);
+ mboxThread.start();
+ // expecting no timeout exception
+ String testResult = timeoutMboxReference.sendArgs("");
+ assertEquals(stringResult, testResult);
+
+ // doing test for response time which will cause timeout. This time
+ // there is no declared exception in users operation so we expect no
+ // exception and null result
+ mboxListener = new MboxListener(serMbox, new byte[1],
+ timeBiggerThanTimeout);
+ mboxThread = new Thread(mboxListener);
+ mboxThread.start();
+ // expecting no timeout exception
+ byte[] result = timeoutMboxReference.sendArgs(new byte[1]);
+ assertEquals(null, result);
+ }
+
+ /**
+ * Tests timeout feature for reference binding RPC
+ *
+ * @throws Exception
+ */
+ @Test(timeout = 4000)
+ public void testRpcReferenceTimeouts() throws Exception {
+
+ // doing test for response time which will cause timeout. Method does
+ // not
+ // declare exception so only null value will be returned
+ String result1 = timeoutModuleReference.sayHello("hello", "world");
+ assertEquals(null, result1);
+
+ // doing test for response time which will cause timeout. Method declare
+ // exception, so expecting one
+ try {
+ timeoutModuleReference.sayHellos();
+ fail();
+ } catch (Exception e) {
+ assertEquals(ErlangException.class, e.getClass());
+ }
+
+ // doing test for response time shorter than timeout
+ timeoutModuleReference.doNothing();
+ }
+
+ /**
+ * Tests timeout feature for reference binding RPC
+ *
+ * @throws Exception
+ */
+ @Test(timeout = 1000)
+ public void testReferenceCookies() throws Exception {
+ try {
+ cookieModuleReference.sayHellos();
+ fail();
+ } catch (Exception e) {
+ assertEquals(ErlangException.class, e.getClass());
+ assertEquals(OtpAuthException.class, e.getCause().getClass());
+ }
+ }
+
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceTestComponentImpl.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceTestComponentImpl.java
index 71041a2be2..f36a711f66 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceTestComponentImpl.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceTestComponentImpl.java
@@ -27,17 +27,41 @@ import org.osoa.sca.annotations.Reference;
public class ReferenceTestComponentImpl implements ReferenceTestComponent {
private MboxInterface mboxReference;
+ private MboxInterface timeoutMboxReference;
+ private MboxInterface cookieMboxReference;
private ServiceInterface moduleReference;
+ private ServiceInterface cookieModuleReference;
+ private ServiceInterface timeoutModuleReference;
private ServiceInterface clonedModuleReference;
@Reference
public void setMboxReference(MboxInterface mboxReference) {
this.mboxReference = mboxReference;
}
+
+ @Reference
+ public void setTimeoutMboxReference(MboxInterface timeoutMboxReference) {
+ this.timeoutMboxReference = timeoutMboxReference;
+ }
+
+ @Reference
+ public void setCookieMboxReference(MboxInterface cookieMboxReference) {
+ this.cookieMboxReference = cookieMboxReference;
+ }
@Reference
- public void setModuleReference(ServiceInterface moduleReference) {
- this.moduleReference = moduleReference;
+ public void setModuleReference(ServiceInterface timeoutModuleReference) {
+ this.moduleReference = timeoutModuleReference;
+ }
+
+ @Reference
+ public void setCookieModuleReference(ServiceInterface cookieModuleReference) {
+ this.cookieModuleReference = cookieModuleReference;
+ }
+
+ @Reference
+ public void setTimeoutModuleReference(ServiceInterface timeoutModuleReference) {
+ this.timeoutModuleReference = timeoutModuleReference;
}
@Reference
@@ -49,10 +73,26 @@ public class ReferenceTestComponentImpl implements ReferenceTestComponent {
return mboxReference;
}
+ public MboxInterface getTimeoutMboxReference() {
+ return timeoutMboxReference;
+ }
+
+ public MboxInterface getCookieMboxReference() {
+ return cookieMboxReference;
+ }
+
public ServiceInterface getModuleReference() {
return moduleReference;
}
+ public ServiceInterface getCookieModuleReference() {
+ return cookieModuleReference;
+ }
+
+ public ServiceInterface getTimeoutModuleReference() {
+ return timeoutModuleReference;
+ }
+
public ServiceInterface getClonedModuleReference() {
return clonedModuleReference;
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceInterface.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceInterface.java
index 95c0ea06f6..76ff3fdbe0 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceInterface.java
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceInterface.java
@@ -41,4 +41,5 @@ public interface ServiceInterface {
void notExistWithException() throws Exception;
void notExist();
+
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceTestComponentImplTimeout.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceTestComponentImplTimeout.java
new file mode 100644
index 0000000000..bcf454837e
--- /dev/null
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceTestComponentImplTimeout.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.sca.binding.erlang.testing;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ServiceTestComponentImplTimeout implements ServiceTestComponent {
+
+ private long duration = 1000;
+
+ public String sayHello(String arg1, String arg2) {
+ try {
+ Thread.sleep(duration);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return "Bye " + arg1 + " " + arg2;
+ }
+
+ public String[] sayHellos() {
+ try {
+ Thread.sleep(duration);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ String[] result = new String[] { "-1", "-2" };
+ return result;
+ }
+
+ public StructuredTuple passComplexArgs(StructuredTuple arg1, String[] arg2) {
+ try {
+ Thread.sleep(duration);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return arg1;
+ }
+
+ public void doNothing() {
+
+ }
+
+}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangReference.composite b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangReference.composite
index a27fd224c2..0d52cbae32 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangReference.composite
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangReference.composite
@@ -9,11 +9,27 @@
<reference name="mboxReference">
<tuscany:binding.erlang node="MboxServer" mbox="true"/>
</reference>
+
+ <reference name="timeoutMboxReference">
+ <tuscany:binding.erlang node="MboxServer" mbox="true" timeout="500"/>
+ </reference>
+
+ <reference name="timeoutMboxReference">
+ <tuscany:binding.erlang node="MboxServer" mbox="true" timeout="500"/>
+ </reference>
<reference name="moduleReference">
<tuscany:binding.erlang node="RPCServer" module="hello"/>
</reference>
+ <reference name="cookieModuleReference">
+ <tuscany:binding.erlang node="RPCServer" module="hello" cookie="cookie"/>
+ </reference>
+
+ <reference name="timeoutModuleReference">
+ <tuscany:binding.erlang node="RPCServer" module="hello_timeout" timeout="500"/>
+ </reference>
+
<reference name="clonedModuleReference">
<tuscany:binding.erlang node="RPCServer" module="hello_clone"/>
</reference>
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangService.composite b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangService.composite
index ce94340bb6..d946cd6141 100644
--- a/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangService.composite
+++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/test/resources/ErlangService.composite
@@ -23,5 +23,13 @@
<interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
<tuscany:binding.erlang node="RPCServer" module="hello_clone"/>
</service>
+
+ <component name="ServiceTestTimeout">
+ <implementation.java class="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponentImplTimeout" />
+ </component>
+ <service name="ServiceTestTimeout" promote="ServiceTestTimeout">
+ <interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
+ <tuscany:binding.erlang node="RPCServer" module="hello_timeout"/>
+ </service>
</composite> \ No newline at end of file