From 420526884c2571aa4b17c69e98128bfd0612046e Mon Sep 17 00:00:00 2001 From: wjaniszewski Date: Thu, 19 Mar 2009 21:28:08 +0000 Subject: 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 --- .../erlang/impl/ErlangBindingProviderFactory.java | 7 +- .../sca/binding/erlang/impl/ErlangInvoker.java | 91 ++++++++--- .../sca/binding/erlang/impl/ErlangNode.java | 34 ++-- .../sca/binding/erlang/impl/ServiceExecutor.java | 54 ++++-- .../erlang/impl/types/BinaryTypeHelper.java | 39 +++++ .../binding/erlang/impl/types/TupleTypeHelper.java | 13 +- .../erlang/impl/types/TypeHelpersProxy.java | 8 +- .../sca/binding/erlang/testing/MboxInterface.java | 3 + .../sca/binding/erlang/testing/MboxListener.java | 10 +- .../erlang/testing/ReferenceServiceTestCase.java | 134 ++++++++++++++- .../erlang/testing/ReferenceTestComponentImpl.java | 44 ++++- .../binding/erlang/testing/ServiceInterface.java | 1 + .../testing/ServiceTestComponentImplTimeout.java | 61 +++++++ .../src/test/resources/ErlangReference.composite | 16 ++ .../src/test/resources/ErlangService.composite | 8 + .../tuscany/sca/binding/erlang/ErlangBinding.java | 8 + .../sca/binding/erlang/impl/ErlangBindingImpl.java | 181 ++++++++++++--------- .../erlang/impl/ErlangBindingProcessor.java | 122 +++++++------- .../testing/ErlangBindingProcessorTestCase.java | 52 ++++++ 19 files changed, 688 insertions(+), 198 deletions(-) create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BinaryTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ServiceTestComponentImplTimeout.java (limited to 'sandbox/wjaniszewski') 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 { private Map nodes = new HashMap(); + 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 erlangModules = new HashMap(); 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 erlangModules; private ErlangNodeElement erlangMbox; private OtpConnection connection; private Map> groupedOperations; + private String name; public ServiceExecutor(OtpConnection connection, Map> groupedOperations, Map 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 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 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); @@ -319,6 +340,30 @@ public class ReferenceServiceTestCase { .booleanValue()); } + /** + * 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 * @@ -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 @@ + + + + + + + + + + + + + + + + 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 @@ + + + + + + + + \ No newline at end of file diff --git a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/ErlangBinding.java b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/ErlangBinding.java index c1c699cffe..a6bb801005 100644 --- a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/ErlangBinding.java +++ b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/ErlangBinding.java @@ -42,5 +42,13 @@ public interface ErlangBinding extends Binding { boolean isMbox(); void setMbox(boolean mbox); + + long getTimeout(); + + void setTimeout(long timeout); + + String getCookie(); + + void setCookie(String cookie); } diff --git a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingImpl.java b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingImpl.java index 36b8df9fab..ad1bec06d3 100644 --- a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingImpl.java +++ b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingImpl.java @@ -33,83 +33,108 @@ import org.apache.tuscany.sca.policy.PolicySetAttachPoint; */ public class ErlangBindingImpl implements ErlangBinding, PolicySetAttachPoint { - private String node; - private String module; - private boolean mbox; - - private List requiredIntents = new ArrayList(); - private List policySets = new ArrayList(); - private IntentAttachPointType intentAttachPointType; - private List applicablePolicySets = new ArrayList(); - - public String getNode() { - return node; - } - - public void setNode(String nodeName) { - this.node = nodeName; - } - - public String getName() { - return null; - } - - public String getURI() { - return null; - } - - public void setName(String arg0) { - } - - public void setURI(String arg0) { - } - - public boolean isUnresolved() { - return false; - } - - public void setUnresolved(boolean arg0) { - } - - public List getApplicablePolicySets() { - return applicablePolicySets; - } - - public List getPolicySets() { - return policySets; - } - - public List getRequiredIntents() { - return requiredIntents; - } - - public IntentAttachPointType getType() { - return intentAttachPointType; - } - - public void setType(IntentAttachPointType intentAttachPointType) { - this.intentAttachPointType = intentAttachPointType; - } - - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } - - public String getModule() { - return module; - } - - public boolean isMbox() { - return mbox; - } - - public void setMbox(boolean mbox) { - this.mbox = mbox; - } - - public void setModule(String module) { - this.module = module; - } + public static final long DEFAULT_TIMEOUT = 10000; + + private String node; + private String module; + private boolean mbox; + private String cookie; + + private List requiredIntents = new ArrayList(); + private List policySets = new ArrayList(); + private IntentAttachPointType intentAttachPointType; + private List applicablePolicySets = new ArrayList(); + private long timeout = DEFAULT_TIMEOUT; + + public String getNode() { + return node; + } + + public void setNode(String nodeName) { + this.node = nodeName; + } + + public String getName() { + return null; + } + + public String getURI() { + return null; + } + + public void setName(String arg0) { + } + + public void setURI(String arg0) { + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean arg0) { + } + + public List getApplicablePolicySets() { + return applicablePolicySets; + } + + public List getPolicySets() { + return policySets; + } + + public List getRequiredIntents() { + return requiredIntents; + } + + public IntentAttachPointType getType() { + return intentAttachPointType; + } + + public void setType(IntentAttachPointType intentAttachPointType) { + this.intentAttachPointType = intentAttachPointType; + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + public String getModule() { + return module; + } + + public boolean isMbox() { + return mbox; + } + + public void setMbox(boolean mbox) { + this.mbox = mbox; + } + + public void setModule(String module) { + this.module = module; + } + + public long getTimeout() { + return timeout; + } + + public void setTimeout(long timeout) { + // NOTE: 0 timeout will cause setting to default + if (timeout == 0) { + this.timeout = DEFAULT_TIMEOUT; + } else { + this.timeout = timeout; + } + } + + public String getCookie() { + return cookie; + } + + public void setCookie(String cookie) { + this.cookie = cookie; + } } diff --git a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProcessor.java b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProcessor.java index f601e3327c..b611cb2296 100644 --- a/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProcessor.java +++ b/sandbox/wjaniszewski/binding-erlang/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProcessor.java @@ -38,69 +38,77 @@ import org.apache.tuscany.sca.policy.PolicyFactory; /** * @version $Rev: $ $Date: $ */ -public class ErlangBindingProcessor implements StAXArtifactProcessor { +public class ErlangBindingProcessor implements + StAXArtifactProcessor { - private PolicyFactory policyFactory; - private PolicyAttachPointProcessor policyProcessor; - - public ErlangBindingProcessor(ModelFactoryExtensionPoint modelFactories) { - this.policyFactory = modelFactories.getFactory(PolicyFactory.class); - this.policyProcessor = new PolicyAttachPointProcessor(policyFactory); - } + private PolicyFactory policyFactory; + private PolicyAttachPointProcessor policyProcessor; - /** - * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#getArtifactType() - */ - public QName getArtifactType() { - return ErlangBinding.BINDING_ERLANG_QNAME; - } + public ErlangBindingProcessor(ModelFactoryExtensionPoint modelFactories) { + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.policyProcessor = new PolicyAttachPointProcessor(policyFactory); + } - /** - * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#read(javax.xml.stream.XMLStreamReader) - */ - public ErlangBinding read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { - ErlangBinding binding = new ErlangBindingImpl(); + /** + * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#getArtifactType() + */ + public QName getArtifactType() { + return ErlangBinding.BINDING_ERLANG_QNAME; + } - // Read the policies - policyProcessor.readPolicies(binding, reader); - binding.setNode(reader.getAttributeValue(null, "node")); - String mboxValue = reader.getAttributeValue(null, "mbox"); - if (mboxValue != null && mboxValue.length() > 0) { - boolean boolMboxValue = false; - try { - boolMboxValue = Boolean.parseBoolean(mboxValue); - binding.setMbox(boolMboxValue); - } catch (Exception e) { - e.printStackTrace(); - } - } - binding.setModule(reader.getAttributeValue(null, "module")); - return binding; - } + /** + * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#read(javax.xml.stream.XMLStreamReader) + */ + public ErlangBinding read(XMLStreamReader reader) + throws ContributionReadException, XMLStreamException { + ErlangBinding binding = new ErlangBindingImpl(); - /** - * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#write(java.lang.Object, - * javax.xml.stream.XMLStreamWriter) - */ - public void write(ErlangBinding model, XMLStreamWriter writer) throws ContributionWriteException, - XMLStreamException { - writer.writeStartElement(Constants.SCA10_TUSCANY_NS, "binding.erlang"); - //TODO: implement - writer.writeEndElement(); - } + // Read the policies + policyProcessor.readPolicies(binding, reader); + binding.setNode(reader.getAttributeValue(null, "node")); + String mboxValue = reader.getAttributeValue(null, "mbox"); + if (mboxValue != null && mboxValue.length() > 0) { + try { + boolean boolMboxValue = Boolean.parseBoolean(mboxValue); + binding.setMbox(boolMboxValue); + } catch (Exception e) { + } + } + String timeoutValue = reader.getAttributeValue(null, "timeout"); + try { + long longTimeoutValue = Long.parseLong(timeoutValue); + binding.setTimeout(longTimeoutValue); + } catch (NumberFormatException e) { + } + binding.setModule(reader.getAttributeValue(null, "module")); + binding.setCookie(reader.getAttributeValue(null, "cookie")); + return binding; + } - /** - * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#getModelType() - */ - public Class getModelType() { - return ErlangBinding.class; - } + /** + * @see org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor#write(java.lang.Object, + * javax.xml.stream.XMLStreamWriter) + */ + public void write(ErlangBinding model, XMLStreamWriter writer) + throws ContributionWriteException, XMLStreamException { + writer.writeStartElement(Constants.SCA10_TUSCANY_NS, "binding.erlang"); + // TODO: implement writing binding element + writer.writeEndElement(); + } - /** - * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#resolve(java.lang.Object, - * org.apache.tuscany.sca.contribution.resolver.ModelResolver) - */ - public void resolve(ErlangBinding model, ModelResolver resolver) throws ContributionResolveException { - } + /** + * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#getModelType() + */ + public Class getModelType() { + return ErlangBinding.class; + } + + /** + * @see org.apache.tuscany.sca.contribution.processor.ArtifactProcessor#resolve(java.lang.Object, + * org.apache.tuscany.sca.contribution.resolver.ModelResolver) + */ + public void resolve(ErlangBinding model, ModelResolver resolver) + throws ContributionResolveException { + } } diff --git a/sandbox/wjaniszewski/binding-erlang/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ErlangBindingProcessorTestCase.java b/sandbox/wjaniszewski/binding-erlang/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ErlangBindingProcessorTestCase.java index 72574b2fb6..6a229d1ea2 100644 --- a/sandbox/wjaniszewski/binding-erlang/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ErlangBindingProcessorTestCase.java +++ b/sandbox/wjaniszewski/binding-erlang/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ErlangBindingProcessorTestCase.java @@ -28,6 +28,7 @@ import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.binding.erlang.impl.ErlangBindingImpl; import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -46,6 +47,16 @@ import org.junit.Test; public class ErlangBindingProcessorTestCase { private static final String COMPOSITE = + "" + "" + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + + private static final String COMPOSITE_DEFAULT_TIMEOUT = "" + "" + " " + " " @@ -54,6 +65,16 @@ public class ErlangBindingProcessorTestCase { + " " + " " + ""; + + private static final String COMPOSITE_ZERO_TIMEOUT = + "" + "" + + " " + + " " + + " " + + " " + + " " + + " " + + ""; private static XMLInputFactory inputFactory; private static StAXArtifactProcessor staxProcessor; @@ -87,6 +108,37 @@ public class ErlangBindingProcessorTestCase { ErlangBinding binding = (ErlangBinding)composite.getComponents().get(0).getServices().get(0).getBindings().get(0); assertEquals("SomeNode", binding.getNode()); + assertEquals(1000, binding.getTimeout()); + assertEquals("cookie", binding.getCookie()); + } + + /** + * Tests using default "resultTimeout" value + * + * @throws Exception + */ + @Test + public void testLoadDefaultTimeout() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE_DEFAULT_TIMEOUT)); + Composite composite = (Composite)staxProcessor.read(reader); + ErlangBinding binding = + (ErlangBinding)composite.getComponents().get(0).getServices().get(0).getBindings().get(0); + assertEquals(ErlangBindingImpl.DEFAULT_TIMEOUT, binding.getTimeout()); + assertEquals(null, binding.getCookie()); } + /** + * Tests "resultTimeout" attribute set to 0 + * + * @throws Exception + */ + @Test + public void testLoadZeroTimeout() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(COMPOSITE_ZERO_TIMEOUT)); + Composite composite = (Composite)staxProcessor.read(reader); + ErlangBinding binding = + (ErlangBinding)composite.getComponents().get(0).getServices().get(0).getBindings().get(0); + assertEquals(ErlangBindingImpl.DEFAULT_TIMEOUT, binding.getTimeout()); + } + } -- cgit v1.2.3