1 16 17 package org.pentaho.plugin; 18 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.text.MessageFormat ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.activation.DataSource ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.dom4j.Node; 32 import org.pentaho.core.component.IComponent; 33 import org.pentaho.core.repository.IContentItem; 34 import org.pentaho.core.runtime.IActionParameter; 35 import org.pentaho.core.runtime.IRuntimeContext; 36 import org.pentaho.core.runtime.SelectionMapper; 37 import org.pentaho.core.session.IPentahoSession; 38 import org.pentaho.core.solution.IActionResource; 39 import org.pentaho.core.system.PentahoSystem; 40 import org.pentaho.messages.Messages; 41 import org.pentaho.plugin.core.StandardSettings; 42 import org.pentaho.util.VersionHelper; 43 import org.pentaho.util.logging.ILogger; 44 45 public class ComponentSubclassExample extends Object implements IComponent { 46 47 protected int loggingLevel = UNKNOWN; 48 49 public static final String LOGID_MASK1 = "{0}:{1}:{2}: "; 51 public static final String LOGID_MASK2 = "{0}:{1}:{2}:{3} "; 53 public static final String LOGID_SEPARATOR = ":"; 55 public String EMPTYLOGID = "::: "; 57 private String logId = EMPTYLOGID; 58 59 protected static final boolean debug = PentahoSystem.debug; 60 61 private IRuntimeContext runtimeContext; 62 63 private IPentahoSession sessionContext; 64 65 private String processId; 66 67 private String actionName; 68 69 private String instanceId; 70 71 private String id; 72 73 private boolean baseInitOk; 74 75 private boolean componentInitOk; 76 77 private Node componentDefinition; 78 79 private HashMap settings; 80 81 protected boolean executeAction() throws Throwable { 82 return false; 84 } 85 86 public boolean validateAction() { 87 return false; 89 } 90 91 protected boolean validateSystemSettings() { 92 return false; 94 } 95 96 public void done() { 97 } 99 100 public boolean init() { 101 return false; 103 } 104 105 public void setLogId(String lId) { 106 logId = lId; 107 } 108 109 public Log getLogger() { 110 return LogFactory.getLog(this.getClass()); 111 } 112 113 public void genLogIdFromSession(IPentahoSession sess) { 114 genLogIdFromInfo(sess.getId() != null ? sess.getId() : "", sess.getProcessId() != null ? sess.getProcessId() : "", sess.getActionName() != null ? sess.getActionName() : "" ); 118 } 119 120 public void genLogIdFromInfo(String sessId, String procId, String actName) { 121 Object [] args = { sessId, procId, actName }; 122 setLogId(MessageFormat.format(LOGID_MASK1, args)); 123 } 124 125 public void genLogIdFromInfo(String sessId, String procId, String actName, String instId) { 126 Object [] args = { sessId, procId, actName, instId }; 127 setLogId(MessageFormat.format(LOGID_MASK2, args)); 128 } 129 130 131 132 public String getObjectName() { 133 return this.getClass().getName(); 134 } 135 136 public int getLoggingLevel() { 137 return loggingLevel; 138 } 139 140 public void setLoggingLevel(int logLevel) { 141 this.loggingLevel = logLevel; 142 } 143 144 private List messages; 145 146 public List getMessages() { 147 return messages; 148 } 149 150 public void setMessages(List messages) { 151 this.messages = messages; 152 } 153 154 public void trace(String message) { 155 if (loggingLevel <= TRACE) { 156 if (messages != null) { 157 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); } 159 getLogger().trace(getLogId() + message); 160 } 161 } 162 163 public void debug(String message) { 164 if (loggingLevel <= DEBUG) { 165 if (messages != null) { 166 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); } 168 getLogger().debug(getLogId() + message); 169 } 170 } 171 172 public void info(String message) { 173 if (loggingLevel <= INFO) { 174 if (messages != null) { 175 messages.add(Messages.getString("Message.USER_INFO", message, getClass().getName())); } 177 getLogger().info(getLogId() + message); 178 } 179 } 180 181 public void warn(String message) { 182 if (loggingLevel <= WARN) { 183 if (messages != null) { 184 messages.add(Messages.getString("Message.USER_WARNING", message, getClass().getName())); } 186 getLogger().warn(getLogId() + message); 187 } 188 } 189 190 public void error(String message) { 191 if (loggingLevel <= ERROR) { 192 if (messages != null) { 193 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); } 195 getLogger().error(getLogId() + message); 196 } 197 } 198 199 public void fatal(String message) { 200 if (loggingLevel <= FATAL) { 201 if (messages != null) { 202 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); } 204 getLogger().fatal(getLogId() + message); 205 } 206 } 207 208 public void trace(String message, Throwable error) { 209 if (loggingLevel <= TRACE) { 210 if (messages != null) { 211 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); } 213 getLogger().trace(getLogId() + message, error); 214 } 215 } 216 217 public void debug(String message, Throwable error) { 218 if (loggingLevel <= DEBUG) { 219 if (messages != null) { 220 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); } 222 getLogger().debug(getLogId() + message, error); 223 } 224 } 225 226 public void info(String message, Throwable error) { 227 if (loggingLevel <= INFO) { 228 if (messages != null) { 229 messages.add(Messages.getString("Message.USER_INFO", message, getClass().getName())); } 231 getLogger().info(getLogId() + message, error); 232 } 233 } 234 235 public void warn(String message, Throwable error) { 236 if (loggingLevel <= WARN) { 237 if (messages != null) { 238 messages.add(Messages.getString("Message.USER_WARNING", message, getClass().getName())); } 240 getLogger().warn(getLogId() + message, error); 241 } 242 } 243 244 public void error(String message, Throwable error) { 245 if (loggingLevel <= ERROR) { 246 if (messages != null) { 247 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); } 249 getLogger().error("Error Start: Pentaho " + VersionHelper.getVersion() + " build " + VersionHelper.getBuild()); getLogger().error(getLogId() + message, error); 251 getLogger().error("Error end:"); } 253 } 254 255 public void fatal(String message, Throwable error) { 256 if (loggingLevel <= FATAL) { 257 if (messages != null) { 258 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); } 260 getLogger().error("Error: Pentaho " + VersionHelper.getVersion() + " build " + VersionHelper.getBuild()); getLogger().fatal(getLogId() + message, error); 262 getLogger().error("Error end:"); } 264 } 265 266 public static String getUserString(String type) { 267 return Messages.getString("Message.USER_" + type); } 269 270 public void setInstanceId( String instanceId ) { 271 this.instanceId = instanceId; 272 } 273 274 public String getInstanceId() { 275 return instanceId; 276 } 277 278 public void setActionName( String actionName ) { 279 this.actionName = actionName; 280 } 281 282 public String getActionName( ) { 283 return actionName; 284 } 285 286 public void setProcessId( String processId ) { 287 this.processId = processId; 288 } 289 290 public String getProcessId( ) { 291 return processId; 292 } 293 294 public void setComponentDefinition( Node componentDefinition ) { 295 this.componentDefinition = componentDefinition; 296 } 297 298 public Node getComponentDefinition() { 299 return componentDefinition; 300 } 301 302 public void setRuntimeContext( IRuntimeContext runtimeContext ) { 303 this.runtimeContext = runtimeContext; 304 } 305 306 public IRuntimeContext getRuntimeContext( ) { 307 return runtimeContext; 308 } 309 310 public void setSession( IPentahoSession session ) { 311 this.sessionContext = session; 312 } 313 314 public IPentahoSession getSession( ) { 315 return sessionContext; 316 } 317 318 protected void saveSetting(String name, Object value) { 319 settings.put(name, value); 320 } 321 322 protected Object getSetting(String name) { 323 return settings.get(name); 324 } 325 326 protected String getStringSetting(String name) { 327 Object value = settings.get(name); 328 if (value == null) { 329 return null; 330 } else if (value instanceof String ) { 331 return (String ) value; 332 } else { 333 return value.toString(); 334 } 335 } 336 337 public String getLogId() { 338 return logId; 339 } 340 341 protected boolean isDefinedInput(String inputName) { 342 343 if (runtimeContext.getInputNames().contains(inputName)) { 344 return true; 345 } else { 346 return getComponentSetting(inputName) != null; 347 } 348 } 349 350 protected boolean isDefinedOutput(String outputName) { 351 return runtimeContext.getOutputNames().contains(outputName); 352 } 353 354 protected boolean isDefinedResource(String resourceName) { 355 return runtimeContext.getResourceNames().contains(resourceName); 356 } 357 358 public final int validate() { 359 360 logId = Messages.getString("Base.CODE_LOG_ID", instanceId, runtimeContext.getHandle(), actionName); if (debug) 362 debug(Messages.getString("Base.DEBUG_VALIDATING_COMPONENT", actionName)); 365 id = Messages.getString("Base.CODE_COMPONENT_ID", processId, actionName); 367 baseInitOk = (instanceId != null && sessionContext != null && processId != null && actionName != null); 369 370 boolean systemSettingsValidate = validateSystemSettings(); 371 372 if (baseInitOk && systemSettingsValidate) { 373 componentInitOk = validateAction(); 374 } 375 if (getInitOk()) { 376 return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK; 377 } 378 return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL; 379 } 380 381 public boolean getInitOk() { 382 return baseInitOk && componentInitOk; 383 } 384 385 protected Set getOutputNames() { 386 return runtimeContext.getOutputNames(); 387 } 388 389 protected Set getInputNames() { 390 return runtimeContext.getInputNames(); 391 } 392 393 protected Set getResourceNames() { 394 return runtimeContext.getResourceNames(); 395 } 396 397 protected boolean feedbackAllowed() { 398 return runtimeContext.feedbackAllowed(); 399 } 400 401 protected IActionResource getResource(String resourceName) { 402 return runtimeContext.getResourceDefintion(resourceName); 403 } 404 405 protected InputStream getResourceInputStream(IActionResource resource) { 406 return runtimeContext.getResourceInputStream(resource); 407 } 408 409 protected InputStream getInputStream(String inputName) { 410 return runtimeContext.getInputStream(inputName); 411 } 412 413 protected int getOutputPreference() { 414 return runtimeContext.getOutputPreference(); 415 } 416 417 protected OutputStream getOutputStream(String outputName, String mimeType, String extension) { 418 return runtimeContext.getOutputStream(outputName, mimeType, extension); 419 } 420 421 protected void audit(String messageType, String message, String value, int duration) { 422 runtimeContext.audit(messageType, message, value, duration); 423 } 424 425 protected boolean getInputBooleanValue(String inputName, boolean defaultValue) { 426 String strValue = getInputStringValue(inputName); 427 if (strValue == null) { 428 return defaultValue; 429 } else if ("true".equalsIgnoreCase(strValue)) { return true; 431 } else if ("false".equalsIgnoreCase(strValue)) { return false; 433 } else { 434 return defaultValue; 435 } 436 437 } 438 439 protected long getInputLongValue(String inputName, long defaultValue) { 440 String strValue = getInputStringValue(inputName); 441 if (strValue == null) { 442 return defaultValue; 443 } 444 try { 445 return Long.parseLong(strValue); 446 } catch (Exception e) { 447 return defaultValue; 448 } 449 450 } 451 452 protected String getInputStringValue(String inputName) { 453 String value = null; 456 if (runtimeContext.getInputNames().contains(inputName)) { 457 value = runtimeContext.getInputParameterStringValue(inputName); 458 } else { 459 Node node = componentDefinition.selectSingleNode(inputName); 461 if (node == null) { 462 return null; 463 } 464 value = node.getText(); 465 } 466 if (value != null) { 467 value = this.applyInputsToFormat(value); 468 } 469 return value; 470 } 471 472 protected Object getInputValue(String inputName) { 473 if (runtimeContext.getInputNames().contains(inputName)) { 476 return runtimeContext.getInputParameterValue(inputName); 477 } 478 Node node = componentDefinition.selectSingleNode(inputName); 480 if (node == null) { 481 return null; 482 } 483 return node.getText(); 484 } 485 486 private String getComponentSetting(String path) { 487 if (runtimeContext.getInputNames().contains(path)) { 490 return runtimeContext.getInputParameterStringValue(path); 491 } 492 Node node = componentDefinition.selectSingleNode(path); 494 if (node == null) { 495 return null; 496 } 497 return node.getText(); 498 } 499 500 public void promptNeeded() { 501 runtimeContext.promptNeeded(); 502 } 503 504 public void promptNow() { 505 runtimeContext.promptNow(); 506 } 507 508 public String getResourceAsString(IActionResource resource) { 509 try { 510 return runtimeContext.getResourceAsString(resource); 511 } catch (Exception e) { 512 return null; 513 } 514 } 515 516 public String getInitFailMessage() { 517 return null; 518 } 519 520 public String createNewInstance(boolean persisted, Map parameters, boolean forceImmediateWrite) { 521 return runtimeContext.createNewInstance(persisted, parameters, forceImmediateWrite); 522 } 523 524 public void inputMissingError(String paramName) { 525 error(Messages.getErrorString("ComponentBase.ERROR_0003_INPUT_PARAM_MISSING", paramName)); } 527 528 public void outputMissingError(String paramName) { 529 error(Messages.getErrorString("ComponentBase.ERROR_0004_OUTPUT_PARAM_MISSING", paramName)); } 531 532 public void resourceMissingError(String paramName) { 533 error(Messages.getErrorString("ComponentBase.ERROR_0005_RESOURCE_PARAM_MISSING", paramName)); } 535 536 public void resourceComponentSettingError(String paramName) { 537 error(Messages.getErrorString("ComponentBase.ERROR_0006_COMPONENT_SETTING_PARAM_MISSING", paramName)); } 539 540 public int execute() { 541 542 String xsl = getComponentSetting("xsl"); if (xsl != null) { 545 runtimeContext.setParameterXsl(xsl); 546 } 547 548 String target = getComponentSetting("target"); if (target != null) { 551 runtimeContext.setParameterTarget(target); 552 } 553 554 if (loggingLevel == UNKNOWN) { 555 warn(Messages.getString("Base.WARNING_LOGGING_LEVEL_UNKNOWN")); loggingLevel = ILogger.DEBUG; 557 } 558 int result = IRuntimeContext.RUNTIME_STATUS_FAILURE; 559 560 if (sessionContext == null) { 561 error(Messages.getErrorString("Base.ERROR_0001_INVALID_SESSION")); return result; 563 } 564 565 if (debug) 566 debug(Messages.getString("Base.DEBUG_VALIDATION_RESULT") + getInitOk()); if (!getInitOk()) { 568 return result; 569 } 570 571 try { 572 result = (executeAction() ? IRuntimeContext.RUNTIME_STATUS_SUCCESS : IRuntimeContext.RUNTIME_STATUS_FAILURE); 573 if (result == IRuntimeContext.RUNTIME_STATUS_SUCCESS && runtimeContext.isPromptPending()) { 574 if (isDefinedInput(StandardSettings.HANDLE_ALL_PROMPTS)) { 576 runtimeContext.promptNow(); 577 } 578 } 579 } catch (Throwable e) { 580 error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED"), e); } 582 return result; 583 } 584 585 public String getId() { 586 return id; 587 } 588 589 public String getActionTitle() { 590 return runtimeContext.getActionTitle(); 591 } 592 593 protected IContentItem getOutputContentItem() { 594 return runtimeContext.getOutputContentItem(); 595 } 596 597 protected IContentItem getOutputContentItem(String outputName) { 598 return runtimeContext.getOutputContentItem(outputName); 599 } 600 601 protected void setOutputValue(String outputName, Object value) { 602 runtimeContext.setOutputValue(outputName, value); 603 } 604 605 protected OutputStream getDefaultOutputStream() { 606 IContentItem contentItem = runtimeContext.getOutputContentItem(); 607 if (contentItem != null) { 608 try { 609 return contentItem.getOutputStream(getActionName()); 610 } catch (Exception e) { 611 } 612 } 613 return null; 614 } 615 616 protected String applyInputsToFormat(String format) { 617 return runtimeContext.applyInputsToFormat(format); 618 } 619 620 protected IActionParameter getOutputItem(String outputName) { 621 return runtimeContext.getOutputParameter(outputName); 622 } 623 624 protected String getSolutionName() { 625 return runtimeContext.getSolutionName(); 626 } 627 628 protected String getSolutionPath() { 629 return runtimeContext.getSolutionPath(); 630 } 631 632 protected IActionParameter getInputParameter(String parameterName) { 633 return runtimeContext.getInputParameter(parameterName); 634 } 635 636 protected String getContentUrl(IContentItem contentItem) { 637 return runtimeContext.getContentUrl(contentItem); 638 } 639 640 protected boolean isPromptPending() { 641 return runtimeContext.isPromptPending(); 642 } 643 644 protected void setFeedbackMimeType(String mimeType) { 645 IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem(); 646 feedbackContentItem.setMimeType(mimeType); 647 } 648 649 protected void setOutputMimeType(String mimeType) { 650 IContentItem outputContentItem = runtimeContext.getOutputContentItem(); 651 outputContentItem.setMimeType(mimeType); 652 } 653 654 protected OutputStream getFeedbackOutputStream() { 655 IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem(); 656 if (feedbackContentItem != null) { 657 try { 658 return feedbackContentItem.getOutputStream(getActionName()); 659 } catch (Exception e) { 660 } 661 } 662 return null; 663 } 664 665 protected void createFeedbackParameter(IActionParameter actionParam) { 666 runtimeContext.createFeedbackParameter(actionParam); 667 runtimeContext.promptNeeded(); 668 } 669 670 protected void createFeedbackParameter(SelectionMapper selMap, String fieldName, Object defaultValues) { 671 runtimeContext.createFeedbackParameter(selMap, fieldName, defaultValues); 672 runtimeContext.promptNeeded(); 673 } 674 675 protected void createFeedbackParameter(String fieldName, String displayName, String hint, String defaultValue, boolean visible) { 676 runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValue, visible); 677 runtimeContext.promptNeeded(); 678 } 679 680 public void createFeedbackParameter(String fieldName, String displayName, String hint, Object defaultValues, List values, Map dispNames, String displayStyle) { 681 runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValues, values, dispNames, displayStyle); 682 runtimeContext.promptNeeded(); 683 } 684 685 protected DataSource getDataSource(String parameterName) { 686 return runtimeContext.getDataSource(parameterName); 687 } 688 689 protected DataSource getResourceDataSource(IActionResource resource) { 690 return runtimeContext.getResourceDataSource(resource); 691 } 692 693 } 694
| Popular Tags
|