diff options
Diffstat (limited to 'sandbox/wjaniszewski/binding-erlang-runtime/src/main')
23 files changed, 677 insertions, 301 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 f823be2d8f..f4114132c1 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 @@ -32,7 +32,7 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentReference; import org.apache.tuscany.sca.runtime.RuntimeComponentService;
/**
- * @version $Rev: $ $Date: $
+ * @version $Rev$ $Date$
*/
public class ErlangBindingProviderFactory implements
BindingProviderFactory<ErlangBinding> {
@@ -40,9 +40,9 @@ public class ErlangBindingProviderFactory implements private Map<String, ErlangNode> nodes = new HashMap<String, ErlangNode>();
public ErlangBindingProviderFactory(ExtensionPointRegistry registry) {
-
+
}
-
+
/**
* @see org.apache.tuscany.sca.provider.BindingProviderFactory#createReferenceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent,
* org.apache.tuscany.sca.runtime.RuntimeComponentReference,
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 fd9e89bc2d..9ed3713db4 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 @@ -37,7 +37,7 @@ import com.ericsson.otp.erlang.OtpPeer; import com.ericsson.otp.erlang.OtpSelf;
/**
- * @version $Rev: $ $Date: $
+ * @version $Rev$ $Date$
*/
public class ErlangInvoker implements Invoker {
@@ -59,6 +59,7 @@ public class ErlangInvoker implements Invoker { tmpMbox.send(msg.getOperation().getName(), binding.getNode(),
msgPayload);
if (msg.getOperation().getOutputType() != null) {
+ // TODO: add timeouts, timeout declaration method?
OtpMsg resultMsg = tmpMbox.receiveMsg();
OtpErlangObject result = resultMsg.getMsg();
msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation()
@@ -92,14 +93,15 @@ public class ErlangInvoker implements Invoker { OtpErlangTuple message = MessageHelper.rpcMessage(self.pid(), self
.createRef(), binding.getModule(), msg.getOperation()
.getName(), params);
- connection.send("rex", message);
+ connection.send(MessageHelper.RPC_MBOX, message);
OtpErlangObject result = connection.receiveRPC();
if (MessageHelper.isfunctionUndefMessage(result)) {
// TODO: externalize message?
Exception e = new ErlangException(
"No such function in referenced Erlang node.");
if (msg.getOperation().getFaultTypes().size() == 0) {
- // TODO: no way to throw exception, log it (temporary as System.out)
+ // TODO: 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
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 a5cfbe6d90..78ffdfaa49 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 @@ -1,25 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.tuscany.sca.binding.erlang.impl; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.tuscany.sca.binding.erlang.ErlangBinding; import org.apache.tuscany.sca.binding.erlang.impl.exceptions.ErlangException; +import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.runtime.RuntimeComponentService; import com.ericsson.otp.erlang.OtpConnection; import com.ericsson.otp.erlang.OtpSelf; +/** + * @version $Rev$ $Date$ + */ public class ErlangNode implements Runnable { - private Map<String, RuntimeComponentService> services = new HashMap<String, RuntimeComponentService>(); - private Map<String, ErlangBinding> bindings = new HashMap<String, ErlangBinding>(); + private Map<String, ErlangNodeElement> erlangModules = new HashMap<String, ErlangNodeElement>(); + private ErlangNodeElement erlangMbox; + private boolean mboxNode; private String name; private OtpSelf self; private ExecutorService executors; private boolean stopRequested; + private Map<String, List<Operation>> groupedOperations; public ErlangNode(String name) throws Exception { this.name = name; @@ -42,8 +69,8 @@ public class ErlangNode implements Runnable { while (!stopRequested) { try { OtpConnection connection = self.accept(); - executors.execute(new RpcExecutor(services, bindings, - connection)); + executors.execute(new ServiceExecutor(connection, + groupedOperations, erlangModules, erlangMbox)); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); @@ -51,30 +78,59 @@ public class ErlangNode implements Runnable { } } - public void registerModule(ErlangBinding binding, + public void registerBinding(ErlangBinding binding, RuntimeComponentService service) throws ErlangException { - if (services.containsKey(binding.getModule())) { - // TODO: externalize message - // TODO: really want to throw exception? Log only? - throw new ErlangException("Module " + binding.getModule() - + " already defined under " + name - + " node. Duplicate module won't be started"); + if (binding.isMbox()) { + if (mboxNode) { + // TODO: externalize message + // TODO: really want to throw exception? Log only? + throw new ErlangException("Node " + binding.getNode() + + " already defined as mbox node"); + } else { + List<Operation> operations = service.getInterfaceContract() + .getInterface().getOperations(); + groupedOperations = new HashMap<String, List<Operation>>(); + for (Operation operation : operations) { + List<Operation> operationsGroup = groupedOperations + .get(operation.getName()); + if (operationsGroup == null) { + operationsGroup = new ArrayList<Operation>(); + groupedOperations.put(operation.getName(), + operationsGroup); + } + operationsGroup.add(operation); + } + mboxNode = true; + erlangMbox = new ErlangNodeElement(); + erlangMbox.setService(service); + erlangMbox.setBinding(binding); + } } else { - if (services.size() == 0) { - // TODO: should ErlangNode manage its thread? - Thread selfThread = new Thread(this); - selfThread.start(); + if (erlangModules.containsKey(binding.getModule())) { + // TODO: externalize message + // TODO: really want to throw exception? Log only? + throw new ErlangException("Module " + binding.getModule() + + " already defined under " + name + + " node. Duplicate module won't be started"); + } else { + if (erlangModules.size() == 0) { + // TODO: should ErlangNode manage its thread? + Thread selfThread = new Thread(this); + selfThread.start(); + } + ErlangNodeElement module = new ErlangNodeElement(); + module.setService(service); + module.setBinding(binding); + erlangModules.put(binding.getModule(), module); } - services.put(binding.getModule(), service); - bindings.put(binding.getModule(), binding); } } - public void unregisterModule(ErlangBinding binding) throws ErlangException { - if (services.containsKey(binding.getModule())) { - services.remove(binding.getModule()); - bindings.remove(binding.getModule()); - if (services.size() == 0) { + public void unregisterBinding(ErlangBinding binding) throws ErlangException { + if (erlangModules.containsKey(binding.getModule())) { + erlangModules.remove(binding.getModule()); + erlangModules.remove(binding.getModule()); + if (erlangModules.size() == 0) { stop(); } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNodeElement.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNodeElement.java new file mode 100644 index 0000000000..f945a841ec --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangNodeElement.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.erlang.impl; + +import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Holds information (RuntimeComponentService, ErlangBindin) for element (program module, mbox) binded on node. + * @version $Rev$ $Date$ + */ +public class ErlangNodeElement { + + private ErlangBinding binding; + private RuntimeComponentService service; + + public void setBinding(ErlangBinding binding) { + this.binding = binding; + } + + public ErlangBinding getBinding() { + return binding; + } + + public void setService(RuntimeComponentService service) { + this.service = service; + } + + public RuntimeComponentService getService() { + return service; + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java index 0ce9d33238..b96a4524e1 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java @@ -30,55 +30,58 @@ import org.apache.tuscany.sca.provider.ReferenceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
/**
- * @version $Rev: $ $Date: $
+ * @version $Rev$ $Date$
*/
public class ErlangReferenceBindingProvider implements ReferenceBindingProvider {
- private static final Logger logger = Logger.getLogger(ErlangReferenceBindingProvider.class.getName());
- private RuntimeComponentReference reference;
- private ErlangBinding binding;
+ private static final Logger logger = Logger
+ .getLogger(ErlangReferenceBindingProvider.class.getName());
+ private RuntimeComponentReference reference;
+ private ErlangBinding binding;
- public ErlangReferenceBindingProvider(ErlangBinding binding, RuntimeComponentReference reference) {
- this.reference = reference;
- this.binding = binding;
- }
+ public ErlangReferenceBindingProvider(ErlangBinding binding,
+ RuntimeComponentReference reference) {
+ this.reference = reference;
+ this.binding = binding;
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#createInvoker(org.apache.tuscany.sca.interfacedef.Operation)
- */
- public Invoker createInvoker(Operation operation) {
- try {
- return new ErlangInvoker(binding);
- } catch (Exception e) {
- logger.log(Level.WARNING, "Exception during creating Erlang invoker", e);
- }
- return null;
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#createInvoker(org.apache.tuscany.sca.interfacedef.Operation)
+ */
+ public Invoker createInvoker(Operation operation) {
+ try {
+ return new ErlangInvoker(binding);
+ } catch (Exception e) {
+ logger.log(Level.WARNING,
+ "Exception during creating Erlang invoker", e);
+ }
+ return null;
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#getBindingInterfaceContract()
- */
- public InterfaceContract getBindingInterfaceContract() {
- return reference.getInterfaceContract();
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#getBindingInterfaceContract()
+ */
+ public InterfaceContract getBindingInterfaceContract() {
+ return reference.getInterfaceContract();
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#start()
- */
- public void start() {
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#start()
+ */
+ public void start() {
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#stop()
- */
- public void stop() {
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#stop()
+ */
+ public void stop() {
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#supportsOneWayInvocation()
- */
- public boolean supportsOneWayInvocation() {
- return false;
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#supportsOneWayInvocation()
+ */
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java index 651067f4e7..38b9b197d6 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java @@ -26,56 +26,57 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentService; import org.osoa.sca.ServiceRuntimeException;
/**
- * @version $Rev: 685180 $ $Date: 2008-08-12 16:17:26 +0100 (Tue, 12 Aug 2008) $
+ * @version $Rev$ $Date$
*/
public class ErlangServiceBindingProvider implements ServiceBindingProvider {
- private RuntimeComponentService service;
- private ErlangNode node;
- private ErlangBinding binding;
+ private RuntimeComponentService service;
+ private ErlangNode node;
+ private ErlangBinding binding;
- public ErlangServiceBindingProvider(ErlangNode node, ErlangBinding binding, RuntimeComponentService service) {
- this.service = service;
- this.binding = binding;
- this.node = node;
- }
+ public ErlangServiceBindingProvider(ErlangNode node, ErlangBinding binding,
+ RuntimeComponentService service) {
+ this.service = service;
+ this.binding = binding;
+ this.node = node;
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#getBindingInterfaceContract()
- */
- public InterfaceContract getBindingInterfaceContract() {
- return service.getInterfaceContract();
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#getBindingInterfaceContract()
+ */
+ public InterfaceContract getBindingInterfaceContract() {
+ return service.getInterfaceContract();
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#start()
- */
- public void start() {
- try {
- node.registerModule(binding, service);
- } catch (Exception e) {
- throw new ServiceRuntimeException(e);
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#start()
+ */
+ public void start() {
+ try {
+ node.registerBinding(binding, service);
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
- }
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#stop()
- */
- public void stop() {
- try {
- node.unregisterModule(binding);
- } catch (Exception e) {
- throw new ServiceRuntimeException(e);
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#stop()
+ */
+ public void stop() {
+ try {
+ node.unregisterBinding(binding);
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
- }
+ }
- /**
- * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#supportsOneWayInvocation()
- */
- public boolean supportsOneWayInvocation() {
- return false;
- }
+ /**
+ * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#supportsOneWayInvocation()
+ */
+ public boolean supportsOneWayInvocation() {
+ return false;
+ }
}
diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/MessageHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/MessageHelper.java index 55134782d5..ee7957406b 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/MessageHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/MessageHelper.java @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.tuscany.sca.binding.erlang.impl; import com.ericsson.otp.erlang.OtpErlangAtom; @@ -7,34 +26,48 @@ import com.ericsson.otp.erlang.OtpErlangPid; import com.ericsson.otp.erlang.OtpErlangRef; import com.ericsson.otp.erlang.OtpErlangTuple; +/** + * @version $Rev$ $Date$ + */ public class MessageHelper { - private static String ATOM_BADRPC = "badrpc"; - private static String ATOM_EXIT = "EXIT"; - private static String ATOM_UNDEF = "undef"; + /** + * Mbox name used for Remote Procedure Calls + */ + public static String RPC_MBOX = "rex"; + + public static final OtpErlangAtom ATOM_OK = new OtpErlangAtom("ok"); + public static final OtpErlangAtom ATOM_ERROR = new OtpErlangAtom("error"); + public static final OtpErlangAtom ATOM_BADRPC = new OtpErlangAtom("badrpc"); + private static final OtpErlangAtom ATOM_EXIT = new OtpErlangAtom("EXIT"); + private static final OtpErlangAtom ATOM_UNDEF = new OtpErlangAtom("undef"); + private static final OtpErlangAtom ATOM_CALL = new OtpErlangAtom("call"); + private static final OtpErlangAtom ATOM_GEN_CALL = new OtpErlangAtom( + "$gen_call"); - public static OtpErlangObject functionUndefMessage(String module, String function, OtpErlangList args, String tuscanyMsg) { + public static OtpErlangObject functionUndefMessage(String module, + String function, OtpErlangList args, String tuscanyMsg) { OtpErlangObject[] args4 = new OtpErlangObject[3]; args4[0] = new OtpErlangAtom(module); args4[1] = new OtpErlangAtom(function); args4[2] = args; - + OtpErlangObject[] args3 = new OtpErlangObject[2]; args3[0] = new OtpErlangTuple(args4); args3[1] = new OtpErlangAtom(tuscanyMsg); - + OtpErlangObject[] args2 = new OtpErlangObject[2]; - args2[0] = new OtpErlangAtom(ATOM_UNDEF); + args2[0] = ATOM_UNDEF; args2[1] = new OtpErlangList(args3); - + OtpErlangObject[] args1 = new OtpErlangObject[2]; - args1[0] = new OtpErlangAtom(ATOM_EXIT); + args1[0] = ATOM_EXIT; args1[1] = new OtpErlangTuple(args2); - - OtpErlangTuple result = new OtpErlangTuple(args1); + + OtpErlangTuple result = new OtpErlangTuple(args1); return result; } - + public static boolean isfunctionUndefMessage(OtpErlangObject msg) { if (msg.getClass().equals(OtpErlangTuple.class)) { OtpErlangTuple tupleMsg = (OtpErlangTuple) msg; @@ -44,7 +77,7 @@ public class MessageHelper { && tupleMsg.elementAt(1).getClass().equals( OtpErlangTuple.class) && ((OtpErlangAtom) tupleMsg.elementAt(0)).atomValue() - .equals(ATOM_BADRPC)) { + .equals(ATOM_BADRPC.atomValue())) { OtpErlangTuple badrpcTuple = (OtpErlangTuple) tupleMsg .elementAt(1); if (badrpcTuple.arity() == 2 @@ -53,14 +86,14 @@ public class MessageHelper { && badrpcTuple.elementAt(1).getClass().equals( OtpErlangTuple.class) && ((OtpErlangAtom) badrpcTuple.elementAt(0)) - .atomValue().equals(ATOM_EXIT)) { + .atomValue().equals(ATOM_EXIT.atomValue())) { OtpErlangTuple exitTuple = (OtpErlangTuple) badrpcTuple .elementAt(1); if (exitTuple.arity() == 2 && exitTuple.elementAt(0).getClass().equals( OtpErlangAtom.class) && ((OtpErlangAtom) exitTuple.elementAt(0)) - .atomValue().equals(ATOM_UNDEF)) { + .atomValue().equals(ATOM_UNDEF.atomValue())) { return true; } } @@ -69,24 +102,25 @@ public class MessageHelper { } return false; } - - public static OtpErlangTuple rpcMessage(OtpErlangPid senderPid, OtpErlangRef ref, String module, String function, OtpErlangList args) { + + public static OtpErlangTuple rpcMessage(OtpErlangPid senderPid, + OtpErlangRef ref, String module, String function, OtpErlangList args) { OtpErlangObject[] args3 = new OtpErlangObject[5]; - args3[0] = new OtpErlangAtom("call"); + args3[0] = ATOM_CALL; args3[1] = new OtpErlangAtom(module); args3[2] = new OtpErlangAtom(function); args3[3] = args; args3[4] = senderPid; - + OtpErlangObject[] args2 = new OtpErlangObject[2]; args2[0] = senderPid; args2[1] = ref; - + OtpErlangObject[] args1 = new OtpErlangObject[3]; - args1[0] = new OtpErlangAtom("$gen_call"); + args1[0] = ATOM_GEN_CALL; args1[1] = new OtpErlangTuple(args2); args1[2] = new OtpErlangTuple(args3); - + OtpErlangTuple result = new OtpErlangTuple(args1); return result; } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java index 03e83cec61..63c58cb696 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ServiceExecutor.java @@ -38,22 +38,30 @@ import com.ericsson.otp.erlang.OtpErlangPid; import com.ericsson.otp.erlang.OtpErlangRef; import com.ericsson.otp.erlang.OtpErlangString; import com.ericsson.otp.erlang.OtpErlangTuple; +import com.ericsson.otp.erlang.OtpMbox; +import com.ericsson.otp.erlang.OtpMsg; +import com.ericsson.otp.erlang.OtpNode; -public class RpcExecutor implements Runnable { +/** + * @version $Rev$ $Date$ + */ +public class ServiceExecutor implements Runnable { - private Map<String, RuntimeComponentService> services; - private Map<String, ErlangBinding> bindings; - private OtpConnection connection; + private static final long RECEIVE_TIMEOUT = 60000; - private static final OtpErlangAtom OK = new OtpErlangAtom("ok"); - private static final OtpErlangAtom ERROR = new OtpErlangAtom("error"); - private static final OtpErlangAtom BADRPC = new OtpErlangAtom("badrpc"); + private Map<String, ErlangNodeElement> erlangModules; + private ErlangNodeElement erlangMbox; + private OtpConnection connection; + private Map<String, List<Operation>> groupedOperations; - public RpcExecutor(Map<String, RuntimeComponentService> services, Map<String, ErlangBinding> bindings, - OtpConnection connection) { - this.bindings = bindings; - this.services = services; + public ServiceExecutor(OtpConnection connection, + Map<String, List<Operation>> groupedOperations, + Map<String, ErlangNodeElement> erlangModules, + ErlangNodeElement erlangMbox) { + this.erlangModules = erlangModules; this.connection = connection; + this.groupedOperations = groupedOperations; + this.erlangMbox = erlangMbox; } private void sendMessage(OtpConnection connection, OtpErlangPid pid, @@ -66,12 +74,12 @@ public class RpcExecutor implements Runnable { connection.send(pid, msg); } - public void run() { + private void handleRpc(OtpMsg msg) { OtpErlangTuple request = null; OtpErlangPid senderPid = null; OtpErlangRef senderRef = null; try { - OtpErlangTuple call = (OtpErlangTuple) connection.receive(); + OtpErlangTuple call = (OtpErlangTuple) msg.getMsg(); OtpErlangTuple from = (OtpErlangTuple) call.elementAt(1); request = (OtpErlangTuple) call.elementAt(2); senderPid = (OtpErlangPid) from.elementAt(0); @@ -81,20 +89,23 @@ public class RpcExecutor implements Runnable { .atomValue(); OtpErlangObject args = request.elementAt(3); OtpErlangList argsList = null; + // normalize input if (args instanceof OtpErlangList) { argsList = (OtpErlangList) args; } else { argsList = new OtpErlangList(args); } - if (!services.containsKey(module)) { + if (!erlangModules.containsKey(module)) { // TODO: externalize message? OtpErlangObject errorMsg = MessageHelper.functionUndefMessage( module, function, argsList, "Module not found in SCA component."); - sendMessage(connection, senderPid, senderRef, BADRPC, errorMsg); + sendMessage(connection, senderPid, senderRef, + MessageHelper.ATOM_BADRPC, errorMsg); } else { - RuntimeComponentService service = services.get(module); - ErlangBinding binding = bindings.get(module); + RuntimeComponentService service = erlangModules.get(module) + .getService(); + ErlangBinding binding = erlangModules.get(module).getBinding(); List<Operation> operations = service.getInterfaceContract() .getInterface().getOperations(); Operation operation = null; @@ -128,8 +139,8 @@ public class RpcExecutor implements Runnable { Object[] arrArg = new Object[] { result }; response = TypeHelpersProxy.toErlang(arrArg); } - sendMessage(connection, senderPid, senderRef, OK, - response); + sendMessage(connection, senderPid, senderRef, + MessageHelper.ATOM_OK, response); } catch (Exception e) { if ((e.getClass().equals( InvocationTargetException.class) && e @@ -143,7 +154,7 @@ public class RpcExecutor implements Runnable { argsList, "Operation name found in SCA component, but parameters types didn't match."); sendMessage(connection, senderPid, senderRef, - BADRPC, errorMsg); + MessageHelper.ATOM_BADRPC, errorMsg); } else { throw e; } @@ -153,22 +164,97 @@ public class RpcExecutor implements Runnable { OtpErlangObject errorMsg = MessageHelper .functionUndefMessage(module, function, argsList, "Operation name not found in SCA component."); - sendMessage(connection, senderPid, senderRef, BADRPC, - errorMsg); + sendMessage(connection, senderPid, senderRef, + MessageHelper.ATOM_BADRPC, errorMsg); } } } catch (Exception e) { // TODO: distinguish and describe errors! try { e.printStackTrace(); - sendMessage(connection, senderPid, senderRef, ERROR, - new OtpErlangString( + sendMessage(connection, senderPid, senderRef, + MessageHelper.ATOM_ERROR, new OtpErlangString( "Unhandled error while processing request: " + e.getClass().getCanonicalName() + ", message: " + e.getMessage())); } catch (IOException e1) { // error while sending error message. Can't do anything now } + } + } + + private void handleMsg(OtpMsg msg) { + Operation matchedOperation = null; + Object args[] = null; + List<Operation> operations = groupedOperations.get(msg + .getRecipientName()); + if (operations == null) { + // TODO: no such mbox, send error message? + } else { + for (Operation operation : operations) { + List<DataType> iTypes = operation.getInputType().getLogical(); + Class<?>[] forClasses = new Class<?>[iTypes.size()]; + for (int i = 0; i < iTypes.size(); i++) { + forClasses[i] = iTypes.get(i).getPhysical(); + } + try { + args = TypeHelpersProxy.toJavaAsArgs(msg.getMsg(), + forClasses); + matchedOperation = operation; + break; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + if (matchedOperation != null) { + try { + Object result = erlangMbox.getService().getRuntimeWire( + erlangMbox.getBinding()).invoke(matchedOperation, + args); + OtpErlangObject response = null; + if (matchedOperation.getOutputType() != null + && matchedOperation.getOutputType().getPhysical() + .isArray()) { + response = TypeHelpersProxy.toErlangAsList(result); + } else if (matchedOperation.getOutputType() != null) { + Object[] arrArg = new Object[] { result }; + response = TypeHelpersProxy.toErlang(arrArg); + } + if (response != null) { + OtpNode node = new OtpNode("_response_connector_to_" + + msg.getSenderPid()); + OtpMbox mbox = node.createMbox(); + mbox.send(msg.getSenderPid(), response); + } + } catch (InvocationTargetException e) { + // TODO send some error? + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else { + // TODO: send some error - no mapping for such arguments + System.out + .println("TODO: send some error - no mapping for such arguments"); + } + } + } + + public void run() { + try { + OtpMsg msg = connection.receiveMsg(RECEIVE_TIMEOUT); + if (msg.getRecipientName().equals(MessageHelper.RPC_MBOX)) { + handleRpc(msg); + } else if (msg != null) { + handleMsg(msg); + } else { + // message receive timeout + } + } catch (Exception e) { + // TODO: log, send error? + e.printStackTrace(); } finally { connection.close(); } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java index 03b53b60bd..b3562cc413 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java @@ -19,28 +19,32 @@ package org.apache.tuscany.sca.binding.erlang.impl; +/** + * @version $Rev$ $Date$ + */ public class TypeMismatchException extends Exception { - private static final long serialVersionUID = 1L; - private Class<?> expected; - private Class<?> received; - - public TypeMismatchException(Class<?> expected, Class<?> received) { - super("Received " + received + " cannot be mapped to " + expected.getCanonicalName()); - this.expected = expected; - this.received = received; - } - - public TypeMismatchException() { - super("Arguments don't match"); - } - - public Class<?> getExpected() { - return expected; - } - - public Class<?> getReceived() { - return received; - } + private static final long serialVersionUID = 1L; + private Class<?> expected; + private Class<?> received; + + public TypeMismatchException(Class<?> expected, Class<?> received) { + super("Received " + received + " cannot be mapped to " + + expected.getCanonicalName()); + this.expected = expected; + this.received = received; + } + + public TypeMismatchException() { + super("Arguments don't match"); + } + + public Class<?> getExpected() { + return expected; + } + + public Class<?> getReceived() { + return received; + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java index cf573def95..587d7a509c 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/exceptions/ErlangException.java @@ -1,5 +1,27 @@ +/* + * 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.exceptions; +/** + * @version $Rev$ $Date$ + */ public class ErlangException extends Exception { private static final long serialVersionUID = 1L; @@ -7,9 +29,9 @@ public class ErlangException extends Exception { public ErlangException(String message) { super(message); } - + public ErlangException(String message, Throwable cause) { super(message, cause); } - + } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java index dd9f65b4e9..9463b72eb9 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java @@ -23,18 +23,22 @@ import com.ericsson.otp.erlang.OtpErlangAtom; import com.ericsson.otp.erlang.OtpErlangBoolean; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class BooleanTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangBoolean((Boolean)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangBoolean((Boolean) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - if (object.getClass().equals(OtpErlangAtom.class)) { - return ((OtpErlangAtom)object).booleanValue(); - } else { - return ((OtpErlangBoolean)object).booleanValue(); - } - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + if (object.getClass().equals(OtpErlangAtom.class)) { + return ((OtpErlangAtom) object).booleanValue(); + } else { + return ((OtpErlangBoolean) object).booleanValue(); + } + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java index d4a92e768a..c6c8f6690f 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java @@ -23,13 +23,17 @@ import com.ericsson.otp.erlang.OtpErlangByte; import com.ericsson.otp.erlang.OtpErlangLong; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class ByteTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangByte((Byte)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangByte((Byte) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return (byte)((OtpErlangLong)object).longValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return (byte) ((OtpErlangLong) object).longValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java index 17100c65b8..b0479c9b0c 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java @@ -23,14 +23,18 @@ import com.ericsson.otp.erlang.OtpErlangChar; import com.ericsson.otp.erlang.OtpErlangLong; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class CharTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangChar((Character)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangChar((Character) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return (char)((OtpErlangLong)object).longValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return (char) ((OtpErlangLong) object).longValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java index 760fb0723b..92dd4749ac 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java @@ -22,14 +22,18 @@ package org.apache.tuscany.sca.binding.erlang.impl.types; import com.ericsson.otp.erlang.OtpErlangDouble; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class DoubleTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangDouble((Double)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Double) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return ((OtpErlangDouble)object).doubleValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return ((OtpErlangDouble) object).doubleValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java index 851d0982f3..3ee695c3cc 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java @@ -22,14 +22,18 @@ package org.apache.tuscany.sca.binding.erlang.impl.types; import com.ericsson.otp.erlang.OtpErlangDouble; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class FloatTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangDouble((Float)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Float) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return (float)((OtpErlangDouble)object).doubleValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return (float) ((OtpErlangDouble) object).doubleValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java index 3e79577b1e..3ee8e7d9ac 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java @@ -23,14 +23,18 @@ import com.ericsson.otp.erlang.OtpErlangInt; import com.ericsson.otp.erlang.OtpErlangLong; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class IntTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangInt((Integer)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangInt((Integer) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return (int)((OtpErlangLong)object).longValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return (int) ((OtpErlangLong) object).longValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java index 86dcd4fc0f..adac10bcef 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java @@ -26,32 +26,39 @@ import java.util.List; import com.ericsson.otp.erlang.OtpErlangList; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class ListTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - int i = 0; - List<OtpErlangObject> elements = new ArrayList<OtpErlangObject>(); - while (true) { - try { - Object arrElement = Array.get(object, i); - Object[] args = new Object[] {arrElement}; - elements.add(TypeHelpersProxy.toErlang(args)); - i++; - } catch (ArrayIndexOutOfBoundsException e) { - // expected - break; - } - } - return new OtpErlangList(elements.toArray(new OtpErlangObject[elements.size()])); - } + public OtpErlangObject toErlang(Object object) { + int i = 0; + List<OtpErlangObject> elements = new ArrayList<OtpErlangObject>(); + while (true) { + try { + Object arrElement = Array.get(object, i); + Object[] args = new Object[] { arrElement }; + elements.add(TypeHelpersProxy.toErlang(args)); + i++; + } catch (ArrayIndexOutOfBoundsException e) { + // expected + break; + } + } + return new OtpErlangList(elements.toArray(new OtpErlangObject[elements + .size()])); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - OtpErlangList erlangList = (OtpErlangList)object; - Object result = Array.newInstance(forClass.getComponentType(), erlangList.arity()); - for (int i = 0; i < erlangList.arity(); i++) { - Array.set(result, i, TypeHelpersProxy.toJava(erlangList.elementAt(i), forClass.getComponentType())); - } - return result; - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + OtpErlangList erlangList = (OtpErlangList) object; + Object result = Array.newInstance(forClass.getComponentType(), + erlangList.arity()); + for (int i = 0; i < erlangList.arity(); i++) { + Array.set(result, i, TypeHelpersProxy.toJava(erlangList + .elementAt(i), forClass.getComponentType())); + } + return result; + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java index 1437c8f8da..711057569a 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java @@ -22,14 +22,18 @@ package org.apache.tuscany.sca.binding.erlang.impl.types; import com.ericsson.otp.erlang.OtpErlangLong; import com.ericsson.otp.erlang.OtpErlangObject; +/** + * @version $Rev$ $Date$ + */ public class LongTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangLong((Long)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangLong((Long) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return ((OtpErlangLong)object).longValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return ((OtpErlangLong) object).longValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java index 594875f348..73a68691a4 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java @@ -23,14 +23,18 @@ import com.ericsson.otp.erlang.OtpErlangLong; import com.ericsson.otp.erlang.OtpErlangObject; import com.ericsson.otp.erlang.OtpErlangShort; +/** + * @version $Rev$ $Date$ + */ public class ShortTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangShort((Short)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangShort((Short) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return (short)((OtpErlangLong)object).longValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return (short) ((OtpErlangLong) object).longValue(); + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java index 8a10e1818a..001ec47655 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java @@ -22,14 +22,18 @@ package org.apache.tuscany.sca.binding.erlang.impl.types; import com.ericsson.otp.erlang.OtpErlangObject; import com.ericsson.otp.erlang.OtpErlangString; +/** + * @version $Rev$ $Date$ + */ public class StringTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - return new OtpErlangString((String)object); - } + public OtpErlangObject toErlang(Object object) { + return new OtpErlangString((String) object); + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - return ((OtpErlangString)object).stringValue(); - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + return ((OtpErlangString) object).stringValue(); + } } 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 86a78ccf87..3b5b30ac4d 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 @@ -26,43 +26,49 @@ import java.util.List; import com.ericsson.otp.erlang.OtpErlangObject; import com.ericsson.otp.erlang.OtpErlangTuple; +/** + * @version $Rev$ $Date$ + */ public class TupleTypeHelper implements TypeHelper { - public OtpErlangObject toErlang(Object object) { - Class<?> forClass = object.getClass(); - List<OtpErlangObject> tupleMembers = new ArrayList<OtpErlangObject>(); - Field[] fields = forClass.getFields(); - for (int i = 0; i < fields.length; i++) { - Object[] args; - try { - args = new Object[] {fields[i].get(object)}; - OtpErlangObject member = TypeHelpersProxy.toErlang(args); - tupleMembers.add(member); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - OtpErlangObject result = new OtpErlangTuple(tupleMembers.toArray(new OtpErlangObject[tupleMembers.size()])); - return result; - } + public OtpErlangObject toErlang(Object object) { + Class<?> forClass = object.getClass(); + List<OtpErlangObject> tupleMembers = new ArrayList<OtpErlangObject>(); + Field[] fields = forClass.getFields(); + for (int i = 0; i < fields.length; i++) { + Object[] args; + try { + args = new Object[] { fields[i].get(object) }; + OtpErlangObject member = TypeHelpersProxy.toErlang(args); + tupleMembers.add(member); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + OtpErlangObject result = new OtpErlangTuple(tupleMembers + .toArray(new OtpErlangObject[tupleMembers.size()])); + return result; + } - public Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception { - Object result = null; - OtpErlangTuple tuple = (OtpErlangTuple)object; - Field[] fields = forClass.getFields(); - if (fields.length != tuple.arity()) { - // TODO: received tuple with different element count - wrong - // message, exception! - } - result = forClass.newInstance(); - for (int i = 0; i < tuple.arity(); i++) { - OtpErlangObject tupleMember = tuple.elementAt(i); - Object javaMember = TypeHelpersProxy.toJava(tupleMember, fields[i].getType()); - fields[i].setAccessible(true); - fields[i].set(result, javaMember); - } - return result; - } + public Object toJava(OtpErlangObject object, Class<?> forClass) + throws Exception { + Object result = null; + OtpErlangTuple tuple = (OtpErlangTuple) object; + Field[] fields = forClass.getFields(); + if (fields.length != tuple.arity()) { + // TODO: received tuple with different element count - wrong + // message, exception! + } + result = forClass.newInstance(); + for (int i = 0; i < tuple.arity(); i++) { + OtpErlangObject tupleMember = tuple.elementAt(i); + Object javaMember = TypeHelpersProxy.toJava(tupleMember, fields[i] + .getType()); + fields[i].setAccessible(true); + fields[i].set(result, javaMember); + } + return result; + } } diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java index 2d32ed586b..add885ef6e 100644 --- a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java @@ -21,10 +21,13 @@ package org.apache.tuscany.sca.binding.erlang.impl.types; import com.ericsson.otp.erlang.OtpErlangObject;
+/**
+ * @version $Rev$ $Date$
+ */
public interface TypeHelper {
- Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception;
+ Object toJava(OtpErlangObject object, Class<?> forClass) throws Exception;
- OtpErlangObject toErlang(Object object);
+ OtpErlangObject toErlang(Object object);
}
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 208216eef2..0d0dd25a41 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 @@ -31,6 +31,9 @@ import com.ericsson.otp.erlang.OtpErlangList; import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;
+/**
+ * @version $Rev$ $Date$
+ */
public class TypeHelpersProxy {
private static Map<Class<?>, TypeHelper> primitiveTypes = null;
@@ -72,6 +75,13 @@ public class TypeHelpersProxy { return typeHelper;
}
+ /**
+ * Converts Java objects arrays to Erlang: 1. single object (if array arity
+ * == 1) or 2. tuple (if array arity > 1)
+ *
+ * @param objects
+ * @return
+ */
public static OtpErlangObject toErlang(Object[] objects) {
OtpErlangObject result = null;
if (objects != null) {
@@ -97,12 +107,15 @@ public class TypeHelpersProxy { return result;
}
+ /**
+ * Creates Erlang list basing on unknown Java arrays
+ *
+ * @param array
+ * @return
+ */
public static OtpErlangList toErlangAsList(Object array) {
OtpErlangList result = null;
if (array != null) {
- if (!array.getClass().isArray()) {
- array = new Object[] { array };
- }
List<OtpErlangObject> attrsList = new ArrayList<OtpErlangObject>();
int i = 0;
while (true) {
@@ -123,6 +136,14 @@ public class TypeHelpersProxy { return result;
}
+ /**
+ * Converts single Erlang object to Java
+ *
+ * @param object
+ * @param forClass
+ * @return
+ * @throws Exception
+ */
public static Object toJava(OtpErlangObject object, Class<?> forClass)
throws Exception {
try {
@@ -133,6 +154,14 @@ public class TypeHelpersProxy { }
}
+ /**
+ * Creates array of Java objects from Erlang list
+ *
+ * @param objects
+ * @param forClass
+ * @return
+ * @throws Exception
+ */
public static Object[] toJavaFromList(OtpErlangList objects,
Class<?>[] forClass) throws Exception {
Object[] result = new Object[objects.arity()];
@@ -151,4 +180,41 @@ public class TypeHelpersProxy { return result;
}
+ /**
+ * Converts incoming Erlang message to operation arguments
+ *
+ * @param objects
+ * @param forClass
+ * @return
+ * @throws Exception
+ */
+ public static Object[] toJavaAsArgs(OtpErlangObject objects,
+ Class<?>[] forClass) throws Exception {
+ OtpErlangObject[] args = null;
+ // normalize input
+ if (objects.getClass().equals(OtpErlangTuple.class)) {
+ args = new OtpErlangObject[((OtpErlangTuple) objects).arity()];
+ for (int i = 0; i < ((OtpErlangTuple) objects).arity(); i++) {
+ args[i] = ((OtpErlangTuple) objects).elementAt(i);
+ }
+ } else {
+ args = new OtpErlangObject[1];
+ args[0] = objects;
+ }
+ Object[] result = new Object[args.length];
+ try {
+ for (int i = 0; i < args.length; i++) {
+ TypeHelper helper = getTypeHelper(forClass[i]);
+ result[i] = helper.toJava(args[i], forClass[i]);
+ }
+ } catch (Exception e) {
+ // type mismatch as mismatch of parameters count or parameters type
+ if (e.getClass().equals(ClassCastException.class)
+ || e.getClass()
+ .equals(ArrayIndexOutOfBoundsException.class))
+ throw new TypeMismatchException();
+ }
+ return result;
+ }
+
}
|