1 25 package org.objectweb.easybeans.tests.common.ejbs.base.interceptoraccess; 26 27 import static org.objectweb.easybeans.tests.common.resources.EntityManagerTester.checkInstance; 28 import static org.testng.Assert.assertTrue; 29 30 import javax.annotation.Resource; 31 import javax.ejb.EJB ; 32 import javax.ejb.SessionContext ; 33 import javax.interceptor.AroundInvoke; 34 import javax.interceptor.InvocationContext; 35 import javax.persistence.EntityManager; 36 import javax.persistence.EntityManagerFactory; 37 import javax.persistence.PersistenceContext; 38 import javax.persistence.PersistenceUnit; 39 import javax.sql.DataSource ; 40 import javax.transaction.UserTransaction ; 41 42 import org.objectweb.easybeans.tests.common.db.TableManager; 43 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessEJB; 44 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessEMFactory; 45 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessEntityManager; 46 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessJNDI; 47 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessResourceManager; 48 import org.objectweb.easybeans.tests.common.ejbs.base.ItfAccessSessionContext; 49 import org.objectweb.easybeans.tests.common.ejbs.base.ItfOneMethod01; 50 import org.objectweb.easybeans.tests.common.helper.JNDIHelper; 51 import org.objectweb.easybeans.tests.common.resources.EMFactoryTester; 52 import org.objectweb.easybeans.tests.common.resources.EntityManagerTester; 53 54 55 63 public class BeanInterceptorAccess01 implements ItfAccessJNDI, ItfAccessEJB, ItfAccessResourceManager, 64 ItfAccessEntityManager, ItfAccessEMFactory, ItfAccessSessionContext{ 65 66 69 @EJB (name="ejb/bean", beanName="EJBInjectionBean") 70 private ItfOneMethod01 bean; 71 72 75 @Resource(name = "jdbc/jdbc_1", mappedName="jdbc_1") 76 private DataSource ds; 77 78 81 @PersistenceContext(unitName="testEntity00") 82 private EntityManager eManager = null; 83 84 87 @PersistenceUnit(unitName="testEntity00") 88 private EntityManagerFactory entityFactory = null; 89 90 93 @Resource 94 private SessionContext ctx; 95 96 102 public Object accessJNDI(final Object obj) throws Exception { 103 return null; 104 } 105 106 112 public Object accessEJB(final Object obj) throws Exception { 113 return null; 114 } 115 116 122 public Object accessResManager(final Object obj) throws Exception { 123 return null; 124 } 125 126 132 public Object accessEntityManager(final Object obj) throws Exception { 133 return null; 134 } 135 136 142 public Object accessEntityManagerFactory(final Object obj) throws Exception { 143 return null; 144 } 145 146 152 public Object accessSessionContext(final Object obj) throws Exception { 153 return null; 154 } 155 156 162 @AroundInvoke 163 public Object intercept(final InvocationContext ic) throws Exception { 164 if (ic.getMethod().toString().contains("accessJNDI")) { 165 166 DataSource dsViaJNDI = (DataSource ) JNDIHelper.getJavaCompEnvResource("jdbc/jdbc_1"); 168 TableManager cTest = new TableManager(dsViaJNDI); 169 cTest.test("tmpTable" + cTest.hashCode()); 170 171 } else if (ic.getMethod().toString().contains("accessEJB")) { 172 173 assertTrue(bean.getBool()); 175 176 } else if (ic.getMethod().toString().contains("accessResManager")) { 177 178 TableManager cTest = new TableManager(ds); 180 cTest.test("tmpTable" + cTest.hashCode()); 181 182 } else if (ic.getMethod().toString().contains("accessEntityManager")) { 183 184 UserTransaction utx; 186 try{ 187 utx = ctx.getUserTransaction(); 188 utx.begin(); 189 }catch(Exception e){ 190 utx = null; 191 } 192 193 checkInstance(eManager, EntityManagerTester.NAME); 194 195 if (utx != null){ 196 utx.commit(); 197 } 198 199 200 } else if (ic.getMethod().toString().contains("accessEntityManagerFactory")) { 201 202 EntityManager entityManager = entityFactory.createEntityManager(); 204 entityManager.getTransaction().begin(); 205 checkInstance(entityManager, EMFactoryTester.NAME); 206 entityManager.getTransaction().commit(); 207 208 } else if (ic.getMethod().toString().contains("accessSessionContext")) { 209 210 if (ctx == null){ 212 throw new Exception ("SessionContext is null."); 213 } 214 215 } else{ 216 throw new Exception ("Invalid method to intercept."); 217 } 218 return ic.proceed(); 219 } 220 } 221 | Popular Tags |