类javax.ejb.EntityBean源码实例Demo

下面列出了怎么用javax.ejb.EntityBean的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: tomee   文件: EntityInstanceManager.java
public SynchronizationWrapper(final BeanContext beanContext, final Object primaryKey, final EntityBean bean, final boolean available, final Key readyPoolKey, final TransactionPolicy txPolicy) {
    if (bean == null) {
        throw new IllegalArgumentException("bean is null");
    }
    if (readyPoolKey == null) {
        throw new IllegalArgumentException("key is null");
    }
    if (beanContext == null) {
        throw new IllegalArgumentException("deploymentInfo is null");
    }
    if (primaryKey == null) {
        throw new IllegalArgumentException("primaryKey is null");
    }
    if (txPolicy == null) {
        throw new IllegalArgumentException("txEnv is null");
    }

    this.beanContext = beanContext;
    this.bean = bean;
    this.primaryKey = primaryKey;
    this.available = available;
    this.readyPoolKey = readyPoolKey;
    this.txPolicy = txPolicy;
    associated = true;
}
 
源代码2 项目: tomee   文件: EntityInstanceManager.java
public void beforeCompletion() {
    if (associated) {
        final EntityBean bean;
        synchronized (this) {
            bean = this.bean;
        }

        final ThreadContext callContext = new ThreadContext(beanContext, primaryKey);
        callContext.setCurrentOperation(Operation.STORE);

        final ThreadContext oldCallContext = ThreadContext.enter(callContext);

        try {
            bean.ejbStore();
        } catch (final Exception re) {
            logger.error("Exception occured during ejbStore()", re);
            txPolicy.setRollbackOnly(re);
        } finally {
            ThreadContext.exit(oldCallContext);
        }
    }
}
 
源代码3 项目: tomee   文件: EntityContainer.java
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();
        }
    }
}
 
源代码4 项目: tomee   文件: EntityContainer.java
protected void removeEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    callContext.setCurrentOperation(Operation.REMOVE);

    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);

    EntityBean bean = null;
    try {

        bean = instanceManager.obtainInstance(callContext);

        ejbLoad_If_No_Transaction(callContext, bean);
        bean.ejbRemove();
        didRemove(bean, callContext);
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
}
 
源代码5 项目: tomee   文件: JpaCmpEngine.java
public Object createBean(EntityBean bean, final ThreadContext callContext) throws CreateException {
    // TODO verify that extract primary key requires a flush followed by a merge
    final TransactionPolicy txPolicy = startTransaction("persist", callContext);
    creating.get().add(bean);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final EntityManager entityManager = getEntityManager(beanContext);

        entityManager.persist(bean);
        entityManager.flush();
        bean = entityManager.merge(bean);

        // extract the primary key from the bean
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);

        return primaryKey;
    } finally {
        creating.get().remove(bean);
        commitTransaction("persist", callContext, txPolicy);
    }
}
 
源代码6 项目: tomee   文件: Cmp2Util.java
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;
}
 
源代码7 项目: tomee   文件: SetValuedCmr.java
public SetValuedCmr(final EntityBean source, final String sourceProperty, final Class<Bean> relatedType, final String relatedProperty) {
    if (source == null) {
        throw new NullPointerException("source is null");
    }
    if (relatedType == null) {
        throw new NullPointerException("relatedType is null");
    }

    this.source = source;
    this.sourceProperty = sourceProperty;
    this.relatedProperty = relatedProperty;

    this.relatedInfo = Cmp2Util.getBeanContext(relatedType);

    transactionRegistry = SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
}
 
源代码8 项目: tomee   文件: CmrSet.java
public CmrSet(final EntityBean source, final String sourceProperty, final BeanContext relatedInfo, final String relatedProperty, final Collection<Bean> relatedBeans) {
    this.source = source;
    this.sourceProperty = sourceProperty;
    this.relatedInfo = relatedInfo;
    this.relatedProperty = relatedProperty;
    this.relatedBeans = relatedBeans;

    relatedLocal = relatedInfo.getLocalInterface();
    final TransactionSynchronizationRegistry transactionRegistry = SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
    try {
        transactionRegistry.registerInterposedSynchronization(new Synchronization() {
            public void beforeCompletion() {
            }

            public void afterCompletion(final int i) {
                mutable = false;
            }
        });
    } catch (final IllegalStateException ignored) {
        // no tx so not mutable
        mutable = false;
    }
}
 
源代码9 项目: tomee   文件: CmrSet.java
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<>();
    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;
}
 
源代码10 项目: tomee   文件: CmpContainer.java
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);
    }
}
 
源代码11 项目: tomee   文件: CmpContainer.java
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);
    }
}
 
源代码12 项目: tomee   文件: CmpContainer.java
private void ejbStore(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }

    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.STORE);

    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbStore();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
 
源代码13 项目: tomee   文件: CmpContainer.java
private void ejbActivate(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }

    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.ACTIVATE);

    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbActivate();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
 
源代码14 项目: tomee   文件: CmpContainer.java
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);
    }
}
 
源代码15 项目: tomee   文件: CmpContainer.java
private Object findByPrimaryKey(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);

    try {
        final EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]);
        if (bean == null) {
            throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]);
        }

        // rebuild the primary key
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);

        // create a new ProxyInfo based on the deployment info and primary key
        return new ProxyInfo(beanContext, primaryKey);
    } catch (final FinderException fe) {
        handleApplicationException(txPolicy, fe, false);
    } catch (final Throwable e) {// handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    throw new AssertionError("Should not get here");
}
 
源代码16 项目: tomee   文件: EntityInstanceManager.java
public void freeInstance(final ThreadContext callContext, final EntityBean bean) throws SystemException {

        discardInstance(callContext, bean);

        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.UNSET_CONTEXT);

        try {
            /*
            * unsetEntityContext executes in an unspecified transactional context. In this case we choose to
            * allow it to have what every transaction context is current. Better then suspending it
            * unnecessarily.
            *
            * We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
            * logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
            * we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
            * to do.
            */
            bean.unsetEntityContext();
        } catch (final Exception e) {
            /*
            * The EJB 1.1 specification does not specify how exceptions thrown by unsetEntityContext impact the
            * transaction, if there is one.  In this case we choose to do nothing since the instance is being disposed
            * of anyway.
            */

            logger.info(getClass().getName() + ".freeInstance: ignoring exception " + e + " on bean instance " + bean);
        } finally {
            callContext.setCurrentOperation(currentOp);
        }

    }
 
源代码17 项目: tomee   文件: EntityInstanceManager.java
public void discardInstance(final ThreadContext callContext, final EntityBean bean) throws SystemException {
    final Object primaryKey = callContext.getPrimaryKey();
    final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
    if (primaryKey == null || txPolicy == null || !txPolicy.isTransactionActive()) {
        return;
    }

    // The wrapper is removed (if pooled) so that it can not be accessed again. This is
    // especially important in the obtainInstance( ) method where a disassociated wrapper
    // in the txReadyPool is indicative of an entity bean that has been removed via
    // ejbRemove() rather than freed because of an error condition as is the case here.
    final Key key = new Key(callContext.getBeanContext().getDeploymentID(), primaryKey);
    final SynchronizationWrapper wrapper = (SynchronizationWrapper) txPolicy.getResource(key);
    if (wrapper != null) {
        /*
         It's not possible to deregister a wrapper with the transaction,
         but it can be removed from the tx pool and made inoperative by
         calling its disassociate method. The wrapper will be returned to the
         wrapper pool after the transaction completes
         (see SynchronizationWrapper.afterCompletion( ) method).  The wrapper must
         be returned after the transaction completes so that it is not in the service
         of another bean when the TransactionManager calls its Synchronization methods.

         In addition, the bean instance is dereferenced so it can be garbage
         collected.
        */
        wrapper.disassociate();
    }
}
 
源代码18 项目: tomee   文件: EntityContainer.java
protected Object invoke(final InterfaceType type,
                        final Method callMethod,
                        final Method runMethod,
                        final Object[] args,
                        final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);

    EntityBean bean = null;

    Object returnValue = null;
    entrancyTracker.enter(callContext.getBeanContext(), callContext.getPrimaryKey());
    try {
        bean = instanceManager.obtainInstance(callContext);

        ejbLoad_If_No_Transaction(callContext, bean);
        returnValue = runMethod.invoke(bean, args);
        ejbStore_If_No_Transaction(callContext, bean);
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        entrancyTracker.exit(callContext.getBeanContext(), callContext.getPrimaryKey());
        afterInvoke(txPolicy, callContext);
    }

    return returnValue;
}
 
源代码19 项目: tomee   文件: EntityContainer.java
private void handleException(final TransactionPolicy txPolicy, Throwable e, final ThreadContext callContext, final EntityBean bean) throws OpenEJBException {
    final ExceptionType type;
    if (e instanceof InvocationTargetException) {
        e = ((InvocationTargetException) e).getTargetException();
        type = callContext.getBeanContext().getExceptionType(e);
    } else if (e instanceof ApplicationException) {
        e = ((ApplicationException) e).getRootCause();
        type = ExceptionType.APPLICATION;
    } else if (e instanceof SystemException) {
        e = ((SystemException) e).getRootCause();
        type = ExceptionType.SYSTEM;
    } else {
        type = ExceptionType.SYSTEM;
    }

    if (type == ExceptionType.SYSTEM) {
        // System Exception
        if (bean != null) {
            try {
                instanceManager.discardInstance(callContext, bean);
            } catch (final SystemException e1) {
                logger.error("The instance manager encountered an unkown system exception while trying to discard the entity instance with primary key " +
                    callContext.getPrimaryKey());
            }
        }
        handleSystemException(txPolicy, e, callContext);
    } else {
        // Application Exception
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
        handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
    }
}
 
源代码20 项目: tomee   文件: ProxyFactory.java
public Object createRemoteProxy(final EntityBean bean, final RpcContainer container) {
    // The KeyGenerator creates a new primary key and populates its fields with the
    // primary key fields of the bean instance.  Each deployment has its own KeyGenerator.
    final Object primaryKey = keyGenerator.getPrimaryKey(bean);

    // create the proxy
    final Object proxy = remoteHandler.createProxy(primaryKey, beanContext.getRemoteInterface());
    return proxy;
}
 
源代码21 项目: tomee   文件: ProxyFactory.java
public Object createLocalProxy(final EntityBean bean, final RpcContainer container) {
    // The KeyGenerator creates a new primary key and populates its fields with the
    // primary key fields of the bean instance.  Each deployment has its own KeyGenerator.
    final Object primaryKey = keyGenerator.getPrimaryKey(bean);

    // create the proxy
    final Object proxy = localHandler.createProxy(primaryKey, beanContext.getLocalInterface());
    return proxy;

}
 
源代码22 项目: tomee   文件: JpaCmpEngine.java
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;
}
 
源代码23 项目: tomee   文件: JpaCmpEngine.java
public void afterLoad(final LifecycleEvent lifecycleEvent) {
    eventOccurred(lifecycleEvent);
    final Object bean = lifecycleEvent.getSource();
    // This may seem a bit strange to call ejbActivate immedately followed by ejbLoad,
    // but it is completely legal.  Since the ejbActivate method is not allowed to access
    // persistent state of the bean (EJB 3.0fr 8.5.2) there should be no concern that the
    // call back method clears the bean state before ejbLoad is called.
    cmpCallback.setEntityContext((EntityBean) bean);
    cmpCallback.ejbActivate((EntityBean) bean);
    cmpCallback.ejbLoad((EntityBean) bean);
}
 
源代码24 项目: tomee   文件: JpaCmpEngine.java
public void beforeStore(final LifecycleEvent lifecycleEvent) {
    eventOccurred(lifecycleEvent);
    final EntityBean bean = (EntityBean) lifecycleEvent.getSource();
    if (!creating.get().contains(bean)) {
        cmpCallback.ejbStore(bean);
    }
}
 
源代码25 项目: tomee   文件: JpaCmpEngine.java
public void beforeDelete(final LifecycleEvent lifecycleEvent) {
    eventOccurred(lifecycleEvent);
    try {
        final Object bean = lifecycleEvent.getSource();
        cmpCallback.ejbRemove((EntityBean) bean);
    } catch (final RemoveException e) {
        throw new PersistenceException(e);
    }
}
 
源代码26 项目: tomee   文件: JpaCmpEngine.java
public void afterDetach(final LifecycleEvent lifecycleEvent) {
    eventOccurred(lifecycleEvent);
    // todo detach is called after ejbRemove which does not need ejbPassivate
    final Object bean = lifecycleEvent.getSource();
    cmpCallback.ejbPassivate((EntityBean) bean);
    cmpCallback.unsetEntityContext((EntityBean) bean);
}
 
源代码27 项目: tomee   文件: Cmp2Util.java
public static Object getPrimaryKey(final BeanContext beanContext, final EntityBean entity) {
    if (entity == null) {
        return null;
    }

    // build the primary key
    final KeyGenerator kg = beanContext.getKeyGenerator();
    final Object primaryKey = kg.getPrimaryKey(entity);
    return primaryKey;
}
 
源代码28 项目: tomee   文件: SingleValuedCmr.java
public SingleValuedCmr(final EntityBean source, final String sourceProperty, final Class<Bean> relatedType, final String relatedProperty) {
    if (source == null) {
        throw new NullPointerException("source is null");
    }
    if (relatedType == null) {
        throw new NullPointerException("relatedType is null");
    }
    this.source = source;
    this.sourceProperty = sourceProperty;
    this.relatedProperty = relatedProperty;

    this.relatedInfo = Cmp2Util.getBeanContext(relatedType);
}
 
源代码29 项目: tomee   文件: CmpContainer.java
private EntityBean createNewInstance(final ThreadContext callContext) {
    final BeanContext beanContext = callContext.getBeanContext();
    try {
        return (EntityBean) beanContext.getCmpImplClass().newInstance();
    } catch (final Exception e) {
        throw new EJBException("Unable to create new entity bean instance " + beanContext.getCmpImplClass(), e);
    }
}
 
源代码30 项目: tomee   文件: CmpContainer.java
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);
}