1 7 8 package org.jboss.security; 9 10 import java.lang.reflect.Method ; 11 import java.util.HashMap ; 12 import javax.ejb.EJBContext ; 13 14 25 public abstract class AbstractSecurityProxy implements SecurityProxy 26 { 27 30 private HashMap methodMap; 31 32 private Method setContextMethod; 33 34 private Method setBeanMethod; 35 36 protected Object delegate; 37 44 protected boolean strict = false; 45 46 AbstractSecurityProxy(Object delegate) 47 { 48 this.delegate = delegate; 49 methodMap = new HashMap (); 50 } 51 52 63 protected abstract void invokeHomeOnDelegate(Method m, Object [] args, 64 Object delegate) throws Exception ; 65 66 76 protected abstract void invokeOnDelegate(Method m, Object [] args, Object delegate) 77 throws Exception ; 78 79 89 public void init(Class beanHome, Class beanRemote, Object securityMgr) 90 throws InstantiationException 91 { 92 init(beanHome, beanRemote, null, null, securityMgr); 93 } 94 95 111 public void init(Class beanHome, Class beanRemote, 112 Class beanLocalHome, Class beanLocal, Object securityMgr) 113 throws InstantiationException 114 { 115 mapHomeMethods(beanHome); 117 mapHomeMethods(beanLocalHome); 119 mapRemoteMethods(beanRemote); 121 mapRemoteMethods(beanLocal); 123 try 125 { 126 Class [] parameterTypes = {EJBContext .class}; 127 setContextMethod = delegate.getClass().getMethod("setEJBContext", parameterTypes); 128 } 129 catch(Exception ignore) 130 { 131 } 132 133 try 135 { 136 Class [] parameterTypes = {Object .class}; 137 setBeanMethod = delegate.getClass().getMethod("setBean", parameterTypes); 138 } 139 catch(Exception ignore) 140 { 141 } 142 143 try 145 { 146 Class [] parameterTypes = {}; 147 Object [] args = {}; 148 Method isStrict = delegate.getClass().getMethod("isStrict", parameterTypes); 149 Boolean flag = (Boolean ) isStrict.invoke(delegate, args); 150 strict = flag.booleanValue(); 151 } 152 catch(Exception ignore) 153 { 154 } 155 } 156 157 162 public void setEJBContext(EJBContext ctx) 163 { 164 if(setContextMethod != null) 165 { 166 Object [] args = {ctx}; 167 try 168 { 169 setContextMethod.invoke(delegate, args); 170 } 171 catch(Exception e) 172 { 173 e.printStackTrace(); 174 } 175 } 176 } 177 178 184 public void invokeHome(final Method m, Object [] args) 185 throws Exception 186 { 187 Method delegateMethod = (Method )methodMap.get(m); 188 if( delegateMethod != null ) 189 invokeHomeOnDelegate(delegateMethod, args, delegate); 190 } 191 192 199 public void invoke(final Method m, final Object [] args, final Object bean) 200 throws Exception 201 { 202 Method delegateMethod = (Method )methodMap.get(m); 203 if( delegateMethod != null ) 204 { 205 if( setBeanMethod != null ) 206 { 207 Object [] bargs = {bean}; 208 try 209 { 210 setBeanMethod.invoke(delegate, bargs); 211 } 212 catch(Exception e) 213 { 214 e.printStackTrace(); 215 throw new SecurityException ("Failed to set bean on proxy" + e.getMessage()); 216 } 217 } 218 invokeOnDelegate(delegateMethod, args, delegate); 219 } 220 } 221 222 228 protected void mapHomeMethods(Class beanHome) 229 { 230 if( beanHome == null ) 231 return; 232 233 Class delegateClass = delegate.getClass(); 234 Method [] methods = beanHome.getMethods(); 235 for(int m = 0; m < methods.length; m++) 236 { 237 Method hm = methods[m]; 239 Class [] parameterTypes = hm.getParameterTypes(); 240 String name = hm.getName(); 241 name = "ejb" + Character.toUpperCase(name.charAt(0)) + name.substring(1); 242 try 243 { 244 Method match = delegateClass.getMethod(name, parameterTypes); 245 methodMap.put(hm, match); 246 } 247 catch(NoSuchMethodException e) 248 { 249 name = hm.getName(); 251 try 252 { 253 Method match = delegateClass.getMethod(name, parameterTypes); 254 methodMap.put(hm, match); 255 } 256 catch(NoSuchMethodException e2) 257 { 258 if( strict ) 259 { 260 String msg = "Missing home method:" + hm + " in delegate"; 261 throw new SecurityException (msg); 262 } 263 } 264 } 265 } 266 } 267 268 272 protected void mapRemoteMethods(Class beanRemote) 273 { 274 if( beanRemote == null ) 275 return; 276 277 Class delegateClass = delegate.getClass(); 278 Method [] methods = beanRemote.getMethods(); 279 for(int m = 0; m < methods.length; m++) 280 { 281 Method rm = methods[m]; 282 Class [] parameterTypes = rm.getParameterTypes(); 283 String name = rm.getName(); 284 try 285 { 286 Method match = delegateClass.getMethod(name, parameterTypes); 287 methodMap.put(rm, match); 288 } 289 catch(NoSuchMethodException e) 290 { 291 if( strict ) 292 { 293 String msg = "Missing method:" + rm + " in delegate"; 294 throw new SecurityException (msg); 295 } 296 } 297 } 298 } 299 } 300 | Popular Tags |