From 42cc47bf6a9f3cc572864868d25464b4e1980813 Mon Sep 17 00:00:00 2001 From: Jonas Boner Date: Mon, 16 Feb 2009 22:49:33 +0100 Subject: [PATCH] initial guice integration started --- .../akka/api/Configurator.java | 133 ----------------- kernel/pom.xml | 5 + kernel/src/main/scala/ActiveObject.scala | 2 +- kernel/src/main/scala/Configuration.scala | 134 +++++------------- 4 files changed, 40 insertions(+), 234 deletions(-) delete mode 100755 api-java/src/main/java/com/scalablesolutions/akka/api/Configurator.java diff --git a/api-java/src/main/java/com/scalablesolutions/akka/api/Configurator.java b/api-java/src/main/java/com/scalablesolutions/akka/api/Configurator.java deleted file mode 100755 index 067982a2bc..0000000000 --- a/api-java/src/main/java/com/scalablesolutions/akka/api/Configurator.java +++ /dev/null @@ -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 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); -} - diff --git a/kernel/pom.xml b/kernel/pom.xml index 58a19cb49f..1bdd19f43c 100755 --- a/kernel/pom.xml +++ b/kernel/pom.xml @@ -76,6 +76,11 @@ guice-core 2.0-SNAPSHOT + + org.guiceyfruit + guice-jsr250 + 2.0-SNAPSHOT + org.specs specs diff --git a/kernel/src/main/scala/ActiveObject.scala b/kernel/src/main/scala/ActiveObject.scala index a90e41d234..f6c1ac5bf6 100755 --- a/kernel/src/main/scala/ActiveObject.scala +++ b/kernel/src/main/scala/ActiveObject.scala @@ -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)) diff --git a/kernel/src/main/scala/Configuration.scala b/kernel/src/main/scala/Configuration.scala index 52f6a0c0ff..4923c74c96 100755 --- a/kernel/src/main/scala/Configuration.scala +++ b/kernel/src/main/scala/Configuration.scala @@ -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 servers; -// public SupervisorConfig(RestartStrategy restartStrategy, List servers) { -// this.restartStrategy = restartStrategy; -// this.servers = servers; -// } -// public RestartStrategy getRestartStrategy() { -// return restartStrategy; -// } -// public List getServer() { -// return servers; -// } -// public scala.actors.behavior.SupervisorConfig scalaVersion() { -// List ss = new ArrayList(); -// 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); -// } -// } -// } -