001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created May 4, 2006
014: * @author James Dixon
015: */
016:
017: package org.pentaho.plugin;
018:
019: import java.io.FileNotFoundException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.text.MessageFormat;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.activation.DataSource;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.dom4j.Element;
033: import org.dom4j.Node;
034: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
035: import org.pentaho.core.component.IComponent;
036: import org.pentaho.core.repository.IContentItem;
037: import org.pentaho.core.runtime.IActionParameter;
038: import org.pentaho.core.runtime.IRuntimeContext;
039: import org.pentaho.core.runtime.SelectionMapper;
040: import org.pentaho.core.session.IPentahoSession;
041: import org.pentaho.core.solution.IActionResource;
042: import org.pentaho.core.system.PentahoSystem;
043: import org.pentaho.messages.Messages;
044: import org.pentaho.plugin.core.StandardSettings;
045: import org.pentaho.util.IVersionHelper;
046: import org.pentaho.util.logging.ILogger;
047:
048: public class ComponentSubclassExample extends Object implements
049: IComponent {
050:
051: protected int loggingLevel = UNKNOWN;
052:
053: public static final String LOGID_MASK1 = "{0}:{1}:{2}: "; //$NON-NLS-1$
054:
055: public static final String LOGID_MASK2 = "{0}:{1}:{2}:{3} "; //$NON-NLS-1$
056:
057: public static final String LOGID_SEPARATOR = ":"; //$NON-NLS-1$
058:
059: public String EMPTYLOGID = "::: "; //$NON-NLS-1$
060:
061: private String logId = EMPTYLOGID;
062:
063: protected static final boolean debug = PentahoSystem.debug;
064:
065: private IRuntimeContext runtimeContext;
066:
067: private IPentahoSession sessionContext;
068:
069: private String processId;
070:
071: private String actionName;
072:
073: private String instanceId;
074:
075: private String id;
076:
077: private boolean baseInitOk;
078:
079: private boolean componentInitOk;
080:
081: private Node componentDefinition;
082:
083: private HashMap settings;
084:
085: private ActionDefinition actionDefinition;
086:
087: protected boolean executeAction() throws Throwable {
088: // TODO add your execute code here
089: return false;
090: }
091:
092: public boolean validateAction() {
093: // TODO add any validation code in here and return true
094: return false;
095: }
096:
097: protected boolean validateSystemSettings() {
098: // TODO add any validation of system settings here and return true
099: return false;
100: }
101:
102: public void done() {
103: // TODO add any cleanup code here
104: }
105:
106: public boolean init() {
107: // TODO add any initialization code and return true
108: return false;
109: }
110:
111: public void setLogId(String lId) {
112: logId = lId;
113: }
114:
115: public Log getLogger() {
116: return LogFactory.getLog(this .getClass());
117: }
118:
119: public void genLogIdFromSession(IPentahoSession sess) {
120: genLogIdFromInfo(sess.getId() != null ? sess.getId() : "", //$NON-NLS-1$
121: sess.getProcessId() != null ? sess.getProcessId() : "", //$NON-NLS-1$
122: sess.getActionName() != null ? sess.getActionName()
123: : "" //$NON-NLS-1$
124: );
125: }
126:
127: public void genLogIdFromInfo(String sessId, String procId,
128: String actName) {
129: Object[] args = { sessId, procId, actName };
130: setLogId(MessageFormat.format(LOGID_MASK1, args));
131: }
132:
133: public void genLogIdFromInfo(String sessId, String procId,
134: String actName, String instId) {
135: Object[] args = { sessId, procId, actName, instId };
136: setLogId(MessageFormat.format(LOGID_MASK2, args));
137: }
138:
139: /* ILogger Implementation */
140:
141: public String getObjectName() {
142: return this .getClass().getName();
143: }
144:
145: public int getLoggingLevel() {
146: return loggingLevel;
147: }
148:
149: public void setLoggingLevel(int logLevel) {
150: this .loggingLevel = logLevel;
151: }
152:
153: private List messages;
154:
155: public List getMessages() {
156: return messages;
157: }
158:
159: public void setMessages(List messages) {
160: this .messages = messages;
161: }
162:
163: public void trace(String message) {
164: if (loggingLevel <= TRACE) {
165: if (messages != null) {
166: messages
167: .add(Messages
168: .getString(
169: "Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
170: }
171: getLogger().trace(getLogId() + message);
172: }
173: }
174:
175: public void debug(String message) {
176: if (loggingLevel <= DEBUG) {
177: if (messages != null) {
178: messages
179: .add(Messages
180: .getString(
181: "Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
182: }
183: getLogger().debug(getLogId() + message);
184: }
185: }
186:
187: public void info(String message) {
188: if (loggingLevel <= INFO) {
189: if (messages != null) {
190: messages
191: .add(Messages
192: .getString(
193: "Message.USER_INFO", message, getClass().getName())); //$NON-NLS-1$
194: }
195: getLogger().info(getLogId() + message);
196: }
197: }
198:
199: public void warn(String message) {
200: if (loggingLevel <= WARN) {
201: if (messages != null) {
202: messages
203: .add(Messages
204: .getString(
205: "Message.USER_WARNING", message, getClass().getName())); //$NON-NLS-1$
206: }
207: getLogger().warn(getLogId() + message);
208: }
209: }
210:
211: public void error(String message) {
212: if (loggingLevel <= ERROR) {
213: if (messages != null) {
214: messages
215: .add(Messages
216: .getString(
217: "Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
218: }
219: getLogger().error(getLogId() + message);
220: }
221: }
222:
223: public void fatal(String message) {
224: if (loggingLevel <= FATAL) {
225: if (messages != null) {
226: messages
227: .add(Messages
228: .getString(
229: "Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
230: }
231: getLogger().fatal(getLogId() + message);
232: }
233: }
234:
235: public void trace(String message, Throwable error) {
236: if (loggingLevel <= TRACE) {
237: if (messages != null) {
238: messages
239: .add(Messages
240: .getString(
241: "Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
242: }
243: getLogger().trace(getLogId() + message, error);
244: }
245: }
246:
247: public void debug(String message, Throwable error) {
248: if (loggingLevel <= DEBUG) {
249: if (messages != null) {
250: messages
251: .add(Messages
252: .getString(
253: "Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
254: }
255: getLogger().debug(getLogId() + message, error);
256: }
257: }
258:
259: public void info(String message, Throwable error) {
260: if (loggingLevel <= INFO) {
261: if (messages != null) {
262: messages
263: .add(Messages
264: .getString(
265: "Message.USER_INFO", message, getClass().getName())); //$NON-NLS-1$
266: }
267: getLogger().info(getLogId() + message, error);
268: }
269: }
270:
271: public void warn(String message, Throwable error) {
272: if (loggingLevel <= WARN) {
273: if (messages != null) {
274: messages
275: .add(Messages
276: .getString(
277: "Message.USER_WARNING", message, getClass().getName())); //$NON-NLS-1$
278: }
279: getLogger().warn(getLogId() + message, error);
280: }
281: }
282:
283: public void error(String message, Throwable error) {
284: if (loggingLevel <= ERROR) {
285: if (messages != null) {
286: messages
287: .add(Messages
288: .getString(
289: "Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
290: }
291: IVersionHelper versionHelper = PentahoSystem
292: .getVersionHelper(null);
293: getLogger()
294: .error(
295: "Error Start: Pentaho " + versionHelper.getVersionInformation(this .getClass())); //$NON-NLS-1$
296: getLogger().error(getLogId() + message, error);
297: getLogger().error("Error end:"); //$NON-NLS-1$
298: }
299: }
300:
301: public void fatal(String message, Throwable error) {
302: if (loggingLevel <= FATAL) {
303: if (messages != null) {
304: messages
305: .add(Messages
306: .getString(
307: "Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
308: }
309: IVersionHelper versionHelper = PentahoSystem
310: .getVersionHelper(null);
311: getLogger()
312: .error(
313: "Error: Pentaho " + versionHelper.getVersionInformation(this .getClass())); //$NON-NLS-1$
314: getLogger().fatal(getLogId() + message, error);
315: getLogger().error("Error end:"); //$NON-NLS-1$
316: }
317: }
318:
319: public static String getUserString(String type) {
320: return Messages.getString("Message.USER_" + type); //$NON-NLS-1$
321: }
322:
323: public void setInstanceId(String instanceId) {
324: this .instanceId = instanceId;
325: }
326:
327: public String getInstanceId() {
328: return instanceId;
329: }
330:
331: public void setActionName(String actionName) {
332: this .actionName = actionName;
333: }
334:
335: public String getActionName() {
336: return actionName;
337: }
338:
339: public void setProcessId(String processId) {
340: this .processId = processId;
341: }
342:
343: public String getProcessId() {
344: return processId;
345: }
346:
347: public void setComponentDefinition(Node componentDefinition) {
348: this .componentDefinition = componentDefinition;
349: }
350:
351: public Node getComponentDefinition() {
352: return componentDefinition;
353: }
354:
355: public void setRuntimeContext(IRuntimeContext runtimeContext) {
356: this .runtimeContext = runtimeContext;
357: }
358:
359: public IRuntimeContext getRuntimeContext() {
360: return runtimeContext;
361: }
362:
363: public void setSession(IPentahoSession session) {
364: this .sessionContext = session;
365: }
366:
367: public IPentahoSession getSession() {
368: return sessionContext;
369: }
370:
371: protected void saveSetting(String name, Object value) {
372: settings.put(name, value);
373: }
374:
375: protected Object getSetting(String name) {
376: return settings.get(name);
377: }
378:
379: protected String getStringSetting(String name) {
380: Object value = settings.get(name);
381: if (value == null) {
382: return null;
383: } else if (value instanceof String) {
384: return (String) value;
385: } else {
386: return value.toString();
387: }
388: }
389:
390: public String getLogId() {
391: return logId;
392: }
393:
394: protected boolean isDefinedInput(String inputName) {
395:
396: if (runtimeContext.getInputNames().contains(inputName)) {
397: return true;
398: } else {
399: return getComponentSetting(inputName) != null;
400: }
401: }
402:
403: protected boolean isDefinedOutput(String outputName) {
404: return runtimeContext.getOutputNames().contains(outputName);
405: }
406:
407: protected boolean isDefinedResource(String resourceName) {
408: return runtimeContext.getResourceNames().contains(resourceName);
409: }
410:
411: public final int validate() {
412:
413: logId = Messages
414: .getString(
415: "Base.CODE_LOG_ID", instanceId, runtimeContext.getHandle(), actionName); //$NON-NLS-1$
416: if (debug)
417: debug(Messages.getString(
418: "Base.DEBUG_VALIDATING_COMPONENT", actionName)); //$NON-NLS-1$
419: // grab the parameters first
420:
421: id = Messages.getString(
422: "Base.CODE_COMPONENT_ID", processId, actionName); //$NON-NLS-1$
423:
424: // now get picky about values
425: baseInitOk = (instanceId != null && sessionContext != null
426: && processId != null && actionName != null);
427:
428: boolean systemSettingsValidate = validateSystemSettings();
429:
430: if (baseInitOk && systemSettingsValidate) {
431: componentInitOk = validateAction();
432: }
433: if (getInitOk()) {
434: return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK;
435: }
436: return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL;
437: }
438:
439: public boolean getInitOk() {
440: return baseInitOk && componentInitOk;
441: }
442:
443: protected Set getOutputNames() {
444: return runtimeContext.getOutputNames();
445: }
446:
447: protected Set getInputNames() {
448: return runtimeContext.getInputNames();
449: }
450:
451: protected Set getResourceNames() {
452: return runtimeContext.getResourceNames();
453: }
454:
455: protected boolean feedbackAllowed() {
456: return runtimeContext.feedbackAllowed();
457: }
458:
459: protected IActionResource getResource(String resourceName) {
460: return runtimeContext.getResourceDefintion(resourceName);
461: }
462:
463: protected InputStream getResourceInputStream(
464: IActionResource resource) throws FileNotFoundException {
465: return runtimeContext.getResourceInputStream(resource);
466: }
467:
468: protected InputStream getInputStream(String inputName) {
469: return runtimeContext.getInputStream(inputName);
470: }
471:
472: protected int getOutputPreference() {
473: return runtimeContext.getOutputPreference();
474: }
475:
476: protected IContentItem getOutputItem(String outputName,
477: String mimeType, String extension) {
478: return runtimeContext.getOutputItem(outputName, mimeType,
479: extension);
480: }
481:
482: protected void audit(String messageType, String message,
483: String value, int duration) {
484: runtimeContext.audit(messageType, message, value, duration);
485: }
486:
487: protected boolean getInputBooleanValue(String inputName,
488: boolean defaultValue) {
489: String strValue = getInputStringValue(inputName);
490: if (strValue == null) {
491: return defaultValue;
492: } else if ("true".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
493: return true;
494: } else if ("false".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
495: return false;
496: } else {
497: return defaultValue;
498: }
499:
500: }
501:
502: protected long getInputLongValue(String inputName, long defaultValue) {
503: String strValue = getInputStringValue(inputName);
504: if (strValue == null) {
505: return defaultValue;
506: }
507: try {
508: return Long.parseLong(strValue);
509: } catch (Exception e) {
510: return defaultValue;
511: }
512:
513: }
514:
515: protected String getInputStringValue(String inputName) {
516: // first check to see if we have an input parameter that we can use for
517: // this.
518: String value = null;
519: if (runtimeContext.getInputNames().contains(inputName)) {
520: value = runtimeContext
521: .getInputParameterStringValue(inputName);
522: } else {
523: // now check the component node from the action definition.
524: Node node = componentDefinition.selectSingleNode(inputName);
525: if (node == null) {
526: return null;
527: }
528: value = node.getText();
529: }
530: if (value != null) {
531: value = this .applyInputsToFormat(value);
532: }
533: return value;
534: }
535:
536: protected Object getInputValue(String inputName) {
537: // first check to see if we have an input parameter that we can use for
538: // this.
539: if (runtimeContext.getInputNames().contains(inputName)) {
540: return runtimeContext.getInputParameterValue(inputName);
541: }
542: // now check the component node from the action definition.
543: Node node = componentDefinition.selectSingleNode(inputName);
544: if (node == null) {
545: return null;
546: }
547: return node.getText();
548: }
549:
550: private String getComponentSetting(String path) {
551: // first check to see if we have an input parameter that we can use for
552: // this.
553: if (runtimeContext.getInputNames().contains(path)) {
554: return runtimeContext.getInputParameterStringValue(path);
555: }
556: // now check the component node from the action definition.
557: Node node = componentDefinition.selectSingleNode(path);
558: if (node == null) {
559: return null;
560: }
561: return node.getText();
562: }
563:
564: public void promptNeeded() {
565: runtimeContext.promptNeeded();
566: }
567:
568: public void promptNow() {
569: runtimeContext.promptNow();
570: }
571:
572: public String getResourceAsString(IActionResource resource) {
573: try {
574: return runtimeContext.getResourceAsString(resource);
575: } catch (Exception e) {
576: return null;
577: }
578: }
579:
580: public String getInitFailMessage() {
581: return null;
582: }
583:
584: public String createNewInstance(boolean persisted, Map parameters,
585: boolean forceImmediateWrite) {
586: return runtimeContext.createNewInstance(persisted, parameters,
587: forceImmediateWrite);
588: }
589:
590: public void inputMissingError(String paramName) {
591: error(Messages
592: .getErrorString(
593: "ComponentBase.ERROR_0003_INPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
594: }
595:
596: public void outputMissingError(String paramName) {
597: error(Messages
598: .getErrorString(
599: "ComponentBase.ERROR_0004_OUTPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
600: }
601:
602: public void resourceMissingError(String paramName) {
603: error(Messages
604: .getErrorString(
605: "ComponentBase.ERROR_0005_RESOURCE_PARAM_MISSING", paramName)); //$NON-NLS-1$
606: }
607:
608: public void resourceComponentSettingError(String paramName) {
609: error(Messages
610: .getErrorString(
611: "ComponentBase.ERROR_0006_COMPONENT_SETTING_PARAM_MISSING", paramName)); //$NON-NLS-1$
612: }
613:
614: public int execute() {
615:
616: // see if we have a custom XSL for the parameter page, if required
617: String xsl = getComponentSetting("xsl"); //$NON-NLS-1$
618: if (xsl != null) {
619: runtimeContext.setParameterXsl(xsl);
620: }
621:
622: // see if we have a target window for the output
623: String target = getComponentSetting("target"); //$NON-NLS-1$
624: if (target != null) {
625: runtimeContext.setParameterTarget(target);
626: }
627:
628: if (loggingLevel == UNKNOWN) {
629: warn(Messages
630: .getString("Base.WARNING_LOGGING_LEVEL_UNKNOWN")); //$NON-NLS-1$
631: loggingLevel = ILogger.DEBUG;
632: }
633: int result = IRuntimeContext.RUNTIME_STATUS_FAILURE;
634:
635: if (sessionContext == null) {
636: error(Messages
637: .getErrorString("Base.ERROR_0001_INVALID_SESSION")); //$NON-NLS-1$
638: return result;
639: }
640:
641: if (debug)
642: debug(Messages.getString("Base.DEBUG_VALIDATION_RESULT") + getInitOk()); //$NON-NLS-1$
643: if (!getInitOk()) {
644: return result;
645: }
646:
647: try {
648: result = (executeAction() ? IRuntimeContext.RUNTIME_STATUS_SUCCESS
649: : IRuntimeContext.RUNTIME_STATUS_FAILURE);
650: if (result == IRuntimeContext.RUNTIME_STATUS_SUCCESS
651: && runtimeContext.isPromptPending()) {
652: // see if we need to prevent further components from executing
653: if (isDefinedInput(StandardSettings.HANDLE_ALL_PROMPTS)) {
654: runtimeContext.promptNow();
655: }
656: }
657: } catch (Throwable e) {
658: error(
659: Messages
660: .getErrorString("Base.ERROR_0002_EXECUTION_FAILED"), e); //$NON-NLS-1$
661: }
662: return result;
663: }
664:
665: public String getId() {
666: return id;
667: }
668:
669: public String getActionTitle() {
670: return runtimeContext.getActionTitle();
671: }
672:
673: protected IContentItem getOutputContentItem(String mimeType) {
674: return runtimeContext.getOutputContentItem(mimeType);
675: }
676:
677: protected IContentItem getOutputContentItem(String outputName,
678: String mimeType) {
679: return runtimeContext
680: .getOutputContentItem(outputName, mimeType);
681: }
682:
683: protected void setOutputValue(String outputName, Object value) {
684: runtimeContext.setOutputValue(outputName, value);
685: }
686:
687: protected OutputStream getDefaultOutputStream(String mimeType) {
688: IContentItem contentItem = runtimeContext
689: .getOutputContentItem(mimeType);
690: if (contentItem != null) {
691: try {
692: return contentItem.getOutputStream(getActionName());
693: } catch (Exception e) {
694: }
695: }
696: return null;
697: }
698:
699: protected String applyInputsToFormat(String format) {
700: return runtimeContext.applyInputsToFormat(format);
701: }
702:
703: protected IActionParameter getOutputItem(String outputName) {
704: return runtimeContext.getOutputParameter(outputName);
705: }
706:
707: protected String getSolutionName() {
708: return runtimeContext.getSolutionName();
709: }
710:
711: protected String getSolutionPath() {
712: return runtimeContext.getSolutionPath();
713: }
714:
715: protected IActionParameter getInputParameter(String parameterName) {
716: return runtimeContext.getInputParameter(parameterName);
717: }
718:
719: protected String getContentUrl(IContentItem contentItem) {
720: return runtimeContext.getContentUrl(contentItem);
721: }
722:
723: protected boolean isPromptPending() {
724: return runtimeContext.isPromptPending();
725: }
726:
727: protected void setFeedbackMimeType(String mimeType) {
728: IContentItem feedbackContentItem = runtimeContext
729: .getFeedbackContentItem();
730: feedbackContentItem.setMimeType(mimeType);
731: }
732:
733: protected void setOutputMimeType(String mimeType) {
734: IContentItem outputContentItem = runtimeContext
735: .getOutputContentItem(mimeType);
736: outputContentItem.setMimeType(mimeType);
737: }
738:
739: protected OutputStream getFeedbackOutputStream() {
740: IContentItem feedbackContentItem = runtimeContext
741: .getFeedbackContentItem();
742: if (feedbackContentItem != null) {
743: try {
744: return feedbackContentItem
745: .getOutputStream(getActionName());
746: } catch (Exception e) {
747: }
748: }
749: return null;
750: }
751:
752: protected void createFeedbackParameter(IActionParameter actionParam) {
753: runtimeContext.createFeedbackParameter(actionParam);
754: runtimeContext.promptNeeded();
755: }
756:
757: protected void createFeedbackParameter(SelectionMapper selMap,
758: String fieldName, Object defaultValues) {
759: runtimeContext.createFeedbackParameter(selMap, fieldName,
760: defaultValues);
761: runtimeContext.promptNeeded();
762: }
763:
764: protected void createFeedbackParameter(String fieldName,
765: String displayName, String hint, String defaultValue,
766: boolean visible) {
767: runtimeContext.createFeedbackParameter(fieldName, displayName,
768: hint, defaultValue, visible);
769: runtimeContext.promptNeeded();
770: }
771:
772: public void createFeedbackParameter(String fieldName,
773: String displayName, String hint, Object defaultValues,
774: List values, Map dispNames, String displayStyle) {
775: runtimeContext.createFeedbackParameter(fieldName, displayName,
776: hint, defaultValues, values, dispNames, displayStyle);
777: runtimeContext.promptNeeded();
778: }
779:
780: protected DataSource getDataSource(String parameterName) {
781: return runtimeContext.getDataSource(parameterName);
782: }
783:
784: protected DataSource getResourceDataSource(IActionResource resource)
785: throws FileNotFoundException {
786: return runtimeContext.getResourceDataSource(resource);
787: }
788:
789: public void setActionDefinition(ActionDefinition actionDefinition) {
790: this .actionDefinition = actionDefinition;
791: }
792:
793: public ActionDefinition getActionDefinition() {
794: return actionDefinition;
795: }
796: }
|