Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.core.cmp.jpa;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EJBLocalObject;
import javax.ejb.EJBObject;
import javax.ejb.EntityBean;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.apache.openejb.BeanContext;
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.core.ThreadContext;
import org.apache.openejb.core.cmp.CmpCallback;
import org.apache.openejb.core.cmp.CmpEngine;
import org.apache.openejb.core.cmp.ComplexKeyGenerator;
import org.apache.openejb.core.cmp.KeyGenerator;
import org.apache.openejb.core.cmp.SimpleKeyGenerator;
import org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator;
import org.apache.openejb.core.cmp.cmp2.Cmp2Util;
import org.apache.openejb.core.transaction.TransactionType;
import org.apache.openejb.core.transaction.TransactionPolicy;
import static org.apache.openejb.core.transaction.EjbTransactionUtil.afterInvoke;
import static org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy;
import org.apache.openjpa.event.AbstractLifecycleListener;
import org.apache.openjpa.event.LifecycleEvent;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
public class JpaCmpEngine implements CmpEngine {
private static final Object[] NO_ARGS = new Object[0];
public static final String CMP_PERSISTENCE_CONTEXT_REF_NAME = "comp/env/openejb/cmp";
/**
* Used to notify call CMP callback methods.
*/
private final CmpCallback cmpCallback;
/**
* Thread local to track the beans we are creating to avoid an extra ejbStore callback
*/
private final ThreadLocal> creating = new ThreadLocal>() {
protected Set initialValue() {
return new HashSet();
}
};
/**
* Listener added to entity managers.
*/
protected Object entityManagerListener;
public JpaCmpEngine(CmpCallback cmpCallback) {
this.cmpCallback = cmpCallback;
}
public synchronized void deploy(BeanContext beanContext) throws OpenEJBException {
configureKeyGenerator(beanContext);
}
public synchronized void undeploy(BeanContext beanContext) throws OpenEJBException {
beanContext.setKeyGenerator(null);
}
private EntityManager getEntityManager(BeanContext beanContext) {
EntityManager entityManager = null;
try {
entityManager = (EntityManager) beanContext.getJndiEnc().lookup(CMP_PERSISTENCE_CONTEXT_REF_NAME);
} catch (NamingException ignored) {
//TODO see OPENEJB-1259 temporary hack until geronimo jndi integration works better
try {
entityManager = (EntityManager) new InitialContext().lookup("java:" + CMP_PERSISTENCE_CONTEXT_REF_NAME);
} catch (NamingException ignored2) {
//ignore
}
}
if (entityManager == null) {
throw new EJBException("Entity manager not found at \"openejb/cmp\" in jndi ejb " + beanContext.getDeploymentID());
}
registerListener(entityManager);
return entityManager;
}
private synchronized void registerListener(EntityManager entityManager) {
if (entityManager instanceof OpenJPAEntityManagerSPI) {
OpenJPAEntityManagerSPI openjpaEM = (OpenJPAEntityManagerSPI) entityManager;
OpenJPAEntityManagerFactorySPI openjpaEMF = (OpenJPAEntityManagerFactorySPI) openjpaEM.getEntityManagerFactory();
if (entityManagerListener == null) {
entityManagerListener = new OpenJPALifecycleListener();
}
openjpaEMF.addLifecycleListener(entityManagerListener, (Class[])null);
return;
}
Object delegate = entityManager.getDelegate();
if (delegate != entityManager && delegate instanceof EntityManager) {
registerListener((EntityManager) delegate);
}
}
public Object createBean(EntityBean bean, ThreadContext callContext) throws CreateException {
// TODO verify that extract primary key requires a flush followed by a merge
TransactionPolicy txPolicy = startTransaction("persist", callContext);
creating.get().add(bean);
try {
BeanContext beanContext = callContext.getBeanContext();
EntityManager entityManager = getEntityManager(beanContext);
entityManager.persist(bean);
entityManager.flush();
bean = entityManager.merge(bean);
// extract the primary key from the bean
KeyGenerator kg = beanContext.getKeyGenerator();
Object primaryKey = kg.getPrimaryKey(bean);
return primaryKey;
} finally {
creating.get().remove(bean);
commitTransaction("persist", callContext, txPolicy);
}
}
public Object loadBean(ThreadContext callContext, Object primaryKey) {
TransactionPolicy txPolicy = startTransaction("load", callContext);
try {
BeanContext beanContext = callContext.getBeanContext();
Class beanClass = beanContext.getCmpImplClass();
// Try to load it from the entity manager
EntityManager entityManager = getEntityManager(beanContext);
return entityManager.find(beanClass, primaryKey);
} finally {
commitTransaction("load", callContext, txPolicy);
}
}
public void storeBeanIfNoTx(ThreadContext callContext, Object bean) {
TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
return;
}
TransactionPolicy txPolicy = startTransaction("store", callContext);
try {
// only store if we started a new transaction
if (txPolicy.isNewTransaction()) {
EntityManager entityManager = getEntityManager(callContext.getBeanContext());
entityManager.merge(bean);
}
} finally {
commitTransaction("store", callContext, txPolicy);
}
}
public void removeBean(ThreadContext callContext) {
TransactionPolicy txPolicy = startTransaction("remove", callContext);
try {
BeanContext deploymentInfo = callContext.getBeanContext();
Class beanClass = deploymentInfo.getCmpImplClass();
EntityManager entityManager = getEntityManager(deploymentInfo);
Object primaryKey = callContext.getPrimaryKey();
// Try to load it from the entity manager
Object bean = entityManager.find(beanClass, primaryKey);
// remove the bean
entityManager.remove(bean);
} finally {
commitTransaction("remove", callContext, txPolicy);
}
}
public List