1 16 17 package org.springframework.jndi; 18 19 import javax.naming.NamingException ; 20 21 import org.springframework.aop.framework.ProxyFactory; 22 import org.springframework.beans.factory.BeanClassLoaderAware; 23 import org.springframework.beans.factory.FactoryBean; 24 import org.springframework.util.ClassUtils; 25 26 58 public class JndiObjectFactoryBean extends JndiObjectLocator implements FactoryBean, BeanClassLoaderAware { 59 60 private Class proxyInterface; 61 62 private boolean lookupOnStartup = true; 63 64 private boolean cache = true; 65 66 private Object defaultObject; 67 68 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 69 70 private Object jndiObject; 71 72 73 81 public void setProxyInterface(Class proxyInterface) { 82 if (!proxyInterface.isInterface()) { 83 throw new IllegalArgumentException ("[" + proxyInterface.getName() + "] is not an interface"); 84 } 85 this.proxyInterface = proxyInterface; 86 } 87 88 96 public void setLookupOnStartup(boolean lookupOnStartup) { 97 this.lookupOnStartup = lookupOnStartup; 98 } 99 100 109 public void setCache(boolean cache) { 110 this.cache = cache; 111 } 112 113 122 public void setDefaultObject(Object defaultObject) { 123 this.defaultObject = defaultObject; 124 } 125 126 public void setBeanClassLoader(ClassLoader classLoader) { 127 this.beanClassLoader = classLoader; 128 } 129 130 131 134 public void afterPropertiesSet() throws IllegalArgumentException , NamingException { 135 super.afterPropertiesSet(); 136 137 if (this.proxyInterface != null) { 138 if (this.defaultObject != null) { 139 throw new IllegalArgumentException ( 140 "'defaultObject' is not supported in combination with 'proxyInterface'"); 141 } 142 this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this); 144 } 145 146 else { 147 if (!this.lookupOnStartup || !this.cache) { 148 throw new IllegalArgumentException ( 149 "Cannot deactivate 'lookupOnStartup' or 'cache' without specifying a 'proxyInterface'"); 150 } 151 if (this.defaultObject != null && getExpectedType() != null && 152 !getExpectedType().isInstance(this.defaultObject)) { 153 throw new IllegalArgumentException ("Default object [" + this.defaultObject + 154 "] of type [" + this.defaultObject.getClass().getName() + 155 "] is not of expected type [" + getExpectedType().getName() + "]"); 156 } 157 this.jndiObject = lookupWithFallback(); 159 } 160 } 161 162 169 protected Object lookupWithFallback() throws NamingException { 170 try { 171 return lookup(); 172 } 173 catch (TypeMismatchNamingException ex) { 174 throw ex; 177 } 178 catch (NamingException ex) { 179 if (this.defaultObject != null) { 180 if (logger.isDebugEnabled()) { 181 logger.debug("JNDI lookup failed - returning specified default object instead", ex); 182 } 183 else if (logger.isInfoEnabled()) { 184 logger.info("JNDI lookup failed - returning specified default object instead: " + ex); 185 } 186 return this.defaultObject; 187 } 188 throw ex; 189 } 190 } 191 192 193 196 public Object getObject() { 197 return this.jndiObject; 198 } 199 200 public Class getObjectType() { 201 if (this.proxyInterface != null) { 202 return this.proxyInterface; 203 } 204 else if (this.jndiObject != null) { 205 return this.jndiObject.getClass(); 206 } 207 else { 208 return getExpectedType(); 209 } 210 } 211 212 public boolean isSingleton() { 213 return true; 214 } 215 216 217 220 private static class JndiObjectProxyFactory { 221 222 private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { 223 JndiObjectTargetSource targetSource = new JndiObjectTargetSource(); 225 targetSource.setJndiTemplate(jof.getJndiTemplate()); 226 targetSource.setJndiName(jof.getJndiName()); 227 targetSource.setExpectedType(jof.getExpectedType()); 228 targetSource.setResourceRef(jof.isResourceRef()); 229 targetSource.setLookupOnStartup(jof.lookupOnStartup); 230 targetSource.setCache(jof.cache); 231 targetSource.afterPropertiesSet(); 232 233 ProxyFactory proxyFactory = new ProxyFactory(); 235 proxyFactory.addInterface(jof.proxyInterface); 236 proxyFactory.setTargetSource(targetSource); 237 return proxyFactory.getProxy(jof.beanClassLoader); 238 } 239 } 240 241 } 242 | Popular Tags |