public function lookup(BeanContextInterface $beanManager, $className, $sessionId = null, array $args = array())
{
// load the object manager
/** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ObjectManagerInterface $objectManager */
$objectManager = $beanManager->getApplication()->search('ObjectManagerInterface');
// load the bean descriptor
$descriptor = $objectManager->getObjectDescriptors()->get($className);
// query whether we've a SFSB
if ($descriptor instanceof StatefulSessionBeanDescriptorInterface) {
// try to load the stateful session bean from the bean manager
if ($instance = $beanManager->lookupStatefulSessionBean($sessionId, $className)) {
// load the object manager and re-inject the dependencies
/** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ProviderInterface $provider */
$provider = $beanManager->getApplication()->search('ProviderInterface');
$provider->injectDependencies($instance, $sessionId);
// we've to check for post-detach callbacks
foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) {
$instance->{$postDetachCallback}();
}
// return the instance
return $instance;
}
// if not create a new instance and return it
$instance = $beanManager->newInstance($className, $sessionId, $args);
// we've to check for post-construct callbacks
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
$instance->{$postConstructCallback}();
}
// return the instance
return $instance;
}
// query whether we've a SSB
if ($descriptor instanceof SingletonSessionBeanDescriptorInterface) {
// try to load the singleton session bean from the bean manager
if ($instance = $beanManager->lookupSingletonSessionBean($className)) {
// load the object manager and re-inject the dependencies
/** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ProviderInterface $provider */
$provider = $beanManager->getApplication()->search('ProviderInterface');
$provider->injectDependencies($instance, $sessionId);
// we've to check for post-detach callbacks
foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) {
$instance->{$postDetachCallback}();
}
// return the instance
return $instance;
}
// singleton session beans MUST extends \Stackable
if (is_subclass_of($className, '\\Stackable') === false) {
throw new EnterpriseBeansException(sprintf('Singleton session bean %s MUST extend \\Stackable', $className));
}
// if not create a new instance and return it
$instance = $beanManager->newSingletonSessionBeanInstance($className, $sessionId, $args);
// add the singleton session bean to the container
$beanManager->getSingletonSessionBeans()->set($className, $instance);
// we've to check for post-construct callback
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
$instance->{$postConstructCallback}();
}
// return the instance
return $instance;
}
// query whether we've a SLSB
if ($descriptor instanceof StatelessSessionBeanDescriptorInterface) {
// if not create a new instance and return it
$instance = $beanManager->newInstance($className, $sessionId, $args);
// we've to check for post-construct callback
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
$instance->{$postConstructCallback}();
}
// return the instance
return $instance;
}
// query whether we've a MDB
if ($descriptor instanceof MessageDrivenBeanDescriptorInterface) {
// create a new instance and return it
return $beanManager->newInstance($className, $sessionId, $args);
}
// we've an unknown bean type => throw an exception
throw new InvalidBeanTypeException(sprintf('Try to lookup a bean %s with missing enterprise annotation', $className));
}