Service RPC updated by allowing to have more than one SCA-Erlang module on single SCA-Erlang node

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@751092 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
wjaniszewski 2009-03-06 21:57:58 +00:00
commit 9320e6220c
12 changed files with 359 additions and 118 deletions

View file

@ -19,6 +19,9 @@
package org.apache.tuscany.sca.binding.erlang.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.provider.BindingProviderFactory;
@ -31,34 +34,61 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentService;
/**
* @version $Rev: $ $Date: $
*/
public class ErlangBindingProviderFactory implements BindingProviderFactory<ErlangBinding> {
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);
}
public class ErlangBindingProviderFactory implements
BindingProviderFactory<ErlangBinding> {
/**
* @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);
}
private Map<String, ErlangNode> nodes = new HashMap<String, ErlangNode>();
/**
* @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType()
*/
public Class<ErlangBinding> getModelType() {
return ErlangBinding.class;
}
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) {
ServiceBindingProvider provider = null;
try {
provider = new ErlangServiceBindingProvider(getErlangNode(binding
.getNode()), binding, service);
} catch (Exception e) {
// TODO: log, throw, do something with this error
e.printStackTrace();
}
return provider;
}
/**
* @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType()
*/
public Class<ErlangBinding> getModelType() {
return ErlangBinding.class;
}
private ErlangNode getErlangNode(String name) throws Exception {
ErlangNode result = null;
if (nodes.containsKey(name)) {
result = nodes.get(name);
} else {
result = new ErlangNode(name);
nodes.put(name, result);
}
return result;
}
}

View file

@ -0,0 +1,83 @@
package org.apache.tuscany.sca.binding.erlang.impl;
import java.util.HashMap;
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.runtime.RuntimeComponentService;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpSelf;
public class ErlangNode implements Runnable {
private Map<String, RuntimeComponentService> services = new HashMap<String, RuntimeComponentService>();
private Map<String, ErlangBinding> bindings = new HashMap<String, ErlangBinding>();
private String name;
private OtpSelf self;
private ExecutorService executors;
private boolean stopRequested;
public ErlangNode(String name) throws Exception {
this.name = name;
self = new OtpSelf(name);
boolean registered = self.publishPort();
if (!registered) {
// TODO: externalize messages?
throw new ErlangException(
"Problem with publishing service under epmd server.");
}
}
private void stop() {
stopRequested = true;
executors.shutdownNow();
}
public void run() {
executors = Executors.newFixedThreadPool(10);
while (!stopRequested) {
try {
OtpConnection connection = self.accept();
executors.execute(new RpcExecutor(services, bindings,
connection));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public void registerModule(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");
} else {
if (services.size() == 0) {
// TODO: should ErlangNode manage its thread?
Thread selfThread = new Thread(this);
selfThread.start();
}
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) {
stop();
}
}
}
}

View file

@ -31,12 +31,13 @@ import org.osoa.sca.ServiceRuntimeException;
public class ErlangServiceBindingProvider implements ServiceBindingProvider {
private RuntimeComponentService service;
private ErlangNode node;
private ErlangBinding binding;
private RpcServer rpcServer;
public ErlangServiceBindingProvider(ErlangBinding binding, RuntimeComponentService service) {
public ErlangServiceBindingProvider(ErlangNode node, ErlangBinding binding, RuntimeComponentService service) {
this.service = service;
this.binding = binding;
this.node = node;
}
/**
@ -51,9 +52,7 @@ public class ErlangServiceBindingProvider implements ServiceBindingProvider {
*/
public void start() {
try {
rpcServer = new RpcServer(service, binding);
Thread thread = new Thread(rpcServer);
thread.start();
node.registerModule(binding, service);
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}
@ -65,7 +64,7 @@ public class ErlangServiceBindingProvider implements ServiceBindingProvider {
*/
public void stop() {
try {
rpcServer.stop();
node.unregisterModule(binding);
} catch (Exception e) {
throw new ServiceRuntimeException(e);
}

View file

@ -22,6 +22,7 @@ package org.apache.tuscany.sca.binding.erlang.impl;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import org.apache.tuscany.sca.binding.erlang.ErlangBinding;
import org.apache.tuscany.sca.binding.erlang.impl.types.TypeHelpersProxy;
@ -40,18 +41,18 @@ import com.ericsson.otp.erlang.OtpErlangTuple;
public class RpcExecutor implements Runnable {
private RuntimeComponentService service;
private ErlangBinding binding;
private Map<String, RuntimeComponentService> services;
private Map<String, ErlangBinding> bindings;
private OtpConnection connection;
private static final OtpErlangAtom OK = new OtpErlangAtom("ok");
private static final OtpErlangAtom ERROR = new OtpErlangAtom("error");
private static final OtpErlangAtom BADRPC = new OtpErlangAtom("badrpc");
public RpcExecutor(RuntimeComponentService service, ErlangBinding binding,
public RpcExecutor(Map<String, RuntimeComponentService> services, Map<String, ErlangBinding> bindings,
OtpConnection connection) {
this.service = service;
this.binding = binding;
this.bindings = bindings;
this.services = services;
this.connection = connection;
}
@ -85,13 +86,15 @@ public class RpcExecutor implements Runnable {
} else {
argsList = new OtpErlangList(args);
}
if (!binding.getModule().equals(module)) {
if (!services.containsKey(module)) {
// TODO: externalize message?
OtpErlangObject errorMsg = MessageHelper.functionUndefMessage(
module, function, argsList,
"Module not found in SCA component.");
sendMessage(connection, senderPid, senderRef, BADRPC, errorMsg);
} else {
RuntimeComponentService service = services.get(module);
ErlangBinding binding = bindings.get(module);
List<Operation> operations = service.getInterfaceContract()
.getInterface().getOperations();
Operation operation = null;

View file

@ -1,69 +0,0 @@
/*
* 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.binding.erlang.impl.exceptions.ErlangException;
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;
ExecutorService executors;
private boolean stopRequested;
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: externalize messages?
throw new ErlangException("Problem with publishing service under epmd server.");
}
executors = Executors.newFixedThreadPool(10);
}
public void stop() {
stopRequested = true;
executors.shutdownNow();
}
public void run() {
while (!stopRequested) {
try {
OtpConnection connection = self.accept();
executors.execute(new RpcExecutor(service, binding, connection));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}

View file

@ -2,8 +2,14 @@ package org.apache.tuscany.sca.binding.erlang.impl.exceptions;
public class ErlangException extends Exception {
private static final long serialVersionUID = 1L;
public ErlangException(String message) {
super(message);
}
public ErlangException(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -33,6 +33,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osoa.sca.ServiceRuntimeException;
import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangDouble;
@ -43,6 +44,7 @@ import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpNode;
//this test runner will ignore tests if epmd is not available
@RunWith(IgnorableRunner.class)
public class ReferenceServiceTestCase {
@ -50,6 +52,7 @@ public class ReferenceServiceTestCase {
private static MboxInterface mboxReference;
private static ServiceInterface moduleReference;
private static ServiceInterface clonedModuleReference;
private static OtpNode node;
private static OtpMbox mbox;
private static Process epmdProcess;
@ -65,6 +68,7 @@ public class ReferenceServiceTestCase {
ReferenceTestComponentImpl.class, "ReferenceTest");
mboxReference = component.getMboxReference();
moduleReference = component.getModuleReference();
clonedModuleReference = component.getClonedModuleReference();
node = new OtpNode("MboxServer");
mbox = node.createMbox("sendArgs");
} catch (IOException e) {
@ -87,6 +91,11 @@ public class ReferenceServiceTestCase {
}
}
/**
* Tests passing strings
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testStrings() throws Exception {
String strArg = "Test message";
@ -100,6 +109,11 @@ public class ReferenceServiceTestCase {
assertEquals(strResult, testResult);
}
/**
* Tests passing booleans
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testBooleans() throws Exception {
boolean booleanArg = true;
@ -113,6 +127,11 @@ public class ReferenceServiceTestCase {
assertEquals(booleanResult, testResult);
}
/**
* Tests passing floats
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testFloats() throws Exception {
float floatArg = 1.0f;
@ -126,6 +145,11 @@ public class ReferenceServiceTestCase {
assertEquals(floatResult, testResult, 0);
}
/**
* Tests passing doubles
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testDoubles() throws Exception {
double doubleArg = 1.0f;
@ -139,6 +163,11 @@ public class ReferenceServiceTestCase {
assertEquals(doubleResult, testResult, 0);
}
/**
* Tests passing long values
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testLongs() throws Exception {
long longArg = 1;
@ -152,6 +181,11 @@ public class ReferenceServiceTestCase {
assertEquals(longResult, testResult, 0);
}
/**
* Tests passing integers
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testInts() throws Exception {
int intArg = 1;
@ -165,6 +199,11 @@ public class ReferenceServiceTestCase {
assertEquals(intResult, testResult, 0);
}
/**
* Tests passing chars
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testChars() throws Exception {
char charArg = 1;
@ -178,6 +217,11 @@ public class ReferenceServiceTestCase {
assertEquals(charResult, testResult, 0);
}
/**
* Tests passing shorts
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testShorts() throws Exception {
short shortArg = 1;
@ -191,6 +235,11 @@ public class ReferenceServiceTestCase {
assertEquals(shortResult, testResult, 0);
}
/**
* Tests passing bytes
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testBytes() throws Exception {
byte byteArg = 1;
@ -204,6 +253,11 @@ public class ReferenceServiceTestCase {
assertEquals(byteResult, testResult, 0);
}
/**
* Tests passing multiple arguments
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testMultipleArguments() throws Exception {
MboxListener mboxListener = new MboxListener(mbox, true);
@ -219,6 +273,11 @@ public class ReferenceServiceTestCase {
.getMsg()).elementAt(1)).stringValue());
}
/**
* Tests passing tuples
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testTuples() throws Exception {
StructuredTuple tupleResult = new StructuredTuple();
@ -250,6 +309,11 @@ public class ReferenceServiceTestCase {
.booleanValue());
}
/**
* Tests passing lists
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testLists() throws Exception {
String[] testArg = new String[] { "One", "Two", "Three" };
@ -269,6 +333,11 @@ public class ReferenceServiceTestCase {
}
}
/**
* Tests passing multidimensional lists
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testMultiDimLists() throws Exception {
String[][] testArg = new String[][] { { "One", "Two" },
@ -294,6 +363,11 @@ public class ReferenceServiceTestCase {
}
}
/**
* Tests mismatched interface
*
* @throws Exception
*/
@Test(timeout = 1000)
public void typeMismatch() throws Exception {
try {
@ -418,6 +492,11 @@ public class ReferenceServiceTestCase {
}
}
/**
* Basic RPC test, without arguments
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testRPC() throws Exception {
String[] result = moduleReference.sayHellos();
@ -426,6 +505,11 @@ public class ReferenceServiceTestCase {
assertEquals("2", result[1]);
}
/**
* Tests RPC with arguments
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testRPCWithArgs() throws Exception {
String arg1 = "One";
@ -434,6 +518,11 @@ public class ReferenceServiceTestCase {
assertEquals("Hello " + arg1 + " " + arg2, testResult);
}
/**
* Tests RPC with structured arguments
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testRPCWithComplexArgs() throws Exception {
StructuredTuple arg = new StructuredTuple();
@ -443,34 +532,39 @@ public class ReferenceServiceTestCase {
new String[] { "some", "array" });
assertEquals(arg, testResult);
}
/**
* Tests handling requests pointing to unknown functions
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testUnknownFunction() throws Exception {
//following functions differs by parameters
// following functions differs by parameters
try {
moduleReference.sayHello();
} catch (Exception e) {
assertEquals(ErlangException.class, e.getClass());
}
try {
moduleReference.sayHello("1");
} catch (Exception e) {
assertEquals(ErlangException.class, e.getClass());
}
try {
moduleReference.sayHello(1, 2);
} catch (Exception e) {
assertEquals(ErlangException.class, e.getClass());
}
//for following ones name not exists
// for following ones name not exists
moduleReference.notExist();
try {
moduleReference.notExistWithException();
} catch (Exception e) {
@ -478,4 +572,35 @@ public class ReferenceServiceTestCase {
}
}
/**
* Tests using multiple Erlang modules on one SCA Erlang node
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testMultipleModulesOnNode() throws Exception {
String[] mr = moduleReference.sayHellos();
String[] cmr = clonedModuleReference.sayHellos();
assertEquals("1", mr[0]);
assertEquals("2", mr[1]);
assertEquals("-1", cmr[0]);
assertEquals("-2", cmr[1]);
}
/**
* Tests nodes with duplcated components (the same node and module
* parameters)
*
* @throws Exception
*/
@Test(timeout = 1000)
public void testModuleDuplicatedOnNode() throws Exception {
try {
SCADomain.newInstance("ErlangServiceModuleDuplicate.composite");
} catch (ServiceRuntimeException e) {
assertEquals(ErlangException.class, e.getCause().getClass());
}
}
}

View file

@ -6,6 +6,7 @@ public class ReferenceTestComponentImpl implements ReferenceTestComponent {
private MboxInterface mboxReference;
private ServiceInterface moduleReference;
private ServiceInterface clonedModuleReference;
@Reference
public void setMboxReference(MboxInterface mboxReference) {
@ -16,6 +17,11 @@ public class ReferenceTestComponentImpl implements ReferenceTestComponent {
public void setModuleReference(ServiceInterface moduleReference) {
this.moduleReference = moduleReference;
}
@Reference
public void setClonedModuleReference(ServiceInterface clonedModuleReference) {
this.clonedModuleReference = clonedModuleReference;
}
public MboxInterface getMboxReference() {
return mboxReference;
@ -24,5 +30,9 @@ public class ReferenceTestComponentImpl implements ReferenceTestComponent {
public ServiceInterface getModuleReference() {
return moduleReference;
}
public ServiceInterface getClonedModuleReference() {
return clonedModuleReference;
}
}

View file

@ -0,0 +1,19 @@
package org.apache.tuscany.sca.binding.erlang.testing;
public class ServiceTestComponentImplClone implements ServiceTestComponent {
public String sayHello(String arg1, String arg2) {
return "Bye " + arg1 + " " + arg2;
}
public String[] sayHellos() {
String[] result = new String[] {"-1", "-2"};
return result;
}
public StructuredTuple passComplexArgs(StructuredTuple arg1, String[] arg2) {
return arg1;
}
}

View file

@ -13,6 +13,10 @@
<reference name="moduleReference">
<tuscany:binding.erlang node="RPCServer" module="hello"/>
</reference>
<reference name="clonedModuleReference">
<tuscany:binding.erlang node="RPCServer" module="hello_clone"/>
</reference>
</component>
</composite>

View file

@ -11,5 +11,13 @@
<interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
<tuscany:binding.erlang node="RPCServer" module="hello"/>
</service>
<component name="ServiceTestClone">
<implementation.java class="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponentImplClone" />
</component>
<service name="ServiceTestClone" promote="ServiceTestClone">
<interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
<tuscany:binding.erlang node="RPCServer" module="hello_clone"/>
</service>
</composite>

View file

@ -0,0 +1,23 @@
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
targetNamespace="http://sample"
xmlns:sample="http://sample"
name="ErlangServiceBinding">
<component name="ServiceTest">
<implementation.java class="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponentImpl" />
</component>
<service name="ServiceTest" promote="ServiceTest">
<interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
<tuscany:binding.erlang node="DuplicateTest" module="hello"/>
</service>
<component name="ServiceTestClone">
<implementation.java class="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponentImplClone" />
</component>
<service name="ServiceTestClone" promote="ServiceTestClone">
<interface.java interface="org.apache.tuscany.sca.binding.erlang.testing.ServiceTestComponent" />
<tuscany:binding.erlang node="DuplicateTest" module="hello"/>
</service>
</composite>