1 19 package org.openharmonise.rm.commands; 20 21 import java.util.*; 22 import java.util.logging.*; 23 24 import org.openharmonise.commons.dsi.AbstractDataStoreInterface; 25 import org.openharmonise.rm.*; 26 import org.openharmonise.rm.config.ConfigSettings; 27 import org.openharmonise.rm.factory.*; 28 import org.openharmonise.rm.publishing.*; 29 import org.openharmonise.rm.resources.AbstractObject; 30 import org.openharmonise.rm.resources.publishing.*; 31 import org.w3c.dom.*; 32 33 34 43 public class CommandProcessHandler implements Publishable { 44 45 49 public static final String TAG_WORKFLOW = "WorkflowEvent"; 50 51 54 public static final String JMP_CMD = "Jmp"; 55 56 59 public static final String JMPIF_CMD = "JmpIf"; 60 61 64 public static final String STOP_CMD = "Stop"; 65 66 72 public static final String COMMANDS_NAMESPACE = 73 AbstractCmd.class.getPackage().getName() + ".Cmd"; 74 75 79 public static final String PROJECT_COMMAND = "PROJECT_CMD_"; 80 81 84 private Element m_workflow_output; 85 86 89 private HarmoniseOutput m_workflow_output_document; 90 91 94 private AbstractDataStoreInterface m_dsi; 95 96 99 private State m_state = null; 100 101 104 private List m_cmd_names; 105 106 109 private List m_cmd_parameters; 110 111 114 private static final Logger m_logger = Logger.getLogger(CommandProcessHandler.class.getName()); 115 116 122 public CommandProcessHandler(AbstractDataStoreInterface dsi) { 123 m_dsi = dsi; 124 } 125 126 131 public State getState() { 132 return m_state; 133 } 134 135 140 public org.w3c.dom.Document getXMLDocument() { 141 return m_workflow_output_document; 142 } 143 144 145 148 public Element publish(Template template, HarmoniseOutput output, State state) 149 throws PublishException { 150 Element resultEl = null; 151 152 try { 153 resultEl = 154 publish(template.getTemplateRootElement(), output, state); 155 } catch (DataAccessException e) { 156 throw new PublishException(e.getLocalizedMessage()); 157 } 158 159 return resultEl; 160 } 161 162 165 public Element publish(Element topEl, HarmoniseOutput output, State state) 166 throws PublishException { 167 if (topEl 168 .getTagName() 169 .equalsIgnoreCase(CommandProcessHandler.TAG_WORKFLOW) 170 == false) { 171 throw new InvalidXMLElementException("Workflow tag needed"); 172 } 173 174 Element returnEl = null; 175 176 List wrkflowObjs; 177 try { 178 wrkflowObjs = 179 HarmoniseObjectFactory.instantiateWorkflowObjects( 180 this.m_dsi, 181 topEl, 182 state); 183 } catch (HarmoniseFactoryException e) { 184 throw new PublishException( 185 "Error occured getting objects from factory", 186 e); 187 } 188 189 m_workflow_output_document = output; 190 m_state = state; 191 m_cmd_names = new ArrayList(); 192 m_cmd_parameters = new ArrayList(); 193 194 m_workflow_output = 195 output.createElement(CommandProcessHandler.TAG_WORKFLOW); 196 197 NodeList event_nodes = 198 topEl.getElementsByTagName(AbstractCmd.TAG_COMMAND); 199 200 for (int i = 0; i < event_nodes.getLength(); i++) { 201 Element this_workflow_element = (Element) event_nodes.item(i); 203 204 String cmd_name = 205 this_workflow_element.getAttribute(AbstractObject.ATTRIB_NAME); 206 207 if ((cmd_name == null) || (cmd_name.length() == 0)) { 208 throw new InvalidXMLElementException( 209 "Command names for workflow events must be given in the " 210 + AbstractObject.ATTRIB_NAME 211 + " tag of the " 212 + AbstractCmd.TAG_COMMAND 213 + " element. " 214 + " cmd_name:" 215 + cmd_name); 216 } 217 218 m_cmd_names.add(cmd_name); 219 220 m_cmd_parameters.add( 221 getWorkflowParameters(this_workflow_element.getChildNodes())); 222 } 223 224 Iterator iter = wrkflowObjs.iterator(); 225 226 while (iter.hasNext()) { 227 Object wf_obj = (Object ) iter.next(); 228 executeCommands(wf_obj); 229 } 230 231 return m_workflow_output; 232 } 233 234 235 238 public void populate(Element xmlElement, State state) 239 throws PopulateException { 240 242 } 243 244 247 public String getTagName() { 248 return TAG_WORKFLOW; 249 } 250 251 252 255 256 264 protected void executeCommands(Object commandObject) throws PublishException { 265 266 boolean cmd_successful = true; 267 String cmd_name; 268 int cmd_execution_point = 0; 269 270 Context context = new Context(); 271 272 while (cmd_execution_point < m_cmd_parameters.size()) { 273 cmd_name = (String ) (m_cmd_names.get(cmd_execution_point)); 275 276 Map cmd_parameter_map; 278 279 if (m_cmd_parameters.size() >= cmd_execution_point) { 280 cmd_parameter_map = 281 (Map) m_cmd_parameters.get(cmd_execution_point); 282 } else { 283 cmd_parameter_map = null; 284 } 285 286 cmd_execution_point++; 287 288 if (cmd_name.startsWith("-")) { 290 String scmd_name = cmd_name.substring(1); 292 int param_at_idx = scmd_name.indexOf('-'); 293 294 String sparam_value; 295 296 if (param_at_idx != -1) { 297 scmd_name = cmd_name.substring(1, param_at_idx + 1); 298 sparam_value = cmd_name.substring(param_at_idx + 2); 300 } else { 301 sparam_value = ""; 302 } 303 304 if (scmd_name.equals(JMP_CMD) 305 || (scmd_name.equals(JMPIF_CMD) && cmd_successful)) { 306 307 cmd_execution_point += getNumberOfCommandsToSkip(sparam_value); 308 309 continue; 310 } else if ((scmd_name.equals(JMPIF_CMD) && !cmd_successful)) { 311 312 continue; 313 } else if (scmd_name.equals(STOP_CMD)) { 314 315 break; 316 } 317 } 318 319 String classname; 321 String proj_cmd = PROJECT_COMMAND + cmd_name; 322 323 try { 324 classname = ConfigSettings.getProperty(proj_cmd); 325 } catch (Exception e) { 326 classname = COMMANDS_NAMESPACE + cmd_name; 327 } 328 329 if(classname == null) { 330 classname = COMMANDS_NAMESPACE + cmd_name; 331 } 332 333 AbstractCmd cmd = null; 334 335 try { 336 Class cls = Class.forName(classname); 337 cmd = (AbstractCmd) cls.newInstance(); 338 } catch (InstantiationException e) { 339 throw new PublishException( 340 "Cannot instantiate command class " + classname, 341 e); 342 } catch (ClassNotFoundException e) { 343 throw new PublishException( 344 cmd_name 345 + " is not a valid workflow command, expect command class " 346 + classname, 347 e); 348 } catch (IllegalAccessException e) { 349 throw new PublishException("Illegal access to class", e); 350 } 351 352 if (cmd.isValidCommandObject(commandObject) == true) { 353 354 if (commandObject != null) { 355 cmd.setCommandObject(commandObject); 356 } 357 358 cmd.setParameters(cmd_parameter_map); 359 cmd.setDataStoreInteface(m_dsi); 360 361 if (m_state != null) { 362 cmd.setState(m_state); 363 } 364 365 Object cmdResult = null; 366 try { 367 cmdResult = cmd.execute(context); 368 } catch (CommandException e) { 369 throw new PublishException("Error executing command", e); 370 } 371 372 String sTemplateId = 373 cmd.getParameter(AbstractCmd.PARAM_TEMPLATE_ID); 374 int nTemplateId = -1; 375 376 if (sTemplateId != null) { 377 378 nTemplateId = Integer.parseInt(sTemplateId); 379 if (nTemplateId > 0) { 380 try { 381 Template template = (Template) HarmoniseObjectFactory.instantiateHarmoniseObject( 382 m_dsi, 383 Template.class.getName(), 384 nTemplateId); 385 386 if (cmdResult instanceof Publishable) { 387 Publishable pObj = (Publishable) cmdResult; 388 389 addResult(pObj.publish(template, m_workflow_output_document, m_state)); 391 392 393 } else if (cmdResult instanceof Collection) { 394 Collection collObj = (Collection) cmdResult; 395 396 Iterator iter = collObj.iterator(); 397 398 while (iter.hasNext()) { 399 Object obj = iter.next(); 400 401 if (obj instanceof Publishable) { 402 Publishable pObj = (Publishable) obj; 403 addResult(pObj.publish(template, m_workflow_output_document, m_state)); 404 405 } 406 } 407 } 408 } catch (HarmoniseFactoryException e) { 409 throw new PublishException("Error getting template from Factory",e); 410 } 411 } 412 } 413 String out_text = cmd.getParameter(AbstractCmd.PARAM_OUT_TEXT); 414 addSimpleTextElement(WebPage.TAG_ANCILLARY_TEXT, out_text); 415 } 416 } 417 418 } 419 420 427 protected void addResult(Element result_root) { 428 m_workflow_output.appendChild( 429 m_workflow_output_document.importNode((Node) result_root, true)); 430 } 431 432 440 protected Map getWorkflowParameters(NodeList param_nodes) { 441 HashMap cmd_params = new HashMap(); 443 444 for (int j = 0; j < param_nodes.getLength(); j++) { 445 if (param_nodes.item(j).getNodeType() != Node.ELEMENT_NODE) { 446 continue; 447 } 448 449 Element current_element = (Element) param_nodes.item(j); 450 451 if (!current_element 452 .getTagName() 453 .equals(AbstractCmd.TAG_COMMAND_PARAM)) { 454 throw new RuntimeException ( 455 "Workflow event must only contain tags named:" 456 + AbstractCmd.TAG_COMMAND_PARAM 457 + " the node:" 458 + current_element 459 + " is not allowed "); 460 } 461 462 String param_name = 463 current_element.getAttribute(AbstractObject.ATTRIB_NAME); 464 465 Text txt_node = (Text) current_element.getFirstChild(); 466 String param_value = ""; 467 468 if (txt_node != null) { 469 param_value = txt_node.getNodeValue(); 470 } 471 472 Vector existing_values = (Vector) cmd_params.get(param_name); 474 475 if (existing_values == null) { 476 existing_values = new Vector(); 477 cmd_params.put(param_name, existing_values); 478 } 479 480 existing_values.addElement(param_value); 481 } 482 483 return cmd_params; 484 } 485 486 487 490 491 498 private void addSimpleTextElement(String tag_name, String tag_txt) { 499 Element simple_elm = m_workflow_output_document.createElement(tag_name); 500 Text simple_text = m_workflow_output_document.createTextNode(tag_txt); 501 502 simple_elm.appendChild(simple_text); 503 504 addResult(simple_elm); 505 } 506 507 514 private int getNumberOfCommandsToSkip(String scmd_param) { 515 int num_to_skip = 0; 516 517 try { 518 num_to_skip = Integer.parseInt(scmd_param); 519 } catch (NumberFormatException e) { 520 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 521 } 522 523 return num_to_skip; 524 } 525 } | Popular Tags |