1 16 17 package org.springframework.jndi; 18 19 import javax.naming.NamingException ; 20 21 import org.springframework.aop.TargetSource; 22 23 61 public class JndiObjectTargetSource extends JndiObjectLocator implements TargetSource { 62 63 private boolean lookupOnStartup = true; 64 65 private boolean cache = true; 66 67 private Object cachedObject; 68 69 private Class targetClass; 70 71 72 78 public void setLookupOnStartup(boolean lookupOnStartup) { 79 this.lookupOnStartup = lookupOnStartup; 80 } 81 82 89 public void setCache(boolean cache) { 90 this.cache = cache; 91 } 92 93 public void afterPropertiesSet() throws NamingException { 94 super.afterPropertiesSet(); 95 if (this.lookupOnStartup) { 96 Object object = lookup(); 97 if (this.cache) { 98 this.cachedObject = object; 99 } 100 else { 101 this.targetClass = object.getClass(); 102 } 103 } 104 } 105 106 107 public Class getTargetClass() { 108 return (this.cachedObject != null ? this.cachedObject.getClass() : this.targetClass); 109 } 110 111 public boolean isStatic() { 112 return (this.cachedObject != null); 113 } 114 115 public Object getTarget() { 116 try { 117 if (this.lookupOnStartup || !this.cache) { 118 return (this.cachedObject != null ? this.cachedObject : lookup()); 119 } 120 else { 121 synchronized (this) { 122 if (this.cachedObject == null) { 123 this.cachedObject = lookup(); 124 } 125 return this.cachedObject; 126 } 127 } 128 } 129 catch (NamingException ex) { 130 throw new JndiLookupFailureException("JndiObjectTargetSource failed to obtain new target object", ex); 131 } 132 } 133 134 public void releaseTarget(Object target) { 135 } 136 137 } 138 | Popular Tags |