1 package org.ocltf.common; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.Map; 8 9 import org.apache.commons.lang.builder.ToStringBuilder; 10 import org.ocltf.utils.ExceptionUtils; 11 12 /*** 13 * A configurable context object. These are passed to Plugin 14 * instances (Cartridges, Libraries, etc.). 15 * 16 * @author Chad Brandon 17 */ 18 public class Context { 19 20 private String name; 21 private Map properties; 22 private Collection initCollection = new ArrayList(); 23 24 /*** 25 * This method normally be unnecessary. It is here because of the way Ant behaves. 26 * Ant calls addProperty() before the Property javabean is fully 27 * initialized (therefore the 'name' isn't set). So we kept the javabeans in an 28 * ArrayList that we have to copy into the properties Map. 29 */ 30 public void init() { 31 if (this.properties == null) { 32 this.properties = new HashMap(); 33 for (Iterator iter = initCollection.iterator(); iter.hasNext();) { 34 Property property = (Property)iter.next(); 35 this.properties.put(property.getName(), property); 36 } 37 } 38 } 39 40 /*** 41 * Returns name of this Context. Will correspond 42 * to a Plugin name (or it can be be 'default' 43 * if we want it's settings to be used everywhere). 44 * 45 * @return String 46 */ 47 public String getName() { 48 this.init(); 49 return name; 50 } 51 52 /*** 53 * Sets the name of this Context. 54 * 55 * @param name The name to set 56 */ 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 /*** 62 * Adds a property to this Context object. A property 63 * must correspond to a java bean property name on a 64 * Plugin in order for it to be set during processing. 65 * Otherwise the property will just be ignored. 66 * 67 * @param property 68 */ 69 public void addProperty(Property property) { 70 String methodName = "addProperty"; 71 ExceptionUtils.checkNull(methodName, "property", property); 72 this.initCollection.add(property); 73 } 74 75 /*** 76 * Retrieves the property with the specified name. 77 * 78 * @param name 79 * 80 * @return Property. 81 */ 82 public Property getProperty(String name) { 83 this.init(); 84 return (Property)this.properties.get(name); 85 } 86 87 /*** 88 * @see java.lang.Object#toString() 89 */ 90 public String toString() { 91 return ToStringBuilder.reflectionToString(this); 92 } 93 94 }