KickJava   Java API By Example, From Geeks To Geeks.

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


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 Apr 15, 2005
14  * @author James Dixon
15  */

16 package org.pentaho.plugin;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26
27 import javax.activation.DataSource JavaDoc;
28 import org.dom4j.Node;
29 import org.pentaho.core.component.IComponent;
30 import org.pentaho.core.repository.IContentItem;
31 import org.pentaho.core.runtime.ActionParameter;
32 import org.pentaho.core.runtime.IActionParameter;
33 import org.pentaho.core.runtime.IRuntimeContext;
34 import org.pentaho.core.runtime.InvalidParameterException;
35 import org.pentaho.core.runtime.SelectionMapper;
36 import org.pentaho.core.session.IPentahoSession;
37 import org.pentaho.core.solution.IActionResource;
38 import org.pentaho.core.system.PentahoMessenger;
39 import org.pentaho.core.system.PentahoSystem;
40 import org.pentaho.core.util.IParameterResolver;
41 import org.pentaho.messages.Messages;
42 import org.pentaho.plugin.core.StandardSettings;
43 import org.pentaho.util.logging.ILogger;
44
45 /**
46  */

47 public abstract class ComponentBase extends PentahoMessenger implements IComponent, IParameterResolver {
48
49     protected static final String JavaDoc UNKNOWN_COMPONENT_ID = "unknown"; //$NON-NLS-1$
50

51     public static final String JavaDoc MISSING_SESSION = "session missing"; //$NON-NLS-1$
52

53     public static final String JavaDoc COMPONENT_EXECUTE_FAIL = "component failed"; //$NON-NLS-1$
54

55     protected static final boolean debug = PentahoSystem.debug;
56
57     private IRuntimeContext runtimeContext;
58
59     private IPentahoSession sessionContext;
60
61     private String JavaDoc processId;
62
63     private String JavaDoc actionName;
64
65     private String JavaDoc instanceId;
66
67     private String JavaDoc id;
68
69     private boolean baseInitOk;
70
71     private boolean componentInitOk;
72
73     // private int loggingLevel = UNKNOWN;
74
private String JavaDoc logId;
75
76     private Node componentDefinition;
77
78     private HashMap JavaDoc settings = new HashMap JavaDoc();
79
80     public void setInstanceId( String JavaDoc instanceId ) {
81         this.instanceId = instanceId;
82     }
83     
84     public String JavaDoc getInstanceId() {
85         return instanceId;
86     }
87
88     public void setActionName( String JavaDoc actionName ) {
89         this.actionName = actionName;
90     }
91     
92     public String JavaDoc getActionName( ) {
93         return actionName;
94     }
95     
96     public void setProcessId( String JavaDoc processId ) {
97         this.processId = processId;
98     }
99
100     public String JavaDoc getProcessId( ) {
101         return processId;
102     }
103
104     public void setComponentDefinition( Node componentDefinition ) {
105         this.componentDefinition = componentDefinition;
106     }
107
108     public Node getComponentDefinition() {
109         return getComponentDefinition( false );
110     }
111
112     public Node getComponentDefinition( boolean process ) {
113             if( process ) {
114                 List JavaDoc nodes = componentDefinition.selectNodes("//*"); //$NON-NLS-1$
115
Iterator JavaDoc it = nodes.iterator();
116                 while( it.hasNext() ) {
117                         Node node = (Node) it.next();
118                         String JavaDoc txt = node.getText();
119                         if( txt != null && !node.isReadOnly() ) {
120                             node.setText( applyInputsToFormat( txt ) );
121                         }
122                 }
123             }
124             return componentDefinition;
125     }
126     
127     public void setRuntimeContext( IRuntimeContext runtimeContext ) {
128         this.runtimeContext = runtimeContext;
129     }
130             
131     public IRuntimeContext getRuntimeContext( ) {
132         return runtimeContext;
133     }
134             
135     public void setSession( IPentahoSession session ) {
136         this.sessionContext = session;
137     }
138     
139     public IPentahoSession getSession( ) {
140         return sessionContext;
141     }
142         
143     protected void saveSetting(String JavaDoc name, Object JavaDoc value) {
144         settings.put(name, value);
145     }
146
147     protected Object JavaDoc getSetting(String JavaDoc name) {
148         return settings.get(name);
149     }
150
151     protected String JavaDoc getStringSetting(String JavaDoc name) {
152         Object JavaDoc value = settings.get(name);
153         if (value == null) {
154             return null;
155         } else if (value instanceof String JavaDoc) {
156             return (String JavaDoc) value;
157         } else {
158             return value.toString();
159         }
160     }
161
162     protected abstract boolean validateAction();
163
164     protected abstract boolean validateSystemSettings();
165
166     public abstract void done();
167
168     protected abstract boolean executeAction() throws Throwable JavaDoc;
169
170     public abstract boolean init();
171
172     public String JavaDoc getLogId() {
173         return logId;
174     }
175
176     protected boolean isDefinedInput(String JavaDoc inputName) {
177
178         if (runtimeContext.getInputNames().contains(inputName)) {
179             return true;
180         } else {
181             return getComponentSetting(inputName) != null;
182         }
183     }
184
185     protected boolean isDefinedOutput(String JavaDoc outputName) {
186         return runtimeContext.getOutputNames().contains(outputName);
187     }
188
189     protected boolean isDefinedResource(String JavaDoc resourceName) {
190         return runtimeContext.getResourceNames().contains(resourceName);
191     }
192
193     public final int validate() {
194
195         logId = Messages.getString("Base.CODE_LOG_ID", instanceId, runtimeContext.getHandle(), actionName); //$NON-NLS-1$
196
if (debug)
197             debug(Messages.getString("Base.DEBUG_VALIDATING_COMPONENT", actionName)); //$NON-NLS-1$
198
// grab the parameters first
199

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

202         // now get picky about values
203
baseInitOk = (instanceId != null && sessionContext != null && processId != null && actionName != null);
204
205         boolean systemSettingsValidate = validateSystemSettings();
206
207         if (baseInitOk && systemSettingsValidate) {
208             try {
209                 componentInitOk = validateAction();
210             }
211             catch ( Exception JavaDoc e ) {
212                 error(Messages.getErrorString("Base.ERROR_0004_VALIDATION_FAILED"), e); //$NON-NLS-1$
213
}
214         }
215         if (getInitOk()) {
216             return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK;
217         }
218         return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL;
219     }
220
221     public int resolveParameter(String JavaDoc template, String JavaDoc parameterName, Matcher JavaDoc parameterMatcher, int copyStart, StringBuffer JavaDoc result) {
222       // Overriding components should return non-negative value if they handle resolving the parameter
223
return -1;
224     }
225     
226     public boolean getInitOk() {
227         return baseInitOk && componentInitOk;
228     }
229
230     protected Set JavaDoc getOutputNames() {
231         return runtimeContext.getOutputNames();
232     }
233
234     protected Set JavaDoc getInputNames() {
235         return runtimeContext.getInputNames();
236     }
237
238     protected Set JavaDoc getResourceNames() {
239         return runtimeContext.getResourceNames();
240     }
241
242     protected boolean feedbackAllowed() {
243         return runtimeContext.feedbackAllowed();
244     }
245
246     protected IActionResource getResource(String JavaDoc resourceName) {
247         return runtimeContext.getResourceDefintion(resourceName);
248     }
249
250     protected InputStream JavaDoc getResourceInputStream(IActionResource resource) {
251         return runtimeContext.getResourceInputStream(resource);
252     }
253
254     protected InputStream JavaDoc getInputStream(String JavaDoc inputName) {
255         return runtimeContext.getInputStream(inputName);
256     }
257
258     protected int getOutputPreference() {
259         return runtimeContext.getOutputPreference();
260     }
261
262     protected OutputStream JavaDoc getOutputStream(String JavaDoc outputName, String JavaDoc mimeType, String JavaDoc extension) {
263         return runtimeContext.getOutputStream(outputName, mimeType, extension);
264     }
265
266     protected void audit(String JavaDoc messageType, String JavaDoc message, String JavaDoc value, int duration) {
267         runtimeContext.audit(messageType, message, value, duration);
268     }
269
270     protected boolean getInputBooleanValue(String JavaDoc inputName, boolean defaultValue) {
271         String JavaDoc strValue = getInputStringValue(inputName);
272         if (strValue == null) {
273             return defaultValue;
274         } else if ("true".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
275
return true;
276         } else if ("false".equalsIgnoreCase(strValue)) { //$NON-NLS-1$
277
return false;
278         } else {
279             return defaultValue;
280         }
281
282     }
283
284     protected long getInputLongValue(String JavaDoc inputName, long defaultValue) {
285         String JavaDoc strValue = getInputStringValue(inputName);
286         if (strValue == null) {
287             return defaultValue;
288         }
289         try {
290             return Long.parseLong(strValue);
291         } catch (Exception JavaDoc e) {
292             return defaultValue;
293         }
294
295     }
296
297     protected String JavaDoc getInputStringValue(String JavaDoc inputName) {
298         // first check to see if we have an input parameter that we can use for
299
// this.
300
String JavaDoc value = null;
301         if (runtimeContext.getInputNames().contains(inputName)) {
302             value = runtimeContext.getInputParameterStringValue(inputName);
303         } else {
304             // now check the component node from the action definition.
305
Node node = componentDefinition.selectSingleNode(inputName);
306             if (node == null) {
307                 return null;
308             }
309             value = node.getText();
310         }
311         if (value != null) {
312             // TODO make the format appliation configurable
313
value = this.applyInputsToFormat(value);
314         }
315         return value;
316     }
317
318     protected Object JavaDoc getInputValue(String JavaDoc inputName) {
319         // first check to see if we have an input parameter that we can use for
320
// this.
321
if (runtimeContext.getInputNames().contains(inputName)) {
322             return runtimeContext.getInputParameterValue(inputName);
323         }
324         // now check the component node from the action definition.
325
Node node = componentDefinition.selectSingleNode(inputName);
326         if (node == null) {
327             return null;
328         }
329         return node.getText();
330     }
331
332     private String JavaDoc getComponentSetting(String JavaDoc path) {
333         // first check to see if we have an input parameter that we can use for
334
// this.
335
if (runtimeContext.getInputNames().contains(path)) {
336             return runtimeContext.getInputParameterStringValue(path);
337         }
338         // now check the component node from the action definition.
339
Node node = componentDefinition.selectSingleNode(path);
340         if (node == null) {
341             return null;
342         }
343         return node.getText();
344     }
345
346     public void promptNeeded() {
347         runtimeContext.promptNeeded();
348     }
349
350     public void promptNow() {
351         runtimeContext.promptNow();
352     }
353
354     public String JavaDoc getResourceAsString(IActionResource resource) {
355         try {
356             return runtimeContext.getResourceAsString(resource);
357         } catch (Exception JavaDoc e) {
358             return null;
359         }
360     }
361
362     /*
363      * protected IRuntimeContext getRuntimeContextX() { return runtimeContext; }
364      */

365     public String JavaDoc getInitFailMessage() {
366         // TODO: return a meaningful message here
367
return null;
368     }
369
370     public String JavaDoc createNewInstance(boolean persisted, Map JavaDoc parameters, boolean forceImmediateWrite) {
371         return runtimeContext.createNewInstance(persisted, parameters, forceImmediateWrite);
372     }
373
374     public void inputMissingError(String JavaDoc paramName) {
375         error(Messages.getErrorString("ComponentBase.ERROR_0003_INPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
376
}
377
378     public void outputMissingError(String JavaDoc paramName) {
379         error(Messages.getErrorString("ComponentBase.ERROR_0004_OUTPUT_PARAM_MISSING", paramName)); //$NON-NLS-1$
380
}
381
382     public void resourceMissingError(String JavaDoc paramName) {
383         error(Messages.getErrorString("ComponentBase.ERROR_0005_RESOURCE_PARAM_MISSING", paramName)); //$NON-NLS-1$
384
}
385
386     public void resourceComponentSettingError(String JavaDoc paramName) {
387         error(Messages.getErrorString("ComponentBase.ERROR_0006_COMPONENT_SETTING_PARAM_MISSING", paramName)); //$NON-NLS-1$
388
}
389
390     public int execute() {
391
392         // see if we have a custom XSL for the parameter page, if required
393
String JavaDoc xsl = getComponentSetting("xsl"); //$NON-NLS-1$
394
if (xsl != null) {
395             runtimeContext.setParameterXsl(xsl);
396         }
397
398         // see if we have a target window for the output
399
String JavaDoc target = getComponentSetting("target"); //$NON-NLS-1$
400
if (target != null) {
401             runtimeContext.setParameterTarget(target);
402         }
403
404         if (loggingLevel == UNKNOWN) {
405             warn(Messages.getString("Base.WARNING_LOGGING_LEVEL_UNKNOWN")); //$NON-NLS-1$
406
loggingLevel = ILogger.DEBUG;
407         }
408         int result = IRuntimeContext.RUNTIME_STATUS_FAILURE;
409
410         if (sessionContext == null) {
411             error(Messages.getErrorString("Base.ERROR_0001_INVALID_SESSION")); //$NON-NLS-1$
412
return result;
413         }
414
415         if (debug)
416             debug(Messages.getString("Base.DEBUG_VALIDATION_RESULT") + getInitOk()); //$NON-NLS-1$
417
if (!getInitOk()) {
418             return result;
419         }
420
421         try {
422             result = (executeAction() ? IRuntimeContext.RUNTIME_STATUS_SUCCESS : IRuntimeContext.RUNTIME_STATUS_FAILURE);
423             if (result == IRuntimeContext.RUNTIME_STATUS_SUCCESS && runtimeContext.isPromptPending()) {
424                 // see if we need to prevent further components from executing
425
if (isDefinedInput(StandardSettings.HANDLE_ALL_PROMPTS)) {
426                     runtimeContext.promptNow();
427                 }
428             }
429         }
430         catch (InvalidParameterException e) {
431             // No reason to do a stack trace
432
error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED") ); //$NON-NLS-1$
433
}
434         catch (Throwable JavaDoc e) {
435             error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED"), e); //$NON-NLS-1$
436
}
437         return result;
438     }
439
440     public String JavaDoc getObjectName() {
441         return this.getClass().getName();
442     }
443
444     public String JavaDoc getId() {
445         return id;
446     }
447
448     public String JavaDoc getActionTitle() {
449         return runtimeContext.getActionTitle();
450     }
451
452     /**
453      * @deprecated
454      * @return
455      */

456     protected IContentItem getOutputContentItem() {
457         return runtimeContext.getOutputContentItem();
458     }
459
460     protected IContentItem getOutputContentItem(String JavaDoc outputName) {
461         return runtimeContext.getOutputContentItem(outputName);
462     }
463
464     protected void setOutputValue(String JavaDoc outputName, Object JavaDoc value) {
465         runtimeContext.setOutputValue(outputName, value);
466     }
467
468     protected void addTempParameter( String JavaDoc name, IActionParameter param ) {
469         runtimeContext.addTempParameter( name, param );
470     }
471
472     protected void addTempParameterObject( String JavaDoc name, Object JavaDoc paramObject ) {
473       String JavaDoc pType = "object"; //$NON-NLS-1$
474
IActionParameter actionParameter = new ActionParameter(name,
475           pType, paramObject, null, null);
476       addTempParameter(name, actionParameter);
477     }
478     
479     /**
480      *
481      * @deprecated
482      * @return
483      */

484     protected OutputStream JavaDoc getDefaultOutputStream() {
485         IContentItem contentItem = runtimeContext.getOutputContentItem();
486         if (contentItem != null) {
487             try {
488                 return contentItem.getOutputStream(getActionName());
489             } catch (Exception JavaDoc e) {
490             }
491         }
492         return null;
493     }
494
495     protected String JavaDoc applyInputsToFormat(String JavaDoc format) {
496         return runtimeContext.applyInputsToFormat(format, this);
497     }
498
499     protected IActionParameter getOutputItem(String JavaDoc outputName) {
500         return runtimeContext.getOutputParameter(outputName);
501     }
502
503     protected String JavaDoc getSolutionName() {
504         return runtimeContext.getSolutionName();
505     }
506
507     protected String JavaDoc getSolutionPath() {
508         return runtimeContext.getSolutionPath();
509     }
510
511     protected IActionParameter getInputParameter(String JavaDoc parameterName) {
512         return runtimeContext.getInputParameter(parameterName);
513     }
514
515     protected String JavaDoc getContentUrl(IContentItem contentItem) {
516         return runtimeContext.getContentUrl(contentItem);
517     }
518
519     protected boolean isPromptPending() {
520         return runtimeContext.isPromptPending();
521     }
522
523     protected void setFeedbackMimeType(String JavaDoc mimeType) {
524         IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem();
525         feedbackContentItem.setMimeType(mimeType);
526     }
527
528     /**
529      *
530      * @deprecated
531      * @return
532      */

533     protected void setOutputMimeType(String JavaDoc mimeType) {
534         IContentItem outputContentItem = runtimeContext.getOutputContentItem();
535         outputContentItem.setMimeType(mimeType);
536     }
537
538     protected void setOutputMimeType(String JavaDoc outputName, String JavaDoc mimeType) {
539         IContentItem outputContentItem = runtimeContext.getOutputContentItem(outputName);
540         outputContentItem.setMimeType(mimeType);
541     }
542     
543     protected OutputStream JavaDoc getFeedbackOutputStream() {
544         IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem();
545         if (feedbackContentItem != null) {
546             try {
547                 return feedbackContentItem.getOutputStream(getActionName());
548             } catch (Exception JavaDoc e) {
549             }
550         }
551         return null;
552     }
553
554     /**
555      * @deprecated
556      * @param actionParam
557      */

558     protected void createFeedbackParameter(IActionParameter actionParam) {
559         runtimeContext.createFeedbackParameter(actionParam);
560         runtimeContext.promptNeeded();
561     }
562
563     protected void createFeedbackParameter(SelectionMapper selMap, String JavaDoc fieldName, Object JavaDoc defaultValues) {
564         runtimeContext.createFeedbackParameter(selMap, fieldName, defaultValues);
565         runtimeContext.promptNeeded();
566     }
567
568     protected void createFeedbackParameter(String JavaDoc fieldName, String JavaDoc displayName, String JavaDoc hint, String JavaDoc defaultValue, boolean visible) {
569         runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValue, visible);
570         runtimeContext.promptNeeded();
571     }
572
573     public void createFeedbackParameter(String JavaDoc fieldName, String JavaDoc displayName, String JavaDoc hint, Object JavaDoc defaultValues, List JavaDoc values, Map JavaDoc dispNames, String JavaDoc displayStyle) {
574         runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValues, values, dispNames, displayStyle);
575         runtimeContext.promptNeeded();
576     }
577
578     protected DataSource JavaDoc getDataSource(String JavaDoc parameterName) {
579         return runtimeContext.getDataSource(parameterName);
580     }
581
582     protected DataSource JavaDoc getResourceDataSource(IActionResource resource) {
583         return runtimeContext.getResourceDataSource(resource);
584     }
585 }
586
Popular Tags