javax.ejb.EntityBean Java Examples
The following examples show how to use
javax.ejb.EntityBean.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EntityContainer.java From tomee with Apache License 2.0 | 6 votes |
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(); } } }
Example #2
Source File: JpaCmpEngine.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #3
Source File: CmrSet.java From tomee with Apache License 2.0 | 6 votes |
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; } }
Example #4
Source File: CmrSet.java From tomee with Apache License 2.0 | 6 votes |
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; }
Example #5
Source File: SetValuedCmr.java From tomee with Apache License 2.0 | 6 votes |
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); }
Example #6
Source File: EntityContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #7
Source File: Cmp2Util.java From tomee with Apache License 2.0 | 6 votes |
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; }
Example #8
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #9
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #10
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #11
Source File: EntityInstanceManager.java From tomee with Apache License 2.0 | 6 votes |
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); } } }
Example #12
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #13
Source File: EntityInstanceManager.java From tomee with Apache License 2.0 | 6 votes |
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; }
Example #14
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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); } }
Example #15
Source File: CmpContainer.java From tomee with Apache License 2.0 | 6 votes |
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"); }
Example #16
Source File: ProxyFactory.java From tomee with Apache License 2.0 | 5 votes |
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; }
Example #17
Source File: EntityContainer.java From tomee with Apache License 2.0 | 5 votes |
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; }
Example #18
Source File: JpaCmpEngine.java From tomee with Apache License 2.0 | 5 votes |
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); } }
Example #19
Source File: EntityInstanceManager.java From tomee with Apache License 2.0 | 5 votes |
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(); } }
Example #20
Source File: EntityContainer.java From tomee with Apache License 2.0 | 5 votes |
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); } }
Example #21
Source File: ProxyFactory.java From tomee with Apache License 2.0 | 5 votes |
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; }
Example #22
Source File: EntityInstanceManager.java From tomee with Apache License 2.0 | 5 votes |
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); } }
Example #23
Source File: CheckNoCreateMethodsTest.java From tomee with Apache License 2.0 | 5 votes |
@Keys({@Key(value = "no.home.create", count = 4), @Key(value = "unused.ejb.create", count = 2, type = KeyType.WARNING), @Key(value = "unused.ejbPostCreate", type = KeyType.WARNING), @Key("entity.no.ejb.create"), @Key(value = "session.no.ejb.create", count = 2)}) public EjbJar noCreateMethod() throws OpenEJBException { System.setProperty("openejb.validation.output.level", "VERBOSE"); final EjbJar ejbJar = new EjbJar(); final StatelessBean stateless = new StatelessBean(FooStateless.class); stateless.setHomeAndRemote(FooStatelessHome.class, FooStatelessRemote.class); stateless.setHomeAndLocal(FooStatelessLocalHome.class, FooStatelessLocal.class); ejbJar.addEnterpriseBean(stateless); final StatefulBean stateful = new StatefulBean(FooStateful.class); stateful.setHomeAndRemote(FooStatefulHome.class, FooStatefulRemote.class); stateful.setHomeAndLocal(FooStatefulLocalHome.class, FooStatefulLocal.class); ejbJar.addEnterpriseBean(stateful); final org.apache.openejb.jee.EntityBean bean = new org.apache.openejb.jee.EntityBean(MyEntity.class, PersistenceType.BEAN); bean.setLocalHome(MyLocalHome.class.getName()); bean.setLocal(MyLocal.class.getName()); ejbJar.addEnterpriseBean(bean); final org.apache.openejb.jee.EntityBean bean1 = new org.apache.openejb.jee.EntityBean(YourEntity.class, PersistenceType.BEAN); bean1.setLocalHome(MyLocalHome.class.getName()); bean1.setLocal(MyLocal.class.getName()); ejbJar.addEnterpriseBean(bean1); final StatelessBean bar = new StatelessBean(BarStateless.class); bar.setHomeAndRemote(BarStatelessHome.class, BarStatelessRemote.class); ejbJar.addEnterpriseBean(bar); final StatefulBean bazStateful = new StatefulBean(BazStateful.class); ejbJar.addEnterpriseBean(bazStateful); return ejbJar; }
Example #24
Source File: JpaCmpEngine.java From tomee with Apache License 2.0 | 5 votes |
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); }
Example #25
Source File: Cmp2Util.java From tomee with Apache License 2.0 | 5 votes |
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; }
Example #26
Source File: SingleValuedCmr.java From tomee with Apache License 2.0 | 5 votes |
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); }
Example #27
Source File: CmpContainer.java From tomee with Apache License 2.0 | 5 votes |
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); } }
Example #28
Source File: CmpContainer.java From tomee with Apache License 2.0 | 5 votes |
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); }
Example #29
Source File: CmpContainer.java From tomee with Apache License 2.0 | 5 votes |
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); } }
Example #30
Source File: ComplexKeyGenerator.java From tomee with Apache License 2.0 | 5 votes |
public Object getPrimaryKey(final EntityBean bean) { Object pkObject = null; try { pkObject = pkClass.newInstance(); } catch (final Exception e) { throw new EJBException("Unable to create complex primary key instance: " + pkClass.getName(), e); } for (final PkField pkField : fields) { pkField.copyToPkObject(bean, pkObject); } return pkObject; }