@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; } }
/** * Return the JNDI environment to use for JNDI lookups. */ @Nullable public Properties getJndiEnvironment() { return this.jndiTemplate.getEnvironment(); }
/** * Unbind the RMI service from JNDI on bean factory shutdown. */ @Override public void destroy() throws NamingException, RemoteException { if (logger.isDebugEnabled()) { logger.debug("Unbinding RMI service from JNDI location [" + this.jndiName + "]"); } this.jndiTemplate.unbind(this.jndiName); invokePortableRemoteObject(unexportObject); }
/** * 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; }
/** * Set the JNDI template to use for JNDI lookups. * You can also specify JNDI environment settings via "jndiEnvironment". * @see #setJndiEnvironment */ public void setJndiTemplate(JndiTemplate jndiTemplate) { this.jndiTemplate = (jndiTemplate != null ? jndiTemplate : new JndiTemplate()); }
public class JndiExporter implements InitializingBean { private final JndiTemplate template = new JndiTemplate(); private Map<String, Object> jndiMapping = null; @Override public void afterPropertiesSet() throws Exception { for(Entry<String, Object> addToJndi: jndiMapping.entrySet()){ template.bind(addToJndi.getKey(), addToJndi.getValue()); } } public void setJndiMapping(Map<String, Object> jndiMapping) { this.jndiMapping = jndiMapping; } }
@Bean public JndiTemplate jndiTemplate() { JndiTemplate jndiTemplate = new JndiTemplate(); Properties environment = new Properties(); environment.setProperty(com....SOLACE_JMS_RESPECT_TIME_TO_LIVE, "false"); jndiTemplate.setEnvironment(environment); return jndiTemplate; }
/** * Rebind the specified service to JNDI, for recovering in case * of the target registry having been restarted. * @throws NamingException if service binding failed */ public void rebind() throws NamingException { if (logger.isDebugEnabled()) { logger.debug("Binding RMI service to JNDI location [" + this.jndiName + "]"); } this.jndiTemplate.rebind(this.jndiName, this.exportedObject); }
@Test public void testBind() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; jt.bind(name, o); verify(context).bind(name, o); verify(context).close(); }
/** * 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; } }
/** * Set the JNDI environment to use for JNDI lookups. * Creates a JndiTemplate with the given environment settings. * @see #setJndiTemplate */ public void setJndiEnvironment(Properties jndiEnvironment) { this.jndiTemplate = new JndiTemplate(jndiEnvironment); }
/** * Return an instance of a jndiTemplate from a given map of properties. * If the map is null or empty then return the default jndiTemplate. * @param map * @return */ protected static JndiTemplate getJndiTemplate(Map<String,String> map) { if(map == null || map.size() == 0) { return new org.springframework.jndi.JndiTemplate(); } Properties properties = new Properties(); properties.putAll(map); JndiTemplate jndiTemplate = new org.springframework.jndi.JndiTemplate(); jndiTemplate.setEnvironment(properties); return jndiTemplate; } }
/** * Rebind the specified service to JNDI, for recovering in case * of the target registry having been restarted. * @throws NamingException if service binding failed */ public void rebind() throws NamingException { if (logger.isDebugEnabled()) { logger.debug("Binding RMI service to JNDI location [" + this.jndiName + "]"); } this.jndiTemplate.rebind(this.jndiName, this.exportedObject); }
public void afterPropertiesSet() throws NamingException { logger.info("Binding " + BONITA_NON_MANAGED_DS_JNDI_NAME + " @ " + datasource.toString()); jndiTemplate.bind(BONITA_NON_MANAGED_DS_JNDI_NAME, datasource); }
@Bean DataSource dataSource() { DataSource dataSource = null; JndiTemplate jndi = new JndiTemplate(); try { dataSource = (DataSource) jndi.lookup("java:comp/env/jdbc/jndiName"); } catch (NamingException e) { logger.error("NamingException for java:comp/env/jdbc/jndiName", e); } return dataSource; }
TransactionManager tm = getJndiTemplate().lookup(jndiName, TransactionManager.class); if (logger.isDebugEnabled()) { logger.debug("JTA TransactionManager found at fallback JNDI location [" + jndiName + "]");
/** * Set the JNDI template to use for JNDI lookups. * You can also specify JNDI environment settings via "jndiEnvironment". * @see #setJndiEnvironment */ public void setJndiTemplate(JndiTemplate jndiTemplate) { this.jndiTemplate = (jndiTemplate != null ? jndiTemplate : new JndiTemplate()); }
/** * Return the JNDI environment to use for JNDI lookups. */ @Nullable public Properties getJndiEnvironment() { return this.jndiTemplate.getEnvironment(); }
/** * Unbind the RMI service from JNDI on bean factory shutdown. */ @Override public void destroy() throws NamingException, RemoteException { if (logger.isDebugEnabled()) { logger.debug("Unbinding RMI service from JNDI location [" + this.jndiName + "]"); } this.jndiTemplate.unbind(this.jndiName); invokePortableRemoteObject(unexportObject); }
@Test public void testRebind() throws Exception { Object o = new Object(); String name = "foo"; final Context context = mock(Context.class); JndiTemplate jt = new JndiTemplate() { @Override protected Context createInitialContext() { return context; } }; jt.rebind(name, o); verify(context).rebind(name, o); verify(context).close(); }