KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > helper > ContextHelper


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: ContextHelper.java 1167 2006-10-18 13:44:57Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.helper;
26
27 import static org.objectweb.easybeans.tests.common.resources.EMFactoryTester.checkInstance;
28
29 import java.net.URL JavaDoc;
30
31 import javax.ejb.EJBContext JavaDoc;
32 import javax.ejb.TimerService JavaDoc;
33 import javax.jms.ConnectionFactory JavaDoc;
34 import javax.jms.Queue JavaDoc;
35 import javax.jms.QueueConnectionFactory JavaDoc;
36 import javax.jms.Topic JavaDoc;
37 import javax.jms.TopicConnectionFactory JavaDoc;
38 import javax.mail.Session JavaDoc;
39 import javax.naming.Context JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.persistence.EntityManager;
43 import javax.persistence.EntityManagerFactory;
44 import javax.sql.DataSource JavaDoc;
45 import javax.transaction.UserTransaction JavaDoc;
46
47 import org.objectweb.easybeans.log.JLog;
48 import org.objectweb.easybeans.log.JLogFactory;
49 import org.objectweb.easybeans.tests.common.resources.EJBContextTester;
50 import org.objectweb.easybeans.tests.common.resources.EntityManagerTester;
51 import org.objectweb.easybeans.tests.common.resources.TimerServiceTester;
52 import org.objectweb.easybeans.tests.common.resources.UserTransactionTester;
53
54 /**
55  * Used to do common operations on bean contexts.
56  * @author Eduardo Studzinski Estima de Castro
57  * @author Gisele Pinheiro Souza
58  */

59 public final class ContextHelper {
60
61     /**
62      * Logger.
63      */

64     private static JLog logger = JLogFactory.getLog(ContextHelper.class);
65
66     /**
67      * Error message.
68      */

69     public static final String JavaDoc ERROR_MSG_INJECTION = "The container did not inject the default value specified "
70             + "in the descriptor.";
71
72     /**
73      * Error message.
74      */

75     public static final String JavaDoc ERROR_MSG_NOT_FOUND = "Entry did not find in the environment.";
76
77     /**
78      * Error message.
79      */

80     public static final String JavaDoc ERROR_MSG_SESSION_CONTEXT = "Error in access using the Session Context.";
81
82     /**
83      * Error message.
84      */

85     public static final String JavaDoc ERROR_MSG_JNDI_ACCESS = "Error in access using the JNDI API directly.";
86
87     /**
88      * Error message.
89      */

90     public static final String JavaDoc ERROR_REFERENCE_NULL = "Reference is null.";
91
92     /**
93      * Creates a new instance of ContextHelper.
94      *
95      */

96     private ContextHelper(){
97
98     }
99
100     /**
101      * Checks the following items:<li>if a simple environment entry value specified
102      * in the descriptor was correctly injected;</li> <li>if the session
103      * context can be used to access the simple environment entry;</li> <li>if the
104      * JNDI API can be used directly to access the simple environment entry;</li> If
105      * fails, an IllegalStateException is thrown.
106      * @param <E> Element Type
107      * @param ejbContext ejb context instance
108      * @param entryName entry name in the environment
109      * @param beanValue value obtained in the bean object.
110      * @param expectedValue value expected by the element
111      */

112     @SuppressWarnings JavaDoc("unchecked")
113     public static <E> void checkSimpleEntry(final EJBContext JavaDoc ejbContext, final String JavaDoc entryName,
114             final E beanValue, final E expectedValue) {
115
116         // Injection
117
logger.debug("Checking injection. Name = {0}", entryName);
118         if (!expectedValue.equals(beanValue)) {
119             throw new IllegalStateException JavaDoc(ERROR_MSG_INJECTION + " Name: " + entryName);
120         }
121         logger.debug("Injection is ok. Name = {0}", entryName);
122
123         checkSimpleEntry(ejbContext, entryName, expectedValue);
124     }
125
126     /**
127      * Checks the following items:<li>if the ejb context can be used to
128      * access the environment entry;</li> <li>if the JNDI API can be used
129      * directly to access the environment entry;</li> If fails, an
130      * IllegalStateException is thrown.
131      * @param <E> Element Type
132      * @param ejbContext ejb context instance
133      * @param entryName entry name in the environment
134      * @param beanInterface bean interface.
135      */

136     @SuppressWarnings JavaDoc("unchecked")
137     public static <E> void checkBeanRef(final EJBContext JavaDoc ejbContext, final String JavaDoc entryName,
138             final E beanInterface) {
139
140         logger.debug("Checking ejb reference. Name = {0}", entryName);
141
142         //Session Context
143
E sctxRef = (E) getEntryByEJBContext(ejbContext, entryName);
144         checkBeanRef(sctxRef);
145
146         //JNDI Access
147
E jndiRef = (E) getEntryByJNDI(entryName);
148         checkBeanRef(jndiRef);
149
150         logger.debug("EJB reference is ok. Name = {0}", entryName);
151     }
152
153     /**
154      * Gets an entry using the ejb context.
155      * @param <E> entry type
156      * @param ejbContext reference
157      * @param entryName name in the enviroment
158      * @return reference
159      */

160     @SuppressWarnings JavaDoc("unchecked")
161     private static <E> E getEntryByEJBContext(final EJBContext JavaDoc ejbContext, final String JavaDoc entryName){
162         //Check if the entry exists in the environment
163
logger.debug("Getting reference using the ejb context. Name = {0}", entryName);
164
165         E sctxEntry = (E) ejbContext.lookup(entryName);
166
167         if (sctxEntry == null) {
168             logger.debug("Entry reference is null. Name = {0} ", entryName);
169         }
170
171         logger.debug("Reference was gotten. Name = {0}", entryName);
172
173         return sctxEntry;
174     }
175
176     /**
177      * Gets an entry using the JNDI API.
178      * @param <E> entry type
179      * @param entryName name in the enviroment
180      * @return reference
181      */

182     @SuppressWarnings JavaDoc("unchecked")
183     private static <E> E getEntryByJNDI(final String JavaDoc entryName){
184         //Check if the entry exists in the environment
185
logger.debug("Getting reference using the JNDI API. Name = {0}", entryName);
186
187         E eJNDI = null;
188         try {
189             Context JavaDoc initCtx = new InitialContext JavaDoc();
190             Context JavaDoc myEnv = (Context JavaDoc) initCtx.lookup("java:comp/env");
191
192             eJNDI = (E) myEnv.lookup(entryName);
193
194             if (eJNDI == null) {
195                 logger.debug("Entry reference is null. Name = {0}", entryName);
196             }
197         } catch (NamingException JavaDoc e) {
198             throw new IllegalStateException JavaDoc("The context could not be obtained or entry not found. Name = " + entryName);
199         }
200
201         logger.debug("Reference was gotten. Name = {0}", entryName);
202         return eJNDI;
203     }
204
205     /**
206      * Checks if a bean reference is not null.
207      * @param <E> bean type
208      * @param ref reference
209      */

210     private static <E> void checkBeanRef(final E ref){
211         logger.debug("Checking ejb reference.");
212
213         if (ref == null) {
214             throw new IllegalStateException JavaDoc(ERROR_REFERENCE_NULL);
215         }
216
217         logger.debug("Ejb reference is ok.");
218     }
219
220     /**
221      * Checks the following items:<li>if the ejb context can be used to
222      * access the simple environment entry;</li> <li>if the JNDI API can be
223      * used directly to access the simple environment entry;</li> If fails, an
224      * IllegalStateException is thrown.
225      * @param <E> Element Type
226      * @param ejbContext ejb context instance
227      * @param entryName entry name in the environment.
228      * @param expectedValue value expected by the element.
229      */

230     @SuppressWarnings JavaDoc("unchecked")
231     public static <E> void checkSimpleEntry(final EJBContext JavaDoc ejbContext, final String JavaDoc entryName,
232             final E expectedValue) {
233
234         logger.debug("Checking simple entry. Name = {0}", entryName);
235
236         // Session Context
237
E sctxValue = (E) getEntryByEJBContext(ejbContext, entryName);
238
239         if (!expectedValue.equals(sctxValue)) {
240             throw new IllegalStateException JavaDoc(ERROR_MSG_SESSION_CONTEXT + " Entry: " + entryName);
241         }
242
243         // JNDI Access
244
E eJNDI = (E) getEntryByJNDI(entryName);
245
246         if (!expectedValue.equals(eJNDI)) {
247             throw new IllegalStateException JavaDoc(ERROR_MSG_JNDI_ACCESS);
248         }
249
250         logger.debug("Simple entry is ok. Name = {0}", entryName);
251     }
252
253     /**
254      * Checks the following items:<li>if the resource was correctly injected;</li>
255      * <li>if the ejb context can be used to
256      * access the resource;</li> <li>if the JNDI API can be used
257      * directly to access the resource;</li> If fails, an
258      * IllegalStateException is thrown.
259      * @param <E> Element Type
260      * @param ejbContext session Context instance
261      * @param resource resource object
262      * @param resourceName resource name in the environment
263      */

264     @SuppressWarnings JavaDoc("unchecked")
265     public static <E> void checkResource(final EJBContext JavaDoc ejbContext, final E resource, final String JavaDoc resourceName){
266         //Injection
267
checkResource(resource);
268         //Environment access
269
checkResource(ejbContext, resourceName);
270     }
271
272     /**
273      * Checks the following items:<li>if the ejb context can be used to
274      * access the entry;</li> <li>if the JNDI API can be used
275      * directly to access the entry;</li> If fails, an
276      * IllegalStateException is thrown.
277      * @param <E> Element Type
278      * @param ejbContext ejb context instance
279      * @param entryName entry name in the environment
280      */

281     @SuppressWarnings JavaDoc("unchecked")
282     public static <E> void checkResource(final EJBContext JavaDoc ejbContext, final String JavaDoc entryName) {
283
284         logger.debug("Checking resource. Name = {0}", entryName);
285
286         //Session Context
287
E sctxEntry = (E) getEntryByEJBContext(ejbContext, entryName);
288         checkResource(sctxEntry);
289
290         //JNDI Access
291
E eJNDI = (E) getEntryByJNDI(entryName);
292         checkResource(eJNDI);
293
294         logger.debug("Resource is ok. Name = {0}", entryName);
295     }
296
297     /**
298      * Checks if an entry is working properly.
299      * @param <E> entry type
300      * @param entry reference
301      */

302     public static <E> void checkResource(final E entry) {
303         if (entry == null) {
304             throw new IllegalStateException JavaDoc(ERROR_REFERENCE_NULL);
305         }
306
307         Class JavaDoc entryClass = entry.getClass();
308         String JavaDoc entryClassName = entryClass.getName();
309
310         try {
311             if (DataSource JavaDoc.class.isAssignableFrom(entryClass)) {
312                 // Checks DataSource
313
logger.debug("Checking DataSource.");
314                 ((DataSource JavaDoc) entry).getConnection().close();
315
316             } else if (Topic JavaDoc.class.isAssignableFrom(entryClass)) {
317                 // Checks Topic
318
logger.debug("Checking Topic.");
319                 ((Topic JavaDoc) entry).getTopicName();
320
321             }else if (Queue JavaDoc.class.isAssignableFrom(entryClass)) {
322                 // Checks Queue
323
logger.debug("Checking Queue.");
324                 ((Queue JavaDoc) entry).getQueueName();
325
326             }else if (ConnectionFactory JavaDoc.class.isAssignableFrom(entryClass)) {
327                 // Checks ConnectionFactory
328
logger.debug("Checking ConnectionFactory.");
329                 ((ConnectionFactory JavaDoc) entry).createConnection().close();
330
331             } else if (QueueConnectionFactory JavaDoc.class.isAssignableFrom(entryClass)) {
332                 // Checks QueueConnectionFactory
333
logger.debug("Checking QueueConnectionFactory.");
334                 ((QueueConnectionFactory JavaDoc) entry).createConnection().close();
335
336             } else if (TopicConnectionFactory JavaDoc.class.isAssignableFrom(entryClass)) {
337                 // Checks TopicConnectionFactory
338
logger.debug("Checking TopicConnectionFactory.");
339                 ((TopicConnectionFactory JavaDoc) entry).createConnection().close();
340
341             } else if (Session JavaDoc.class.isAssignableFrom(entryClass)) {
342                 // Checks Mail Session
343
logger.debug("Checking Mail Session.");
344                 ((Session JavaDoc) entry).getProperties().keySet();
345
346             } else if (URL JavaDoc.class.isAssignableFrom(entryClass)) {
347                 // Checks TopicConnectionFactory
348
logger.debug("Checking URL.");
349                 ((URL JavaDoc) entry).getHost().toString();
350
351             } else if (UserTransaction JavaDoc.class.isAssignableFrom(entryClass)) {
352                 // Checks UserTransaction
353
logger.debug("Checking UserTransaction.");
354                 UserTransactionTester.checkInstance(((UserTransaction JavaDoc) entry));
355
356             }else if (TimerService JavaDoc.class.isAssignableFrom(entryClass)) {
357                 // Checks TimerService
358
logger.debug("Checking TimerService.");
359                 TimerServiceTester.checkInstance(((TimerService JavaDoc) entry));
360
361             }else if (EJBContext JavaDoc.class.isAssignableFrom(entryClass)) {
362                 // Checks EJBContext
363
logger.debug("Checking EJBContext.");
364                 EJBContextTester.checkInstance(((EJBContext JavaDoc) entry));
365
366             }else if (EJBContext JavaDoc.class.isAssignableFrom(entryClass)) {
367                 // Checks EJBContext
368
logger.debug("Checking EJBContext.");
369                 EJBContextTester.checkInstance(((EJBContext JavaDoc) entry));
370
371             }else{
372                 logger.debug("Unknow resource. Type = {0}", entryClassName);
373                 throw new IllegalStateException JavaDoc("Unknow resource. Type = " + entryClassName);
374             }
375         } catch (Exception JavaDoc e) {
376             logger.debug("Exception: {0}", e);
377             throw new IllegalStateException JavaDoc("Exception: " + e.toString());
378         }
379     }
380
381     /**
382      * Checks the following items: <li>if the injected reference is ok;</li><li>if the ejb context can be used to
383      * access the persitence unit entry;</li> <li>if the JNDI API can be used
384      * directly to access the persistence unit entry;</li> If fails, an
385      * IllegalStateException is thrown.
386      * @param ref reference
387      * @param ejbContext ejb context instance
388      * @param pUnitName persistence unit name
389      */

390     public static void checkEntityManagerFactory(final EJBContext JavaDoc ejbContext, final EntityManagerFactory ref,
391             final String JavaDoc pUnitName){
392         logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName);
393
394         //Check injection
395
checkEntityManagerFactory(ref);
396
397         //Environment
398
checkEntityManagerFactory(ejbContext, pUnitName);
399
400         logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName);
401     }
402
403     /**
404      * Checks the following items:<li>if the ejb context can be used to
405      * access the persitence unit entry;</li> <li>if the JNDI API can be used
406      * directly to access the persistence unit entry;</li> If fails, an
407      * IllegalStateException is thrown.
408      * @param ejbContext ejb context instance
409      * @param pUnitName persistence unit name
410      */

411     public static void checkEntityManagerFactory(final EJBContext JavaDoc ejbContext, final String JavaDoc pUnitName){
412         logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName);
413
414         // Session Context
415
EntityManagerFactory sctxEntry = getEntryByEJBContext(ejbContext, pUnitName);
416         checkEntityManagerFactory(sctxEntry);
417
418         // JNDI Access
419
EntityManagerFactory eJNDI = getEntryByJNDI(pUnitName);
420         checkEntityManagerFactory(eJNDI);
421
422         logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName);
423     }
424
425     /**
426      * Checks if an entity manager factory reference is working properly.
427      * @param ref reference
428      */

429     public static void checkEntityManagerFactory(final EntityManagerFactory ref){
430         try{
431             checkInstance(ref, "cemf");
432         }catch(Exception JavaDoc e){
433             throw new IllegalStateException JavaDoc("Error checking Entity Manager Factory reference.");
434         }
435     }
436
437     /**
438      * Checks the following items: <li>if the injected reference is ok;</li><li>if the ejb context can be used to
439      * access the persitence unit entry;</li> <li>if the JNDI API can be used
440      * directly to access the persistence context entry;</li> If fails, an
441      * IllegalStateException is thrown.
442      * @param ref reference
443      * @param ejbContext ejb context instance
444      * @param pUnitName persistence unit name
445      */

446     public static void checkEntityManager(final EJBContext JavaDoc ejbContext, final EntityManager ref,
447             final String JavaDoc pUnitName){
448         logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName);
449
450         //Check injection
451
checkEntityManager(ref);
452
453         //Environment
454
checkEntityManager(ejbContext, pUnitName);
455
456         logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName);
457     }
458
459     /**
460      * Checks the following items:<li>if the ejb context can be used to
461      * access the persitence unit entry;</li> <li>if the JNDI API can be used
462      * directly to access the persistence context entry;</li> If fails, an
463      * IllegalStateException is thrown.
464      * @param ejbContext ejb context instance
465      * @param pUnitName persistence unit name
466      */

467     public static void checkEntityManager(final EJBContext JavaDoc ejbContext, final String JavaDoc pUnitName){
468         logger.debug("Checking Entity Manager. Name = {0}", pUnitName);
469
470         // Session Context
471
EntityManager sctxEntry = getEntryByEJBContext(ejbContext, pUnitName);
472         checkEntityManager(sctxEntry);
473
474         // JNDI Access
475
EntityManager eJNDI = getEntryByJNDI(pUnitName);
476         checkEntityManager(eJNDI);
477
478         logger.debug("Entity Manager is ok. Name = {0}", pUnitName);
479     }
480
481     /**
482      * Checks if an entity manager reference is working properly.
483      * @param ref reference
484      */

485     public static void checkEntityManager(final EntityManager ref){
486         try{
487             EntityManagerTester.checkInstance(ref, "cem");
488         }catch(Exception JavaDoc e){
489             throw new IllegalStateException JavaDoc("Error checking Entity Manager Factory reference.");
490         }
491     }
492
493 }
494
Popular Tags