1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.web; import java.io.File; import java.util.Enumeration; import java.util.Hashtable; import net.jot.web.ctrl.JOTController; import net.jot.web.flowparams.JOTParamBase; import net.jot.web.forms.JOTForm; import net.jot.web.view.JOTView; /** * Object representation of the flow config file: flow.conf * Also provide validation methods to cross-check the flow.conf. * @author thibautc */ public class JOTFlowConfig { private String errorRequest=null; private String notFoundRequest=null; private String forbiddenRequest=null; /** * Usually only one template root, but can be multiples to keep searchign through several until object is found. */ private String[] templateRoots=null; private String flowExtension=".do"; private Hashtable requests=new Hashtable(); private Hashtable actions=new Hashtable(); private Hashtable bundles=new Hashtable(); private Hashtable forms=new Hashtable(); private String error="Parsing error."; private boolean valid=false; private String confPath; /** * Returns wether the parsing of flow.conf fails * Note: this does not run the extra validations in runValidation(), so usually would be called after runValidation() * @return */ public boolean isValid() { return valid; } public String getError() { return error; } public void setError(String error) { this.error = error; } public void setValid(boolean valid) { this.valid = valid; } public String getNotFoundRequest() { return notFoundRequest; } public void setNotFoundRequest(String request) { notFoundRequest=request; } /** * Adds a directive found in flow.conf * @param directive * @throws java.lang.Exception */ public void addDirective(JOTFlowDirective directive) throws Exception { if(directive.getType()==JOTFlowDirective.DIRECTIVE_REQUEST) { String name=directive.getArgs()[0]; if(requests.get(name)!=null) { throw new Exception("Duplicated Request name '"+name+"' in flow.conf"); } requests.put(name,directive); } else if(directive.getType()==JOTFlowDirective.DIRECTIVE_CONTROLLER) { String name=directive.getArgs()[0]; if(actions.get(name)!=null || bundles.get(name)!=null ) { throw new Exception("Duplicated Action/ActionBundle name '"+name+"' in flow.conf"); } actions.put(name,directive); } else if(directive.getType()==JOTFlowDirective.DIRECTIVE_CONTROLLER_BUNDLE) { String name=directive.getArgs()[0]; if(actions.get(name)!=null || bundles.get(name)!=null) { throw new Exception("Duplicated Controller name '"+name+"' in flow.conf"); } bundles.put(name,directive); } /*else if(directive.getType()==JOTFlowDirective.DIRECTIVE_FORM) { String name=directive.getArgs()[0]; String code=directive.getArgs()[1]; if(forms.get(name)!=null) { throw new Exception("Duplicated Form name '"+name+"' in flow.conf"); } forms.put(name,code); }*/ else { throw new Exception("Directive as no type ??"); } } public void setErrorRequest(String request) { errorRequest=request; } public String getErrorRequest() { return errorRequest; } public String getFlowExtension() { return flowExtension; } public void setFlowExtension(String flowExtension) { this.flowExtension = flowExtension; } public String getForbiddenRequest() { return forbiddenRequest; } public void setForbiddenRequest(String request) { forbiddenRequest=request; } public String[] getTemplateRoots() { return templateRoots; } public void setTemplateRoots(String templateRoots[]) { this.templateRoots = templateRoots; } public void setTemplateRoot(String templateRoot) { templateRoots=new String[1]; templateRoots[0] = templateRoot; } public void setConfigPath(String confPath) { this.confPath=confPath; } public String getConfigPath() { return confPath; } public Hashtable getActions() { return actions; } public Hashtable getBundles() { return bundles; } public Hashtable getRequests() { return requests; } public Hashtable getForms() { return forms; } /** * Runs a validation of the loaded flow.conf, calls setValidationError if an error is found * Checks that view/controller/forms classes exists and are of right type etc... */ public void runValidation() { // if parsing the file failed, no need to validate any further. if(isValid()) { //default pages if(errorRequest!=null && ! requests.containsKey(errorRequest)) { setValidationError("The default error request is not defined in flow.conf: "+errorRequest); return; } if(notFoundRequest!=null && ! requests.containsKey(notFoundRequest)) { setValidationError("The default 404 not found request is not defined in flow.conf: "+notFoundRequest); return; } if(forbiddenRequest!=null && ! requests.containsKey(forbiddenRequest)) { setValidationError("The default forbidden request is not defined in flow.conf: "+forbiddenRequest); return; } //templateRoot for(int i=0;i!=templateRoots.length;i++) { if(templateRoots[i]!=null && !new File(templateRoots[i]).exists()) { setValidationError("The provided template root does not exist: "+templateRoots[i]); return; } } //actions Enumeration keys=actions.keys(); while(keys.hasMoreElements()) { String key=(String)keys.nextElement(); if(!validateDirective((JOTFlowDirective)actions.get(key))) return; } //requests keys=requests.keys(); while(keys.hasMoreElements()) { String key=(String)keys.nextElement(); if(!validateDirective((JOTFlowDirective)requests.get(key))) return; } //bundles keys=bundles.keys(); while(keys.hasMoreElements()) { String key=(String)keys.nextElement(); if(!validateDirective((JOTFlowDirective)bundles.get(key))) return; } //forms keys=forms.keys(); while(keys.hasMoreElements()) { String key=(String)keys.nextElement(); if(!validateDirective((JOTFlowDirective)forms.get(key))) return; } } } /** * Validates a particular directive * @param dir * @return */ private boolean validateDirective(JOTFlowDirective dir) { if(dir.getType()==JOTFlowDirective.DIRECTIVE_CONTROLLER && !validateClass(dir.getArgs()[1],JOTController.class)) { setValidationError("Flow.conf("+dir.getLineNumber()+") error. Invalid action class not found: "+dir.getArgs()[1]); return false; } for(int i=0;i!=dir.size();i++) { JOTParamBase param=(JOTParamBase)dir.get(i); if(param.getType()==JOTParamBase.FLOW_PARAM_CALL && (!actions.containsKey(param.getParams()[0]) && !bundles.containsKey(param.getParams()[0]))) { setValidationError("Flow.conf("+param.getLineNumber()+") error. Call action/bundle not found: "+param.getParams()[0]); return false; } if(param.getType()==JOTParamBase.FLOW_PARAM_CONTINUE_TO && !requests.containsKey("/"+param.getParams()[0])) { setValidationError("Flow.conf("+param.getLineNumber()+") error. ContinueTo not found: /"+param.getParams()[0]); return false; } if(param.getType()==JOTParamBase.FLOW_PARAM_PROCESS_FORM && !validateClass(param.getParams()[0],JOTForm.class)) { setValidationError("Flow.conf("+param.getLineNumber()+") error. Invalid ProcessForm class: /"+param.getParams()[0]); return false; } if(param.getType()==JOTParamBase.FLOW_PARAM_RENDER_PAGE) { if(param.getParams().length>1) { if(!validateClass(param.getParams()[0], JOTView.class)) { setValidationError("Flow.conf("+param.getLineNumber()+") error. Invalid RenderPage class: "+param.getParams()[0]); return false; } } //check that the page exists ?? -> no. } } return true; } /** * Sets a validation error message (can be retrieved by getError()) and setValid(false); * @param error */ private void setValidationError(String error) { setError(error); setValid(false); } /** * Validates that a class can be loaded (forName) and that it's an instance of classType(if classType!=null) * @param code * @param classType * @return */ private boolean validateClass(String code, Class classType) { boolean result=false; if(code!=null) { try { Class c=Class.forName(code); if(c!=null) { return classType==null || (classType.isInstance(c.newInstance())); } } catch(Exception e) { } } return result; } }