Source Code Cross Referenced for ContextHelper.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » tests » common » helper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » ow2 easybeans » org.ow2.easybeans.tests.common.helper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: ContextHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.tests.common.helper;
025:
026:        import static org.ow2.easybeans.tests.common.resources.EMFactoryTester.checkInstance;
027:
028:        import java.net.URL;
029:
030:        import javax.ejb.EJBContext;
031:        import javax.ejb.TimerService;
032:        import javax.jms.ConnectionFactory;
033:        import javax.jms.Queue;
034:        import javax.jms.QueueConnectionFactory;
035:        import javax.jms.Topic;
036:        import javax.jms.TopicConnectionFactory;
037:        import javax.mail.Session;
038:        import javax.naming.Context;
039:        import javax.naming.InitialContext;
040:        import javax.naming.NamingException;
041:        import javax.persistence.EntityManager;
042:        import javax.persistence.EntityManagerFactory;
043:        import javax.sql.DataSource;
044:        import javax.transaction.UserTransaction;
045:
046:        import org.ow2.easybeans.tests.common.resources.EJBContextTester;
047:        import org.ow2.easybeans.tests.common.resources.EntityManagerTester;
048:        import org.ow2.easybeans.tests.common.resources.TimerServiceTester;
049:        import org.ow2.easybeans.tests.common.resources.UserTransactionTester;
050:        import org.ow2.util.log.Log;
051:        import org.ow2.util.log.LogFactory;
052:
053:        /**
054:         * Used to do common operations on bean contexts.
055:         * @author Eduardo Studzinski Estima de Castro
056:         * @author Gisele Pinheiro Souza
057:         */
058:        public final class ContextHelper {
059:
060:            /**
061:             * Logger.
062:             */
063:            private static Log logger = LogFactory.getLog(ContextHelper.class);
064:
065:            /**
066:             * Error message.
067:             */
068:            public static final String ERROR_MSG_INJECTION = "The container did not inject the default value specified "
069:                    + "in the descriptor.";
070:
071:            /**
072:             * Error message.
073:             */
074:            public static final String ERROR_MSG_NOT_FOUND = "Entry did not find in the environment.";
075:
076:            /**
077:             * Error message.
078:             */
079:            public static final String ERROR_MSG_SESSION_CONTEXT = "Error in access using the Session Context.";
080:
081:            /**
082:             * Error message.
083:             */
084:            public static final String ERROR_MSG_JNDI_ACCESS = "Error in access using the JNDI API directly.";
085:
086:            /**
087:             * Error message.
088:             */
089:            public static final String ERROR_REFERENCE_NULL = "Reference is null.";
090:
091:            /**
092:             * Creates a new instance of ContextHelper.
093:             *
094:             */
095:            private ContextHelper() {
096:
097:            }
098:
099:            /**
100:             * Checks the following items:<li>if a simple environment entry value specified
101:             * in the descriptor was correctly injected;</li> <li>if the session
102:             * context can be used to access the simple environment entry;</li> <li>if the
103:             * JNDI API can be used directly to access the simple environment entry;</li> If
104:             * fails, an IllegalStateException is thrown.
105:             * @param <E> Element Type
106:             * @param ejbContext ejb context instance
107:             * @param entryName entry name in the environment
108:             * @param beanValue value obtained in the bean object.
109:             * @param expectedValue value expected by the element
110:             */
111:            @SuppressWarnings("unchecked")
112:            public static <E> void checkSimpleEntry(
113:                    final EJBContext ejbContext, final String 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(ERROR_MSG_INJECTION
120:                            + " Name: " + entryName);
121:                }
122:                logger.debug("Injection is ok. Name = {0}", entryName);
123:
124:                checkSimpleEntry(ejbContext, entryName, expectedValue);
125:            }
126:
127:            /**
128:             * Checks the following items:<li>if the ejb context can be used to
129:             * access the environment entry;</li> <li>if the JNDI API can be used
130:             * directly to access the environment entry;</li> If fails, an
131:             * IllegalStateException is thrown.
132:             * @param <E> Element Type
133:             * @param ejbContext ejb context instance
134:             * @param entryName entry name in the environment
135:             * @param beanInterface bean interface.
136:             */
137:            @SuppressWarnings("unchecked")
138:            public static <E> void checkBeanRef(final EJBContext ejbContext,
139:                    final String entryName, final E beanInterface) {
140:
141:                logger.debug("Checking ejb reference. Name = {0}", entryName);
142:
143:                //Session Context
144:                E sctxRef = (E) getEntryByEJBContext(ejbContext, entryName);
145:                checkBeanRef(sctxRef);
146:
147:                //JNDI Access
148:                E jndiRef = (E) getEntryByJNDI(entryName);
149:                checkBeanRef(jndiRef);
150:
151:                logger.debug("EJB reference is ok. Name = {0}", entryName);
152:            }
153:
154:            /**
155:             * Gets an entry using the ejb context.
156:             * @param <E> entry type
157:             * @param ejbContext reference
158:             * @param entryName name in the enviroment
159:             * @return reference
160:             */
161:            @SuppressWarnings("unchecked")
162:            private static <E> E getEntryByEJBContext(
163:                    final EJBContext ejbContext, final String entryName) {
164:                //Check if the entry exists in the environment
165:                logger.debug(
166:                        "Getting reference using the ejb context. Name = {0}",
167:                        entryName);
168:
169:                E sctxEntry = (E) ejbContext.lookup(entryName);
170:
171:                if (sctxEntry == null) {
172:                    logger.debug("Entry reference is null. Name = {0} ",
173:                            entryName);
174:                }
175:
176:                logger.debug("Reference was gotten. Name = {0}", entryName);
177:
178:                return sctxEntry;
179:            }
180:
181:            /**
182:             * Gets an entry using the JNDI API.
183:             * @param <E> entry type
184:             * @param entryName name in the enviroment
185:             * @return reference
186:             */
187:            @SuppressWarnings("unchecked")
188:            private static <E> E getEntryByJNDI(final String entryName) {
189:                //Check if the entry exists in the environment
190:                logger.debug(
191:                        "Getting reference using the JNDI API. Name = {0}",
192:                        entryName);
193:
194:                E eJNDI = null;
195:                try {
196:                    Context initCtx = new InitialContext();
197:                    Context myEnv = (Context) initCtx.lookup("java:comp/env");
198:
199:                    eJNDI = (E) myEnv.lookup(entryName);
200:
201:                    if (eJNDI == null) {
202:                        logger.debug("Entry reference is null. Name = {0}",
203:                                entryName);
204:                    }
205:                } catch (NamingException e) {
206:                    throw new IllegalStateException(
207:                            "The context could not be obtained or entry not found. Name = "
208:                                    + entryName);
209:                }
210:
211:                logger.debug("Reference was gotten. Name = {0}", entryName);
212:                return eJNDI;
213:            }
214:
215:            /**
216:             * Checks if a bean reference is not null.
217:             * @param <E> bean type
218:             * @param ref reference
219:             */
220:            private static <E> void checkBeanRef(final E ref) {
221:                logger.debug("Checking ejb reference.");
222:
223:                if (ref == null) {
224:                    throw new IllegalStateException(ERROR_REFERENCE_NULL);
225:                }
226:
227:                logger.debug("Ejb reference is ok.");
228:            }
229:
230:            /**
231:             * Checks the following items:<li>if the ejb context can be used to
232:             * access the simple environment entry;</li> <li>if the JNDI API can be
233:             * used directly to access the simple environment entry;</li> If fails, an
234:             * IllegalStateException is thrown.
235:             * @param <E> Element Type
236:             * @param ejbContext ejb context instance
237:             * @param entryName entry name in the environment.
238:             * @param expectedValue value expected by the element.
239:             */
240:            @SuppressWarnings("unchecked")
241:            public static <E> void checkSimpleEntry(
242:                    final EJBContext ejbContext, final String entryName,
243:                    final E expectedValue) {
244:
245:                logger.debug("Checking simple entry. Name = {0}", entryName);
246:
247:                // Session Context
248:                E sctxValue = (E) getEntryByEJBContext(ejbContext, entryName);
249:
250:                if (!expectedValue.equals(sctxValue)) {
251:                    throw new IllegalStateException(ERROR_MSG_SESSION_CONTEXT
252:                            + " Entry: " + entryName);
253:                }
254:
255:                // JNDI Access
256:                E eJNDI = (E) getEntryByJNDI(entryName);
257:
258:                if (!expectedValue.equals(eJNDI)) {
259:                    throw new IllegalStateException(ERROR_MSG_JNDI_ACCESS);
260:                }
261:
262:                logger.debug("Simple entry is ok. Name = {0}", entryName);
263:            }
264:
265:            /**
266:             * Checks the following items:<li>if the resource was correctly injected;</li>
267:             * <li>if the ejb context can be used to
268:             * access the resource;</li> <li>if the JNDI API can be used
269:             * directly to access the resource;</li> If fails, an
270:             * IllegalStateException is thrown.
271:             * @param <E> Element Type
272:             * @param ejbContext session Context instance
273:             * @param resource resource object
274:             * @param resourceName resource name in the environment
275:             */
276:            @SuppressWarnings("unchecked")
277:            public static <E> void checkResource(final EJBContext ejbContext,
278:                    final E resource, final String resourceName) {
279:                //Injection
280:                checkResource(resource);
281:                //Environment access
282:                checkResource(ejbContext, resourceName);
283:            }
284:
285:            /**
286:             * Checks the following items:<li>if the ejb context can be used to
287:             * access the entry;</li> <li>if the JNDI API can be used
288:             * directly to access the entry;</li> If fails, an
289:             * IllegalStateException is thrown.
290:             * @param <E> Element Type
291:             * @param ejbContext ejb context instance
292:             * @param entryName entry name in the environment
293:             */
294:            @SuppressWarnings("unchecked")
295:            public static <E> void checkResource(final EJBContext ejbContext,
296:                    final String entryName) {
297:
298:                logger.debug("Checking resource. Name = {0}", entryName);
299:
300:                //Session Context
301:                E sctxEntry = (E) getEntryByEJBContext(ejbContext, entryName);
302:                checkResource(sctxEntry);
303:
304:                //JNDI Access
305:                E eJNDI = (E) getEntryByJNDI(entryName);
306:                checkResource(eJNDI);
307:
308:                logger.debug("Resource is ok. Name = {0}", entryName);
309:            }
310:
311:            /**
312:             * Checks if an entry is working properly.
313:             * @param <E> entry type
314:             * @param entry reference
315:             */
316:            public static <E> void checkResource(final E entry) {
317:                if (entry == null) {
318:                    throw new IllegalStateException(ERROR_REFERENCE_NULL);
319:                }
320:
321:                Class entryClass = entry.getClass();
322:                String entryClassName = entryClass.getName();
323:
324:                try {
325:                    if (DataSource.class.isAssignableFrom(entryClass)) {
326:                        // Checks DataSource
327:                        logger.debug("Checking DataSource.");
328:                        ((DataSource) entry).getConnection().close();
329:
330:                    } else if (Topic.class.isAssignableFrom(entryClass)) {
331:                        // Checks Topic
332:                        logger.debug("Checking Topic.");
333:                        ((Topic) entry).getTopicName();
334:
335:                    } else if (Queue.class.isAssignableFrom(entryClass)) {
336:                        // Checks Queue
337:                        logger.debug("Checking Queue.");
338:                        ((Queue) entry).getQueueName();
339:
340:                    } else if (ConnectionFactory.class
341:                            .isAssignableFrom(entryClass)) {
342:                        // Checks ConnectionFactory
343:                        logger.debug("Checking ConnectionFactory.");
344:                        ((ConnectionFactory) entry).createConnection().close();
345:
346:                    } else if (QueueConnectionFactory.class
347:                            .isAssignableFrom(entryClass)) {
348:                        // Checks QueueConnectionFactory
349:                        logger.debug("Checking QueueConnectionFactory.");
350:                        ((QueueConnectionFactory) entry).createConnection()
351:                                .close();
352:
353:                    } else if (TopicConnectionFactory.class
354:                            .isAssignableFrom(entryClass)) {
355:                        // Checks TopicConnectionFactory
356:                        logger.debug("Checking TopicConnectionFactory.");
357:                        ((TopicConnectionFactory) entry).createConnection()
358:                                .close();
359:
360:                    } else if (Session.class.isAssignableFrom(entryClass)) {
361:                        // Checks Mail Session
362:                        logger.debug("Checking Mail Session.");
363:                        ((Session) entry).getProperties().keySet();
364:
365:                    } else if (URL.class.isAssignableFrom(entryClass)) {
366:                        // Checks TopicConnectionFactory
367:                        logger.debug("Checking URL.");
368:                        ((URL) entry).getHost().toString();
369:
370:                    } else if (UserTransaction.class
371:                            .isAssignableFrom(entryClass)) {
372:                        // Checks UserTransaction
373:                        logger.debug("Checking UserTransaction.");
374:                        UserTransactionTester
375:                                .checkInstance(((UserTransaction) entry));
376:
377:                    } else if (TimerService.class.isAssignableFrom(entryClass)) {
378:                        // Checks TimerService
379:                        logger.debug("Checking TimerService.");
380:                        TimerServiceTester
381:                                .checkInstance(((TimerService) entry));
382:
383:                    } else if (EJBContext.class.isAssignableFrom(entryClass)) {
384:                        // Checks EJBContext
385:                        logger.debug("Checking EJBContext.");
386:                        EJBContextTester.checkInstance(((EJBContext) entry));
387:
388:                    } else if (EJBContext.class.isAssignableFrom(entryClass)) {
389:                        // Checks EJBContext
390:                        logger.debug("Checking EJBContext.");
391:                        EJBContextTester.checkInstance(((EJBContext) entry));
392:
393:                    } else {
394:                        logger.debug("Unknow resource. Type = {0}",
395:                                entryClassName);
396:                        throw new IllegalStateException(
397:                                "Unknow resource. Type = " + entryClassName);
398:                    }
399:                } catch (Exception e) {
400:                    logger.debug("Exception: {0}", e);
401:                    throw new IllegalStateException("Exception: "
402:                            + e.toString());
403:                }
404:            }
405:
406:            /**
407:             * Checks the following items: <li>if the injected reference is ok;</li><li>if the ejb context can be used to
408:             * access the persitence unit entry;</li> <li>if the JNDI API can be used
409:             * directly to access the persistence unit entry;</li> If fails, an
410:             * IllegalStateException is thrown.
411:             * @param ref reference
412:             * @param ejbContext ejb context instance
413:             * @param pUnitName persistence unit name
414:             */
415:            public static void checkEntityManagerFactory(
416:                    final EJBContext ejbContext,
417:                    final EntityManagerFactory ref, final String pUnitName) {
418:                logger.debug("Checking Entity Manager Factory. Name = {0}",
419:                        pUnitName);
420:
421:                //Check injection
422:                checkEntityManagerFactory(ref);
423:
424:                //Environment
425:                checkEntityManagerFactory(ejbContext, pUnitName);
426:
427:                logger.debug("Entity Manager Factory is ok. Name = {0}",
428:                        pUnitName);
429:            }
430:
431:            /**
432:             * Checks the following items:<li>if the ejb context can be used to
433:             * access the persitence unit entry;</li> <li>if the JNDI API can be used
434:             * directly to access the persistence unit entry;</li> If fails, an
435:             * IllegalStateException is thrown.
436:             * @param ejbContext ejb context instance
437:             * @param pUnitName persistence unit name
438:             */
439:            public static void checkEntityManagerFactory(
440:                    final EJBContext ejbContext, final String pUnitName) {
441:                logger.debug("Checking Entity Manager Factory. Name = {0}",
442:                        pUnitName);
443:
444:                // Session Context
445:                EntityManagerFactory sctxEntry = getEntryByEJBContext(
446:                        ejbContext, pUnitName);
447:                checkEntityManagerFactory(sctxEntry);
448:
449:                // JNDI Access
450:                EntityManagerFactory eJNDI = getEntryByJNDI(pUnitName);
451:                checkEntityManagerFactory(eJNDI);
452:
453:                logger.debug("Entity Manager Factory is ok. Name = {0}",
454:                        pUnitName);
455:            }
456:
457:            /**
458:             * Checks if an entity manager factory reference is working properly.
459:             * @param ref reference
460:             */
461:            public static void checkEntityManagerFactory(
462:                    final EntityManagerFactory ref) {
463:                try {
464:                    checkInstance(ref, "cemf");
465:                } catch (Exception e) {
466:                    throw new IllegalStateException(
467:                            "Error checking Entity Manager Factory reference.");
468:                }
469:            }
470:
471:            /**
472:             * Checks the following items: <li>if the injected reference is ok;</li><li>if the ejb context can be used to
473:             * access the persitence unit entry;</li> <li>if the JNDI API can be used
474:             * directly to access the persistence context entry;</li> If fails, an
475:             * IllegalStateException is thrown.
476:             * @param ref reference
477:             * @param ejbContext ejb context instance
478:             * @param pUnitName persistence unit name
479:             */
480:            public static void checkEntityManager(final EJBContext ejbContext,
481:                    final EntityManager ref, final String pUnitName) {
482:                logger.debug("Checking Entity Manager Factory. Name = {0}",
483:                        pUnitName);
484:
485:                //Check injection
486:                checkEntityManager(ref);
487:
488:                //Environment
489:                checkEntityManager(ejbContext, pUnitName);
490:
491:                logger.debug("Entity Manager Factory is ok. Name = {0}",
492:                        pUnitName);
493:            }
494:
495:            /**
496:             * Checks the following items:<li>if the ejb context can be used to
497:             * access the persitence unit entry;</li> <li>if the JNDI API can be used
498:             * directly to access the persistence context entry;</li> If fails, an
499:             * IllegalStateException is thrown.
500:             * @param ejbContext ejb context instance
501:             * @param pUnitName persistence unit name
502:             */
503:            public static void checkEntityManager(final EJBContext ejbContext,
504:                    final String pUnitName) {
505:                logger.debug("Checking Entity Manager. Name = {0}", pUnitName);
506:
507:                // Session Context
508:                EntityManager sctxEntry = getEntryByEJBContext(ejbContext,
509:                        pUnitName);
510:                checkEntityManager(sctxEntry);
511:
512:                // JNDI Access
513:                EntityManager eJNDI = getEntryByJNDI(pUnitName);
514:                checkEntityManager(eJNDI);
515:
516:                logger.debug("Entity Manager is ok. Name = {0}", pUnitName);
517:            }
518:
519:            /**
520:             * Checks if an entity manager reference is working properly.
521:             * @param ref reference
522:             */
523:            public static void checkEntityManager(final EntityManager ref) {
524:                try {
525:                    EntityManagerTester.checkInstance(ref, "cem");
526:                } catch (Exception e) {
527:                    throw new IllegalStateException(
528:                            "Error checking Entity Manager Factory reference.",
529:                            e);
530:                }
531:            }
532:
533:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.