From c9189ac741751cb41a78805025b0f90ceecfa3b9 Mon Sep 17 00:00:00 2001 From: Michael Kober Date: Tue, 20 Apr 2010 10:54:10 +0200 Subject: [PATCH] fixed #154 added ActiveObjectConfiguration with fluent API --- .../src/main/scala/actor/ActiveObject.scala | 74 ++++++++++++++++--- .../actor/ActiveObjectConfiguration.scala | 39 ++++++++++ 2 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 akka-core/src/main/scala/actor/ActiveObjectConfiguration.scala diff --git a/akka-core/src/main/scala/actor/ActiveObject.scala b/akka-core/src/main/scala/actor/ActiveObject.scala index 8cace19031..2446472e0e 100644 --- a/akka-core/src/main/scala/actor/ActiveObject.scala +++ b/akka-core/src/main/scala/actor/ActiveObject.scala @@ -41,141 +41,191 @@ object ActiveObject { def newInstance[T](target: Class[T], timeout: Long): T = newInstance(target, new Dispatcher(false, None), None, timeout) + def newInstance[T](target: Class[T]): T = + newInstance(target, new Dispatcher(false, None), None, Actor.TIMEOUT) + + def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long): T = + newInstance(intf, target, new Dispatcher(false, None), None, timeout) + + def newInstance[T](intf: Class[T], target: AnyRef): T = + newInstance(intf, target, new Dispatcher(false, None), None, Actor.TIMEOUT) + + def newRemoteInstance[T](target: Class[T], timeout: Long, hostname: String, port: Int): T = + newInstance(target, new Dispatcher(false, None), Some(new InetSocketAddress(hostname, port)), timeout) + + def newRemoteInstance[T](target: Class[T], hostname: String, port: Int): T = + newInstance(target, new Dispatcher(false, None), Some(new InetSocketAddress(hostname, port)), Actor.TIMEOUT) + + def newInstance[T](target: Class[T], config: ActiveObjectConfiguration): T = { + val actor = new Dispatcher(config._transactionRequired, config._restartCallbacks) + if (config._messageDispatcher.isDefined) { + actor.messageDispatcher = config._messageDispatcher.get + } + newInstance(target, actor, config._host, config._timeout) + } + + def newInstance[T](intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration): T = { + val actor = new Dispatcher(config._transactionRequired, config._restartCallbacks) + if (config._messageDispatcher.isDefined) { + actor.messageDispatcher = config._messageDispatcher.get + } + newInstance(intf, target, actor, config._host, config._timeout) + } + + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, restartCallbacks: Option[RestartCallbacks]): T = newInstance(target, new Dispatcher(false, restartCallbacks), None, timeout) - def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long): T = - newInstance(intf, target, new Dispatcher(false, None), None, timeout) - + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, restartCallbacks: Option[RestartCallbacks]): T = newInstance(intf, target, new Dispatcher(false, restartCallbacks), None, timeout) + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean): T = newInstance(target, new Dispatcher(transactionRequired, None), None, timeout) + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, restartCallbacks: Option[RestartCallbacks]): T = newInstance(target, new Dispatcher(transactionRequired, restartCallbacks), None, timeout) + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean): T = newInstance(intf, target, new Dispatcher(transactionRequired, None), None, timeout) + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, restartCallbacks: Option[RestartCallbacks]): T = newInstance(intf, target, new Dispatcher(transactionRequired, restartCallbacks), None, timeout) - def newRemoteInstance[T](target: Class[T], timeout: Long, hostname: String, port: Int): T = - newInstance(target, new Dispatcher(false, None), Some(new InetSocketAddress(hostname, port)), timeout) - - def newRemoteInstance[T](target: Class[T], timeout: Long, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = - newInstance(target, new Dispatcher(false, restartCallbacks), Some(new InetSocketAddress(hostname, port)), timeout) - + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, hostname: String, port: Int): T = newInstance(intf, target, new Dispatcher(false, None), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = newInstance(intf, target, new Dispatcher(false, restartCallbacks), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, hostname: String, port: Int): T = newInstance(target, new Dispatcher(transactionRequired, None), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = newInstance(target, new Dispatcher(transactionRequired, restartCallbacks), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, hostname: String, port: Int): T = newInstance(intf, target, new Dispatcher(transactionRequired, None), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = newInstance(intf, target, new Dispatcher(transactionRequired, restartCallbacks), Some(new InetSocketAddress(hostname, port)), timeout) + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, dispatcher: MessageDispatcher): T = { val actor = new Dispatcher(false, None) actor.messageDispatcher = dispatcher newInstance(target, actor, None, timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, dispatcher: MessageDispatcher, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(false, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(target, actor, None, timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, dispatcher: MessageDispatcher): T = { val actor = new Dispatcher(false, None) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, None, timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, dispatcher: MessageDispatcher, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(false, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, None, timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher): T = { val actor = new Dispatcher(transactionRequired, None) actor.messageDispatcher = dispatcher newInstance(target, actor, None, timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(transactionRequired, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(target, actor, None, timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher): T = { val actor = new Dispatcher(transactionRequired, None) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, None, timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(transactionRequired, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, None, timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, dispatcher: MessageDispatcher, hostname: String, port: Int): T = { val actor = new Dispatcher(false, None) actor.messageDispatcher = dispatcher newInstance(target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, dispatcher: MessageDispatcher, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(false, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, dispatcher: MessageDispatcher, hostname: String, port: Int): T = { val actor = new Dispatcher(false, None) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, dispatcher: MessageDispatcher, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(false, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, hostname: String, port: Int): T = { val actor = new Dispatcher(transactionRequired, None) actor.messageDispatcher = dispatcher newInstance(target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(target: Class[T], config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](target: Class[T], timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(transactionRequired, restartCallbacks) actor.messageDispatcher = dispatcher newInstance(target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, hostname: String, port: Int): T = { val actor = new Dispatcher(transactionRequired, None) actor.messageDispatcher = dispatcher newInstance(intf, target, actor, Some(new InetSocketAddress(hostname, port)), timeout) } + @deprecated("use newInstance(intf: Class[T], target: AnyRef, config: ActiveObjectConfiguration) instead") def newRemoteInstance[T](intf: Class[T], target: AnyRef, timeout: Long, transactionRequired: Boolean, dispatcher: MessageDispatcher, hostname: String, port: Int, restartCallbacks: Option[RestartCallbacks]): T = { val actor = new Dispatcher(transactionRequired, restartCallbacks) actor.messageDispatcher = dispatcher @@ -186,6 +236,9 @@ object ActiveObject { val proxy = Proxy.newInstance(target, false, true) actor.initialize(target, proxy) actor.timeout = timeout + if (remoteAddress.isDefined) { + actor.makeRemote(remoteAddress.get) + } AspectInitRegistry.register(proxy, AspectInit(target, actor, remoteAddress, timeout)) actor.start proxy.asInstanceOf[T] @@ -195,6 +248,9 @@ object ActiveObject { val proxy = Proxy.newInstance(Array(intf), Array(target), false, true) actor.initialize(target.getClass, target) actor.timeout = timeout + if (remoteAddress.isDefined) { + actor.makeRemote(remoteAddress.get) + } AspectInitRegistry.register(proxy, AspectInit(intf, actor, remoteAddress, timeout)) actor.start proxy.asInstanceOf[T] diff --git a/akka-core/src/main/scala/actor/ActiveObjectConfiguration.scala b/akka-core/src/main/scala/actor/ActiveObjectConfiguration.scala new file mode 100644 index 0000000000..8390d2bdbb --- /dev/null +++ b/akka-core/src/main/scala/actor/ActiveObjectConfiguration.scala @@ -0,0 +1,39 @@ +package se.scalablesolutions.akka.actor + +import _root_.java.net.InetSocketAddress +import _root_.se.scalablesolutions.akka.config.ScalaConfig.RestartCallbacks +import _root_.se.scalablesolutions.akka.dispatch.MessageDispatcher + + +final class ActiveObjectConfiguration { + private[akka] var _timeout: Long = Actor.TIMEOUT + private[akka] var _restartCallbacks: Option[RestartCallbacks] = None + private[akka] var _transactionRequired = false + private[akka] var _host: Option[InetSocketAddress] = None + private[akka] var _messageDispatcher: Option[MessageDispatcher] = None + + def timeout(timeout: Long) : ActiveObjectConfiguration = { + _timeout = timeout + this + } + + def restartCallbacks(pre: String, post: String) : ActiveObjectConfiguration = { + _restartCallbacks = Some(new RestartCallbacks(pre, post)) + this + } + + def makeTransactionRequired() : ActiveObjectConfiguration = { + _transactionRequired = true; + this + } + + def makeRemote(hostname: String, port: Int) : ActiveObjectConfiguration = { + _host = Some(new InetSocketAddress(hostname, port)) + this + } + + def dispatcher(messageDispatcher: MessageDispatcher) : ActiveObjectConfiguration = { + _messageDispatcher = Some(messageDispatcher) + this + } +} \ No newline at end of file