下面列出了怎么用javax.ejb.EntityBean的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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);
}
}
}
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();
}
}
}
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);
}
}
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);
}
}
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;
}
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);
}
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;
}
}
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;
}
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 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);
}
}
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);
}
}
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);
}
}
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 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");
}
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);
}
}
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();
}
}
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;
}
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);
}
}
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;
}
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;
}
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 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);
}
public void beforeStore(final LifecycleEvent lifecycleEvent) {
eventOccurred(lifecycleEvent);
final EntityBean bean = (EntityBean) lifecycleEvent.getSource();
if (!creating.get().contains(bean)) {
cmpCallback.ejbStore(bean);
}
}
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);
}
}
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);
}
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;
}
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);
}
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);
}
}
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);
}