/** * Sorts the instance with the given comparator * or the PHP ksort() function. * * @return boolean|null Returns TRUE if the instance was sorted successfully */ protected function sort() { // if no comparator is passed sort the internal array // by its keys, else use the comparator if ($this->comparator == null) { return ksort($this->items); } else { return CollectionUtils::sort($this, $this->comparator); } }
/** * Sorts the passed array. * * @param array $src The Array to be sorted * @param integer $low The offset we start sorting * @param integer $high The number of elements to be sorted * @param \AppserverIo\Collections\ComparatorInterface $comperator The comperator used for sorting * * @return array The sorted array */ protected static function arraySort($src, $low, $high, ComparatorInterface $comperator) { // sort the array for ($i = $low; $i < $high; $i++) { for ($j = $i; $j > $low && $comperator->compare($src[$j - 1], $src[$j]) > 0; $j--) { $src = CollectionUtils::swap($src, $j, $j - 1); } } // return the sorted array return $src; }
/** * Invoke the passed remote method on the described session bean and return the result. * * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $remoteMethod The remote method description * @param \AppserverIo\Collections\CollectionInterface $sessions The collection with the sessions * * @return mixed The result of the remote method invocation */ 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); }
/** * Invoke the passed remote method on the described session bean and return the result. * * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $remoteMethod The remote method description * @param \AppserverIo\Collections\CollectionInterface $sessions The collection with the sessions * * @return mixed The result of the remote method invocation */ 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; }