mid camel impl

This commit is contained in:
Jonas Boner 2009-05-09 19:55:42 +02:00
parent 46ede93684
commit b1d91818ef
27 changed files with 560 additions and 513 deletions

View file

@ -1,121 +0,0 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.api;
import com.google.inject.*;
import com.google.inject.jsr250.ResourceProviderFactory;
import se.scalablesolutions.akka.kernel.configuration.*;
import se.scalablesolutions.akka.kernel.ActiveObjectFactory;
import se.scalablesolutions.akka.kernel.ActiveObjectProxy;
import se.scalablesolutions.akka.kernel.Supervisor;
import se.scalablesolutions.akka.kernel.Worker;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
public class ActiveObjectGuiceConfigurator {
private List<Module> modules = new ArrayList<Module>();
private Injector injector;
private Supervisor supervisor; // TODO is supervisor needed
private RestartStrategy restartStrategy;
private Component[] components;
private Map<Class, Component> configRegistry = new HashMap<Class, Component>(); // TODO is configRegistry needed?
private Map<Class, ActiveObjectProxy> activeObjectRegistry = new HashMap<Class, ActiveObjectProxy>();
private ActiveObjectFactory activeObjectFactory = new ActiveObjectFactory();
public synchronized <T> T getExternalDependency(Class<T> clazz) {
return injector.getInstance(clazz);
}
/**
* Returns the active abject that has been put under supervision for the class specified.
*
* @param clazz the class for the active object
* @return the active object for the class
*/
public synchronized <T> T getActiveObject(Class<T> clazz) {
if (injector == null) throw new IllegalStateException("inject() and supervise() must be called before invoking newInstance(clazz)");
if (activeObjectRegistry.containsKey(clazz)) {
final ActiveObjectProxy activeObjectProxy = activeObjectRegistry.get(clazz);
activeObjectProxy.setTargetInstance(injector.getInstance(clazz));
return (T)activeObjectFactory.newInstance(clazz, activeObjectProxy);
} else throw new IllegalStateException("Class " + clazz.getName() + " has not been put under supervision (by passing in the config to the supervise() method");
}
public synchronized ActiveObjectGuiceConfigurator configureActiveObjects(final RestartStrategy restartStrategy, final Component[] components) {
this.restartStrategy = restartStrategy;
this.components = components;
modules.add(new AbstractModule() {
protected void configure() {
bind(ResourceProviderFactory.class);
for (int i = 0; i < components.length; i++) {
Component c = components[i];
bind((Class) c.intf()).to((Class) c.target()).in(Singleton.class);
}
}
});
return this;
}
public synchronized ActiveObjectGuiceConfigurator inject() {
if (injector != null) throw new IllegalStateException("inject() has already been called on this configurator");
injector = Guice.createInjector(modules);
return this;
}
public synchronized ActiveObjectGuiceConfigurator supervise() {
if (injector == null) inject();
injector = Guice.createInjector(modules);
List<Worker> workers = new ArrayList<Worker>();
for (int i = 0; i < components.length; i++) {
final Component c = components[i];
final ActiveObjectProxy activeObjectProxy = new ActiveObjectProxy(c.intf(), c.target(), c.timeout());
workers.add(c.newWorker(activeObjectProxy));
activeObjectRegistry.put(c.intf(), activeObjectProxy);
}
supervisor = activeObjectFactory.supervise(restartStrategy.transform(), workers);
return this;
}
/**
* Add additional services to be wired in.
* <pre>
* ActiveObjectGuiceConfigurator.addExternalGuiceModule(new AbstractModule {
* protected void configure() {
* bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
* bind(BarImpl.class);
* link(Bar.class).to(BarImpl.class);
* bindConstant(named("port")).to(8080);
* }})
* </pre>
*/
public synchronized ActiveObjectGuiceConfigurator addExternalGuiceModule(Module module) {
modules.add(module);
return this;
}
public List<Module> getGuiceModules() {
return modules;
}
public synchronized void reset() {
modules = new ArrayList<Module>();
configRegistry = new HashMap<Class, Component>();
activeObjectRegistry = new HashMap<Class, ActiveObjectProxy>();
injector = null;
restartStrategy = null;
}
public synchronized void stop() {
supervisor.stop();
}
}

View file

@ -5,7 +5,13 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.configuration.*;
import se.scalablesolutions.akka.kernel.config.ActiveObjectGuiceConfiguratorForJava;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.InMemoryTransactionalMap;
import com.google.inject.Inject;
import com.google.inject.AbstractModule;
@ -16,7 +22,7 @@ import junit.framework.TestCase;
public class ActiveObjectGuiceConfiguratorTest extends TestCase {
static String messageLog = "";
final private ActiveObjectGuiceConfigurator conf = new ActiveObjectGuiceConfigurator();
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.addExternalGuiceModule(new AbstractModule() {
@ -26,11 +32,13 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
}).configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000), new Component[]{
new Component(
"foo",
Foo.class,
FooImpl.class,
new LifeCycle(new Permanent(), 1000),
1000),
new Component(
"bar",
Bar.class,
BarImpl.class,
new LifeCycle(new Permanent(), 1000),
@ -41,21 +49,21 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
public void testGuiceActiveObjectInjection() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Bar bar = conf.getActiveObject(Bar.class);
Foo foo = conf.getActiveObject("foo");
Bar bar = conf.getActiveObject("bar");
assertTrue(foo.getBar().toString().equals(bar.toString()));
}
public void testGuiceExternalDependencyInjection() {
messageLog = "";
Bar bar = conf.getActiveObject(Bar.class);
Bar bar = conf.getActiveObject("bar");
Ext ext = conf.getExternalDependency(Ext.class);
assertTrue(bar.getExt().toString().equals(ext.toString()));
}
public void testLookupNonSupervisedInstance() {
try {
String str = conf.getActiveObject(String.class);
String str = conf.getActiveObject("string");
fail("exception should have been thrown");
} catch (Exception e) {
assertEquals("Class java.lang.String has not been put under supervision (by passing in the config to the supervise() method", e.getMessage());
@ -64,7 +72,7 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
public void testActiveObjectInvocation() throws InterruptedException {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Foo foo = conf.getActiveObject("foo");
messageLog += foo.foo("foo ");
foo.bar("bar ");
messageLog += "before_bar ";
@ -74,8 +82,8 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
public void testActiveObjectInvocationsInvocation() throws InterruptedException {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Bar bar = conf.getActiveObject(Bar.class);
Foo foo = conf.getActiveObject("foo");
Bar bar = conf.getActiveObject("bar");
messageLog += foo.foo("foo ");
foo.bar("bar ");
messageLog += "before_bar ";
@ -86,7 +94,7 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
public void testForcedTimeout() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Foo foo = conf.getActiveObject("foo");
try {
foo.longRunning();
fail("exception should have been thrown");
@ -96,7 +104,7 @@ public class ActiveObjectGuiceConfiguratorTest extends TestCase {
public void testForcedException() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Foo foo = conf.getActiveObject("foo");
try {
foo.throwsException();
fail("exception should have been thrown");

View file

@ -5,31 +5,25 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.*;
import se.scalablesolutions.akka.kernel.configuration.LifeCycle;
import se.scalablesolutions.akka.kernel.configuration.Permanent;
import se.scalablesolutions.akka.kernel.configuration.Component;
import se.scalablesolutions.akka.kernel.configuration.AllForOne;
import se.scalablesolutions.akka.kernel.configuration.RestartStrategy;
import com.google.inject.Inject;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.InMemoryTransactionalMap;
import junit.framework.TestCase;
public class InMemoryStateTest extends TestCase {
static String messageLog = "";
final private ActiveObjectGuiceConfigurator conf = new ActiveObjectGuiceConfigurator();
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000),
new Component[] {
new Component(InMemStateful.class, InMemStatefulImpl.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component(InMemFailer.class, InMemFailerImpl.class, new LifeCycle(new Permanent(), 1000), 1000),
new Component(InMemClasher.class, InMemClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
new Component("inmem-stateful", InMemStateful.class, InMemStatefulImpl.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component("inmem-failer", InMemFailer.class, InMemFailerImpl.class, new LifeCycle(new Permanent(), 1000), 1000),
new Component("inmem-clasher", InMemClasher.class, InMemClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
}).inject().supervise();
}
@ -39,17 +33,17 @@ public class InMemoryStateTest extends TestCase {
public void testShouldNotRollbackStateForStatefulServerInCaseOfSuccess() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
InMemStateful stateful = conf.getActiveObject("inmem-stateful");
stateful.setState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "init"); // set init state
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // transactional
assertEquals("new state", stateful.getState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess"));
}
public void testShouldRollbackStateForStatefulServerInCaseOfFailure() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
InMemStateful stateful = conf.getActiveObject("inmem-stateful");
stateful.setState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init"); // set init state
InMemFailer failer = conf.getActiveObject(InMemFailer.class);
InMemFailer failer = conf.getActiveObject("inmem-failer");
try {
stateful.failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer); // call failing transactional method
fail("should have thrown an exception");

View file

@ -5,31 +5,25 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.*;
import se.scalablesolutions.akka.kernel.configuration.LifeCycle;
import se.scalablesolutions.akka.kernel.configuration.Permanent;
import se.scalablesolutions.akka.kernel.configuration.Component;
import se.scalablesolutions.akka.kernel.configuration.AllForOne;
import se.scalablesolutions.akka.kernel.configuration.RestartStrategy;
import com.google.inject.Inject;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.CassandraPersistentTransactionalMap;
import junit.framework.TestCase;
public class PersistentStateTest extends TestCase {
static String messageLog = "";
final private ActiveObjectGuiceConfigurator conf = new ActiveObjectGuiceConfigurator();
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000),
new JavaConfig.RestartStrategy(new JavaConfig.AllForOne(), 3, 5000),
new Component[] {
new Component(PersistentStateful.class, PersistentStatefulImpl.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component(PersistentFailer.class, PersistentFailerImpl.class, new LifeCycle(new Permanent(), 1000), 1000),
new Component(PersistentClasher.class, PersistentClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
new Component("persistent-stateful", PersistentStateful.class, PersistentStatefulImpl.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component("persistent-failer", PersistentFailer.class, PersistentFailerImpl.class, new LifeCycle(new Permanent(), 1000), 1000),
new Component("persistent-clasher", PersistentClasher.class, PersistentClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
}).inject().supervise();
}
@ -63,10 +57,10 @@ interface PersistentStateful {
}
class PersistentStatefulImpl implements PersistentStateful {
private TransactionalMap<String, String> state = new CassandraPersistentTransactionalMap(this);
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
public String getState(String key) {
return state.get(key);
return (String)state.get(key);
}
public void setState(String key, String msg) {
@ -113,10 +107,10 @@ interface PersistentClasher {
}
class PersistentClasherImpl implements PersistentClasher {
private TransactionalMap<String, String> state = new CassandraPersistentTransactionalMap(this);
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
public String getState(String key) {
return state.get(key);
return (String)state.get(key);
}
public void setState(String key, String msg) {