1 18 19 package org.apache.struts.tiles; 20 21 import java.io.Serializable ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.struts.tiles.xmlDefinition.XmlDefinition; 28 import org.apache.struts.util.RequestUtils; 29 30 35 public class ComponentDefinition implements Serializable { 36 37 40 protected static Log log = LogFactory.getLog(ComponentDefinition.class); 41 42 45 protected String name = null; 46 47 50 protected String path = null; 51 52 55 protected Map attributes = null; 56 57 60 protected String role = null; 61 62 63 protected String controller = null; 64 65 69 protected String controllerType = null; 70 71 74 public static final String URL = "url"; 75 76 79 public static final String CONTROLLER = "controller"; 80 81 84 public static final String ACTION = "action"; 85 86 90 private Controller controllerInstance = null; 91 92 95 public ComponentDefinition() { 96 attributes = new HashMap (); 97 } 98 99 105 public ComponentDefinition(ComponentDefinition definition) { 106 attributes = new HashMap (definition.getAttributes()); 107 this.name = definition.getName(); 108 this.path = definition.getPath(); 109 this.role = definition.getRole(); 110 this.controllerInstance = definition.getControllerInstance(); 111 this.controller = definition.getController(); 112 this.controllerType = definition.getControllerType(); 113 } 114 115 129 public ComponentDefinition(XmlDefinition definition) { 130 131 this((ComponentDefinition) definition); 132 } 133 134 137 public ComponentDefinition(String name, String path, Map attributes) { 138 this.name = name; 139 this.path = path; 140 this.attributes = attributes; 141 } 142 143 148 public String getName() { 149 return name; 150 } 151 152 157 public void setName(String aName) { 158 name = aName; 159 } 160 161 166 public String getPage() { 167 return path; 168 } 169 170 175 public void setPage(String page) { 176 path = page; 177 } 178 179 184 public String getPath() { 185 return path; 186 } 187 188 193 public void setPath(String aPath) { 194 path = aPath; 195 } 196 197 202 public String getTemplate() { 203 return path; 204 } 205 206 212 public void setTemplate(String template) { 213 path = template; 214 } 215 216 220 public String getRole() { 221 return role; 222 } 223 224 229 public void setRole(String role) { 230 this.role = role; 231 } 232 233 238 public Map getAttributes() { 239 return attributes; 240 } 241 242 248 public Object getAttribute(String key) { 249 return attributes.get(key); 250 } 251 252 258 public void putAttribute(String key, Object value) { 259 attributes.put(key, value); 260 } 261 262 268 public void put(String name, Object content) { 269 put(name, content, false, null); 270 } 271 272 279 public void put(String name, Object content, boolean direct) { 280 put(name, content, direct, null); 281 } 282 283 291 public void put(String name, Object content, boolean direct, String role) { 292 if (direct == true) { put(name, content, "string", role); 294 } else { 295 put(name, content, "template", role); 296 } 297 298 } 299 300 308 public void put(String name, Object content, String type, String role) { 309 AttributeDefinition attribute = null; 313 314 if (content != null 315 && type != null 316 && !(content instanceof AttributeDefinition)) { 317 318 String strValue = content.toString(); 319 if (type.equalsIgnoreCase("string")) { 320 attribute = new DirectStringAttribute(strValue); 321 322 } else if (type.equalsIgnoreCase("page")) { 323 attribute = new PathAttribute(strValue); 324 325 } else if (type.equalsIgnoreCase("template")) { 326 attribute = new PathAttribute(strValue); 327 328 } else if (type.equalsIgnoreCase("instance")) { 329 attribute = new DefinitionNameAttribute(strValue); 330 331 } else if (type.equalsIgnoreCase("definition")) { 332 attribute = new DefinitionNameAttribute(strValue); 333 } 334 } 335 336 putAttribute(name, attribute); 337 } 338 339 342 public String toString() { 343 return "{name=" 344 + name 345 + ", path=" 346 + path 347 + ", role=" 348 + role 349 + ", controller=" 350 + controller 351 + ", controllerType=" 352 + controllerType 353 + ", controllerInstance=" 354 + controllerInstance 355 + ", attributes=" 356 + attributes 357 + "}\n"; 358 } 359 360 364 public String getControllerType() { 365 return controllerType; 366 } 367 368 373 public void setControllerType(String controllerType) { 374 this.controllerType = controllerType; 375 } 376 377 384 public void setControllerUrl(String controller) { 385 setController(controller); 386 setControllerType("url"); 387 } 388 389 396 public void setControllerClass(String controller) { 397 setController(controller); 398 setControllerType("classname"); 399 } 400 401 406 public String getController() { 407 return controller; 408 } 409 410 416 public void setController(String url) { 417 this.controller = url; 418 } 419 420 424 public Controller getControllerInstance() { 425 return controllerInstance; 426 } 427 428 436 public Controller getOrCreateController() throws InstantiationException { 437 438 if (controllerInstance != null) { 439 return controllerInstance; 440 } 441 442 if (controller == null && controllerType == null) { 444 return null; 445 } 446 447 if (controllerType != null && controller == null) { 449 throw new InstantiationException ("Controller name should be defined if controllerType is set"); 450 } 451 452 controllerInstance = createController(controller, controllerType); 453 454 return controllerInstance; 455 } 456 457 460 public void setControllerInstance(Controller controller) { 461 this.controllerInstance = controller; 462 } 463 464 478 public static Controller createController(String name, String controllerType) 479 throws InstantiationException { 480 481 if (log.isDebugEnabled()) { 482 log.debug("Create controller name=" + name + ", type=" + controllerType); 483 } 484 485 Controller controller = null; 486 487 if (controllerType == null) { try { 489 return createControllerFromClassname(name); 490 491 } catch (InstantiationException ex) { controller = new UrlController(name); 493 } 494 495 } else if ("url".equalsIgnoreCase(controllerType)) { 496 controller = new UrlController(name); 497 498 } else if ("classname".equalsIgnoreCase(controllerType)) { 499 controller = createControllerFromClassname(name); 500 } 501 502 return controller; 503 } 504 505 513 public static Controller createControllerFromClassname(String classname) 514 throws InstantiationException { 515 516 try { 517 Class requestedClass = RequestUtils.applicationClass(classname); 518 Object instance = requestedClass.newInstance(); 519 520 if (log.isDebugEnabled()) { 521 log.debug("Controller created : " + instance); 522 } 523 return (Controller) instance; 524 525 } catch (java.lang.ClassNotFoundException ex) { 526 throw new InstantiationException ( 527 "Error - Class not found :" + ex.getMessage()); 528 529 } catch (java.lang.IllegalAccessException ex) { 530 throw new InstantiationException ( 531 "Error - Illegal class access :" + ex.getMessage()); 532 533 } catch (java.lang.InstantiationException ex) { 534 throw ex; 535 536 } catch (java.lang.ClassCastException ex) { 537 throw new InstantiationException ( 538 "Controller of class '" 539 + classname 540 + "' should implements 'Controller' or extends 'Action'"); 541 } 542 } 543 544 } 545 | Popular Tags |