use of org.apache.openejb.BeanContext in project tomee by apache.
the class EntityContainer method ejbStore_If_No_Transaction.
public void ejbStore_If_No_Transaction(final ThreadContext callContext, final EntityBean bean) throws Exception {
final Operation currentOp = callContext.getCurrentOperation();
if (currentOp == Operation.BUSINESS) {
final TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
return;
}
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = beanContext.getTransactionPolicyFactory().createTransactionPolicy(TransactionType.Supports);
try {
// double check we don't have an active transaction
if (!txPolicy.isTransactionActive()) {
callContext.setCurrentOperation(Operation.STORE);
bean.ejbStore();
}
} catch (final Exception e) {
instanceManager.discardInstance(callContext, bean);
throw e;
} finally {
callContext.setCurrentOperation(currentOp);
txPolicy.commit();
}
}
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class EntityContainer method createEJBObject.
protected ProxyInfo createEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
callContext.setCurrentOperation(Operation.CREATE);
/*
* According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
* method executes in the same transaction context as the previous ejbCreate(...) method."
*
* For this reason the TransactionScopeHandler methods usally preformed by the invoke( )
* operation must be handled here along with the call explicitly.
* This ensures that the afterInvoke() is not processed between the ejbCreate and ejbPostCreate methods to
* ensure that the ejbPostCreate executes in the same transaction context of the ejbCreate.
* This would otherwise not be possible if container-managed transactions were used because
* the TransactionScopeManager would attempt to commit the transaction immediately after the ejbCreate
* and before the ejbPostCreate had a chance to execute. Once the ejbPostCreate method execute the
* super classes afterInvoke( ) method will be executed committing the transaction if its a CMT.
*/
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
EntityBean bean = null;
Object primaryKey = null;
try {
// Get new ready instance
bean = instanceManager.obtainInstance(callContext);
// Obtain the proper ejbCreate() method
final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod);
// invoke the ejbCreate which returns the primary key
primaryKey = ejbCreateMethod.invoke(bean, args);
didCreateBean(callContext, bean);
// determine post create callback method
final Method ejbPostCreateMethod = beanContext.getMatchingPostCreateMethod(ejbCreateMethod);
// create a new context containing the pk for the post create call
final ThreadContext postCreateContext = new ThreadContext(beanContext, primaryKey);
postCreateContext.setCurrentOperation(Operation.POST_CREATE);
final ThreadContext oldContext = ThreadContext.enter(postCreateContext);
try {
// Invoke the ejbPostCreate method on the bean instance
ejbPostCreateMethod.invoke(bean, args);
// According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
// method executes in the same transaction context as the previous ejbCreate(...) method."
//
// The bean is first insterted using db.create( ) and then after ejbPostCreate( ) its
// updated using db.update(). This protocol allows for visablity of the bean after ejbCreate
// within the current trasnaction.
} finally {
ThreadContext.exit(oldContext);
}
// update pool
instanceManager.poolInstance(callContext, bean, primaryKey);
} catch (final Throwable e) {
handleException(txPolicy, e, callContext, bean);
} finally {
afterInvoke(txPolicy, callContext);
}
return new ProxyInfo(beanContext, primaryKey);
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class EntityContainer method homeMethod.
protected Object homeMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
callContext.setCurrentOperation(Operation.HOME);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
return invoke(type, callMethod, runMethod, args, callContext);
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class EntityContainer method findMethod.
protected Object findMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
callContext.setCurrentOperation(Operation.FIND);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
Object returnValue = invoke(type, callMethod, runMethod, args, callContext);
/*
* Find operations return either a single primary key or a collection of primary keys.
* The primary keys are converted to ProxyInfo objects.
*/
if (returnValue instanceof Collection) {
final Iterator keys = ((Collection) returnValue).iterator();
final Vector<ProxyInfo> proxies = new Vector<>();
while (keys.hasNext()) {
final Object primaryKey = keys.next();
proxies.addElement(new ProxyInfo(beanContext, primaryKey));
}
returnValue = proxies;
} else if (returnValue instanceof Enumeration) {
final Enumeration keys = (Enumeration) returnValue;
final Vector<ProxyInfo> proxies = new Vector<>();
while (keys.hasMoreElements()) {
final Object primaryKey = keys.nextElement();
proxies.addElement(new ProxyInfo(beanContext, primaryKey));
}
returnValue = new ArrayEnumeration<>(proxies);
} else {
returnValue = new ProxyInfo(beanContext, returnValue);
}
return returnValue;
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class BaseEjbProxyHandler method invoke.
@Override
public Object invoke(final Object proxy, Method method, Object[] args) throws Throwable {
try {
isValidReference(method);
} catch (final IllegalStateException ise) {
// bean was undeployed
if (method.getName().equals("writeReplace")) {
// session serialization, we just need to replace this
final BeanContext beanContext = beanContextRef.get();
if (beanContext != null) {
return _writeReplace(proxy);
}
}
throw ise;
}
if (args == null) {
args = new Object[] {};
}
if (method.getDeclaringClass() == Object.class) {
final String methodName = method.getName();
switch(methodName) {
case "toString":
return toString();
case "equals":
return equals(args[0]) ? Boolean.TRUE : Boolean.FALSE;
case "hashCode":
return hashCode();
default:
throw new UnsupportedOperationException("Unknown method: " + method);
}
} else if (method.getDeclaringClass() == IntraVmProxy.class) {
final String methodName = method.getName();
if (methodName.equals("writeReplace")) {
return _writeReplace(proxy);
} else {
throw new UnsupportedOperationException("Unknown method: " + method);
}
} else if (method.getDeclaringClass() == BeanContext.Removable.class) {
return _invoke(proxy, BeanContext.Removable.class, method, args);
}
Class interfce = getInvokedInterface(method);
final ThreadContext callContext = ThreadContext.getThreadContext();
final Object localClientIdentity = ClientSecurity.getIdentity();
try {
if (callContext == null && localClientIdentity != null) {
final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
securityService.associate(localClientIdentity);
}
if (strategy == CLASSLOADER_COPY || getBeanContext().getInterfaceType(interfce) == InterfaceType.BUSINESS_REMOTE) {
IntraVmCopyMonitor.pre(strategy);
final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getBeanContext().getClassLoader());
try {
args = copyArgs(args);
method = copyMethod(method);
interfce = copyObj(interfce);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
IntraVmCopyMonitor.post();
}
} else if (strategy == COPY && args != null && args.length > 0) {
IntraVmCopyMonitor.pre(strategy);
try {
args = copyArgs(args);
} finally {
IntraVmCopyMonitor.post();
}
}
final IntraVmCopyMonitor.State oldStrategy = strategy;
if (getBeanContext().isAsynchronous(method) || getBeanContext().getComponentType().equals(BeanType.MANAGED)) {
strategy = IntraVmCopyMonitor.State.NONE;
}
try {
final Object returnValue = _invoke(proxy, interfce, method, args);
return copy(strategy, returnValue);
} catch (Throwable throwable) {
throwable = copy(strategy, throwable);
throw convertException(throwable, method, interfce);
} finally {
strategy = oldStrategy;
}
} finally {
if (callContext == null && localClientIdentity != null) {
final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
securityService.disassociate();
}
}
}
Aggregations