initial guice integration started
This commit is contained in:
parent
d01a293fa7
commit
42cc47bf6a
4 changed files with 40 additions and 234 deletions
|
|
@ -1,133 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009 Scalable Solutions.
|
||||
*/
|
||||
|
||||
package com.scalablesolutions.akka.api;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.CreationException;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.spi.CloseFailedException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.naming.Context;
|
||||
|
||||
import scala.actors.behavior.*;
|
||||
|
||||
public class Configurator {
|
||||
|
||||
static public Supervisor supervise(Configuration.RestartStrategy restartStrategy, List<Configuration.Component> components) {
|
||||
return null;
|
||||
// SupervisorFactory factory = new SupervisorFactory() {
|
||||
// @Override public SupervisorConfig getSupervisorConfig() {
|
||||
// new SupervisorConfig(restartStrategy, components.map(c => Worker(c.component.server, c.lifeCycle)))
|
||||
// }
|
||||
// }
|
||||
// val supervisor = factory.newSupervisor
|
||||
// supervisor ! scala.actors.behavior.Start
|
||||
// supervisor
|
||||
}
|
||||
|
||||
// private def supervise(proxy: ActiveObjectProxy): Supervisor =
|
||||
// supervise(
|
||||
// RestartStrategy(OneForOne, 5, 1000),
|
||||
// Component(
|
||||
// proxy,
|
||||
// LifeCycle(Permanent, 100))
|
||||
// :: Nil)
|
||||
|
||||
// val fooProxy = new ActiveObjectProxy(new FooImpl, 1000)
|
||||
// val barProxy = new ActiveObjectProxy(new BarImpl, 1000)
|
||||
|
||||
// val supervisor =
|
||||
// ActiveObject.supervise(
|
||||
// RestartStrategy(AllForOne, 3, 100),
|
||||
// Component(
|
||||
// fooProxy,
|
||||
// LifeCycle(Permanent, 100)) ::
|
||||
// Component(
|
||||
// barProxy,
|
||||
// LifeCycle(Permanent, 100))
|
||||
// :: Nil)
|
||||
|
||||
// val foo = ActiveObject.newInstance[Foo](classOf[Foo], fooProxy)
|
||||
// val bar = ActiveObject.newInstance[Bar](classOf[Bar], barProxy)
|
||||
|
||||
|
||||
|
||||
// public void testResourceInjection() throws CreationException, CloseFailedException {
|
||||
// Injector injector = Guice.createInjector(new AbstractModule() {
|
||||
// protected void configure() {
|
||||
// bind(ResourceProviderFactory.class);
|
||||
// bind(MyBean.class).in(Singleton.class);
|
||||
// }
|
||||
|
||||
// @Provides
|
||||
// public Context createJndiContext() throws Exception {
|
||||
// Context answer = new JndiContext();
|
||||
// answer.bind("foo", new AnotherBean("Foo"));
|
||||
// answer.bind("xyz", new AnotherBean("XYZ"));
|
||||
// return answer;
|
||||
// }
|
||||
// });
|
||||
|
||||
// MyBean bean = injector.getInstance(MyBean.class);
|
||||
// assertNotNull("Should have instantiated the bean", bean);
|
||||
// assertNotNull("Should have injected a foo", bean.foo);
|
||||
// assertNotNull("Should have injected a bar", bean.bar);
|
||||
|
||||
// assertEquals("Should have injected correct foo", "Foo", bean.foo.name);
|
||||
// assertEquals("Should have injected correct bar", "XYZ", bean.bar.name);
|
||||
// }
|
||||
|
||||
// public static class MyBean {
|
||||
// @Resource
|
||||
// public AnotherBean foo;
|
||||
|
||||
// public AnotherBean bar;
|
||||
|
||||
// @Resource(name = "xyz")
|
||||
// public void bar(AnotherBean bar) {
|
||||
// this.bar = bar;
|
||||
// }
|
||||
// }
|
||||
|
||||
// static class AnotherBean {
|
||||
// public String name = "undefined";
|
||||
|
||||
// AnotherBean(String name) {
|
||||
// this.name = name;
|
||||
// }
|
||||
|
||||
// Injector injector = Guice.createInjector(new AbstractModule() {
|
||||
// protected void configure() {
|
||||
// Jsr250.bind(binder());
|
||||
|
||||
// bind(MyBean.class).in(Singleton.class);
|
||||
// }
|
||||
// });
|
||||
|
||||
// Injector injector = Guice.createInjector(new AbstractModule() {
|
||||
// protected void configure() {
|
||||
// bind(ResourceProviderFactory.class);
|
||||
// bind(MyBean.class).in(Singleton.class);
|
||||
// }
|
||||
|
||||
// @Provides
|
||||
// public Context createJndiContext() throws Exception {
|
||||
// Context answer = new JndiContext();
|
||||
// answer.bind("foo", new AnotherBean("Foo"));
|
||||
// answer.bind("xyz", new AnotherBean("XYZ"));
|
||||
// return answer;
|
||||
// }
|
||||
// });
|
||||
|
||||
// MyBean bean = injector.getInstance(MyBean.class);
|
||||
}
|
||||
|
||||
|
|
@ -76,6 +76,11 @@
|
|||
<artifactId>guice-core</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.guiceyfruit</groupId>
|
||||
<artifactId>guice-jsr250</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.specs</groupId>
|
||||
<artifactId>specs</artifactId>
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ class ActiveObjectProxy(val target: AnyRef, val timeout: Int) extends Invocation
|
|||
}
|
||||
}
|
||||
|
||||
private[kernel] val server = new GenericServerContainer(target.getClass.getName, () => dispatcher)
|
||||
private[akka] val server = new GenericServerContainer(target.getClass.getName, () => dispatcher)
|
||||
server.setTimeout(timeout)
|
||||
|
||||
def invoke(proxy: AnyRef, m: Method, args: Array[AnyRef]): AnyRef = invoke(Invocation(m, args, target))
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
package com.scalablesolutions.akka.api
|
||||
|
||||
import com.scalablesolutions.akka.kernel.ActiveObject
|
||||
import com.scalablesolutions.akka.kernel.{ActiveObject, ActiveObjectProxy}
|
||||
|
||||
import java.util.{List => JList}
|
||||
|
||||
|
|
@ -47,111 +47,45 @@ abstract class Server extends Configuration
|
|||
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))
|
||||
}
|
||||
class Component(@BeanProperty val serverContainer: GenericServerContainer, @BeanProperty val lifeCycle: LifeCycle) extends Server {
|
||||
def transform = scala.actors.behavior.Worker(serverContainer, lifeCycle.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 {
|
||||
def supervise(restartStrategy: RestartStrategy, components: JList[Component]): Supervisor =
|
||||
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
|
||||
// }
|
||||
})
|
||||
|
||||
val injectedComponents = componentList.map(c => injector.getInstance(c.getClass))
|
||||
|
||||
// TODO: swap 'target' in proxy before running supervise
|
||||
|
||||
ActiveObject.supervise(
|
||||
restartStrategy.transform,
|
||||
components.toArray.toList.asInstanceOf[List[Component]].map(
|
||||
c => scala.actors.behavior.Worker(c.serverContainer, c.lifeCycle.transform)))
|
||||
componentList.map(c => scala.actors.behavior.Worker(c.proxy.server, c.lifeCycle.transform)))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// static class SupervisorConfig extends Server {
|
||||
// private final RestartStrategy restartStrategy;
|
||||
// private final List<Server> servers;
|
||||
// public SupervisorConfig(RestartStrategy restartStrategy, List<Server> servers) {
|
||||
// this.restartStrategy = restartStrategy;
|
||||
// this.servers = servers;
|
||||
// }
|
||||
// public RestartStrategy getRestartStrategy() {
|
||||
// return restartStrategy;
|
||||
// }
|
||||
// public List<Server> getServer() {
|
||||
// return servers;
|
||||
// }
|
||||
// public scala.actors.behavior.SupervisorConfig scalaVersion() {
|
||||
// List<scala.actors.behavior.Server> ss = new ArrayList<scala.actors.behavior.Server>();
|
||||
// for (Server s: servers) {
|
||||
// ss.add(s.scalaVersion());
|
||||
// }
|
||||
// return new scala.actors.behavior.SupervisorConfig(restartStrategy.scalaVersion(), ss);
|
||||
// }
|
||||
// }
|
||||
|
||||
// static class Component extends Server {
|
||||
// private final GenericServerContainer serverContainer;
|
||||
// private final LifeCycle lifeCycle;
|
||||
// public Component(GenericServerContainer serverContainer, LifeCycle lifeCycle) {
|
||||
// this.serverContainer = serverContainer;
|
||||
// this.lifeCycle = lifeCycle;
|
||||
// }
|
||||
// public GenericServerContainer getServerContainer() {
|
||||
// return serverContainer;
|
||||
// }
|
||||
// public LifeCycle getLifeCycle() {
|
||||
// return lifeCycle;
|
||||
// }
|
||||
// public scala.actors.behavior.Server scalaVersion() {
|
||||
// return new scala.actors.behavior.Worker(serverContainer, lifeCycle.scalaVersion());
|
||||
// }
|
||||
// }
|
||||
|
||||
// static class RestartStrategy extends Configuration {
|
||||
// private final FailOverScheme scheme;
|
||||
// private final int maxNrOfRetries;
|
||||
// private final int withinTimeRange;
|
||||
// public RestartStrategy(FailOverScheme scheme, int maxNrOfRetries, int withinTimeRange) {
|
||||
// this.scheme = scheme;
|
||||
// this.maxNrOfRetries = maxNrOfRetries;
|
||||
// this.withinTimeRange = withinTimeRange;
|
||||
// }
|
||||
// public FailOverScheme getFailOverScheme() {
|
||||
// return scheme;
|
||||
// }
|
||||
// public int getMaxNrOfRetries() {
|
||||
// return maxNrOfRetries;
|
||||
// }
|
||||
// public int getWithinTimeRange() {
|
||||
// return withinTimeRange;
|
||||
// }
|
||||
// public scala.actors.behavior.RestartStrategy scalaVersion() {
|
||||
// scala.actors.behavior.FailOverScheme fos;
|
||||
// switch (scheme) {
|
||||
// case AllForOne: fos = new scala.actors.behavior.AllForOne(); break;
|
||||
// case OneForOne: fos = new scala.actors.behavior.OneForOne(); break;
|
||||
// }
|
||||
// return new scala.actors.behavior.RestartStrategy(fos, maxNrOfRetries, withinTimeRange);
|
||||
// }
|
||||
// }
|
||||
|
||||
// static class LifeCycle extends Configuration {
|
||||
// private final Scope scope;
|
||||
// private final int shutdownTime;
|
||||
// public LifeCycle(Scope scope, int shutdownTime) {
|
||||
// this.scope = scope;
|
||||
// this.shutdownTime = shutdownTime;
|
||||
// }
|
||||
// public Scope getScope() {
|
||||
// return scope;
|
||||
// }
|
||||
// public int getShutdownTime() {
|
||||
// return shutdownTime;
|
||||
// }
|
||||
// public scala.actors.behavior.LifeCycle scalaVersion() {
|
||||
// scala.actors.behavior.Scope s;
|
||||
// switch (scope) {
|
||||
// case Permanent: s = new scala.actors.behavior.Permanent(); break;
|
||||
// case Transient: s = new scala.actors.behavior.Transient(); break;
|
||||
// case Temporary: s = new scala.actors.behavior.Temporary(); break;
|
||||
// }
|
||||
// return new scala.actors.behavior.LifeCycle(s, shutdownTime);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue