KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > interceptoraccess > BeanInterceptorAccess01


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BeanInterceptorAccess01.java 810 2006-07-03 14:31:08Z studzine $
23  * --------------------------------------------------------------------------
24  */

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 JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
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 JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
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 /**
56  * Used to test the interceptors access to the following resources.<br> <li>SessionContext
57  * Methods</li> <li>JNDI Access to java:comp/env</li> <li>Resource Manager</li>
58  * <li>Enterprise bean</li> <li>EntityManagerFactory</li> <li>EntityManage</li>
59  * <li>TimerService</li> <li>UserTransaction</li>
60  * @author Eduardo Studzinski Estima de Castro
61  * @author Gisele Pinheiro Souza
62  */

63 public class BeanInterceptorAccess01 implements ItfAccessJNDI, ItfAccessEJB, ItfAccessResourceManager,
64         ItfAccessEntityManager, ItfAccessEMFactory, ItfAccessSessionContext{
65
66     /**
67      * Bean.
68      */

69     @EJB JavaDoc(name="ejb/bean", beanName="EJBInjectionBean")
70     private ItfOneMethod01 bean;
71
72     /**
73      * Injected DataSource.
74      */

75     @Resource(name = "jdbc/jdbc_1", mappedName="jdbc_1")
76     private DataSource JavaDoc ds;
77
78     /**
79      * Entity Manager.
80      */

81     @PersistenceContext(unitName="testEntity00")
82     private EntityManager eManager = null;
83
84     /**
85      * Entity Factory.
86      */

87     @PersistenceUnit(unitName="testEntity00")
88     private EntityManagerFactory entityFactory = null;
89
90     /**
91      * SessionContext.
92      */

93     @Resource
94     private SessionContext JavaDoc ctx;
95
96     /**
97      * This has an interceptor that accesses the java:comp/env via JNDI.
98      * @param obj it's not used.
99      * @return null
100      * @throws Exception if a problem occurs.
101      */

102     public Object JavaDoc accessJNDI(final Object JavaDoc obj) throws Exception JavaDoc {
103         return null;
104     }
105
106     /**
107      * This has an interceptor that accesses an EJB.
108      * @param obj it's not used.
109      * @return null
110      * @throws Exception if a problem occurs.
111      */

112     public Object JavaDoc accessEJB(final Object JavaDoc obj) throws Exception JavaDoc {
113         return null;
114     }
115
116     /**
117      * This has an interceptor that accesses a resource.
118      * @param obj it's not used.
119      * @return null
120      * @throws Exception if a problem occurs.
121      */

122     public Object JavaDoc accessResManager(final Object JavaDoc obj) throws Exception JavaDoc {
123         return null;
124     }
125
126     /**
127      * This has an interceptor that accesses an entity manager.
128      * @param obj it's not used.
129      * @return null
130      * @throws Exception if a problem occurs.
131      */

132     public Object JavaDoc accessEntityManager(final Object JavaDoc obj) throws Exception JavaDoc {
133         return null;
134     }
135
136     /**
137      * This has an interceptor that accesses an entity manager.
138      * @param obj it's not used.
139      * @return null
140      * @throws Exception if a problem occurs.
141      */

142     public Object JavaDoc accessEntityManagerFactory(final Object JavaDoc obj) throws Exception JavaDoc {
143         return null;
144     }
145
146     /**
147      * This has an interceptor that accesses the sessionContext and try to lookup a datasource.
148      * @param obj it's not used.
149      * @return null
150      * @throws Exception if a problem occurs.
151      */

152     public Object JavaDoc accessSessionContext(final Object JavaDoc obj) throws Exception JavaDoc {
153         return null;
154     }
155
156     /**
157      * Business Method Inteceptor that accesses the resource specified by the class comments.
158      * @param ic contains attributes of invocation..
159      * @return method's invocation result
160      * @throws Exception if invocation fails
161      */

162     @AroundInvoke
163     public Object JavaDoc intercept(final InvocationContext ic) throws Exception JavaDoc{
164         if (ic.getMethod().toString().contains("accessJNDI")) {
165
166             //JNDI Access
167
DataSource JavaDoc dsViaJNDI = (DataSource JavaDoc) 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             //EJB Access
174
assertTrue(bean.getBool());
175
176         } else if (ic.getMethod().toString().contains("accessResManager")) {
177
178             //ResourceManager Access
179
TableManager cTest = new TableManager(ds);
180             cTest.test("tmpTable" + cTest.hashCode());
181
182         } else if (ic.getMethod().toString().contains("accessEntityManager")) {
183
184             //EntityManager Access
185
UserTransaction JavaDoc utx;
186             try{
187                 utx = ctx.getUserTransaction();
188                 utx.begin();
189             }catch(Exception JavaDoc 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             //EntityManagerFactory Access
203
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             //SessionContext Access
211
if (ctx == null){
212                 throw new Exception JavaDoc("SessionContext is null.");
213             }
214
215         } else{
216             throw new Exception JavaDoc("Invalid method to intercept.");
217         }
218         return ic.proceed();
219     }
220 }
221
Popular Tags