use of org.apache.openejb.ProxyInfo in project tomee by apache.
the class EntityEjbHomeHandler method findX.
protected Object findX(final Class interfce, final Method method, final Object[] args, final Object proxy) throws Throwable {
final Object retValue;
try {
retValue = container.invoke(deploymentID, interfaceType, interfce, method, args, null);
} catch (final OpenEJBException e) {
logger.debug("entityEjbHomeHandler.containerInvocationFailure", e, e.getMessage());
throw e;
}
if (retValue instanceof Collection) {
final Object[] proxyInfos = ((Collection) retValue).toArray();
final Vector<Object> proxies = new Vector<>();
for (Object proxyInfo1 : proxyInfos) {
final ProxyInfo proxyInfo = (ProxyInfo) proxyInfo1;
proxies.addElement(createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
}
return proxies;
} else if (retValue instanceof ArrayEnumeration) {
@SuppressWarnings("unchecked") final ArrayEnumeration<Object> enumeration = (ArrayEnumeration<Object>) retValue;
for (int i = enumeration.size() - 1; i >= 0; --i) {
final ProxyInfo proxyInfo = (ProxyInfo) enumeration.get(i);
enumeration.set(i, createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
}
return enumeration;
} else if (retValue instanceof Enumeration) {
final Enumeration enumeration = (Enumeration) retValue;
final List<Object> proxies = new ArrayList<>();
while (enumeration.hasMoreElements()) {
final ProxyInfo proxyInfo = (ProxyInfo) enumeration.nextElement();
proxies.add(createProxy(proxyInfo.getPrimaryKey(), getMainInterface()));
}
return new ArrayEnumeration<>(proxies);
} else {
final ProxyInfo proxyInfo = (ProxyInfo) retValue;
return createProxy(proxyInfo.getPrimaryKey(), getMainInterface());
}
}
use of org.apache.openejb.ProxyInfo in project tomee by apache.
the class StatelessContainer method invoke.
@SuppressWarnings("unchecked")
@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) {
final String msg = "Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')";
throw new OpenEJBException(msg);
}
// 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);
Instance bean = null;
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);
}
}
// Check auth before overriding context
final boolean authorized = type == InterfaceType.TIMEOUT || this.securityService.isCallerAuthorized(callMethod, type);
if (!authorized) {
throw new org.apache.openejb.ApplicationException(new javax.ejb.EJBAccessException("Unauthorized Access by Principal Denied"));
}
final Class declaringClass = callMethod.getDeclaringClass();
if (javax.ejb.EJBHome.class.isAssignableFrom(declaringClass) || javax.ejb.EJBLocalHome.class.isAssignableFrom(declaringClass)) {
if (callMethod.getName().startsWith("create")) {
return new ProxyInfo(beanContext, null);
} else {
// EJBHome.remove( ) and other EJBHome methods are not process by the container
return null;
}
} else if (javax.ejb.EJBObject.class == declaringClass || javax.ejb.EJBLocalObject.class == declaringClass) {
// EJBObject.remove( ) and other EJBObject methods are not process by the container
return null;
}
bean = this.instanceManager.getInstance(callContext);
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
callContext.set(Method.class, runMethod);
callContext.setInvokedInterface(callInterface);
if (currentCreationalContext != null) {
currentCreationalContext.set(bean.creationalContext);
}
return _invoke(callMethod, runMethod, args, bean, callContext, type);
} finally {
if (runAs != null) {
try {
securityService.associate(runAs);
} catch (final LoginException e) {
// no-op
}
}
if (bean != null) {
if (callContext.isDiscardInstance()) {
this.instanceManager.discardInstance(callContext, bean);
} else {
this.instanceManager.poolInstance(callContext, bean);
}
}
ThreadContext.exit(oldCallContext);
if (currentCreationalContext != null) {
currentCreationalContext.remove();
}
}
}
use of org.apache.openejb.ProxyInfo in project tomee by apache.
the class EjbRequestHandler method doEjbHome_FIND.
protected void doEjbHome_FIND(final EJBRequest req, final EJBResponse res) throws Exception {
final CallContext call = CallContext.getCallContext();
final RpcContainer c = (RpcContainer) call.getBeanContext().getContainer();
Object result = c.invoke(req.getDeploymentId(), InterfaceType.EJB_HOME, req.getInterfaceClass(), req.getMethodInstance(), req.getMethodParameters(), req.getPrimaryKey());
/* Multiple instances found */
if (result instanceof Collection) {
final Object[] primaryKeys = ((Collection) result).toArray();
for (int i = 0; i < primaryKeys.length; i++) {
final ProxyInfo proxyInfo = ((ProxyInfo) primaryKeys[i]);
if (proxyInfo == null) {
primaryKeys[i] = null;
} else {
primaryKeys[i] = proxyInfo.getPrimaryKey();
}
}
res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND_COLLECTION, primaryKeys);
} else if (result instanceof java.util.Enumeration) {
final java.util.Enumeration resultAsEnum = (java.util.Enumeration) result;
final java.util.List<Object> listOfPKs = new ArrayList<>();
while (resultAsEnum.hasMoreElements()) {
final ProxyInfo proxyInfo = ((ProxyInfo) resultAsEnum.nextElement());
if (proxyInfo == null) {
listOfPKs.add(null);
} else {
listOfPKs.add(proxyInfo.getPrimaryKey());
}
}
res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND_ENUMERATION, listOfPKs.toArray(new Object[listOfPKs.size()]));
/* Single instance found */
} else if (result instanceof ProxyInfo) {
final ProxyInfo proxyInfo = ((ProxyInfo) result);
result = proxyInfo.getPrimaryKey();
res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND, result);
} else if (result == null) {
res.setResponse(req.getVersion(), ResponseCodes.EJB_OK_FOUND, null);
} else {
final String message = "The bean is not EJB compliant. " + "The finder method [" + req.getMethodInstance().getName() + "] is declared " + "to return neither Collection nor the Remote Interface, " + "but [" + result.getClass().getName() + "]";
result = new RemoteException(message);
LOGGER.error(req + " " + message);
res.setResponse(req.getVersion(), ResponseCodes.EJB_SYS_EXCEPTION, result);
}
}
Aggregations