public <T> T lookup(String jndiName, Class<T> requiredType) throws Exception { JndiLocatorDelegate locator = new JndiLocatorDelegate(); if (jndiEnvironment instanceof JndiTemplate) { locator.setJndiTemplate((JndiTemplate) jndiEnvironment); } else if (jndiEnvironment instanceof Properties) { locator.setJndiEnvironment((Properties) jndiEnvironment); } else if (jndiEnvironment != null) { throw new IllegalStateException("Illegal 'jndiEnvironment' type: " + jndiEnvironment.getClass()); } locator.setResourceRef(resourceRef); return locator.lookup(jndiName, requiredType); } }
/** * Create a new {@code JndiPropertySource} with the given name * and a {@link JndiLocatorDelegate} configured to prefix any names with * "java:comp/env/". */ public JndiPropertySource(String name) { this(name, JndiLocatorDelegate.createDefaultResourceRefLocator()); }
/** * Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to * {@code true}, meaning that all names will be prefixed with "java:comp/env/". * @see #setResourceRef */ public static JndiLocatorDelegate createDefaultResourceRefLocator() { JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); jndiLocator.setResourceRef(true); return jndiLocator; }
@Override public MBeanServer getMBeanServer() { try { return new JndiLocatorDelegate().lookup("java:comp/env/jmx/runtime", MBeanServer.class); } catch (NamingException ex) { throw new MBeanServerNotFoundException("Failed to retrieve WebLogic MBeanServer from JNDI", ex); } } },
@Test public void nameBoundWithPrefix() { final SimpleNamingContext context = new SimpleNamingContext(); context.bind("java:comp/env/p1", "v1"); JndiTemplate jndiTemplate = new JndiTemplate() { @Override protected Context createInitialContext() throws NamingException { return context; } }; JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); jndiLocator.setResourceRef(true); jndiLocator.setJndiTemplate(jndiTemplate); JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); assertThat(ps.getProperty("p1"), equalTo("v1")); }
/** * Set whether the lookup occurs in a Java EE container, i.e. if the prefix * "java:comp/env/" needs to be added if the JNDI name doesn't already * contain it. PersistenceAnnotationBeanPostProcessor's default is "true". * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef */ public void setResourceRef(boolean resourceRef) { this.jndiLocator.setResourceRef(resourceRef); }
@Override public void afterPropertiesSet() throws NamingException { if (this.jndiName != null) { try { this.threadFactory = this.jndiLocator.lookup(this.jndiName, ThreadFactory.class); } catch (NamingException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to retrieve [" + this.jndiName + "] from JNDI", ex); } logger.info("Could not find default managed thread factory in JNDI - " + "proceeding with default local thread factory"); } } }
/** * Set the JNDI template to use for JNDI lookups. * @see org.springframework.jndi.JndiAccessor#setJndiTemplate */ public void setJndiTemplate(JndiTemplate jndiTemplate) { this.jndiLocator.setJndiTemplate(jndiTemplate); }
/** * Set the JNDI environment to use for JNDI lookups. * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment */ public void setJndiEnvironment(Properties jndiEnvironment) { this.jndiLocator.setJndiEnvironment(jndiEnvironment); }
@Nullable public Object getProperty(String name) { if (getSource().isResourceRef() && name.indexOf(':') != -1) { Object value = this.source.lookup(name); if (logger.isDebugEnabled()) { logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
@Test public void isDefaultJndiEnvironmentAvailableFalse() throws Exception { Field builderField = NamingManager.class.getDeclaredField("initctx_factory_builder"); builderField.setAccessible(true); Object oldBuilder = builderField.get(null); builderField.set(null, null); try { assertThat(JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable(), equalTo(false)); } finally { builderField.set(null, oldBuilder); } }
@Test public void nameBoundWithoutPrefix() { final SimpleNamingContext context = new SimpleNamingContext(); context.bind("p1", "v1"); JndiTemplate jndiTemplate = new JndiTemplate() { @Override protected Context createInitialContext() throws NamingException { return context; } }; JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); jndiLocator.setResourceRef(true); jndiLocator.setJndiTemplate(jndiTemplate); JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); assertThat(ps.getProperty("p1"), equalTo("v1")); }
@Override public MBeanServer getMBeanServer() { try { return new JndiLocatorDelegate().lookup("java:comp/env/jmx/runtime", MBeanServer.class); } catch (NamingException ex) { throw new MBeanServerNotFoundException("Failed to retrieve WebLogic MBeanServer from JNDI", ex); } } },
/** * Configure a {@code JndiLocatorDelegate} with its "resourceRef" property set to * {@code true}, meaning that all names will be prefixed with "java:comp/env/". * @see #setResourceRef */ public static JndiLocatorDelegate createDefaultResourceRefLocator() { JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); jndiLocator.setResourceRef(true); return jndiLocator; }
/** * Set whether the lookup occurs in a Java EE container, i.e. if the prefix * "java:comp/env/" needs to be added if the JNDI name doesn't already * contain it. PersistenceAnnotationBeanPostProcessor's default is "true". * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef */ public void setResourceRef(boolean resourceRef) { this.jndiLocator.setResourceRef(resourceRef); }
@Override public void afterPropertiesSet() throws NamingException { if (this.jndiName != null) { setConcurrentExecutor(this.jndiLocator.lookup(this.jndiName, Executor.class)); } }
/** * Set the JNDI template to use for JNDI lookups. * @see org.springframework.jndi.JndiAccessor#setJndiTemplate */ public void setJndiTemplate(JndiTemplate jndiTemplate) { this.jndiLocator.setJndiTemplate(jndiTemplate); }
/** * Set the JNDI environment to use for JNDI lookups. * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment */ public void setJndiEnvironment(Properties jndiEnvironment) { this.jndiLocator.setJndiEnvironment(jndiEnvironment); }
@Nullable public Object getProperty(String name) { if (getSource().isResourceRef() && name.indexOf(':') != -1) { Object value = this.source.lookup(name); if (logger.isDebugEnabled()) { logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));