public function invoke(RemoteMethodInterface $remoteMethod, CollectionInterface $sessions)
{
// prepare method name and parameters and invoke method
$className = $remoteMethod->getClassName();
$methodName = $remoteMethod->getMethodName();
$parameters = $remoteMethod->getParameters();
$sessionId = $remoteMethod->getSessionId();
// load the application instance
$application = $this->getApplication();
// try to load the session with the ID passed in the remote method
$session = CollectionUtils::find($sessions, new FilterSessionPredicate($sessionId));
// query whether the session is available or not
if ($session instanceof CollectionInterface) {
// query whether we already have an instance in the session container
if ($instance = $session->exists($className)) {
$instance = $session->get($className);
}
}
// load a fresh bean instance and add it to the session container
if ($instance == null) {
$instance = $application->getNamingDirectory()->search($className);
}
// query whether we already have an instance in the session container
if ($session instanceof CollectionInterface) {
$session->add($className, $instance);
}
// invoke the remote method call on the local instance
return call_user_func_array(array($instance, $methodName), $parameters);
}