From 77dfc8e1d853ce9d14dde770825b61c58cd455b1 Mon Sep 17 00:00:00 2001 From: wjaniszewski Date: Fri, 20 Feb 2009 22:21:20 +0000 Subject: Erlang binding sandbox git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@746388 13f79535-47bb-0310-9956-ffa450edef68 --- .../erlang/impl/ErlangBindingProviderFactory.java | 64 +++++++++ .../sca/binding/erlang/impl/ErlangInvoker.java | 117 ++++++++++++++++ .../impl/ErlangReferenceBindingProvider.java | 84 ++++++++++++ .../erlang/impl/ErlangServiceBindingProvider.java | 81 ++++++++++++ .../sca/binding/erlang/impl/RpcExecutor.java | 143 ++++++++++++++++++++ .../tuscany/sca/binding/erlang/impl/RpcServer.java | 61 +++++++++ .../binding/erlang/impl/TypeMismatchException.java | 42 ++++++ .../erlang/impl/types/BooleanTypeHelper.java | 40 ++++++ .../binding/erlang/impl/types/ByteTypeHelper.java | 35 +++++ .../binding/erlang/impl/types/CharTypeHelper.java | 36 +++++ .../erlang/impl/types/DoubleTypeHelper.java | 35 +++++ .../binding/erlang/impl/types/FloatTypeHelper.java | 35 +++++ .../binding/erlang/impl/types/IntTypeHelper.java | 36 +++++ .../binding/erlang/impl/types/ListTypeHelper.java | 57 ++++++++ .../binding/erlang/impl/types/LongTypeHelper.java | 35 +++++ .../binding/erlang/impl/types/ShortTypeHelper.java | 36 +++++ .../erlang/impl/types/StringTypeHelper.java | 35 +++++ .../binding/erlang/impl/types/TupleTypeHelper.java | 68 ++++++++++ .../sca/binding/erlang/impl/types/TypeHelper.java | 30 +++++ .../erlang/impl/types/TypeHelpersProxy.java | 147 +++++++++++++++++++++ 20 files changed, 1217 insertions(+) create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java create mode 100644 sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java (limited to 'sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany') 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 new file mode 100644 index 0000000000..8953112aaa --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangBindingProviderFactory.java @@ -0,0 +1,64 @@ +/* + * 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.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * @version $Rev: $ $Date: $ + */ +public class ErlangBindingProviderFactory implements BindingProviderFactory { + + public ErlangBindingProviderFactory(ExtensionPointRegistry registry) { + + } + /** + * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createReferenceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentReference, org.apache.tuscany.sca.assembly.Binding) + */ + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + ErlangBinding binding) { + return new ErlangReferenceBindingProvider(binding, reference); + } + + /** + * @see org.apache.tuscany.sca.provider.BindingProviderFactory#createServiceBindingProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentService, org.apache.tuscany.sca.assembly.Binding) + */ + public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + ErlangBinding binding) { + return new ErlangServiceBindingProvider(binding, service); + } + + /** + * @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType() + */ + public Class getModelType() { + return ErlangBinding.class; + } + +} 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 new file mode 100644 index 0000000000..7e963cc34a --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangInvoker.java @@ -0,0 +1,117 @@ +/* + * 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.binding.erlang.impl.types.TypeHelpersProxy; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + +import com.ericsson.otp.erlang.OtpConnection; +import com.ericsson.otp.erlang.OtpEpmd; +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpMbox; +import com.ericsson.otp.erlang.OtpMsg; +import com.ericsson.otp.erlang.OtpNode; +import com.ericsson.otp.erlang.OtpPeer; +import com.ericsson.otp.erlang.OtpSelf; + +/** + * @version $Rev: $ $Date: $ + */ +public class ErlangInvoker implements Invoker { + + private RuntimeComponentReference reference; + private ErlangBinding binding; + private OtpNode node; + + public ErlangInvoker(RuntimeComponentReference reference, ErlangBinding binding) { + this.reference = reference; + this.binding = binding; + } + + private Message sendMessage(Message msg) { + OtpMbox tmpMbox = null; + try { + String nodeName = "_connector_to_" + binding.getNode(); + node = new OtpNode(nodeName); + tmpMbox = node.createMbox(); + Object[] args = msg.getBody(); + OtpErlangObject msgPayload = TypeHelpersProxy.toErlang(args); + tmpMbox.send(msg.getOperation().getName(), binding.getNode(), msgPayload); + if (msg.getOperation().getOutputType() != null) { + OtpMsg resultMsg = tmpMbox.receiveMsg(); + OtpErlangObject result = resultMsg.getMsg(); + msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation().getOutputType().getPhysical())); + } + } catch (Exception e) { + msg.setFaultBody(e); + } finally { + if (tmpMbox != null) { + tmpMbox.close(); + } + if (node != null) { + OtpEpmd.unPublishPort(node); + node.close(); + } + } + return msg; + } + + private Message invokeOperation(Message msg) { + OtpSelf self = null; + OtpPeer other = null; + OtpConnection connection = null; + try { + String nodeName = "_connector_to_" + binding.getNode(); + self = new OtpSelf(nodeName); + other = new OtpPeer(binding.getNode()); + connection = self.connect(other); + OtpErlangList params = TypeHelpersProxy.toErlangAsList((Object[])msg.getBody()); + connection.sendRPC(binding.getModule(), msg.getOperation().getName(), params); + if (msg.getOperation().getOutputType() != null) { + OtpErlangObject result = connection.receiveRPC(); + msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation().getOutputType().getPhysical())); + } + } catch (Exception e) { + msg.setFaultBody(e); + } finally { + if (connection != null) { + connection.close(); + } + } + return msg; + } + + /** + * @see org.apache.tuscany.sca.invocation.Invoker#invoke(org.apache.tuscany.sca.invocation.Message) + */ + public Message invoke(Message msg) { + if (binding.isMbox()) { + return sendMessage(msg); + } else { + return invokeOperation(msg); + } + + } + +} 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 new file mode 100644 index 0000000000..34af39ce41 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangReferenceBindingProvider.java @@ -0,0 +1,84 @@ +/* + * 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.logging.Level; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + +/** + * @version $Rev: $ $Date: $ + */ +public class ErlangReferenceBindingProvider implements ReferenceBindingProvider { + + 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; + } + + /** + * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#createInvoker(org.apache.tuscany.sca.interfacedef.Operation) + */ + public Invoker createInvoker(Operation operation) { + try { + return new ErlangInvoker(reference, 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#start() + */ + public void start() { + } + + /** + * @see org.apache.tuscany.sca.provider.ReferenceBindingProvider#stop() + */ + public void stop() { + } + + /** + * @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 new file mode 100644 index 0000000000..8d6e2e810c --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/ErlangServiceBindingProvider.java @@ -0,0 +1,81 @@ +/* + * 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.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +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) $ + */ +public class ErlangServiceBindingProvider implements ServiceBindingProvider { + + private RuntimeComponentService service; + private ErlangBinding binding; + + public ErlangServiceBindingProvider(ErlangBinding binding, RuntimeComponentService service) { + this.service = service; + this.binding = binding; + } + + /** + * @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 { + Runnable server = new RpcServer(service, binding); + Thread thread = new Thread(server); + thread.start(); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + + } + + /** + * @see org.apache.tuscany.sca.provider.ServiceBindingProvider#stop() + */ + public void stop() { + try { + //TODO: stop RPC server + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + + } + + /** + * @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/RpcExecutor.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java new file mode 100644 index 0000000000..3940c34b7b --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcExecutor.java @@ -0,0 +1,143 @@ +/* + * 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.io.IOException; +import java.util.List; + +import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy; +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.OtpConnection; +import com.ericsson.otp.erlang.OtpErlangAtom; +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangPid; +import com.ericsson.otp.erlang.OtpErlangRef; +import com.ericsson.otp.erlang.OtpErlangString; +import com.ericsson.otp.erlang.OtpErlangTuple; + +public class RpcExecutor implements Runnable { + + private RuntimeComponentService service; + private ErlangBinding binding; + private OtpConnection connection; + + private static final OtpErlangAtom OK = new OtpErlangAtom("ok"); + private static final OtpErlangAtom ERROR = new OtpErlangAtom("error"); + + public RpcExecutor(RuntimeComponentService service, ErlangBinding binding, OtpConnection connection) { + this.service = service; + this.binding = binding; + this.connection = connection; + } + + private void sendMessage(OtpConnection connection, + OtpErlangPid pid, + OtpErlangRef ref, + OtpErlangAtom head, + OtpErlangObject message) throws IOException { + OtpErlangTuple tResult = new OtpErlangTuple(new OtpErlangObject[] {head, message}); + OtpErlangObject msg = null; + if (ref != null) { + msg = new OtpErlangTuple(new OtpErlangObject[] {ref, tResult}); + } else { + msg = tResult; + } + connection.send(pid, msg); + } + + public void run() { + OtpErlangTuple request = null; + OtpErlangPid senderPid = null; + OtpErlangRef senderRef = null; + try { + OtpErlangTuple call = (OtpErlangTuple)connection.receive(); + if (call.arity() == 2) { + // is call from jinterface library + senderPid = (OtpErlangPid)call.elementAt(0); + request = (OtpErlangTuple)call.elementAt(1); + } else { + // is call from native *erl* console + OtpErlangTuple from = (OtpErlangTuple)call.elementAt(1); + request = (OtpErlangTuple)call.elementAt(2); + senderPid = (OtpErlangPid)from.elementAt(0); + senderRef = (OtpErlangRef)from.elementAt(1); + } + String module = ((OtpErlangAtom)request.elementAt(1)).atomValue(); + String function = ((OtpErlangAtom)request.elementAt(2)).atomValue(); + OtpErlangObject args = request.elementAt(3); + OtpErlangList argsList = null; + if (args instanceof OtpErlangList) { + argsList = (OtpErlangList)args; + } else { + argsList = new OtpErlangList(args); + } + if (!binding.getModule().equals(module)) { + sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("No such module")); + } else { + List operations = service.getInterfaceContract().getInterface().getOperations(); + Operation operation = null; + for (Operation o : operations) { + if (o.getName().equals(function)) { + operation = o; + break; + } + } + if (operation != null) { + List iTypes = operation.getInputType().getLogical(); + Class[] forClasses = new Class[iTypes.size()]; + for (int i = 0; i < iTypes.size(); i++) { + forClasses[i] = iTypes.get(i).getPhysical(); + } + Object result = + service.getRuntimeWire(binding).invoke(operation, + TypeHelpersProxy.toJavaFromList(argsList, forClasses)); + OtpErlangObject response = null; + if (operation.getOutputType() != null && operation.getOutputType().getPhysical().isArray()) { + response = TypeHelpersProxy.toErlangAsList(result); + } else if (operation.getOutputType() == null) { + Object[] arrArg = new Object[] {}; + response = TypeHelpersProxy.toErlang(arrArg); + } else { + Object[] arrArg = new Object[] {result}; + response = TypeHelpersProxy.toErlang(arrArg); + } + sendMessage(connection, senderPid, senderRef, OK, response); + } else { + sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("No such operation")); + } + } + } catch (Exception e) { + // TODO: distinguish and describe errors! + try { + sendMessage(connection, senderPid, senderRef, ERROR, new OtpErlangString("Error while processing request")); + } catch (IOException e1) { + // error while sending error message. Can't do anything now + } + } finally { + connection.close(); + } + } + +} diff --git a/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.java new file mode 100644 index 0000000000..90c47091e8 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/RpcServer.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.impl; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.tuscany.sca.binding.erlang.ErlangBinding; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +import com.ericsson.otp.erlang.OtpConnection; +import com.ericsson.otp.erlang.OtpSelf; + +public class RpcServer implements Runnable { + + private RuntimeComponentService service; + private ErlangBinding binding; + private OtpSelf self; + + public RpcServer(RuntimeComponentService service, ErlangBinding binding) throws Exception { + this.service = service; + this.binding = binding; + self = new OtpSelf(binding.getNode()); + boolean registered = self.publishPort(); + if (!registered) { + // TODO: throw exception, no epmd + } + } + + public void run() { + // TODO: thread management + ExecutorService executors = Executors.newFixedThreadPool(10); + while (true) { + try { + OtpConnection connection = self.accept(); + executors.execute(new RpcExecutor(service, binding, connection)); + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + } + +} 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 new file mode 100644 index 0000000000..7a3e727aa4 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/TypeMismatchException.java @@ -0,0 +1,42 @@ +/* + * 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; + +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 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/types/BooleanTypeHelper.java b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java new file mode 100644 index 0000000000..dd9f65b4e9 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/BooleanTypeHelper.java @@ -0,0 +1,40 @@ +/* + * 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.OtpErlangAtom; +import com.ericsson.otp.erlang.OtpErlangBoolean; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class BooleanTypeHelper implements TypeHelper { + + 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(); + } + } + +} 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 new file mode 100644 index 0000000000..d4a92e768a --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ByteTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangByte; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class ByteTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangByte((Byte)object); + } + + 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 new file mode 100644 index 0000000000..17100c65b8 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/CharTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangChar; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class CharTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangChar((Character)object); + } + + 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 new file mode 100644 index 0000000000..760fb0723b --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/DoubleTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangDouble; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class DoubleTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Double)object); + } + + 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 new file mode 100644 index 0000000000..851d0982f3 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/FloatTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangDouble; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class FloatTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangDouble((Float)object); + } + + 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 new file mode 100644 index 0000000000..3e79577b1e --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/IntTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangInt; +import com.ericsson.otp.erlang.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class IntTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangInt((Integer)object); + } + + 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 new file mode 100644 index 0000000000..86dcd4fc0f --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ListTypeHelper.java @@ -0,0 +1,57 @@ +/* + * 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 java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class ListTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + int i = 0; + List elements = new ArrayList(); + 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; + } + +} 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 new file mode 100644 index 0000000000..1437c8f8da --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/LongTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; + +public class LongTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangLong((Long)object); + } + + 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 new file mode 100644 index 0000000000..594875f348 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/ShortTypeHelper.java @@ -0,0 +1,36 @@ +/* + * 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.OtpErlangLong; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangShort; + +public class ShortTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangShort((Short)object); + } + + 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 new file mode 100644 index 0000000000..8a10e1818a --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/StringTypeHelper.java @@ -0,0 +1,35 @@ +/* + * 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.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangString; + +public class StringTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + return new OtpErlangString((String)object); + } + + 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 new file mode 100644 index 0000000000..86a78ccf87 --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TupleTypeHelper.java @@ -0,0 +1,68 @@ +/* + * 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 java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangTuple; + +public class TupleTypeHelper implements TypeHelper { + + public OtpErlangObject toErlang(Object object) { + Class forClass = object.getClass(); + List tupleMembers = new ArrayList(); + 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; + } + +} 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 new file mode 100644 index 0000000000..2d32ed586b --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelper.java @@ -0,0 +1,30 @@ +/* + * 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.OtpErlangObject; + +public interface TypeHelper { + + Object toJava(OtpErlangObject object, Class forClass) throws Exception; + + 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 new file mode 100644 index 0000000000..e695f87dce --- /dev/null +++ b/sandbox/wjaniszewski/binding-erlang-runtime/src/main/java/org/apache/tuscany/sca/binding/erlang/impl/types/TypeHelpersProxy.java @@ -0,0 +1,147 @@ +/* + * 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 java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException; + +import com.ericsson.otp.erlang.OtpErlangList; +import com.ericsson.otp.erlang.OtpErlangObject; +import com.ericsson.otp.erlang.OtpErlangTuple; + +public class TypeHelpersProxy { + + private static Map, TypeHelper> primitiveTypes = null; + + static { + // initiate type helpers + primitiveTypes = new HashMap, TypeHelper>(); + primitiveTypes.put(boolean.class, new BooleanTypeHelper()); + primitiveTypes.put(short.class, new ShortTypeHelper()); + primitiveTypes.put(byte.class, new ByteTypeHelper()); + primitiveTypes.put(char.class, new CharTypeHelper()); + primitiveTypes.put(int.class, new IntTypeHelper()); + primitiveTypes.put(long.class, new LongTypeHelper()); + primitiveTypes.put(float.class, new FloatTypeHelper()); + primitiveTypes.put(double.class, new DoubleTypeHelper()); + primitiveTypes.put(String.class, new StringTypeHelper()); + primitiveTypes.put(Boolean.class, primitiveTypes.get(boolean.class)); + primitiveTypes.put(Character.class, primitiveTypes.get(char.class)); + primitiveTypes.put(Short.class, primitiveTypes.get(char.class)); + primitiveTypes.put(Byte.class, primitiveTypes.get(byte.class)); + primitiveTypes.put(Short.class, primitiveTypes.get(short.class)); + primitiveTypes.put(Integer.class, primitiveTypes.get(int.class)); + 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)); + } + + private static TypeHelper getTypeHelper(Class forClass) { + TypeHelper typeHelper = null; + if (forClass.isArray()) { + typeHelper = new ListTypeHelper(); + } else { + typeHelper = primitiveTypes.get(forClass); + } + if (typeHelper == null) { + typeHelper = new TupleTypeHelper(); + } + return typeHelper; + } + + public static OtpErlangObject toErlang(Object[] objects) { + OtpErlangObject result = null; + if (objects != null) { + TypeHelper helper = null; + switch (objects.length) { + case 0: + result = new OtpErlangList(); + break; + case 1: + helper = getTypeHelper(objects[0].getClass()); + result = helper.toErlang(objects[0]); + break; + default: + OtpErlangObject[] erlObjects = new OtpErlangObject[objects.length]; + for (int i = 0; i < objects.length; i++) { + helper = getTypeHelper(objects[i].getClass()); + erlObjects[i] = helper.toErlang(objects[i]); + } + result = new OtpErlangTuple(erlObjects); + break; + } + } + return result; + } + + public static OtpErlangList toErlangAsList(Object array) { + OtpErlangList result = null; + if (array != null) { + if (!array.getClass().isArray()) { + array = new Object[] {array}; + } + List attrsList = new ArrayList(); + int i = 0; + while (true) { + try { + TypeHelper helper = getTypeHelper(Array.get(array, i).getClass()); + attrsList.add(helper.toErlang(Array.get(array, i))); + i++; + } catch (ArrayIndexOutOfBoundsException e) { + break; + } + } + result = new OtpErlangList(attrsList.toArray(new OtpErlangObject[attrsList.size()])); + } else { + result = new OtpErlangList(); + } + return result; + } + + public static Object toJava(OtpErlangObject object, Class forClass) throws Exception { + try { + TypeHelper helper = getTypeHelper(forClass); + return helper.toJava(object, forClass); + } catch (ClassCastException e) { + throw new TypeMismatchException(forClass, object.getClass()); + } + } + + public static Object[] toJavaFromList(OtpErlangList objects, Class[] forClass) throws Exception { + Object[] result = new Object[objects.arity()]; + try { + for (int i = 0; i < objects.arity(); i++) { + TypeHelper helper = getTypeHelper(forClass[i]); + result[i] = helper.toJava(objects.elementAt(i), forClass[i]); + } + } catch (ClassCastException e) { + e.printStackTrace(); + // throw new TypeMismatchException(forClass, objects[i].getClass()); + } + return result; + } + +} -- cgit v1.2.3