use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class ContextHandler method lookup.
@Override
public Object lookup(final String name) throws NamingException {
try {
if ("java:".equals(name)) {
return context;
}
return context.lookup(name);
} catch (final UndeclaredThrowableException ute) {
Throwable e = ute.getUndeclaredThrowable();
while (e != null) {
if (InvocationTargetException.class.isInstance(e)) {
final Throwable unwrap = InvocationTargetException.class.cast(e).getCause();
if (e == unwrap) {
throw new NameNotFoundException(name);
}
e = unwrap;
} else if (UndeclaredThrowableException.class.isInstance(e)) {
final Throwable unwrap = UndeclaredThrowableException.class.cast(e).getUndeclaredThrowable();
if (e == unwrap) {
throw new NameNotFoundException(name);
}
e = unwrap;
} else {
break;
}
if (NameNotFoundException.class.isInstance(e)) {
throw NameNotFoundException.class.cast(e);
}
}
throw new NameNotFoundException(name);
} catch (final NameNotFoundException nnfe) {
try {
return SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup(name);
} catch (final NameNotFoundException nnfe2) {
// ignore, let it be thrown
}
try {
final ThreadContext threadContext = ThreadContext.getThreadContext();
if (threadContext != null) {
return threadContext.getBeanContext().getModuleContext().getModuleJndiContext().lookup(name);
}
} catch (final Exception nnfe3) {
// ignore, let it be thrown
}
throw nnfe;
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class JndiEncArtifact method readResolve.
public Object readResolve() throws ObjectStreamException {
final ThreadContext thrdCntx = ThreadContext.getThreadContext();
final BeanContext deployment = thrdCntx.getBeanContext();
final Context cntx = deployment.getJndiEnc();
try {
final Object obj = cntx.lookup(path);
if (obj == null) {
throw new InvalidObjectException("JNDI ENC context reference could not be properly resolved when bean instance was activated");
}
return obj;
} catch (final NamingException e) {
throw (InvalidObjectException) new InvalidObjectException("JNDI ENC context reference could not be properly resolved due to a JNDI exception, when bean instance was activated").initCause(e);
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class javaURLContextFactory method getContext.
public static Context getContext() {
final ThreadContext callContext = ThreadContext.getThreadContext();
if (callContext == null) {
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
final ClassLoader current = Thread.currentThread().getContextClassLoader();
final Context globalContext = containerSystem.getJNDIContext();
if (current == null) {
return globalContext;
}
for (final AppContext appContext : containerSystem.getAppContexts()) {
for (final WebContext web : appContext.getWebContexts()) {
// more specific first
if (current.equals(web.getClassLoader())) {
return new ContextHandler(web.getJndiEnc());
}
}
if (current.equals(appContext.getClassLoader())) {
return new ContextHandler(appContext.getAppJndiContext());
}
}
return globalContext;
}
final BeanContext di = callContext.getBeanContext();
if (di != null) {
return di.getJndiEnc();
} else {
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
return containerSystem.getJNDIContext();
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class ManagedContainer method createEJBObject.
protected ProxyInfo createEJBObject(final BeanContext beanContext, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
// generate a new primary key
final Object primaryKey = newPrimaryKey();
final ThreadContext createContext = new ThreadContext(beanContext, primaryKey);
final ThreadContext oldCallContext = ThreadContext.enter(createContext);
try {
// Security check
checkAuthorization(callMethod, interfaceType);
// Create the extended entity managers for this instance
final Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = createEntityManagers(beanContext);
// Register the newly created entity managers
if (entityManagers != null) {
try {
entityManagerRegistry.addEntityManagers((String) beanContext.getDeploymentID(), primaryKey, entityManagers);
} catch (final EntityManagerAlreadyRegisteredException e) {
throw new EJBException(e);
}
}
createContext.setCurrentOperation(Operation.CREATE);
createContext.setCurrentAllowedStates(null);
// Start transaction
final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext);
Instance instance = null;
try {
try {
final InstanceContext context = beanContext.newInstance();
// Wrap-up everthing into a object
instance = new Instance(beanContext, primaryKey, context.getBean(), context.getInterceptors(), context.getCreationalContext(), entityManagers);
} catch (final Throwable throwable) {
final ThreadContext callContext = ThreadContext.getThreadContext();
EjbTransactionUtil.handleSystemException(callContext.getTransactionPolicy(), throwable, callContext);
// should never be reached
throw new IllegalStateException(throwable);
}
// add to cache
cache.add(primaryKey, instance);
// instance starts checked-out
checkedOutInstances.put(primaryKey, instance);
// Register for synchronization callbacks
registerSessionSynchronization(instance, createContext);
// Invoke create for legacy beans
if (!callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessRemoteHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalBeanHome.class)) {
// Setup for business invocation
final Method createOrInit = beanContext.getMatchingBeanMethod(callMethod);
createContext.set(Method.class, createOrInit);
// Initialize interceptor stack
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, createOrInit, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap<String, Object>());
// Invoke
if (args == null) {
interceptorStack.invoke();
} else {
interceptorStack.invoke(args);
}
}
} catch (final Throwable e) {
handleException(createContext, txPolicy, e);
} finally {
afterInvoke(createContext, txPolicy, instance);
}
return new ProxyInfo(beanContext, primaryKey);
} finally {
ThreadContext.exit(oldCallContext);
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class ManagedContainer method businessMethod.
protected Object businessMethod(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
try {
// Security check
checkAuthorization(callMethod, interfaceType);
// Start transaction
final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
Object returnValue = null;
Instance instance = null;
try {
// Obtain instance
instance = obtainInstance(primKey, callContext);
// Resume previous Bean transaction if there was one
if (txPolicy instanceof BeanTransactionPolicy) {
final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
if (suspendedTransaction != null) {
instance.setBeanTransaction(null);
final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
beanTxEnv.resumeUserTransaction(suspendedTransaction);
}
}
// Register the entity managers
registerEntityManagers(instance, callContext);
// Register for synchronization callbacks
registerSessionSynchronization(instance, callContext);
// Setup for business invocation
callContext.setCurrentOperation(Operation.BUSINESS);
callContext.setCurrentAllowedStates(null);
callContext.setInvokedInterface(callInterface);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
callContext.set(Method.class, runMethod);
// Initialize interceptor stack
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS, interceptors, instance.interceptors);
// Invoke
returnValue = interceptorStack.invoke(args);
} catch (final Throwable e) {
handleException(callContext, txPolicy, e);
} finally {
// Commit transaction
afterInvoke(callContext, txPolicy, instance);
}
return returnValue;
} finally {
ThreadContext.exit(oldCallContext);
}
}
Aggregations