/** * Look up the object with the given name in the current JNDI context. * @param name the JNDI name of the object * @param requiredType type the JNDI object must match. Can be an interface or * superclass of the actual class, or {@code null} for any match. For example, * if the value is {@code Object.class}, this method will succeed whatever * the class of the returned instance. * @return object found (cannot be {@code null}; if a not so well-behaved * JNDI implementations returns null, a NamingException gets thrown) * @throws NamingException if there is no object with the given * name bound to JNDI */ @SuppressWarnings("unchecked") public <T> T lookup(String name, @Nullable Class<T> requiredType) throws NamingException { Object jndiObject = lookup(name); if (requiredType != null && !requiredType.isInstance(jndiObject)) { throw new TypeMismatchNamingException(name, requiredType, jndiObject.getClass()); } return (T) jndiObject; }
/** * Find the JTA UserTransaction through a default JNDI lookup: * "java:comp/UserTransaction". * @return the JTA UserTransaction reference, or {@code null} if not found * @see #DEFAULT_USER_TRANSACTION_NAME */ @Nullable protected UserTransaction findUserTransaction() { String jndiName = DEFAULT_USER_TRANSACTION_NAME; try { UserTransaction ut = getJndiTemplate().lookup(jndiName, UserTransaction.class); if (logger.isDebugEnabled()) { logger.debug("JTA UserTransaction found at default JNDI location [" + jndiName + "]"); } this.userTransactionObtainedFromJndi = true; return ut; } catch (NamingException ex) { if (logger.isDebugEnabled()) { logger.debug("No JTA UserTransaction found at default JNDI location [" + jndiName + "]", ex); } return null; } }
TransactionManager tm = getJndiTemplate().lookup(jndiName, TransactionManager.class); if (logger.isDebugEnabled()) { logger.debug("JTA TransactionManager found at fallback JNDI location [" + jndiName + "]");
/** * Obtain the WebSphere UOWManager from the default JNDI location * "java:comp/websphere/UOWManager". * @return the UOWManager object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate */ protected UOWManager lookupDefaultUowManager() throws TransactionSystemException { try { logger.debug("Retrieving WebSphere UOWManager from default JNDI location [" + DEFAULT_UOW_MANAGER_NAME + "]"); return getJndiTemplate().lookup(DEFAULT_UOW_MANAGER_NAME, UOWManager.class); } catch (NamingException ex) { logger.debug("WebSphere UOWManager is not available at default JNDI location [" + DEFAULT_UOW_MANAGER_NAME + "] - falling back to UOWManagerFactory lookup"); return UOWManagerFactory.getUOWManager(); } }
/** * Look up the WebSphere UOWManager in JNDI via the configured name. * @param uowManagerName the JNDI name of the UOWManager * @return the UOWManager object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setUowManagerName */ protected UOWManager lookupUowManager(String uowManagerName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving WebSphere UOWManager from JNDI location [" + uowManagerName + "]"); } return getJndiTemplate().lookup(uowManagerName, UOWManager.class); } catch (NamingException ex) { throw new TransactionSystemException( "WebSphere UOWManager is not available at JNDI location [" + uowManagerName + "]", ex); } }
TransactionSynchronizationRegistry tsr = getJndiTemplate().lookup(jndiName, TransactionSynchronizationRegistry.class); if (logger.isDebugEnabled()) { logger.debug("JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]");
/** * Look up the JTA 1.1 TransactionSynchronizationRegistry in JNDI via the configured name. * <p>Can be overridden in subclasses to provide a different TransactionManager object. * @param registryName the JNDI name of the * TransactionSynchronizationRegistry * @return the TransactionSynchronizationRegistry object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setTransactionSynchronizationRegistryName */ protected TransactionSynchronizationRegistry lookupTransactionSynchronizationRegistry(String registryName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving JTA TransactionSynchronizationRegistry from JNDI location [" + registryName + "]"); } return getJndiTemplate().lookup(registryName, TransactionSynchronizationRegistry.class); } catch (NamingException ex) { throw new TransactionSystemException( "JTA TransactionSynchronizationRegistry is not available at JNDI location [" + registryName + "]", ex); } }
/** * Look up the JTA TransactionManager in JNDI via the configured name. * <p>Called by {@code afterPropertiesSet} if no direct TransactionManager reference was set. * Can be overridden in subclasses to provide a different TransactionManager object. * @param transactionManagerName the JNDI name of the TransactionManager * @return the UserTransaction object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setTransactionManagerName */ protected TransactionManager lookupTransactionManager(String transactionManagerName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving JTA TransactionManager from JNDI location [" + transactionManagerName + "]"); } return getJndiTemplate().lookup(transactionManagerName, TransactionManager.class); } catch (NamingException ex) { throw new TransactionSystemException( "JTA TransactionManager is not available at JNDI location [" + transactionManagerName + "]", ex); } }
/** * Look up the JTA UserTransaction in JNDI via the configured name. * <p>Called by {@code afterPropertiesSet} if no direct UserTransaction reference was set. * Can be overridden in subclasses to provide a different UserTransaction object. * @param userTransactionName the JNDI name of the UserTransaction * @return the UserTransaction object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setUserTransactionName */ protected UserTransaction lookupUserTransaction(String userTransactionName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving JTA UserTransaction from JNDI location [" + userTransactionName + "]"); } return getJndiTemplate().lookup(userTransactionName, UserTransaction.class); } catch (NamingException ex) { throw new TransactionSystemException( "JTA UserTransaction is not available at JNDI location [" + userTransactionName + "]", ex); } }
/** * Look up the object with the given name in the current JNDI context. * @param name the JNDI name of the object * @param requiredType type the JNDI object must match. Can be an interface or * superclass of the actual class, or {@code null} for any match. For example, * if the value is {@code Object.class}, this method will succeed whatever * the class of the returned instance. * @return object found (cannot be {@code null}; if a not so well-behaved * JNDI implementations returns null, a NamingException gets thrown) * @throws NamingException if there is no object with the given * name bound to JNDI */ @SuppressWarnings("unchecked") public <T> T lookup(String name, @Nullable Class<T> requiredType) throws NamingException { Object jndiObject = lookup(name); if (requiredType != null && !requiredType.isInstance(jndiObject)) { throw new TypeMismatchNamingException(name, requiredType, jndiObject.getClass()); } return (T) jndiObject; }
@Configuration public class MainConfig { ... @Bean DataSource dataSource() { DataSource dataSource = null; JndiTemplate jndi = new JndiTemplate(); try { dataSource = (DataSource) jndi.lookup("java:comp/env/jdbc/yourname"); } catch (NamingException e) { logger.error("NamingException for java:comp/env/jdbc/yourname", e); } return dataSource; } }
/** * Obtain the WebSphere UOWManager from the default JNDI location * "java:comp/websphere/UOWManager". * @return the UOWManager object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate */ protected UOWManager lookupDefaultUowManager() throws TransactionSystemException { try { logger.debug("Retrieving WebSphere UOWManager from default JNDI location [" + DEFAULT_UOW_MANAGER_NAME + "]"); return getJndiTemplate().lookup(DEFAULT_UOW_MANAGER_NAME, UOWManager.class); } catch (NamingException ex) { logger.debug("WebSphere UOWManager is not available at default JNDI location [" + DEFAULT_UOW_MANAGER_NAME + "] - falling back to UOWManagerFactory lookup"); return UOWManagerFactory.getUOWManager(); } }
/** * Look up the JTA 1.1 TransactionSynchronizationRegistry in JNDI via the configured name. * <p>Can be overridden in subclasses to provide a different TransactionManager object. * @param registryName the JNDI name of the * TransactionSynchronizationRegistry * @return the TransactionSynchronizationRegistry object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setTransactionSynchronizationRegistryName */ protected TransactionSynchronizationRegistry lookupTransactionSynchronizationRegistry(String registryName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving JTA TransactionSynchronizationRegistry from JNDI location [" + registryName + "]"); } return getJndiTemplate().lookup(registryName, TransactionSynchronizationRegistry.class); } catch (NamingException ex) { throw new TransactionSystemException( "JTA TransactionSynchronizationRegistry is not available at JNDI location [" + registryName + "]", ex); } }
/** * Look up the WebSphere UOWManager in JNDI via the configured name. * @param uowManagerName the JNDI name of the UOWManager * @return the UOWManager object * @throws TransactionSystemException if the JNDI lookup failed * @see #setJndiTemplate * @see #setUowManagerName */ protected UOWManager lookupUowManager(String uowManagerName) throws TransactionSystemException { try { if (logger.isDebugEnabled()) { logger.debug("Retrieving WebSphere UOWManager from JNDI location [" + uowManagerName + "]"); } return getJndiTemplate().lookup(uowManagerName, UOWManager.class); } catch (NamingException ex) { throw new TransactionSystemException( "WebSphere UOWManager is not available at JNDI location [" + uowManagerName + "]", ex); } }
@Test public void testLookupSucceeds() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(o); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; Object o2 = jt.lookup(name); assertEquals(o, o2); verify(context).close(); }
@Test public void testLookupFailsWithTypeMismatch() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(o); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name, String.class); fail("Should have thrown TypeMismatchNamingException"); } catch (TypeMismatchNamingException ex) { // Ok } verify(context).close(); }
@Test public void testLookupReturnsNull() throws Exception { String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willReturn(null); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }
@Test public void testLookupFails() throws Exception { NameNotFoundException ne = new NameNotFoundException(); String name = "foo"; final Context context = mock(Context.class); given(context.lookup(name)).willThrow(ne); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; try { jt.lookup(name); fail("Should have thrown NamingException"); } catch (NameNotFoundException ex) { // Ok } verify(context).close(); }