1 package org.directwebremoting.fluent; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.directwebremoting.AjaxFilter; 11 import org.directwebremoting.Container; 12 import org.directwebremoting.extend.AccessControl; 13 import org.directwebremoting.extend.AjaxFilterManager; 14 import org.directwebremoting.extend.Configurator; 15 import org.directwebremoting.extend.Converter; 16 import org.directwebremoting.extend.ConverterManager; 17 import org.directwebremoting.extend.Creator; 18 import org.directwebremoting.extend.CreatorManager; 19 import org.directwebremoting.impl.SignatureParser; 20 import org.directwebremoting.util.LocalUtil; 21 import org.directwebremoting.util.Logger; 22 23 65 public abstract class FluentConfigurator implements Configurator 66 { 67 70 public abstract void configure(); 71 72 78 public FluentConfigurator withConverterType(String id, String converterClassName) 79 { 80 setState(STATE_INIT_CONVERT); 81 converterManager.addConverterType(id, converterClassName); 82 return this; 83 } 84 85 92 public FluentConfigurator withConverter(String newConverter, String newMatch) 93 { 94 setState(STATE_ALLOW_CONVERT); 95 this.converter = newConverter; 96 this.match = newMatch; 97 return this; 98 } 99 100 106 public FluentConfigurator withCreatorType(String id, String creatorClassName) 107 { 108 setState(STATE_INIT_CREATE); 109 creatorManager.addCreatorType(id, creatorClassName); 110 return this; 111 } 112 113 120 public FluentConfigurator withCreator(String newTypeName, String newScriptName) 121 { 122 setState(STATE_ALLOW_CREATE); 123 this.typeName = newTypeName; 124 this.scriptName = newScriptName; 125 return this; 126 } 127 128 132 public FluentConfigurator withFilter(String newFilterClassName) 133 { 134 setState(STATE_ALLOW_FILTER); 135 this.filterClassName = newFilterClassName; 136 return this; 137 } 138 139 145 public FluentConfigurator addParam(String name, String value) 146 { 147 if (params == null) 148 { 149 params = new HashMap (); 150 } 151 152 params.put(name, value); 153 return this; 154 } 155 156 161 public FluentConfigurator addFilter(String newFilterClassName) 162 { 163 if (filters == null) 164 { 165 filters = new ArrayList (); 166 } 167 168 filters.add(newFilterClassName); 169 return this; 170 } 171 172 178 public FluentConfigurator include(String methodName) 179 { 180 accessControl.addIncludeRule(scriptName, methodName); 181 return this; 182 } 183 184 190 public FluentConfigurator exclude(String methodName) 191 { 192 accessControl.addExcludeRule(scriptName, methodName); 193 return this; 194 } 195 196 203 public FluentConfigurator withAuth(String methodName, String role) 204 { 205 accessControl.addRoleRestriction(scriptName, methodName, role); 206 return this; 207 } 208 209 213 public FluentConfigurator withSignature() 214 { 215 setState(STATE_SIGNATURE); 216 return this; 217 } 218 219 224 public FluentConfigurator addLine(String line) 225 { 226 if (null == line) 227 { 228 return this; 229 } 230 231 if (null == signature) 232 { 233 signature = new StringBuffer (); 234 } 235 236 signature.append(line); 237 signature.append(System.getProperty("line.separator")); 238 239 return this; 240 } 241 242 250 private void setState(int state) 251 { 252 flush(); 253 this.state = state; 254 } 255 256 260 private void flush() 261 { 262 switch (state) 263 { 264 case STATE_INIT_CONVERT: 265 break; 267 268 case STATE_INIT_CREATE: 269 break; 271 272 case STATE_ALLOW_CONVERT: 273 try 274 { 275 if (params == null) 276 { 277 converterManager.addConverter(match, converter, EMPTY_MAP); 278 } 279 else 280 { 281 converterManager.addConverter(match, converter, params); 282 } 283 } 284 catch (Exception e) 285 { 286 log.warn("Failed to add converter of type='" + converter + "', match=" + match + ": ", e); 287 } 288 params = null; 289 match = null; 290 converter = null; 291 break; 292 293 case STATE_ALLOW_CREATE: 294 try 295 { 296 if (params == null) 297 { 298 creatorManager.addCreator(scriptName, typeName, EMPTY_MAP); 299 } 300 else 301 { 302 creatorManager.addCreator(scriptName, typeName, params); 303 } 304 305 if (filters != null) 306 { 307 for (Iterator it = filters.iterator(); it.hasNext();) 308 { 309 String className = (String ) it.next(); 310 AjaxFilter filter = (AjaxFilter) LocalUtil.classNewInstance(scriptName, className, AjaxFilter.class); 311 312 if (filter != null) 313 { 314 LocalUtil.setParams(filter, Collections.EMPTY_MAP, Collections.EMPTY_LIST); 315 ajaxFilterManager.addAjaxFilter(filter, scriptName); 316 } 317 318 } 319 } 320 } 321 catch (Exception e) 322 { 323 log.warn("Failed to add creator of type='" + typeName + "', scriptName=" + scriptName + ": ", e); 324 } 325 params = null; 326 scriptName = null; 327 typeName = null; 328 filters = null; 329 break; 330 331 case STATE_ALLOW_FILTER: 332 try 333 { 334 Class impl = LocalUtil.classForName(filterClassName); 335 AjaxFilter object = (AjaxFilter) impl.newInstance(); 336 337 if (params != null) { 338 LocalUtil.setParams(object, params, Collections.EMPTY_LIST); 339 } 340 341 ajaxFilterManager.addAjaxFilter(object); 342 } 343 catch (ClassCastException ex) 344 { 345 log.error(filterClassName + " does not implement " + AjaxFilter.class.getName(), ex); 346 } 347 catch (NoClassDefFoundError ex) 348 { 349 log.info("Missing class for filter (class='" + filterClassName + "'). Cause: " + ex.getMessage()); 350 } 351 catch (Exception ex) 352 { 353 log.error("Failed to add filter: class=" + filterClassName, ex); 354 } 355 356 params = null; 357 filterClassName = null; 358 359 break; 360 361 case STATE_SIGNATURE: 362 if (signature != null && signature.length() > 0) 363 { 364 SignatureParser sigp = new SignatureParser(converterManager, creatorManager); 365 sigp.parse(signature.toString()); 366 } 367 break; 368 369 default: 370 break; 371 } 372 } 373 374 377 public void configure(Container container) 378 { 379 converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName()); 380 ajaxFilterManager = (AjaxFilterManager) container.getBean(AjaxFilterManager.class.getName()); 381 accessControl = (AccessControl) container.getBean(AccessControl.class.getName()); 382 creatorManager = (CreatorManager) container.getBean(CreatorManager.class.getName()); 383 384 configure(); 385 386 setState(STATE_COMPLETE); 387 } 388 389 392 private String typeName = null; 393 394 397 private String scriptName = null; 398 399 402 private String filterClassName = null; 403 404 407 private String converter = null; 408 409 412 private String match = null; 413 414 417 private Map params = null; 418 419 422 private List filters = null; 423 424 427 private StringBuffer signature = null; 428 429 432 private int state = -1; 433 434 437 private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap ()); 438 439 442 private AjaxFilterManager ajaxFilterManager = null; 443 444 447 private ConverterManager converterManager = null; 448 449 452 private AccessControl accessControl = null; 453 454 457 private CreatorManager creatorManager = null; 458 459 462 private static final int STATE_INIT_CREATE = 0; 463 464 467 private static final int STATE_INIT_CONVERT = 1; 468 469 472 private static final int STATE_ALLOW_CREATE = 2; 473 474 477 private static final int STATE_ALLOW_FILTER = 3; 478 479 482 private static final int STATE_ALLOW_CONVERT = 4; 483 484 487 private static final int STATE_SIGNATURE = 5; 488 489 492 private static final int STATE_COMPLETE = 6; 493 494 497 private static final Logger log = Logger.getLogger(FluentConfigurator.class); 498 } 499
| Popular Tags
|