KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > ComponentSubclassExample


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created May 4, 2006
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.activation.DataSource JavaDoc;
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 JavaDoc implements IComponent {
46
47     protected int loggingLevel = UNKNOWN;
48
49     public static final String JavaDoc LOGID_MASK1 = "{0}:{1}:{2}: "; //$NON-NLS-1$
50

51     public static final String JavaDoc LOGID_MASK2 = "{0}:{1}:{2}:{3} "; //$NON-NLS-1$
52

53     public static final String JavaDoc LOGID_SEPARATOR = ":"; //$NON-NLS-1$
54

55     public String JavaDoc EMPTYLOGID = "::: "; //$NON-NLS-1$
56

57     private String JavaDoc 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 JavaDoc processId;
66
67     private String JavaDoc actionName;
68
69     private String JavaDoc instanceId;
70
71     private String JavaDoc id;
72
73     private boolean baseInitOk;
74
75     private boolean componentInitOk;
76
77     private Node componentDefinition;
78
79     private HashMap JavaDoc settings;
80     
81     protected boolean executeAction() throws Throwable JavaDoc {
82         // TODO add your execute code here
83
return false;
84     }
85
86     public boolean validateAction() {
87         // TODO add any validation code in here and return true
88
return false;
89     }
90
91     protected boolean validateSystemSettings() {
92         // TODO add any validation of system settings here and return true
93
return false;
94     }
95
96     public void done() {
97         // TODO add any cleanup code here
98
}
99
100     public boolean init() {
101         // TODO add any initialization code and return true
102
return false;
103     }
104     
105     public void setLogId(String JavaDoc 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() : "", //$NON-NLS-1$
115
sess.getProcessId() != null ? sess.getProcessId() : "", //$NON-NLS-1$
116
sess.getActionName() != null ? sess.getActionName() : "" //$NON-NLS-1$
117
);
118     }
119
120     public void genLogIdFromInfo(String JavaDoc sessId, String JavaDoc procId, String JavaDoc actName) {
121         Object JavaDoc[] args = { sessId, procId, actName };
122         setLogId(MessageFormat.format(LOGID_MASK1, args));
123     }
124
125     public void genLogIdFromInfo(String JavaDoc sessId, String JavaDoc procId, String JavaDoc actName, String JavaDoc instId) {
126         Object JavaDoc[] args = { sessId, procId, actName, instId };
127         setLogId(MessageFormat.format(LOGID_MASK2, args));
128     }
129
130     /* ILogger Implementation */
131
132     public String JavaDoc 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 JavaDoc messages;
145
146     public List JavaDoc getMessages() {
147         return messages;
148     }
149
150     public void setMessages(List JavaDoc messages) {
151         this.messages = messages;
152     }
153
154     public void trace(String JavaDoc message) {
155         if (loggingLevel <= TRACE) {
156             if (messages != null) {
157                 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
158
}
159             getLogger().trace(getLogId() + message);
160         }
161     }
162
163     public void debug(String JavaDoc message) {
164         if (loggingLevel <= DEBUG) {
165             if (messages != null) {
166                 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
167
}
168             getLogger().debug(getLogId() + message);
169         }
170     }
171
172     public void info(String JavaDoc message) {
173         if (loggingLevel <= INFO) {
174             if (messages != null) {
175                 messages.add(Messages.getString("Message.USER_INFO", message, getClass().getName())); //$NON-NLS-1$
176
}
177             getLogger().info(getLogId() + message);
178         }
179     }
180
181     public void warn(String JavaDoc message) {
182         if (loggingLevel <= WARN) {
183             if (messages != null) {
184                 messages.add(Messages.getString("Message.USER_WARNING", message, getClass().getName())); //$NON-NLS-1$
185
}
186             getLogger().warn(getLogId() + message);
187         }
188     }
189
190     public void error(String JavaDoc message) {
191         if (loggingLevel <= ERROR) {
192             if (messages != null) {
193                 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
194
}
195             getLogger().error(getLogId() + message);
196         }
197     }
198
199     public void fatal(String JavaDoc message) {
200         if (loggingLevel <= FATAL) {
201             if (messages != null) {
202                 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
203
}
204             getLogger().fatal(getLogId() + message);
205         }
206     }
207
208     public void trace(String JavaDoc message, Throwable JavaDoc error) {
209         if (loggingLevel <= TRACE) {
210             if (messages != null) {
211                 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
212
}
213             getLogger().trace(getLogId() + message, error);
214         }
215     }
216
217     public void debug(String JavaDoc message, Throwable JavaDoc error) {
218         if (loggingLevel <= DEBUG) {
219             if (messages != null) {
220                 messages.add(Messages.getString("Message.USER_DEBUG", message, getClass().getName())); //$NON-NLS-1$
221
}
222             getLogger().debug(getLogId() + message, error);
223         }
224     }
225
226     public void info(String JavaDoc message, Throwable JavaDoc error) {
227         if (loggingLevel <= INFO) {
228             if (messages != null) {
229                 messages.add(Messages.getString("Message.USER_INFO", message, getClass().getName())); //$NON-NLS-1$
230
}
231             getLogger().info(getLogId() + message, error);
232         }
233     }
234
235     public void warn(String JavaDoc message, Throwable JavaDoc error) {
236         if (loggingLevel <= WARN) {
237             if (messages != null) {
238                 messages.add(Messages.getString("Message.USER_WARNING", message, getClass().getName())); //$NON-NLS-1$
239
}
240             getLogger().warn(getLogId() + message, error);
241         }
242     }
243
244     public void error(String JavaDoc message, Throwable JavaDoc error) {
245         if (loggingLevel <= ERROR) {
246             if (messages != null) {
247                 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
248
}
249             getLogger().error("Error Start: Pentaho " + VersionHelper.getVersion() + " build " + VersionHelper.getBuild()); //$NON-NLS-1$ //$NON-NLS-2$
250
getLogger().error(getLogId() + message, error);
251             getLogger().error("Error end:"); //$NON-NLS-1$
252
}
253     }
254
255     public void fatal(String JavaDoc message, Throwable JavaDoc error) {
256         if (loggingLevel <= FATAL) {
257             if (messages != null) {
258                 messages.add(Messages.getString("Message.USER_ERROR", message, getClass().getName())); //$NON-NLS-1$
259
}
260             getLogger().error("Error: Pentaho " + VersionHelper.getVersion() + " build " + VersionHelper.getBuild()); //$NON-NLS-1$ //$NON-NLS-2$
261
getLogger().fatal(getLogId() + message, error);
262             getLogger().error("Error end:"); //$NON-NLS-1$
263
}
264     }
265
266     public static String JavaDoc getUserString(String JavaDoc type) {
267         return Messages.getString("Message.USER_" + type); //$NON-NLS-1$
268
}
269
270     public void setInstanceId( String JavaDoc instanceId ) {
271         this.instanceId = instanceId;
272     }
273     
274     public String JavaDoc getInstanceId() {
275         return instanceId;
276     }
277
278     public void setActionName( String JavaDoc actionName ) {
279         this.actionName = actionName;
280     }
281     
282     public String JavaDoc getActionName( ) {
283         return actionName;
284     }
285     
286     public void setProcessId( String JavaDoc processId ) {
287         this.processId = processId;
288     }
289
290     public String JavaDoc 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 JavaDoc name, Object JavaDoc value) {
319         settings.put(name, value);
320     }
321
322     protected Object JavaDoc getSetting(String JavaDoc name) {
323         return settings.get(name);
324     }
325
326     protected String JavaDoc getStringSetting(String JavaDoc name) {
327         Object JavaDoc value = settings.get(name);
328         if (value == null) {
329             return null;
330         } else if (value instanceof String JavaDoc) {
331             return (String JavaDoc) value;
332         } else {
333             return value.toString();
334         }
335     }
336
337     public String JavaDoc getLogId() {
338         return logId;
339     }
340
341     protected boolean isDefinedInput(String JavaDoc 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 JavaDoc outputName) {
351         return runtimeContext.getOutputNames().contains(outputName);
352     }
353
354     protected boolean isDefinedResource(String JavaDoc 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); //$NON-NLS-1$
361
if (debug)
362             debug(Messages.getString("Base.DEBUG_VALIDATING_COMPONENT", actionName)); //$NON-NLS-1$
363
// grab the parameters first
364

365         id = Messages.getString("Base.CODE_COMPONENT_ID", processId, actionName); //$NON-NLS-1$
366

367         // now get picky about values
368
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 JavaDoc getOutputNames() {
386         return runtimeContext.getOutputNames();
387     }
388
389     protected Set JavaDoc getInputNames() {
390         return runtimeContext.getInputNames();
391     }
392
393     protected Set JavaDoc getResourceNames() {
394         return runtimeContext.getResourceNames();
395     }
396
397     protected boolean feedbackAllowed() {
398         return runtimeContext.feedbackAllowed();
399     }
400
401     protected IActionResource getResource(String JavaDoc resourceName) {
402         return runtimeContext.getResourceDefintion(resourceName);
403     }
404
405     protected InputStream JavaDoc getResourceInputStream(IActionResource resource) {
406         return runtimeContext.getResourceInputStream(resource);
407     }
408
409     protected InputStream JavaDoc getInputStream(String JavaDoc inputName) {
410         return runtimeContext.getInputStream(inputName);
411     }
412
413     protected int getOutputPreference() {
414         return runtimeContext.getOutputPreference();
415     }
416
417     protected OutputStream JavaDoc getOutputStream(String JavaDoc outputName, String JavaDoc mimeType, String JavaDoc extension) {
418         return runtimeContext.getOutputStream(outputName, mimeType, extension);
419     }
420
421     protected void audit(String JavaDoc messageType, String JavaDoc message, String JavaDoc value, int duration) {
422         runtimeContext.audit(messageType, message, value, duration);
423     }
424
425     protected boolean getInputBooleanValue(String JavaDoc inputName, boolean defaultValue) {
426         String JavaDoc strValue = getInputStringValue(inputName);
427         if (strValue == null) {
428             return defaultValue;
429         } else if ("true".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
430
return true;
431         } else if ("false".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
432
return false;
433         } else {
434             return defaultValue;
435         }
436
437     }
438
439     protected long getInputLongValue(String JavaDoc inputName, long defaultValue) {
440         String JavaDoc strValue = getInputStringValue(inputName);
441         if (strValue == null) {
442             return defaultValue;
443         }
444         try {
445             return Long.parseLong(strValue);
446         } catch (Exception JavaDoc e) {
447             return defaultValue;
448         }
449
450     }
451
452     protected String JavaDoc getInputStringValue(String JavaDoc inputName) {
453         // first check to see if we have an input parameter that we can use for
454
// this.
455
String JavaDoc value = null;
456         if (runtimeContext.getInputNames().contains(inputName)) {
457             value = runtimeContext.getInputParameterStringValue(inputName);
458         } else {
459             // now check the component node from the action definition.
460
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 JavaDoc getInputValue(String JavaDoc inputName) {
473         // first check to see if we have an input parameter that we can use for
474
// this.
475
if (runtimeContext.getInputNames().contains(inputName)) {
476             return runtimeContext.getInputParameterValue(inputName);
477         }
478         // now check the component node from the action definition.
479
Node node = componentDefinition.selectSingleNode(inputName);
480         if (node == null) {
481             return null;
482         }
483         return node.getText();
484     }
485
486     private String JavaDoc getComponentSetting(String JavaDoc path) {
487         // first check to see if we have an input parameter that we can use for
488
// this.
489
if (runtimeContext.getInputNames().contains(path)) {
490             return runtimeContext.getInputParameterStringValue(path);
491         }
492         // now check the component node from the action definition.
493
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 JavaDoc getResourceAsString(IActionResource resource) {
509         try {
510             return runtimeContext.getResourceAsString(resource);
511         } catch (Exception JavaDoc e) {
512             return null;
513         }
514     }
515
516     public String JavaDoc getInitFailMessage() {
517         return null;
518     }
519
520     public String JavaDoc createNewInstance(boolean persisted, Map JavaDoc parameters, boolean forceImmediateWrite) {
521         return runtimeContext.createNewInstance(persisted, parameters, forceImmediateWrite);
522     }
523
524     public void inputMissingError(String JavaDoc paramName) {
525         error(Messages.getErrorString("ComponentBase.ERROR_0003_INPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
526
}
527
528     public void outputMissingError(String JavaDoc paramName) {
529         error(Messages.getErrorString("ComponentBase.ERROR_0004_OUTPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
530
}
531
532     public void resourceMissingError(String JavaDoc paramName) {
533         error(Messages.getErrorString("ComponentBase.ERROR_0005_RESOURCE_PARAM_MISSING", paramName)); //$NON-NLS-1$
534
}
535
536     public void resourceComponentSettingError(String JavaDoc paramName) {
537         error(Messages.getErrorString("ComponentBase.ERROR_0006_COMPONENT_SETTING_PARAM_MISSING", paramName)); //$NON-NLS-1$
538
}
539
540     public int execute() {
541
542         // see if we have a custom XSL for the parameter page, if required
543
String JavaDoc xsl = getComponentSetting("xsl"); //$NON-NLS-1$
544
if (xsl != null) {
545             runtimeContext.setParameterXsl(xsl);
546         }
547
548         // see if we have a target window for the output
549
String JavaDoc target = getComponentSetting("target"); //$NON-NLS-1$
550
if (target != null) {
551             runtimeContext.setParameterTarget(target);
552         }
553
554         if (loggingLevel == UNKNOWN) {
555             warn(Messages.getString("Base.WARNING_LOGGING_LEVEL_UNKNOWN")); //$NON-NLS-1$
556
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")); //$NON-NLS-1$
562
return result;
563         }
564
565         if (debug)
566             debug(Messages.getString("Base.DEBUG_VALIDATION_RESULT") + getInitOk()); //$NON-NLS-1$
567
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                 // see if we need to prevent further components from executing
575
if (isDefinedInput(StandardSettings.HANDLE_ALL_PROMPTS)) {
576                     runtimeContext.promptNow();
577                 }
578             }
579         } catch (Throwable JavaDoc e) {
580             error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED"), e); //$NON-NLS-1$
581
}
582         return result;
583     }
584
585     public String JavaDoc getId() {
586         return id;
587     }
588
589     public String JavaDoc getActionTitle() {
590         return runtimeContext.getActionTitle();
591     }
592
593     protected IContentItem getOutputContentItem() {
594         return runtimeContext.getOutputContentItem();
595     }
596
597     protected IContentItem getOutputContentItem(String JavaDoc outputName) {
598         return runtimeContext.getOutputContentItem(outputName);
599     }
600
601     protected void setOutputValue(String JavaDoc outputName, Object JavaDoc value) {
602         runtimeContext.setOutputValue(outputName, value);
603     }
604
605     protected OutputStream JavaDoc getDefaultOutputStream() {
606         IContentItem contentItem = runtimeContext.getOutputContentItem();
607         if (contentItem != null) {
608             try {
609                 return contentItem.getOutputStream(getActionName());
610             } catch (Exception JavaDoc e) {
611             }
612         }
613         return null;
614     }
615
616     protected String JavaDoc applyInputsToFormat(String JavaDoc format) {
617         return runtimeContext.applyInputsToFormat(format);
618     }
619
620     protected IActionParameter getOutputItem(String JavaDoc outputName) {
621         return runtimeContext.getOutputParameter(outputName);
622     }
623
624     protected String JavaDoc getSolutionName() {
625         return runtimeContext.getSolutionName();
626     }
627
628     protected String JavaDoc getSolutionPath() {
629         return runtimeContext.getSolutionPath();
630     }
631
632     protected IActionParameter getInputParameter(String JavaDoc parameterName) {
633         return runtimeContext.getInputParameter(parameterName);
634     }
635
636     protected String JavaDoc 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 JavaDoc mimeType) {
645         IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem();
646         feedbackContentItem.setMimeType(mimeType);
647     }
648
649     protected void setOutputMimeType(String JavaDoc mimeType) {
650         IContentItem outputContentItem = runtimeContext.getOutputContentItem();
651         outputContentItem.setMimeType(mimeType);
652     }
653
654     protected OutputStream JavaDoc getFeedbackOutputStream() {
655         IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem();
656         if (feedbackContentItem != null) {
657             try {
658                 return feedbackContentItem.getOutputStream(getActionName());
659             } catch (Exception JavaDoc 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 JavaDoc fieldName, Object JavaDoc defaultValues) {
671         runtimeContext.createFeedbackParameter(selMap, fieldName, defaultValues);
672         runtimeContext.promptNeeded();
673     }
674
675     protected void createFeedbackParameter(String JavaDoc fieldName, String JavaDoc displayName, String JavaDoc hint, String JavaDoc defaultValue, boolean visible) {
676         runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValue, visible);
677         runtimeContext.promptNeeded();
678     }
679
680     public void createFeedbackParameter(String JavaDoc fieldName, String JavaDoc displayName, String JavaDoc hint, Object JavaDoc defaultValues, List JavaDoc values, Map JavaDoc dispNames, String JavaDoc displayStyle) {
681         runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValues, values, dispNames, displayStyle);
682         runtimeContext.promptNeeded();
683     }
684
685     protected DataSource JavaDoc getDataSource(String JavaDoc parameterName) {
686         return runtimeContext.getDataSource(parameterName);
687     }
688
689     protected DataSource JavaDoc getResourceDataSource(IActionResource resource) {
690         return runtimeContext.getResourceDataSource(resource);
691     }
692     
693 }
694
Popular Tags