completed Guice API for ActiveObjects
This commit is contained in:
parent
42cc47bf6a
commit
a2e3406064
8 changed files with 551 additions and 251 deletions
|
|
@ -21,10 +21,9 @@
|
||||||
<version>${akka.version}</version>
|
<version>${akka.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.testng</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>testng</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>5.7</version>
|
<version>4.5</version>
|
||||||
<classifier>jdk15</classifier>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jmock</groupId>
|
<groupId>org.jmock</groupId>
|
||||||
|
|
@ -49,15 +48,18 @@
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- <plugin> -->
|
<plugin>
|
||||||
<!-- <groupId>org.apache.maven.plugins</groupId> -->
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<!-- <artifactId>maven-surefire-plugin</artifactId> -->
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<!-- <configuration> -->
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/Abstract*</exclude>
|
||||||
|
</excludes>
|
||||||
<!--<suiteXmlFiles>-->
|
<!--<suiteXmlFiles>-->
|
||||||
<!--<suiteXmlFile>testng.xml</suiteXmlFile>-->
|
<!--<suiteXmlFile>testng.xml</suiteXmlFile>-->
|
||||||
<!--</suiteXmlFiles>-->
|
<!--</suiteXmlFiles>-->
|
||||||
<!-- </configuration> -->
|
</configuration>
|
||||||
<!-- </plugin> -->
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
package com.scalablesolutions.akka.api;
|
||||||
|
|
||||||
|
import com.google.inject.*;
|
||||||
|
import com.google.inject.jsr250.ResourceProviderFactory;
|
||||||
|
|
||||||
|
import com.scalablesolutions.akka.kernel.ActiveObjectFactory;
|
||||||
|
import com.scalablesolutions.akka.kernel.ActiveObjectProxy;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import scala.actors.behavior.Supervisor;
|
||||||
|
import scala.actors.behavior.Worker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
|
*/
|
||||||
|
public class ActiveObjectGuiceConfigurator {
|
||||||
|
private List<Module> modules = new ArrayList<Module>();
|
||||||
|
private Injector injector;
|
||||||
|
private Supervisor supervisor;
|
||||||
|
private RestartStrategy restartStrategy;
|
||||||
|
private Component[] components;
|
||||||
|
private Map<Class, Component> configRegistry = new HashMap<Class, Component>();
|
||||||
|
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() {
|
||||||
|
// TODO: fix supervisor.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,169 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2009 Scalable Solutions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.scalablesolutions.akka.api;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.AbstractModule;
|
||||||
|
import com.google.inject.Scopes;
|
||||||
|
|
||||||
|
import scala.actors.annotation.oneway;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
public class ActiveObjectGuiceConfiguratorTest extends TestCase {
|
||||||
|
static String messageLog = "";
|
||||||
|
|
||||||
|
final private ActiveObjectGuiceConfigurator conf = new ActiveObjectGuiceConfigurator();
|
||||||
|
|
||||||
|
protected void setUp() {
|
||||||
|
conf.addExternalGuiceModule(new AbstractModule() {
|
||||||
|
protected void configure() {
|
||||||
|
bind(Ext.class).to(ExtImpl.class).in(Scopes.SINGLETON);
|
||||||
|
}
|
||||||
|
}).configureActiveObjects(
|
||||||
|
new RestartStrategy(new AllForOne(), 3, 100), new Component[]{
|
||||||
|
new Component(
|
||||||
|
Foo.class,
|
||||||
|
FooImpl.class,
|
||||||
|
new LifeCycle(new Permanent(), 100),
|
||||||
|
1000),
|
||||||
|
new Component(
|
||||||
|
Bar.class,
|
||||||
|
BarImpl.class,
|
||||||
|
new LifeCycle(new Permanent(), 100),
|
||||||
|
1000)
|
||||||
|
}).inject().supervise();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGuiceActiveObjectInjection() {
|
||||||
|
messageLog = "";
|
||||||
|
Foo foo = conf.getActiveObject(Foo.class);
|
||||||
|
Bar bar = conf.getActiveObject(Bar.class);
|
||||||
|
assertTrue(foo.getBar().toString().equals(bar.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGuiceExternalDependencyInjection() {
|
||||||
|
messageLog = "";
|
||||||
|
Bar bar = conf.getActiveObject(Bar.class);
|
||||||
|
Ext ext = conf.getExternalDependency(Ext.class);
|
||||||
|
assertTrue(bar.getExt().toString().equals(ext.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testActiveObjectInvocation() throws InterruptedException {
|
||||||
|
messageLog = "";
|
||||||
|
Foo foo = conf.getActiveObject(Foo.class);
|
||||||
|
messageLog += foo.foo("foo ");
|
||||||
|
foo.bar("bar ");
|
||||||
|
messageLog += "before_bar ";
|
||||||
|
Thread.sleep(500);
|
||||||
|
assertEquals("foo return_foo before_bar ", messageLog);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testActiveObjectInvocationsInvocation() throws InterruptedException {
|
||||||
|
messageLog = "";
|
||||||
|
Foo foo = conf.getActiveObject(Foo.class);
|
||||||
|
Bar bar = conf.getActiveObject(Bar.class);
|
||||||
|
messageLog += foo.foo("foo ");
|
||||||
|
foo.bar("bar ");
|
||||||
|
messageLog += "before_bar ";
|
||||||
|
Thread.sleep(500);
|
||||||
|
assertEquals("foo return_foo before_bar ", messageLog);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testForcedTimeout() {
|
||||||
|
messageLog = "";
|
||||||
|
Foo foo = conf.getActiveObject(Foo.class);
|
||||||
|
try {
|
||||||
|
foo.longRunning();
|
||||||
|
fail("exception should have been thrown");
|
||||||
|
} catch (com.scalablesolutions.akka.kernel.ActiveObjectInvocationTimeoutException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testForcedException() {
|
||||||
|
messageLog = "";
|
||||||
|
Foo foo = conf.getActiveObject(Foo.class);
|
||||||
|
try {
|
||||||
|
foo.throwsException();
|
||||||
|
fail("exception should have been thrown");
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== TEST SERVICES ===============
|
||||||
|
|
||||||
|
interface Foo {
|
||||||
|
public String foo(String msg);
|
||||||
|
|
||||||
|
public
|
||||||
|
@oneway
|
||||||
|
void bar(String msg);
|
||||||
|
|
||||||
|
public void longRunning();
|
||||||
|
|
||||||
|
public void throwsException();
|
||||||
|
|
||||||
|
public Bar getBar();
|
||||||
|
}
|
||||||
|
|
||||||
|
class FooImpl implements Foo {
|
||||||
|
@Inject
|
||||||
|
private Bar bar;
|
||||||
|
|
||||||
|
public Bar getBar() {
|
||||||
|
return bar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String foo(String msg) {
|
||||||
|
return msg + "return_foo ";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bar(String msg) {
|
||||||
|
bar.bar(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void longRunning() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void throwsException() {
|
||||||
|
throw new RuntimeException("expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Bar {
|
||||||
|
@oneway
|
||||||
|
void bar(String msg);
|
||||||
|
|
||||||
|
Ext getExt();
|
||||||
|
}
|
||||||
|
|
||||||
|
class BarImpl implements Bar {
|
||||||
|
@Inject
|
||||||
|
private Ext ext;
|
||||||
|
|
||||||
|
public Ext getExt() {
|
||||||
|
return ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bar(String msg) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Ext {
|
||||||
|
void ext();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExtImpl implements Ext {
|
||||||
|
public void ext() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
</run>
|
</run>
|
||||||
</groups>
|
</groups>
|
||||||
<packages>
|
<packages>
|
||||||
<package name="com.scalablesolutions.api.*" />
|
<package name="com.scalablesolutions.akka.api.*" />
|
||||||
</packages>
|
</packages>
|
||||||
</test>
|
</test>
|
||||||
</suite>
|
</suite>
|
||||||
|
|
|
||||||
|
|
@ -14,37 +14,6 @@
|
||||||
<version>${akka.version}</version>
|
<version>${akka.version}</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>repo1.maven</id>
|
|
||||||
<name>Maven Main Repository</name>
|
|
||||||
<url>http://repo1.maven.org/maven2</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>scala-tools-snapshots</id>
|
|
||||||
<name>Scala-Tools Maven2 Snapshot Repository</name>
|
|
||||||
<url>http://scala-tools.org/repo-snapshots</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>scala-tools</id>
|
|
||||||
<name>Scala-Tools Maven2 Repository</name>
|
|
||||||
<url>http://scala-tools.org/repo-releases</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>lag</id>
|
|
||||||
<name>Configgy's' Repository</name>
|
|
||||||
<url>http://www.lag.net/repo</url>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>scala-tools.org</id>
|
|
||||||
<name>Scala-Tools Maven2 Repository</name>
|
|
||||||
<url>http://scala-tools.org/repo-releases</url>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.scala-lang</groupId>
|
<groupId>org.scala-lang</groupId>
|
||||||
|
|
|
||||||
|
|
@ -17,46 +17,42 @@ class ActiveObjectInvocationTimeoutException(msg: String) extends ActiveObjectEx
|
||||||
/**
|
/**
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
object ActiveObject {
|
class ActiveObjectFactory {
|
||||||
|
def newInstance[T](intf: Class[_], proxy: ActiveObjectProxy): T = ActiveObject.newInstance(intf, proxy)
|
||||||
|
|
||||||
def newInstance[T](intf: Class[T] forSome {type T}, target: AnyRef, timeout: Int): T = {
|
def supervise(restartStrategy: RestartStrategy, components: JList[Worker]): Supervisor =
|
||||||
val proxy = new ActiveObjectProxy(target, timeout)
|
ActiveObject.supervise(restartStrategy, components.toArray.toList.asInstanceOf[List[Worker]])
|
||||||
supervise(proxy)
|
|
||||||
newInstance(intf, proxy)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def newInstance[T](intf: Class[T] forSome {type T}, proxy: ActiveObjectProxy): T = {
|
|
||||||
|
/**
|
||||||
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
|
*/
|
||||||
|
object ActiveObject {
|
||||||
|
def newInstance[T](intf: Class[_], proxy: ActiveObjectProxy): T = {
|
||||||
Proxy.newProxyInstance(
|
Proxy.newProxyInstance(
|
||||||
proxy.target.getClass.getClassLoader,
|
intf.getClassLoader,
|
||||||
Array(intf),
|
Array(intf),
|
||||||
proxy).asInstanceOf[T]
|
proxy).asInstanceOf[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
def supervise(restartStrategy: RestartStrategy, components: List[Worker]): Supervisor = {
|
def supervise(restartStrategy: RestartStrategy, components: List[Worker]): Supervisor = {
|
||||||
object factory extends SupervisorFactory {
|
object factory extends SupervisorFactory {
|
||||||
override def getSupervisorConfig: SupervisorConfig = {
|
override def getSupervisorConfig = SupervisorConfig(restartStrategy, components)
|
||||||
SupervisorConfig(restartStrategy, components)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val supervisor = factory.newSupervisor
|
val supervisor = factory.newSupervisor
|
||||||
supervisor ! scala.actors.behavior.Start
|
supervisor ! scala.actors.behavior.Start
|
||||||
supervisor
|
supervisor
|
||||||
}
|
}
|
||||||
|
|
||||||
private def supervise(proxy: ActiveObjectProxy): Supervisor =
|
|
||||||
supervise(
|
|
||||||
RestartStrategy(OneForOne, 5, 1000),
|
|
||||||
Worker(
|
|
||||||
proxy.server,
|
|
||||||
LifeCycle(Permanent, 100))
|
|
||||||
:: Nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
class ActiveObjectProxy(val target: AnyRef, val timeout: Int) extends InvocationHandler {
|
class ActiveObjectProxy(val intf: Class[_], val target: Class[_], val timeout: Int) extends InvocationHandler {
|
||||||
private val oneway = classOf[scala.actors.annotation.oneway]
|
private val oneway = classOf[scala.actors.annotation.oneway]
|
||||||
|
private var targetInstance: AnyRef = _
|
||||||
|
private[akka] def setTargetInstance(instance: AnyRef) = targetInstance = instance
|
||||||
|
|
||||||
private[ActiveObjectProxy] object dispatcher extends GenericServer {
|
private[ActiveObjectProxy] object dispatcher extends GenericServer {
|
||||||
override def body: PartialFunction[Any, Unit] = {
|
override def body: PartialFunction[Any, Unit] = {
|
||||||
|
|
@ -72,10 +68,10 @@ class ActiveObjectProxy(val target: AnyRef, val timeout: Int) extends Invocation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private[akka] val server = new GenericServerContainer(target.getClass.getName, () => dispatcher)
|
private[akka] val server = new GenericServerContainer(target.getName, () => dispatcher)
|
||||||
server.setTimeout(timeout)
|
server.setTimeout(timeout)
|
||||||
|
|
||||||
def invoke(proxy: AnyRef, m: Method, args: Array[AnyRef]): AnyRef = invoke(Invocation(m, args, target))
|
def invoke(proxy: AnyRef, m: Method, args: Array[AnyRef]): AnyRef = invoke(Invocation(m, args, targetInstance))
|
||||||
|
|
||||||
def invoke(invocation: Invocation): AnyRef = {
|
def invoke(invocation: Invocation): AnyRef = {
|
||||||
if (invocation.method.isAnnotationPresent(oneway)) server ! invocation
|
if (invocation.method.isAnnotationPresent(oneway)) server ! invocation
|
||||||
|
|
@ -92,6 +88,8 @@ class ActiveObjectProxy(val target: AnyRef, val timeout: Int) extends Invocation
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
case class Invocation(val method: Method, val args: Array[Object], val target: AnyRef) {
|
case class Invocation(val method: Method, val args: Array[Object], val target: AnyRef) {
|
||||||
|
method.setAccessible(true);
|
||||||
|
|
||||||
def invoke: AnyRef = method.invoke(target, args: _*)
|
def invoke: AnyRef = method.invoke(target, args: _*)
|
||||||
|
|
||||||
override def toString: String = "Invocation[method: " + method.getName + ", args: " + args + ", target: " + target + "]"
|
override def toString: String = "Invocation[method: " + method.getName + ", args: " + args + ", target: " + target + "]"
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,16 @@
|
||||||
package com.scalablesolutions.akka.api
|
package com.scalablesolutions.akka.api
|
||||||
|
|
||||||
import com.scalablesolutions.akka.kernel.{ActiveObject, ActiveObjectProxy}
|
import com.scalablesolutions.akka.kernel.{ActiveObject, ActiveObjectProxy}
|
||||||
|
import google.inject.{AbstractModule}
|
||||||
|
|
||||||
import java.util.{List => JList}
|
import java.util.{List => JList, ArrayList}
|
||||||
|
|
||||||
import scala.actors.behavior._
|
import scala.actors.behavior._
|
||||||
import scala.reflect.BeanProperty
|
import scala.reflect.BeanProperty
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Java version of the configuration API
|
||||||
|
|
||||||
sealed abstract class Configuration
|
sealed abstract class Configuration
|
||||||
|
|
||||||
class RestartStrategy(@BeanProperty val scheme: FailOverScheme, @BeanProperty val maxNrOfRetries: Int, @BeanProperty val withinTimeRange: Int) extends Configuration {
|
class RestartStrategy(@BeanProperty val scheme: FailOverScheme, @BeanProperty val maxNrOfRetries: Int, @BeanProperty val withinTimeRange: Int) extends Configuration {
|
||||||
|
|
@ -44,48 +48,58 @@ class OneForOne extends FailOverScheme {
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Server extends Configuration
|
abstract class Server extends Configuration
|
||||||
class SupervisorConfig(@BeanProperty val restartStrategy: RestartStrategy, @BeanProperty val servers: JList[Server]) extends Server {
|
//class SupervisorConfig(@BeanProperty val restartStrategy: RestartStrategy, @BeanProperty val servers: JList[Server]) extends Server {
|
||||||
def transform = scala.actors.behavior.SupervisorConfig(restartStrategy.transform, servers.toArray.toList.asInstanceOf[List[Component]].map(_.transform))
|
// def transform = scala.actors.behavior.SupervisorConfig(restartStrategy.transform, servers.toArray.toList.asInstanceOf[List[Server]].map(_.transform))
|
||||||
}
|
|
||||||
class Component(@BeanProperty val proxy: ActiveObjectProxy, @BeanProperty val lifeCycle: LifeCycle) extends Server {
|
|
||||||
def transform = scala.actors.behavior.Worker(proxy.server, lifeCycle.transform)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
object Configuration {
|
|
||||||
import com.google.inject.{AbstractModule, CreationException, Guice, Injector, Provides, Singleton}
|
|
||||||
import com.google.inject.spi.CloseFailedException
|
|
||||||
import com.google.inject.jsr250.{ResourceProviderFactory}
|
|
||||||
import javax.annotation.Resource
|
|
||||||
import javax.naming.Context
|
|
||||||
|
|
||||||
def supervise(restartStrategy: RestartStrategy, components: JList[Component]): Supervisor = {
|
|
||||||
val componentList = components.toArray.toList.asInstanceOf[List[Component]]
|
|
||||||
|
|
||||||
val injector = Guice.createInjector(new AbstractModule {
|
|
||||||
protected def configure = {
|
|
||||||
bind(classOf[ResourceProviderFactory[_]])
|
|
||||||
componentList.foreach(c => bind(c.getClass).in(classOf[Singleton]))
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Provides
|
|
||||||
// def createJndiContext: Context = {
|
|
||||||
// val answer = new JndiContext
|
|
||||||
// answer.bind("foo", new AnotherBean("Foo"))
|
|
||||||
// answer.bind("xyz", new AnotherBean("XYZ"))
|
|
||||||
// answer
|
|
||||||
//}
|
//}
|
||||||
})
|
class Component(@BeanProperty val intf: Class[_],
|
||||||
|
@BeanProperty val target: Class[_],
|
||||||
val injectedComponents = componentList.map(c => injector.getInstance(c.getClass))
|
@BeanProperty val lifeCycle: LifeCycle,
|
||||||
|
@BeanProperty val timeout: Int) extends Server {
|
||||||
// TODO: swap 'target' in proxy before running supervise
|
def newWorker(proxy: ActiveObjectProxy) = scala.actors.behavior.Worker(proxy.server, lifeCycle.transform)
|
||||||
|
|
||||||
ActiveObject.supervise(
|
|
||||||
restartStrategy.transform,
|
|
||||||
componentList.map(c => scala.actors.behavior.Worker(c.proxy.server, c.lifeCycle.transform)))
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
|
*/
|
||||||
|
//object Configuration {
|
||||||
|
// import com.google.inject.{Module, AbstractModule, CreationException, Guice, Injector, Provides, Singleton, Binder}
|
||||||
|
// import com.google.inject.jsr250.{ResourceProviderFactory}
|
||||||
|
//
|
||||||
|
// private val modules = new ArrayList[Module]
|
||||||
|
//
|
||||||
|
// def addModule(module: Module) = modules.add(module)
|
||||||
|
//
|
||||||
|
// def supervise(restartStrategy: RestartStrategy, components: Array[Component]): Supervisor = {
|
||||||
|
// val componentList = components.toList.asInstanceOf[List[Component]]
|
||||||
|
//
|
||||||
|
// object defaultModule extends AbstractModule {
|
||||||
|
// protected def configure {
|
||||||
|
// bind(classOf[ResourceProviderFactory[_]])
|
||||||
|
// //componentList.foreach(c => bind(c.proxy.intf.asInstanceOf[Class[_]]).to(c.proxy.target.getClass.asInstanceOf[Class[_]]).in(classOf[Singleton]))
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // @Provides
|
||||||
|
// // def createJndiContext: Context = {
|
||||||
|
// // val answer = new JndiContext
|
||||||
|
// // answer.bind("foo", new AnotherBean("Foo"))
|
||||||
|
// // answer.bind("xyz", new AnotherBean("XYZ"))
|
||||||
|
// // answer
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
// modules.add(defaultModule)
|
||||||
|
// val injector = Guice.createInjector(modules)
|
||||||
|
//
|
||||||
|
// // swap 'target' in proxy before running supervise
|
||||||
|
// // componentList.foreach(c => c.proxy.target = injector.getInstance(c.proxy.targetClass))
|
||||||
|
//
|
||||||
|
// ActiveObject.supervise(
|
||||||
|
// restartStrategy.transform,
|
||||||
|
// componentList.map(c => scala.actors.behavior.Worker(c.proxy.server, c.lifeCycle.transform)))
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
31
pom.xml
31
pom.xml
|
|
@ -23,4 +23,35 @@
|
||||||
<module>api-java</module>
|
<module>api-java</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>repo1.maven</id>
|
||||||
|
<name>Maven Main Repository</name>
|
||||||
|
<url>http://repo1.maven.org/maven2</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>scala-tools-snapshots</id>
|
||||||
|
<name>Scala-Tools Maven2 Snapshot Repository</name>
|
||||||
|
<url>http://scala-tools.org/repo-snapshots</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>scala-tools</id>
|
||||||
|
<name>Scala-Tools Maven2 Repository</name>
|
||||||
|
<url>http://scala-tools.org/repo-releases</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>lag</id>
|
||||||
|
<name>Configgy's' Repository</name>
|
||||||
|
<url>http://www.lag.net/repo</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>scala-tools.org</id>
|
||||||
|
<name>Scala-Tools Maven2 Repository</name>
|
||||||
|
<url>http://scala-tools.org/repo-releases</url>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue