use of org.apache.openejb.BeanContext in project tomee by apache.
the class MdbContainer method undeploy.
public void undeploy(final BeanContext beanContext) throws OpenEJBException {
if (!(beanContext instanceof BeanContext)) {
return;
}
try {
final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
if (endpointFactory != null) {
CURRENT.set(beanContext);
try {
final ObjectName jmxBeanToRemove = mbeanNames.remove(beanContext);
if (jmxBeanToRemove != null) {
LocalMBeanServer.unregisterSilently(jmxBeanToRemove);
logger.info("Undeployed MDB control for " + beanContext.getDeploymentID());
}
final MdbActivationContext activationContext = activationContexts.remove(beanContext);
if (activationContext != null && activationContext.isStarted()) {
resourceAdapter.endpointDeactivation(endpointFactory, endpointFactory.getActivationSpec());
}
} finally {
CURRENT.remove();
}
final MBeanServer server = LocalMBeanServer.get();
for (final ObjectName objectName : endpointFactory.jmxNames) {
try {
server.unregisterMBean(objectName);
} catch (final Exception e) {
logger.error("Unable to unregister MBean " + objectName);
}
}
}
} finally {
beanContext.setContainer(null);
beanContext.setContainerData(null);
deployments.remove(beanContext.getDeploymentID());
}
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class MdbContainer method invoke.
public Object invoke(final Object deploymentId, final InterfaceType type, final Class callInterface, final Method method, final Object[] args, final Object primKey) throws OpenEJBException {
final BeanContext beanContext = getBeanContext(deploymentId);
final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
final MdbInstanceFactory instanceFactory = endpointFactory.getInstanceFactory();
final Instance instance;
try {
instance = (Instance) instanceFactory.createInstance(true);
} catch (final UnavailableException e) {
throw new SystemException("Unable to create instance for invocation", e);
}
try {
beforeDelivery(beanContext, instance, method, null);
final Object value = invoke(instance, method, type, args);
afterDelivery(instance);
return value;
} finally {
instanceFactory.freeInstance(instance, true);
}
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class SingletonContainer method invoke.
@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
final BeanContext beanContext = this.getBeanContext(deployID);
if (beanContext == null) {
throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
}
// Use the backup way to determine call type if null was supplied.
if (type == null) {
type = beanContext.getInterfaceType(callInterface);
}
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
final CurrentCreationalContext currentCreationalContext = beanContext.get(CurrentCreationalContext.class);
Object runAs = null;
try {
if (oldCallContext != null) {
final BeanContext oldBc = oldCallContext.getBeanContext();
if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(callContext, beanContext, oldBc);
}
}
final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
if (!authorized) {
throw new org.apache.openejb.ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
}
final Class declaringClass = callMethod.getDeclaringClass();
if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
if (callMethod.getName().startsWith("create")) {
return createEJBObject(beanContext, callMethod);
} else {
// EJBHome.remove( ) and other EJBHome methods are not process by the container
return null;
}
} else if (EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) {
// EJBObject.remove( ) and other EJBObject methods are not process by the container
return null;
}
final Instance instance = instanceManager.getInstance(callContext);
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
callContext.setCurrentAllowedStates(null);
callContext.set(Method.class, runMethod);
callContext.setInvokedInterface(callInterface);
if (currentCreationalContext != null) {
// noinspection unchecked
currentCreationalContext.set(instance.creationalContext);
}
return _invoke(callMethod, runMethod, args, instance, callContext, type);
} finally {
if (runAs != null) {
try {
securityService.associate(runAs);
} catch (final LoginException e) {
// no-op
}
}
ThreadContext.exit(oldCallContext);
if (currentCreationalContext != null) {
currentCreationalContext.remove();
}
}
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class SingletonContainer method _invoke.
protected Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType callType) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
final boolean read = javax.ejb.LockType.READ.equals(beanContext.getConcurrencyAttribute(runMethod));
final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);
Object returnValue;
try {
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, callType), callContext);
returnValue = null;
try {
if (callType == InterfaceType.SERVICE_ENDPOINT) {
callContext.setCurrentOperation(Operation.BUSINESS_WS);
returnValue = invokeWebService(args, beanContext, runMethod, instance);
} else {
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors);
returnValue = interceptorStack.invoke(args);
}
} catch (final Throwable e) {
// handle reflection exception
final ExceptionType type = beanContext.getExceptionType(e);
if (type == ExceptionType.SYSTEM) {
/* System Exception ****************************/
// The bean instance is not put into the pool via instanceManager.poolInstance
// and therefore the instance will be garbage collected and destroyed.
// For this reason the discardInstance method of the StatelessInstanceManager
// does nothing.
handleSystemException(txPolicy, e, callContext);
} else {
/* Application Exception ***********************/
handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
}
} finally {
afterInvoke(txPolicy, callContext);
}
} finally {
lock.unlock();
}
return returnValue;
}
use of org.apache.openejb.BeanContext in project tomee by apache.
the class StatelessInstanceManager method discardInstance.
/**
* This method is called to release the semaphore in case of the business method
* throwing a system exception
*
* @param callContext ThreadContext
* @param bean Object
*/
public void discardInstance(final ThreadContext callContext, final Object bean) throws SystemException {
if (bean == null) {
throw new SystemException("Invalid arguments");
}
final Instance instance = Instance.class.cast(bean);
final BeanContext beanContext = callContext.getBeanContext();
final Data data = (Data) beanContext.getContainerData();
if (null != data) {
final Pool<Instance> pool = data.getPool();
pool.discard(instance.getPoolEntry());
}
}
Aggregations