1 7 package com.inversoft.verge.mvc.controller; 8 9 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import javax.servlet.http.HttpServletRequest ; 15 16 import com.inversoft.beans.BeanException; 17 import com.inversoft.verge.util.WebBean; 18 19 20 44 public class WebBeanHandle { 45 46 50 private static final Class [] HANDLE_PARAMS = new Class [] {Action.class}; 51 52 57 static final boolean CACHING = true; 58 59 62 static Map cache = new HashMap (); 63 64 private WebBean webBean; 65 private String handle; 66 private String nesting; 67 68 69 73 74 85 private static BeanHandle getBeanHandle(String handleName, Class beanClass) 86 throws BeanException { 87 88 if (!CACHING) { 90 return new BeanHandle(handleName, beanClass, HANDLE_PARAMS); 91 } 92 93 Map propMap = null; 95 synchronized (cache) { 96 propMap = (Map ) cache.get(beanClass); 97 if (propMap == null) { 98 propMap = Collections.synchronizedMap(new HashMap ()); 99 cache.put(beanClass, propMap); 100 } 101 } 102 103 BeanHandle bh = null; 105 synchronized (propMap) { 106 bh = (BeanHandle) propMap.get(handleName); 107 if (bh == null) { 108 bh = new BeanHandle(handleName, beanClass, HANDLE_PARAMS); 109 propMap.put(handleName, bh); 110 } 111 } 112 113 return bh; 114 } 115 116 117 132 public WebBeanHandle(String id, String handle, int scope, String className) 133 throws BeanException { 134 super(); 136 137 try { 138 initialize(id, handle, scope, Class.forName(className)); 139 } catch (ClassNotFoundException cnfe) { 140 throw new BeanException(cnfe); 141 } 142 } 143 144 158 public WebBeanHandle(String definition, int scope, String className) 159 throws BeanException { 160 int index = definition.indexOf("."); 161 if (index == -1 || index == definition.length() - 1) { 162 throw new BeanException("Invalid handle definition: " + definition); 163 } 164 165 try { 166 initialize(definition.substring(0, index), definition.substring(index + 1), 167 scope, Class.forName(className)); 168 } catch (ClassNotFoundException cnfe) { 169 throw new BeanException(cnfe); 170 } 171 } 172 173 187 public WebBeanHandle(String definition, int scope, Class klass) 188 throws BeanException { 189 int index = definition.indexOf("."); 190 if (index == -1 || index == definition.length() - 1) { 191 throw new BeanException("Invalid handle definition: " + definition); 192 } 193 194 initialize(definition.substring(0, index), definition.substring(index + 1), 195 scope, klass); 196 } 197 198 209 public WebBeanHandle(WebBean webBean, String handle) 210 throws BeanException { 211 this.webBean = webBean; 212 initialize(webBean.getID(), handle, webBean.getScope(), webBean.getBeanClass()); 213 } 214 215 216 229 protected void initialize(String id, String handle, int scope, Class klass) 230 throws BeanException { 231 232 assert (id != null) : "id == null"; 233 assert (handle != null) : "handle == null"; 234 235 if (webBean == null) { 236 webBean = new WebBean(id, scope, klass); 237 } 238 239 int lastIndex = handle.lastIndexOf("."); 242 if (lastIndex != -1) { 243 nesting = handle.substring(0, lastIndex); 244 this.handle = handle.substring(lastIndex + 1); 245 } else { 246 this.handle = handle; 247 } 248 } 249 250 255 public String getHandleName() { 256 return handle; 257 } 258 259 265 public WebBean getWebBean() { 266 return webBean; 267 } 268 269 275 public Class [] getHandleParams() { 276 return HANDLE_PARAMS; 277 } 278 279 291 public Object invokeHandle(Action action, HttpServletRequest request) 292 throws BeanException { 293 294 Object bean = null; 295 if (nesting == null) { 296 bean = webBean.getInstance(request); 297 } else { 298 bean = webBean.getPropertyValue(nesting, webBean.getInstance(request)); 299 } 300 301 BeanHandle bh = findBeanHandle(handle, bean.getClass()); 302 return bh.invokeHandle(bean, new Object []{action}); 303 } 304 305 317 public Object invokeHandle(Object [] params, HttpServletRequest request) 318 throws BeanException { 319 320 Object bean = null; 321 if (nesting == null) { 322 bean = webBean.getInstance(request); 323 } else { 324 bean = webBean.getPropertyValue(nesting, webBean.getInstance(request)); 325 } 326 327 BeanHandle bh = findBeanHandle(handle, bean.getClass()); 328 return bh.invokeHandle(bean, params); 329 } 330 331 342 protected BeanHandle findBeanHandle(String handleName, Class beanClass) 343 throws BeanException { 344 return WebBeanHandle.getBeanHandle(handle, beanClass); 345 } 346 } | Popular Tags |