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->search($className, array($sessionId, array($application)));
}
// 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
$response = call_user_func_array(array($instance, $methodName), $parameters);
// load the object manager
$objectManager = $application->search(ObjectManagerInterface::IDENTIFIER);
// load the bean descriptor
$descriptor = $objectManager->getObjectDescriptors()->get(get_class($instance));
// initialize the flag to mark the instance to be re-attached
$attach = true;
// query if we've stateful session bean
if ($descriptor instanceof StatefulSessionBeanDescriptorInterface) {
// remove the SFSB instance if a remove method has been called
if ($descriptor->isRemoveMethod($methodName)) {
$this->removeStatefulSessionBean($sessionId, $descriptor->getClassName());
$attach = false;
// query whether the session is available or not
if ($session instanceof CollectionInterface) {
$session->remove($className);
}
}
}
// re-attach the bean instance if necessary
if ($attach === true) {
$this->attach($instance, $sessionId);
}
// return the remote method call result
return $response;
}