Created
June 4, 2013 11:04
-
-
Save karno/5705162 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AgentRuntime(profile: Properties, holder: ServiceBindingHolder[MicroRuntimeServiceBinder]) { | |
def startAgent(name: String, args: Array[AnyRef]): Future[AgentController] = holder.getServiceBinder() flatMap { | |
binder => createContainer(binder) | |
} flatMap { | |
binder => startAgentCore(name, args, binder) | |
} flatMap { | |
_ => future(MicroRuntime.getAgent(name)) | |
} | |
private def startAgentCore[T <: Agent](name: String, args: Array[AnyRef], | |
serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = { | |
val p = Promise[MicroRuntimeServiceBinder] | |
serviceBinder.startAgent(name, classOf[T].getCanonicalName, args, new RuntimeCallback[Void] { | |
def onFailure(t: Throwable) { | |
p.failure(t) | |
} | |
def onSuccess(_null: Void) { | |
p.success(serviceBinder) | |
} | |
}) | |
p.future | |
} | |
private def createContainer(serviceBinder: MicroRuntimeServiceBinder): Future[MicroRuntimeServiceBinder] = { | |
MicroRuntime.isRunning match { | |
case true => future(serviceBinder) | |
case false => { | |
val p = Promise[MicroRuntimeServiceBinder]() | |
serviceBinder.startAgentContainer(profile, new RuntimeCallback[Void] { | |
def onFailure(t: Throwable) { | |
p.failure(t) | |
} | |
def onSuccess(_null: Void) { | |
p.success(serviceBinder) | |
} | |
}) | |
p.future | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment