private void mapObjectInterface(final Class intrface) { if (intrface == BusinessLocalHome.class || intrface == BusinessRemoteHome.class || intrface == ServiceEndpoint.class) { return; } final Method[] interfaceMethods = intrface.getMethods(); for (final Method method : interfaceMethods) { final Class declaringClass = method.getDeclaringClass(); if (declaringClass == EJBObject.class || declaringClass == EJBLocalObject.class) { continue; } try { final Method beanMethod = beanClass.getMethod(method.getName(), method.getParameterTypes()); mapMethods(method, beanMethod); } catch (final NoSuchMethodException nsme) { throw new OpenEJBRuntimeException("Invalid method [" + method + "]. Not declared by " + beanClass.getName() + " class"); } } }
/** * Checks that the values specified via @Local and @Remote are *not*: * <p/> * - classes * - derived from javax.ejb.EJBObject * - derived from javax.ejb.EJBHome * - derived from javax.ejb.EJBLocalObject * - derived from javax.ejb.EJBLocalHome * * @param interfce * @param validation * @param ejbName * @param annotationName * @return */ private boolean isValidInterface(final Class interfce, final ValidationContext validation, final String ejbName, final String annotationName) { if (!interfce.isInterface()) { validation.fail(ejbName, "ann.notAnInterface", annotationName, interfce.getName()); return false; } else if (EJBHome.class.isAssignableFrom(interfce)) { validation.fail(ejbName, "ann.remoteOrLocal.ejbHome", annotationName, interfce.getName()); return false; } else if (EJBObject.class.isAssignableFrom(interfce)) { validation.fail(ejbName, "ann.remoteOrLocal.ejbObject", annotationName, interfce.getName()); return false; } else if (EJBLocalHome.class.isAssignableFrom(interfce)) { validation.fail(ejbName, "ann.remoteOrLocal.ejbLocalHome", annotationName, interfce.getName()); return false; } else if (EJBLocalObject.class.isAssignableFrom(interfce)) { validation.fail(ejbName, "ann.remoteOrLocal.ejbLocalObject", annotationName, interfce.getName()); return false; } return true; }
public EJBLocalObject getEJBLocalObject() throws IllegalStateException { doCheck(Call.getEJBLocalObject); final ThreadContext threadContext = ThreadContext.getThreadContext(); final BeanContext di = threadContext.getBeanContext(); if (di.getLocalInterface() == null) { throw new IllegalStateException("EJB " + di.getDeploymentID() + " does not have a local interface"); } final EjbObjectProxyHandler handler = new EntityEjbObjectHandler(di, threadContext.getPrimaryKey(), InterfaceType.EJB_LOCAL, new ArrayList<Class>(), di.getLocalInterface()); try { final Class[] interfaces = new Class[]{di.getLocalInterface(), IntraVmProxy.class}; return (EJBLocalObject) ProxyManager.newProxyInstance(interfaces, handler); } catch (final IllegalAccessException iae) { throw new InternalErrorException("Could not create IVM proxy for " + di.getLocalInterface() + " interface", iae); } }
public static <Proxy extends EJBLocalObject> Proxy getEjbProxy(final BeanContext beanContext, final EntityBean entity) { if (entity == null) { return null; } // build the primary key final Object primaryKey = getPrimaryKey(beanContext, entity); // get the cmp container if (!(beanContext.getContainer() instanceof CmpContainer)) { throw new IllegalArgumentException("Proxy is not connected to a CMP container but is conect to " + beanContext.getContainer().getClass().getName()); } final Proxy proxy = (Proxy) EjbObjectProxyHandler.createProxy(beanContext, primaryKey, InterfaceType.EJB_LOCAL_HOME, beanContext.getLocalInterface()); return proxy; }
private static <Bean extends EntityBean> Set<Bean> getEntityBeans(final Collection<?> proxies, final Class type) { if (proxies == null) { return null; } final Set<Bean> entities = new HashSet<Bean>(); for (final Object value : proxies) { if (type != null && !type.isInstance(value)) { throw new IllegalArgumentException("Object is not an instance of " + type.getName() + ": " + (value == null ? "null" : value.getClass().getName())); } final Bean entity = Cmp2Util.<Bean>getEntityBean((EJBLocalObject) value); if (entity == null) { throw new IllegalArgumentException("Entity has been deleted"); } entities.add(entity); } return entities; }
public void log() { Principal principal = context.getCallerPrincipal(); Map<String, Object> contextData = context.getContextData(); EJBLocalHome ejbLocalHome = context.getEJBLocalHome(); EJBLocalObject ejbLocalObject = context.getEJBLocalObject(); Ejb21StateLocal stateLocalEngine = context.getBusinessObject(Ejb21StateLocal.class); boolean isCallerInRole = context.isCallerInRole("admin"); logger.info("stateLocalEngineBean principal: " + principal); logger.info("stateLocalEngineBean contextData:" + contextData); logger.info("stateLocalEngineBean ejbLocalHome:" + ejbLocalHome); logger.info("stateLocalEngineBean ejbLocalObject:" + ejbLocalObject); logger.info("stateLocalEngineBean stateLocalEngineBean:" + stateLocalEngine); logger.info("stateLocalEngineBean isCallerInRole:" + isCallerInRole); }
public void log() { Principal principal = context.getCallerPrincipal(); Map<String, Object> contextData = context.getContextData(); EJBLocalHome ejbLocalHome = context.getEJBLocalHome(); EJBLocalObject ejbLocalObject = context.getEJBLocalObject(); Ejb21Local LocalEngine = context.getBusinessObject(Ejb21Local.class); boolean isCallerInRole = context.isCallerInRole("admin"); logger.info("ejb21LocalEngineBean principal: " + principal); logger.info("ejb21LocalEngineBean contextData:" + contextData); logger.info("ejb21LocalEngineBean ejbLocalHome:" + ejbLocalHome); logger.info("ejb21LocalEngineBean ejbLocalObject:" + ejbLocalObject); logger.info("ejb21LocalEngineBean LocalEngineBean:" + LocalEngine); logger.info("ejb21LocalEngineBean isCallerInRole:" + isCallerInRole); }
/** * Remove the given EJB instance. * @param ejb the EJB instance to remove * @see javax.ejb.EJBLocalObject#remove() */ protected void removeSessionBeanInstance(EJBLocalObject ejb) { if (ejb != null && !this.homeAsComponent) { try { ejb.remove(); } catch (Throwable ex) { logger.warn("Could not invoke 'remove' on local EJB proxy", ex); } } }
public InterfaceType getInterfaceType(final Class clazz) { final InterfaceType type = interfaces.get(clazz); if (type != null) { return type; } if (EJBLocalHome.class.isAssignableFrom(clazz)) { return InterfaceType.EJB_LOCAL_HOME; } if (EJBLocalObject.class.isAssignableFrom(clazz)) { return InterfaceType.EJB_LOCAL; } if (EJBHome.class.isAssignableFrom(clazz)) { return InterfaceType.EJB_HOME; } if (EJBObject.class.isAssignableFrom(clazz)) { return InterfaceType.EJB_OBJECT; } for (final Entry<Class, InterfaceType> entry : interfaces.entrySet()) { // for @Remote case where the loaded interface can be different from the stored one if (entry.getKey().getName().equals(clazz.getName())) { return entry.getValue(); } } return null; }
private static boolean containerMethod(final Method method) { return (method.getDeclaringClass() == EJBObject.class || method.getDeclaringClass() == EJBHome.class || method.getDeclaringClass() == EJBLocalObject.class || method.getDeclaringClass() == EJBLocalHome.class) && !method.getName().equals("remove"); }
private boolean isValidEjbInterface(final String b, final Class clazz, final String refName) { if (!clazz.isInterface()) { //NOPMD //It is not an interface. No validation necessary. } else if (EJBObject.class.isAssignableFrom(clazz)) { fail(b, "ann.ejb.ejbObject", clazz.getName(), refName); return false; } else if (EJBLocalObject.class.isAssignableFrom(clazz)) { fail(b, "ann.ejb.ejbLocalObject", clazz.getName(), refName); return false; } return true; }
public EJBLocalObject getEJBLocalObject() throws IllegalStateException { doCheck(Call.getEJBLocalObject); final ThreadContext threadContext = ThreadContext.getThreadContext(); final BeanContext di = threadContext.getBeanContext(); if (di.getLocalHomeInterface() == null) { throw new IllegalStateException("Bean does not have an EJBLocalObject interface: " + di.getDeploymentID()); } return (EJBLocalObject) EjbObjectProxyHandler.createProxy(di, threadContext.getPrimaryKey(), InterfaceType.EJB_LOCAL, di.getLocalInterface()); }
private List<Object> executeSelectQuery(final Query query, Object[] args) { // process args if (args == null) { args = NO_ARGS; } for (int i = 0; i < args.length; i++) { Object arg = args[i]; // ejb proxies need to be swapped out for real instance classes if (arg instanceof EJBObject) { arg = Cmp2Util.getEntityBean((EJBObject) arg); } if (arg instanceof EJBLocalObject) { arg = Cmp2Util.getEntityBean((EJBLocalObject) arg); } try { query.getParameter(i + 1); } catch (final IllegalArgumentException e) { // IllegalArgumentException means that the parameter with the // specified position does not exist continue; } query.setParameter(i + 1, arg); } // todo results should not be iterated over, but should instead // perform all work in a wrapper list on demand by the application code final List results = query.getResultList(); for (final Object value : results) { if (value instanceof EntityBean) { // todo don't activate beans already activated final EntityBean entity = (EntityBean) value; cmpCallback.setEntityContext(entity); cmpCallback.ejbActivate(entity); } } //noinspection unchecked return results; }
public int executeUpdateQuery(final BeanContext beanContext, final String signature, Object[] args) throws FinderException { final EntityManager entityManager = getEntityManager(beanContext); Query query = createNamedQuery(entityManager, signature); if (query == null) { final int parenIndex = signature.indexOf('('); if (parenIndex > 0) { final String shortName = signature.substring(0, parenIndex); query = createNamedQuery(entityManager, shortName); } if (query == null) { throw new FinderException("No query defined for method " + signature); } } // process args if (args == null) { args = NO_ARGS; } for (int i = 0; i < args.length; i++) { Object arg = args[i]; // ejb proxies need to be swapped out for real instance classes if (arg instanceof EJBObject) { arg = Cmp2Util.getEntityBean((EJBObject) arg); } if (arg instanceof EJBLocalObject) { arg = Cmp2Util.getEntityBean((EJBLocalObject) arg); } query.setParameter(i + 1, arg); } final int result = query.executeUpdate(); return result; }
public boolean contains(final Object o) { if (relatedLocal.isInstance(o)) { final Bean entity = getEntityBean((EJBLocalObject) o); return entity != null && getRelatedBeans(false, false).contains(entity); } return false; }
public boolean add(final Object proxy) { if (!relatedLocal.isInstance(proxy)) { throw new IllegalArgumentException("Object is not an instance of " + relatedLocal.getName() + ": " + (proxy == null ? "null" : proxy.getClass().getName())); } final Bean newEntity = getEntityBean((EJBLocalObject) proxy); if (newEntity == null) { throw new IllegalArgumentException("Ejb has been deleted"); } return add(newEntity); }
public boolean remove(final Object o) { if (!relatedLocal.isInstance(o)) { return false; } final Bean entity = getEntityBean((EJBLocalObject) o); final boolean changed = entity != null && getRelatedBeans(false, true).remove(entity); if (changed && relatedProperty != null) { toCmp2Entity(entity).OpenEJB_removeCmr(relatedProperty, source); } return changed; }
private Bean getEntityBean(final EJBLocalObject proxy) { if (proxy == null) { return null; } final Bean bean = Cmp2Util.<Bean>getEntityBean(proxy); return bean; }
@Override public boolean isIdentical(EJBLocalObject obj) throws EJBException { logger.info("isIdentical"); return false; }
@Override public boolean isIdentical(EJBLocalObject obj) throws EJBException { return false; }
@Override public EJBLocalObject getEJBLocalObject() throws IllegalStateException { throw new UnsupportedOperationException(); }
private void check_localInterfaceMethods(final RemoteBean b) { Class intrface = null; Class beanClass = null; try { intrface = loadClass(b.getLocal()); beanClass = loadClass(b.getEjbClass()); } catch (final OpenEJBException e) { return; } if (!EJBLocalObject.class.isAssignableFrom(intrface)) { return; } final Method[] interfaceMethods = intrface.getMethods(); for (int i = 0; i < interfaceMethods.length; i++) { if (interfaceMethods[i].getDeclaringClass() == EJBLocalObject.class) { continue; } final String name = interfaceMethods[i].getName(); try { final Class[] params = interfaceMethods[i].getParameterTypes(); beanClass.getMethod(name, params); } catch (final NoSuchMethodException nsme) { final List<Method> differentArgs = new ArrayList<Method>(); final List<Method> differentCase = new ArrayList<Method>(); for (final Method method : beanClass.getMethods()) { if (method.getName().equals(name)) { differentArgs.add(method); } else if (method.getName().equalsIgnoreCase(name)) { differentCase.add(method); } } if (differentArgs.size() > 0) { fail(b, "no.busines.method.args", interfaceMethods[i].getName(), interfaceMethods[i].toString(), "local", intrface.getName(), beanClass.getName(), differentArgs.size()); } if (differentCase.size() > 0) { fail(b, "no.busines.method.case", interfaceMethods[i].getName(), interfaceMethods[i].toString(), "local", intrface.getName(), beanClass.getName(), differentCase.size()); } if (differentArgs.size() == 0 && differentCase.size() == 0) { fail(b, "no.busines.method", interfaceMethods[i].getName(), interfaceMethods[i].toString(), "local", intrface.getName(), beanClass.getName()); } } } }
@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 ThreadContext callContext = new ThreadContext(beanContext, primKey); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type); if (!authorized) { throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied")); } final Class declaringClass = callMethod.getDeclaringClass(); final String methodName = callMethod.getName(); if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) { if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) { if (methodName.startsWith("create")) { return createEJBObject(callMethod, args, callContext, type); } else if (methodName.startsWith("find")) { return findMethod(callMethod, args, callContext, type); } else { return homeMethod(callMethod, args, callContext, type); } } else if (methodName.equals("remove")) { removeEJBObject(callMethod, args, callContext, type); return null; } } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) { removeEJBObject(callMethod, args, callContext, type); return null; } callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS); final Method runMethod = beanContext.getMatchingBeanMethod(callMethod); callContext.set(Method.class, runMethod); return invoke(type, callMethod, runMethod, args, callContext); } finally { ThreadContext.exit(oldCallContext); } }
@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 ThreadContext callContext = new ThreadContext(beanContext, primKey); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { final boolean authorized = securityService.isCallerAuthorized(callMethod, type); if (!authorized) { throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied")); } final Class declaringClass = callMethod.getDeclaringClass(); final String methodName = callMethod.getName(); if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) { if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) { if (methodName.startsWith("create")) { return createEJBObject(callMethod, args, callContext, type); } else if (methodName.equals("findByPrimaryKey")) { return findByPrimaryKey(callMethod, args, callContext, type); } else if (methodName.startsWith("find")) { return findEJBObject(callMethod, args, callContext, type); } else { return homeMethod(callMethod, args, callContext, type); } } else if (methodName.equals("remove")) { removeEJBObject(callMethod, callContext, type); return null; } } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) { removeEJBObject(callMethod, callContext, type); return null; } // business method callContext.setCurrentOperation(Operation.BUSINESS); final Method runMethod = beanContext.getMatchingBeanMethod(callMethod); callContext.set(Method.class, runMethod); return businessMethod(callMethod, runMethod, args, callContext, type); } finally { ThreadContext.exit(oldCallContext); } }
@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 { return null;// EJBHome.remove( ) and other EJBHome methods are not process by the container } } else if (EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) { return null;// EJBObject.remove( ) and other EJBObject methods are not process by the container } 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(); } } }
/** * {@inheritDoc} */ public EJBLocalObject getEJBLocalObject() throws IllegalStateException { // no-op return null; }
private boolean isValidInterface(final RemoteBean b, final Class clazz, final Class beanClass, final String tag) { if (clazz.equals(beanClass)) { fail(b, "xml." + tag + ".beanClass", clazz.getName()); } else if (!clazz.isInterface()) { fail(b, "xml." + tag + ".notInterface", clazz.getName()); } else if (EJBHome.class.isAssignableFrom(clazz)) { if (tag.equals("home")) { return true; } fail(b, "xml." + tag + ".ejbHome", clazz.getName()); } else if (EJBLocalHome.class.isAssignableFrom(clazz)) { if (tag.equals("localHome")) { return true; } fail(b, "xml." + tag + ".ejbLocalHome", clazz.getName()); } else if (EJBObject.class.isAssignableFrom(clazz)) { if (tag.equals("remote")) { return true; } fail(b, "xml." + tag + ".ejbObject", clazz.getName()); } else if (EJBLocalObject.class.isAssignableFrom(clazz)) { if (tag.equals("local")) { return true; } fail(b, "xml." + tag + ".ejbLocalObject", clazz.getName()); } else { if (tag.equals("businessLocal") || tag.equals("businessRemote")) { return true; } else if (clazz.isAnnotationPresent(Local.class)) { fail(b, "xml." + tag + ".businessLocal", clazz.getName()); } else if (clazz.isAnnotationPresent(Remote.class)) { fail(b, "xml." + tag + ".businessRemote", clazz.getName()); } else { fail(b, "xml." + tag + ".unknown", clazz.getName()); } } // must be tagged as <home>, <local-home>, <remote>, or <local> return false; }
/** * Release the given EJB instance. * Default implementation delegates to removeSessionBeanInstance. * @param ejb the EJB instance to release * @see #removeSessionBeanInstance */ protected void releaseSessionBeanInstance(EJBLocalObject ejb) { removeSessionBeanInstance(ejb); }