@Test public void testLookupWithDefaultObjectAndExpectedType() throws Exception { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiTemplate(new ExpectedLookupTemplate("foo", "")); jof.setJndiName("myFoo"); jof.setExpectedType(String.class); jof.setDefaultObject("myString"); jof.afterPropertiesSet(); assertEquals("myString", jof.getObject()); }
ClassLoader originalClassLoader = ClassUtils.overrideThreadContextClassLoader(this.beanClassLoader); try { return lookup();
@Override public Class<?> getObjectType() { if (this.proxyInterfaces != null) { if (this.proxyInterfaces.length == 1) { return this.proxyInterfaces[0]; } else if (this.proxyInterfaces.length > 1) { return createCompositeInterface(this.proxyInterfaces); } } if (this.jndiObject != null) { return this.jndiObject.getClass(); } else { return getExpectedType(); } }
if (this.defaultObject != null && getExpectedType() != null && !getExpectedType().isInstance(this.defaultObject)) { TypeConverter converter = (this.beanFactory != null ? this.beanFactory.getTypeConverter() : new SimpleTypeConverter()); try { this.defaultObject = converter.convertIfNecessary(this.defaultObject, getExpectedType()); getExpectedType().getName() + "] and cannot be converted either", ex); this.jndiObject = lookupWithFallback();
@Test public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); TestBean tb = new TestBean(); jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb)); jof.setJndiName("myFoo"); jof.setProxyInterface(ITestBean.class); jof.setDefaultObject(Boolean.TRUE); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } }
@Test public void testNotCacheWithoutProxyInterface() throws NamingException { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiName("foo"); jof.setCache(false); jof.setLookupOnStartup(false); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { targetSource.setJndiTemplate(jof.getJndiTemplate()); String jndiName = jof.getJndiName(); Assert.state(jndiName != null, "No JNDI name specified"); targetSource.setJndiName(jndiName); targetSource.setExpectedType(jof.getExpectedType()); targetSource.setResourceRef(jof.isResourceRef()); targetSource.setLookupOnStartup(jof.lookupOnStartup); targetSource.setCache(jof.cache); proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
@Test public void testNoJndiName() throws NamingException { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { } }
JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean(); factoryBean.setResourceRef(true); Object lookup = factoryBean.getJndiTemplate().lookup(propertiesLocation);
/** * Get an instance of a connection factory * @param jndiProperties * @param connectionFactoryName * @return */ public static ConnectionFactory getConnectionFactory(Map<String,String> jndiProperties, String connectionFactoryName) { JndiTemplate jndiTemplate = getJndiTemplate(jndiProperties); JndiObjectFactoryBean jndiObjectFactoryBean = getObjectFactoryInstance(jndiTemplate, connectionFactoryName); Object jndiObject = jndiObjectFactoryBean.getObject(); if(jndiObject instanceof ConnectionFactory) { return (ConnectionFactory) jndiObject; } throw new RuntimeException("Expected a ConnectionFactory class, Found a [" + jndiObject.getClass().getName() + "]"); }
public void afterPropertiesSet() throws IllegalArgumentException, NamingException { super.afterPropertiesSet(); debug = statisticsLog.isDebugEnabled(); if (debug) { statisticsLog.debug("Starting collecting Datasource connection call statistics"); if (openConnectionsLog.isDebugEnabled()) { openConnectionsLog.debug("Starting monitoring open Datasource connections"); } } }
@Test public void testLazyLookupWithoutProxyInterface() throws NamingException { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiName("foo"); jof.setLookupOnStartup(false); try { jof.afterPropertiesSet(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { targetSource.setJndiTemplate(jof.getJndiTemplate()); String jndiName = jof.getJndiName(); Assert.state(jndiName != null, "No JNDI name specified"); targetSource.setJndiName(jndiName); targetSource.setExpectedType(jof.getExpectedType()); targetSource.setResourceRef(jof.isResourceRef()); targetSource.setLookupOnStartup(jof.lookupOnStartup); targetSource.setCache(jof.cache); proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
/** * Get an instance of a Destination * @param jndiProperties * @param destinationJndiName * @return */ public static Destination getDestination(Map<String,String> jndiProperties, String destinationJndiName) { JndiTemplate jndiTemplate = getJndiTemplate(jndiProperties); JndiObjectFactoryBean jndiObjectFactoryBean = getObjectFactoryInstance(jndiTemplate, destinationJndiName); Object jndiObject = jndiObjectFactoryBean.getObject(); if(jndiObject instanceof Destination) { return (Destination)jndiObject; } throw new RuntimeException("Expected a Destination class, Found a [" + jndiObject.getClass().getName() + "]"); }
if (this.defaultObject != null && getExpectedType() != null && !getExpectedType().isInstance(this.defaultObject)) { TypeConverter converter = (this.beanFactory != null ? this.beanFactory.getTypeConverter() : new SimpleTypeConverter()); try { this.defaultObject = converter.convertIfNecessary(this.defaultObject, getExpectedType()); getExpectedType().getName() + "] and cannot be converted either", ex); this.jndiObject = lookupWithFallback();
@Test public void testLookupWithDefaultObject() throws Exception { JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); jof.setJndiTemplate(new ExpectedLookupTemplate("foo", "")); jof.setJndiName("myFoo"); jof.setExpectedType(String.class); jof.setDefaultObject("myString"); jof.afterPropertiesSet(); assertEquals("myString", jof.getObject()); }
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { targetSource.setJndiTemplate(jof.getJndiTemplate()); String jndiName = jof.getJndiName(); Assert.state(jndiName != null, "No JNDI name specified"); targetSource.setJndiName(jndiName); targetSource.setExpectedType(jof.getExpectedType()); targetSource.setResourceRef(jof.isResourceRef()); targetSource.setLookupOnStartup(jof.lookupOnStartup); targetSource.setCache(jof.cache); proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
public Object getObject() { DataSource ds = (DataSource) super.getObject(); if (debug) { synchronized (this) { if (cachedWrapper == null || cachedWrapper.ds != ds) { cachedWrapper = new DataSourceWrapper(ds); } DataSource dsProxy = (DataSource) DelegatingObjectProxy.createProxy(new Class [] { DataSource.class }, cachedWrapper, ds); return dsProxy; } } return ds; }
@Override public Class<?> getObjectType() { if (this.proxyInterfaces != null) { if (this.proxyInterfaces.length == 1) { return this.proxyInterfaces[0]; } else if (this.proxyInterfaces.length > 1) { return createCompositeInterface(this.proxyInterfaces); } } if (this.jndiObject != null) { return this.jndiObject.getClass(); } else { return getExpectedType(); } }
if (this.defaultObject != null && getExpectedType() != null && !getExpectedType().isInstance(this.defaultObject)) { TypeConverter converter = (this.beanFactory != null ? this.beanFactory.getTypeConverter() : new SimpleTypeConverter()); try { this.defaultObject = converter.convertIfNecessary(this.defaultObject, getExpectedType()); getExpectedType().getName() + "] and cannot be converted either", ex); this.jndiObject = lookupWithFallback();