1 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 ; 30 31 import javax.ejb.EJBContext ; 32 import javax.ejb.TimerService ; 33 import javax.jms.ConnectionFactory ; 34 import javax.jms.Queue ; 35 import javax.jms.QueueConnectionFactory ; 36 import javax.jms.Topic ; 37 import javax.jms.TopicConnectionFactory ; 38 import javax.mail.Session ; 39 import javax.naming.Context ; 40 import javax.naming.InitialContext ; 41 import javax.naming.NamingException ; 42 import javax.persistence.EntityManager; 43 import javax.persistence.EntityManagerFactory; 44 import javax.sql.DataSource ; 45 import javax.transaction.UserTransaction ; 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 59 public final class ContextHelper { 60 61 64 private static JLog logger = JLogFactory.getLog(ContextHelper.class); 65 66 69 public static final String ERROR_MSG_INJECTION = "The container did not inject the default value specified " 70 + "in the descriptor."; 71 72 75 public static final String ERROR_MSG_NOT_FOUND = "Entry did not find in the environment."; 76 77 80 public static final String ERROR_MSG_SESSION_CONTEXT = "Error in access using the Session Context."; 81 82 85 public static final String ERROR_MSG_JNDI_ACCESS = "Error in access using the JNDI API directly."; 86 87 90 public static final String ERROR_REFERENCE_NULL = "Reference is null."; 91 92 96 private ContextHelper(){ 97 98 } 99 100 112 @SuppressWarnings ("unchecked") 113 public static <E> void checkSimpleEntry(final EJBContext ejbContext, final String entryName, 114 final E beanValue, final E expectedValue) { 115 116 logger.debug("Checking injection. Name = {0}", entryName); 118 if (!expectedValue.equals(beanValue)) { 119 throw new IllegalStateException (ERROR_MSG_INJECTION + " Name: " + entryName); 120 } 121 logger.debug("Injection is ok. Name = {0}", entryName); 122 123 checkSimpleEntry(ejbContext, entryName, expectedValue); 124 } 125 126 136 @SuppressWarnings ("unchecked") 137 public static <E> void checkBeanRef(final EJBContext ejbContext, final String entryName, 138 final E beanInterface) { 139 140 logger.debug("Checking ejb reference. Name = {0}", entryName); 141 142 E sctxRef = (E) getEntryByEJBContext(ejbContext, entryName); 144 checkBeanRef(sctxRef); 145 146 E jndiRef = (E) getEntryByJNDI(entryName); 148 checkBeanRef(jndiRef); 149 150 logger.debug("EJB reference is ok. Name = {0}", entryName); 151 } 152 153 160 @SuppressWarnings ("unchecked") 161 private static <E> E getEntryByEJBContext(final EJBContext ejbContext, final String entryName){ 162 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 182 @SuppressWarnings ("unchecked") 183 private static <E> E getEntryByJNDI(final String entryName){ 184 logger.debug("Getting reference using the JNDI API. Name = {0}", entryName); 186 187 E eJNDI = null; 188 try { 189 Context initCtx = new InitialContext (); 190 Context myEnv = (Context ) 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 e) { 198 throw new IllegalStateException ("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 210 private static <E> void checkBeanRef(final E ref){ 211 logger.debug("Checking ejb reference."); 212 213 if (ref == null) { 214 throw new IllegalStateException (ERROR_REFERENCE_NULL); 215 } 216 217 logger.debug("Ejb reference is ok."); 218 } 219 220 230 @SuppressWarnings ("unchecked") 231 public static <E> void checkSimpleEntry(final EJBContext ejbContext, final String entryName, 232 final E expectedValue) { 233 234 logger.debug("Checking simple entry. Name = {0}", entryName); 235 236 E sctxValue = (E) getEntryByEJBContext(ejbContext, entryName); 238 239 if (!expectedValue.equals(sctxValue)) { 240 throw new IllegalStateException (ERROR_MSG_SESSION_CONTEXT + " Entry: " + entryName); 241 } 242 243 E eJNDI = (E) getEntryByJNDI(entryName); 245 246 if (!expectedValue.equals(eJNDI)) { 247 throw new IllegalStateException (ERROR_MSG_JNDI_ACCESS); 248 } 249 250 logger.debug("Simple entry is ok. Name = {0}", entryName); 251 } 252 253 264 @SuppressWarnings ("unchecked") 265 public static <E> void checkResource(final EJBContext ejbContext, final E resource, final String resourceName){ 266 checkResource(resource); 268 checkResource(ejbContext, resourceName); 270 } 271 272 281 @SuppressWarnings ("unchecked") 282 public static <E> void checkResource(final EJBContext ejbContext, final String entryName) { 283 284 logger.debug("Checking resource. Name = {0}", entryName); 285 286 E sctxEntry = (E) getEntryByEJBContext(ejbContext, entryName); 288 checkResource(sctxEntry); 289 290 E eJNDI = (E) getEntryByJNDI(entryName); 292 checkResource(eJNDI); 293 294 logger.debug("Resource is ok. Name = {0}", entryName); 295 } 296 297 302 public static <E> void checkResource(final E entry) { 303 if (entry == null) { 304 throw new IllegalStateException (ERROR_REFERENCE_NULL); 305 } 306 307 Class entryClass = entry.getClass(); 308 String entryClassName = entryClass.getName(); 309 310 try { 311 if (DataSource .class.isAssignableFrom(entryClass)) { 312 logger.debug("Checking DataSource."); 314 ((DataSource ) entry).getConnection().close(); 315 316 } else if (Topic .class.isAssignableFrom(entryClass)) { 317 logger.debug("Checking Topic."); 319 ((Topic ) entry).getTopicName(); 320 321 }else if (Queue .class.isAssignableFrom(entryClass)) { 322 logger.debug("Checking Queue."); 324 ((Queue ) entry).getQueueName(); 325 326 }else if (ConnectionFactory .class.isAssignableFrom(entryClass)) { 327 logger.debug("Checking ConnectionFactory."); 329 ((ConnectionFactory ) entry).createConnection().close(); 330 331 } else if (QueueConnectionFactory .class.isAssignableFrom(entryClass)) { 332 logger.debug("Checking QueueConnectionFactory."); 334 ((QueueConnectionFactory ) entry).createConnection().close(); 335 336 } else if (TopicConnectionFactory .class.isAssignableFrom(entryClass)) { 337 logger.debug("Checking TopicConnectionFactory."); 339 ((TopicConnectionFactory ) entry).createConnection().close(); 340 341 } else if (Session .class.isAssignableFrom(entryClass)) { 342 logger.debug("Checking Mail Session."); 344 ((Session ) entry).getProperties().keySet(); 345 346 } else if (URL .class.isAssignableFrom(entryClass)) { 347 logger.debug("Checking URL."); 349 ((URL ) entry).getHost().toString(); 350 351 } else if (UserTransaction .class.isAssignableFrom(entryClass)) { 352 logger.debug("Checking UserTransaction."); 354 UserTransactionTester.checkInstance(((UserTransaction ) entry)); 355 356 }else if (TimerService .class.isAssignableFrom(entryClass)) { 357 logger.debug("Checking TimerService."); 359 TimerServiceTester.checkInstance(((TimerService ) entry)); 360 361 }else if (EJBContext .class.isAssignableFrom(entryClass)) { 362 logger.debug("Checking EJBContext."); 364 EJBContextTester.checkInstance(((EJBContext ) entry)); 365 366 }else if (EJBContext .class.isAssignableFrom(entryClass)) { 367 logger.debug("Checking EJBContext."); 369 EJBContextTester.checkInstance(((EJBContext ) entry)); 370 371 }else{ 372 logger.debug("Unknow resource. Type = {0}", entryClassName); 373 throw new IllegalStateException ("Unknow resource. Type = " + entryClassName); 374 } 375 } catch (Exception e) { 376 logger.debug("Exception: {0}", e); 377 throw new IllegalStateException ("Exception: " + e.toString()); 378 } 379 } 380 381 390 public static void checkEntityManagerFactory(final EJBContext ejbContext, final EntityManagerFactory ref, 391 final String pUnitName){ 392 logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName); 393 394 checkEntityManagerFactory(ref); 396 397 checkEntityManagerFactory(ejbContext, pUnitName); 399 400 logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName); 401 } 402 403 411 public static void checkEntityManagerFactory(final EJBContext ejbContext, final String pUnitName){ 412 logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName); 413 414 EntityManagerFactory sctxEntry = getEntryByEJBContext(ejbContext, pUnitName); 416 checkEntityManagerFactory(sctxEntry); 417 418 EntityManagerFactory eJNDI = getEntryByJNDI(pUnitName); 420 checkEntityManagerFactory(eJNDI); 421 422 logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName); 423 } 424 425 429 public static void checkEntityManagerFactory(final EntityManagerFactory ref){ 430 try{ 431 checkInstance(ref, "cemf"); 432 }catch(Exception e){ 433 throw new IllegalStateException ("Error checking Entity Manager Factory reference."); 434 } 435 } 436 437 446 public static void checkEntityManager(final EJBContext ejbContext, final EntityManager ref, 447 final String pUnitName){ 448 logger.debug("Checking Entity Manager Factory. Name = {0}", pUnitName); 449 450 checkEntityManager(ref); 452 453 checkEntityManager(ejbContext, pUnitName); 455 456 logger.debug("Entity Manager Factory is ok. Name = {0}", pUnitName); 457 } 458 459 467 public static void checkEntityManager(final EJBContext ejbContext, final String pUnitName){ 468 logger.debug("Checking Entity Manager. Name = {0}", pUnitName); 469 470 EntityManager sctxEntry = getEntryByEJBContext(ejbContext, pUnitName); 472 checkEntityManager(sctxEntry); 473 474 EntityManager eJNDI = getEntryByJNDI(pUnitName); 476 checkEntityManager(eJNDI); 477 478 logger.debug("Entity Manager is ok. Name = {0}", pUnitName); 479 } 480 481 485 public static void checkEntityManager(final EntityManager ref){ 486 try{ 487 EntityManagerTester.checkInstance(ref, "cem"); 488 }catch(Exception e){ 489 throw new IllegalStateException ("Error checking Entity Manager Factory reference."); 490 } 491 } 492 493 } 494 | Popular Tags |