1 package net.sf.invicta.handler; 2 3 import java.util.List ; 4 import java.util.Map ; 5 6 import net.sf.invicta.InvictaException; 7 import net.sf.invicta.Logger; 8 import net.sf.invicta.api.DefinedProperty; 9 import net.sf.invicta.api.InvictaComponent; 10 import net.sf.invicta.api.InvictaProject; 11 12 15 public abstract class InvictaBasicHandler implements InvictaHandler { 16 protected Map paramsMap; 17 protected List params; 18 protected InvictaComponent component; 19 20 24 protected InvictaProject getProject() { 25 return this.component.getProject(); 26 } 27 28 32 protected InvictaComponent getComponent() { 33 return this.component; 34 } 35 36 40 protected int getParametersNumber() { 41 if (this.params != null) { 42 return this.params.size(); 43 } else { 44 return this.paramsMap.size(); 45 } 46 } 47 48 53 protected String getParameter(int index) { 54 if ((this.params == null) || (this.params.size() <= index)) 55 return null; 56 57 return (String )this.params.get(index); 58 } 59 60 65 protected String getParameter(Object key) { 66 if (this.paramsMap == null) 67 return null; 68 69 return (String )this.paramsMap.get(key); 70 } 71 72 78 protected String getParameter(int index, String defaultValue) { 79 String value = getParameter(index); 80 if (value == null) 81 return defaultValue; 82 83 return value; 84 } 85 86 92 protected String getParameter(Object key, String defaultValue) { 93 String value = getParameter(key); 94 if (value == null) 95 return defaultValue; 96 97 return value; 98 } 99 100 106 protected boolean getBooleanParameter(int index, boolean defaultValue) { 107 String value = getParameter(index); 108 if (value == null) 109 return defaultValue; 110 111 return Boolean.valueOf(value).booleanValue(); 112 } 113 114 120 protected boolean getBooleanParameter(Object key, boolean defaultValue) { 121 String value = getParameter(key); 122 if (value == null) 123 return defaultValue; 124 125 return Boolean.valueOf(value).booleanValue(); 126 } 127 128 134 protected String getRequiredParameter(int index) throws InvictaHandlerException { 135 String value = getParameter(index); 136 if (value == null) 137 throw new InvictaHandlerException(this, "Missing required parameter at index " + index); 138 139 return value; 140 } 141 142 148 protected String getRequiredParameter(Object key) throws InvictaHandlerException { 149 String value = getParameter(key); 150 151 if (value == null) 152 throw new InvictaHandlerException(this, "Missing required parameter: '" + key + "'"); 153 154 return value; 155 } 156 157 163 protected String getParameterPropertyValue(Object key) throws InvictaHandlerException { 164 String propertyName = getParameter(key); 165 if (propertyName == null) 166 return null; 167 return getRequiredPropertyValue(propertyName); 168 } 169 170 176 protected String getParameterPropertyValue(int index) throws InvictaHandlerException { 177 String propertyName = getParameter(index); 178 if (propertyName == null) 179 return null; 180 return getRequiredPropertyValue(propertyName); 181 } 182 183 190 protected boolean getParameterPropertyBooleanValue(Object key, boolean defaultValue) throws InvictaHandlerException { 191 String propertyName = getParameter(key); 192 if (propertyName == null) 193 return defaultValue; 194 195 String value = getRequiredPropertyValue(propertyName); 196 return Boolean.valueOf(value).booleanValue(); 197 } 198 199 206 protected boolean getParameterPropertyBooleanValue(int index, boolean defaultValue) throws InvictaHandlerException { 207 String propertyName = getParameter(index); 208 if (propertyName == null) 209 return defaultValue; 210 String value = getRequiredPropertyValue(propertyName); 211 return Boolean.valueOf(value).booleanValue(); 212 } 213 214 220 protected String getRequiredPropertyValue(String propertyName) throws InvictaHandlerException { 221 String value = getComponent().getPropertyValue(propertyName); 222 223 if (value == null) 224 throw new InvictaHandlerException(this, 225 "Property '" + propertyName 226 + "' not defined for component '" 227 + getComponent().getName()); 228 229 return value; 230 } 231 232 238 protected String getRequiredPropertyReference(String propertyName) throws InvictaHandlerException { 239 DefinedProperty property = getComponent().getDefinedProperty(propertyName); 240 241 if (property == null) 242 throw new InvictaHandlerException(this, 243 "Property '" + propertyName 244 + "' not defined for component '" 245 + getComponent().getName()); 246 247 return property.getReferenceValue(); 248 } 249 250 255 protected boolean containsParameter(int index) { 256 return getParameter(index) != null; 257 } 258 259 264 protected boolean containsParameter(Object key) { 265 return getParameter(key) != null; 266 } 267 268 275 public Object handle(Object context, List params) throws InvictaException { 276 277 if (!(context instanceof InvictaComponent)) 278 throw new InvictaHandlerException(this, "Template problem: Wrong component paramteter (Must be InvictaComponent)"); 279 280 this.component = (InvictaComponent)context; 281 282 this.params = params; 283 return handle(params); 284 } 285 286 293 public Object handle(Object context, Map paramsMap) throws InvictaException { 294 295 if (!(context instanceof InvictaComponent)) 296 throw new InvictaHandlerException(this, "Template problem: Wrong component paramteter (Must be InvictaComponent)"); 297 298 this.component = (InvictaComponent)context; 299 this.paramsMap = paramsMap; 300 return handle(paramsMap); 301 } 302 303 309 public String handle(List params) throws InvictaException { 310 throw new InvictaHandlerException(this, "Cannot receive list of parameters."); 311 } 312 313 319 public String handle(Map paramsMap) throws InvictaException { 320 throw new InvictaHandlerException(this, "Cannot receive map of parameters."); 321 } 322 323 327 protected void log(String message) { 328 Logger.info(getName() + ": " + message); 329 } 330 331 335 protected void debug(String message) { 336 Logger.info(getName() + ": " + message); 337 } 338 339 } 340 | Popular Tags |