* @return
* @throws OpenEJBException
*/
public Object getInstance(ThreadContext callContext)
throws OpenEJBException {
CoreDeploymentInfo deploymentInfo = callContext.getDeploymentInfo();
Data data = (Data) deploymentInfo.getContainerData();
Stack pool = data.getPool();
if(strictPooling){
boolean acquired;
try {
if(timeout.getTime() <= 0L){
data.getSemaphore().acquire();
acquired = true;
} else {
acquired = data.getSemaphore().tryAcquire(timeout.getTime(),timeout.getUnit());
}
} catch (InterruptedException e2) {
throw new OpenEJBException("Unexpected Interruption of current thread: ",e2);
}
if(!acquired){
throw new IllegalStateException("An invocation of the Stateless Session Bean "+deploymentInfo.getEjbName()+" has timed-out");
}
}
Object bean = pool.pop();
if (bean == null) {
Class beanClass = deploymentInfo.getBeanClass();
ObjectRecipe objectRecipe = new ObjectRecipe(beanClass);
objectRecipe.allow(Option.FIELD_INJECTION);
objectRecipe.allow(Option.PRIVATE_PROPERTIES);
objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
objectRecipe.allow(Option.NAMED_PARAMETERS);
Operation originalOperation = callContext.getCurrentOperation();
BaseContext.State[] originalAllowedStates = callContext.getCurrentAllowedStates();
try {
Context ctx = deploymentInfo.getJndiEnc();
SessionContext sessionContext;
// This needs to be synchronized as this code is multi-threaded.
// In between the lookup and the bind a bind may take place in another Thread.
// This is a fix for GERONIMO-3444
synchronized(this){
try {
sessionContext = (SessionContext) ctx.lookup("java:comp/EJBContext");
} catch (NamingException e1) {
sessionContext = createSessionContext();
// TODO: This should work
ctx.bind("java:comp/EJBContext", sessionContext);
}
}
if (javax.ejb.SessionBean.class.isAssignableFrom(beanClass) || hasSetSessionContext(beanClass)) {
callContext.setCurrentOperation(Operation.INJECTION);
callContext.setCurrentAllowedStates(StatelessContext.getStates());
objectRecipe.setProperty("sessionContext", sessionContext);
}
// This is a fix for GERONIMO-3444
synchronized(this){
try {
ctx.lookup("java:comp/WebServiceContext");
} catch (NamingException e) {
WebServiceContext wsContext = new EjbWsContext(sessionContext);
ctx.bind("java:comp/WebServiceContext", wsContext);
}
}
fillInjectionProperties(objectRecipe, beanClass, deploymentInfo, ctx);
bean = objectRecipe.create(beanClass.getClassLoader());
Map unsetProperties = objectRecipe.getUnsetProperties();
if (unsetProperties.size() > 0) {
for (Object property : unsetProperties.keySet()) {
logger.warning("Injection: No such property '" + property + "' in class " + beanClass.getName());
}
}
HashMap<String, Object> interceptorInstances = new HashMap<String, Object>();
for (InterceptorData interceptorData : deploymentInfo.getAllInterceptors()) {
if (interceptorData.getInterceptorClass().equals(beanClass)) continue;
Class clazz = interceptorData.getInterceptorClass();
ObjectRecipe interceptorRecipe = new ObjectRecipe(clazz);
interceptorRecipe.allow(Option.FIELD_INJECTION);
interceptorRecipe.allow(Option.PRIVATE_PROPERTIES);
interceptorRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
interceptorRecipe.allow(Option.NAMED_PARAMETERS);
fillInjectionProperties(interceptorRecipe, clazz, deploymentInfo, ctx);
try {
Object interceptorInstance = interceptorRecipe.create(clazz.getClassLoader());
interceptorInstances.put(clazz.getName(), interceptorInstance);
} catch (ConstructionException e) {
throw new Exception("Failed to create interceptor: " + clazz.getName(), e);
}
}
interceptorInstances.put(beanClass.getName(), bean);
callContext.setCurrentOperation(Operation.POST_CONSTRUCT);
callContext.setCurrentAllowedStates(StatelessContext.getStates());
List<InterceptorData> callbackInterceptors = deploymentInfo.getCallbackInterceptors();
InterceptorStack interceptorStack = new InterceptorStack(bean, null, Operation.POST_CONSTRUCT, callbackInterceptors, interceptorInstances);
interceptorStack.invoke();
if (bean instanceof SessionBean){
callContext.setCurrentOperation(Operation.CREATE);
callContext.setCurrentAllowedStates(StatelessContext.getStates());
Method create = deploymentInfo.getCreateMethod();
interceptorStack = new InterceptorStack(bean, create, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap());
interceptorStack.invoke();
}
bean = new Instance(bean, interceptorInstances);