1 16 17 package org.springframework.ejb.access; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import javax.naming.NamingException ; 23 24 import org.aopalliance.intercept.MethodInterceptor; 25 26 import org.springframework.jndi.JndiObjectLocator; 27 28 38 public abstract class AbstractSlsbInvokerInterceptor extends JndiObjectLocator 39 implements MethodInterceptor { 40 41 private boolean lookupHomeOnStartup = true; 42 43 private boolean cacheHome = true; 44 45 49 private Object cachedHome; 50 51 54 private Method createMethod; 55 56 private final Object homeMonitor = new Object (); 57 58 59 66 public void setLookupHomeOnStartup(boolean lookupHomeOnStartup) { 67 this.lookupHomeOnStartup = lookupHomeOnStartup; 68 } 69 70 77 public void setCacheHome(boolean cacheHome) { 78 this.cacheHome = cacheHome; 79 } 80 81 82 87 public void afterPropertiesSet() throws NamingException { 88 super.afterPropertiesSet(); 89 if (this.lookupHomeOnStartup) { 90 refreshHome(); 92 } 93 } 94 95 102 protected void refreshHome() throws NamingException { 103 synchronized (this.homeMonitor) { 104 Object home = lookup(); 105 if (this.cacheHome) { 106 this.cachedHome = home; 107 this.createMethod = getCreateMethod(home); 108 } 109 } 110 } 111 112 118 protected Method getCreateMethod(Object home) throws EjbAccessException { 119 try { 120 return home.getClass().getMethod("create", (Class []) null); 122 } 123 catch (NoSuchMethodException ex) { 124 throw new EjbAccessException("EJB home [" + home + "] has no no-arg create() method"); 125 } 126 } 127 128 140 protected Object getHome() throws NamingException { 141 if (!this.cacheHome || (this.lookupHomeOnStartup && !isHomeRefreshable())) { 142 return (this.cachedHome != null ? this.cachedHome : lookup()); 143 } 144 else { 145 synchronized (this.homeMonitor) { 146 if (this.cachedHome == null) { 147 this.cachedHome = lookup(); 148 this.createMethod = getCreateMethod(this.cachedHome); 149 } 150 return this.cachedHome; 151 } 152 } 153 } 154 155 159 protected boolean isHomeRefreshable() { 160 return false; 161 } 162 163 169 protected Object create() throws NamingException , InvocationTargetException { 170 try { 171 Object home = getHome(); 172 Method createMethodToUse = this.createMethod; 173 if (createMethodToUse == null) { 174 createMethodToUse = getCreateMethod(home); 175 } 176 return createMethodToUse.invoke(home, (Object []) null); 178 } 179 catch (IllegalAccessException ex) { 180 throw new EjbAccessException("Could not access EJB home create() method", ex); 181 } 182 } 183 184 } 185 | Popular Tags |