private void ejbRemove(final EntityBean entityBean) throws RemoveException { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } if (isDeleted(entityBean)) { return; } final ThreadContext callContext = createThreadContext(entityBean); callContext.setCurrentOperation(Operation.REMOVE); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { entityBean.ejbRemove(); } catch (final RemoteException e) { throw new EJBException(e); } finally { // clear relationships // todo replace with interface call when CmpEntityBean interface is added try { entityBean.getClass().getMethod("OpenEJB_deleted").invoke(entityBean); } catch (final Exception ignored) { // no-op } cancelTimers(callContext); ThreadContext.exit(oldCallContext); } }
private boolean isDeleted(final EntityBean entityBean) { try { return (Boolean) entityBean.getClass().getMethod("OpenEJB_isDeleted").invoke(entityBean); } catch (final NoSuchMethodException e) { return false; } catch (final Exception e) { throw new EJBException(e); } }
private void setEntityContext(final EntityBean entityBean) { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } // activating entity doen't have a primary key final BeanContext beanContext = getBeanContextByClass(entityBean.getClass()); final ThreadContext callContext = new ThreadContext(beanContext, null); callContext.setCurrentOperation(Operation.SET_CONTEXT); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { entityBean.setEntityContext(new EntityContext(securityService)); } catch (final RemoteException e) { throw new EJBException(e); } finally { ThreadContext.exit(oldCallContext); } }
private ThreadContext createThreadContext(final EntityBean entityBean) { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } final BeanContext beanContext = getBeanContextByClass(entityBean.getClass()); final KeyGenerator keyGenerator = beanContext.getKeyGenerator(); final Object primaryKey = keyGenerator.getPrimaryKey(entityBean); return new ThreadContext(beanContext, primaryKey); }
private void ejbLoad(final EntityBean entityBean) { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } final ThreadContext callContext = createThreadContext(entityBean); callContext.setCurrentOperation(Operation.LOAD); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { entityBean.ejbLoad(); } catch (final RemoteException e) { throw new EJBException(e); } finally { ThreadContext.exit(oldCallContext); } // if we call load we must call store try { //noinspection unchecked Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE); if (registeredEntities == null) { registeredEntities = new LinkedHashSet<EntityBean>(); synchronizationRegistry.putResource(ENTITIES_TO_STORE, registeredEntities); synchronizationRegistry.registerInterposedSynchronization( new Synchronization() { @Override public void beforeCompletion() { //noinspection unchecked final Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE); if (registeredEntities == null) { return; } for (final EntityBean entityBean : registeredEntities) { ejbStore(entityBean); } } @Override public void afterCompletion(final int i) {} }); } registeredEntities.add(entityBean); } catch (final Exception e) { // no-op } }
private void ejbPassivate(final EntityBean entityBean) { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } final ThreadContext callContext = createThreadContext(entityBean); callContext.setCurrentOperation(Operation.PASSIVATE); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { entityBean.ejbPassivate(); } catch (final RemoteException e) { throw new EJBException(e); } finally { ThreadContext.exit(oldCallContext); } }
private void unsetEntityContext(final EntityBean entityBean) { if (entityBean == null) { throw new NullPointerException("entityBean is null"); } final ThreadContext callContext = createThreadContext(entityBean); callContext.setCurrentOperation(Operation.UNSET_CONTEXT); final ThreadContext oldCallContext = ThreadContext.enter(callContext); try { entityBean.unsetEntityContext(); } catch (final RemoteException e) { throw new EJBException(e); } finally { ThreadContext.exit(oldCallContext); } }
public Object select( final BeanContext beanContext, final String methodSignature, final String returnType, final Object... args) throws FinderException { final String signature = beanContext.getAbstractSchemaName() + "." + methodSignature; try { // execute the select query final Collection<Object> results = cmpEngine.queryBeans(beanContext, signature, args); // // process the results // // If we need to return a set... final Collection<Object> proxies; if (returnType.equals("java.util.Set")) { // we collect values into a LinkedHashSet to preserve ordering proxies = new LinkedHashSet<Object>(); } else { // otherwise use a simple array list proxies = new ArrayList<Object>(); } final boolean isSingleValued = !returnType.equals("java.util.Collection") && !returnType.equals("java.util.Set"); ProxyFactory proxyFactory = null; for (Object value : results) { // if this is a single valued query and we already have results, throw FinderException if (isSingleValued && !proxies.isEmpty()) { throw new FinderException( "The single valued query " + methodSignature + "returned more than one item"); } // if we have an EntityBean, we need to proxy it if (value instanceof EntityBean) { final EntityBean entityBean = (EntityBean) value; if (proxyFactory == null) { final BeanContext result = getBeanContextByClass(entityBean.getClass()); if (result != null) { proxyFactory = new ProxyFactory(result); } } if (proxyFactory != null) { if (beanContext.isRemoteQueryResults(methodSignature)) { value = proxyFactory.createRemoteProxy(entityBean, this); } else { value = proxyFactory.createLocalProxy(entityBean, this); } } } proxies.add(value); } // if not single valued, return the set if (!isSingleValued) { return proxies; } // single valued query that returned no rows, is an exception if (proxies.isEmpty()) { throw new ObjectNotFoundException(); } // return the single item.... multiple return values was handled in for loop above return proxies.iterator().next(); } catch (final RuntimeException e) { throw new EJBException(e); } }
private Object homeMethod( final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException { final BeanContext beanContext = callContext.getBeanContext(); final TransactionPolicy txPolicy = createTransactionPolicy( beanContext.getTransactionType(callMethod, interfaceType), callContext); final EntityBean bean; Object returnValue = null; try { /* Obtain a bean instance from the method ready pool */ bean = createNewInstance(callContext); // set the entity context setEntityContext(bean); try { callContext.setCurrentOperation(Operation.HOME); final Method runMethod = beanContext.getMatchingBeanMethod(callMethod); try { returnValue = runMethod.invoke(bean, args); } catch (final IllegalArgumentException e) { System.out.println("********************************************************"); System.out.println("callMethod = " + callMethod); System.out.println("runMethod = " + runMethod); System.out.println("bean = " + bean.getClass().getName()); throw e; } } finally { unsetEntityContext(bean); } } catch (Throwable e) { if (e instanceof InvocationTargetException) { e = ((InvocationTargetException) e).getTargetException(); } final ExceptionType type = callContext.getBeanContext().getExceptionType(e); if (type == ExceptionType.SYSTEM) { /* System Exception ****************************/ handleSystemException(txPolicy, e, callContext); } else { /* Application Exception ***********************/ handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK); } } finally { afterInvoke(txPolicy, callContext); } return returnValue; }