KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > SubActionComponent


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 Jan 26, 2006
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.core;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.pentaho.core.runtime.IActionParameter;
27 import org.pentaho.core.runtime.IRuntimeContext;
28 import org.pentaho.core.session.IPentahoSession;
29 import org.pentaho.core.session.UserSession;
30 import org.pentaho.core.solution.ISolutionEngine;
31 import org.pentaho.core.solution.SolutionHelper;
32 import org.pentaho.messages.util.LocaleHelper;
33 import org.pentaho.plugin.ComponentBase;
34
35 public class SubActionComponent extends ComponentBase {
36
37     private static final long serialVersionUID = 3557732430102823611L;
38
39     private static final String JavaDoc SESSION_PROXY = "session-proxy"; //$NON-NLS-1$
40

41     public Log getLogger() {
42         return LogFactory.getLog(SubActionComponent.class);
43     }
44
45     protected boolean validateAction() {
46
47         return isDefinedInput(StandardSettings.SOLUTION) && isDefinedInput(StandardSettings.PATH) && isDefinedInput(StandardSettings.ACTION);
48
49     }
50
51     protected boolean validateSystemSettings() {
52         return true;
53     }
54
55     public void done() {
56     }
57
58     protected boolean executeAction() throws Throwable JavaDoc {
59
60         String JavaDoc solution = getInputStringValue(StandardSettings.SOLUTION);
61         String JavaDoc path = getInputStringValue(StandardSettings.PATH);
62         String JavaDoc action = getInputStringValue(StandardSettings.ACTION);
63         String JavaDoc actionPath;
64         if (path == null || path.equals("")) { //$NON-NLS-1$
65
actionPath = solution + File.separator + action;
66         } else {
67             actionPath = solution + File.separator + path + File.separator + action;
68         }
69
70         // see if we are supposed to proxy the session
71
IPentahoSession session = getSession();
72         if (isDefinedInput(SESSION_PROXY)) {
73             String JavaDoc sessionProxy = getInputStringValue(SESSION_PROXY);
74             if (isDefinedInput(sessionProxy)) {
75                 String JavaDoc sessionName = getInputStringValue(sessionProxy);
76                 // TODO support user-by-user locales
77
session = new UserSession(sessionName, LocaleHelper.getLocale());
78             }
79         }
80
81         // create a parameter provider
82
HashMap JavaDoc parameters = new HashMap JavaDoc();
83         Iterator JavaDoc iterator = getInputNames().iterator();
84         while (iterator.hasNext()) {
85             String JavaDoc inputName = (String JavaDoc) iterator.next();
86             if (!StandardSettings.SOLUTION.equals(inputName) && !StandardSettings.PATH.equals(inputName) && !StandardSettings.ACTION.equals(inputName)) {
87                 Object JavaDoc value = getInputValue(inputName);
88                 parameters.put(inputName, value);
89             }
90         }
91
92         // get the ouptut stream
93
// TODO verify this with MB and JD
94
ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc(); // getDefaultOutputStream();
95

96         ISolutionEngine solutionEngine = SolutionHelper.execute(getProcessId(), session, actionPath, parameters, outputStream);
97         if ( outputStream.size() > 0 ) {
98             getDefaultOutputStream().write( outputStream.toByteArray() );
99         }
100         
101         int status = solutionEngine.getStatus();
102         if (status == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
103             // now pass any outputs back
104
Iterator JavaDoc it = this.getOutputNames().iterator();
105             while (it.hasNext()) {
106                 String JavaDoc outputName = (String JavaDoc) it.next();
107                 IActionParameter param = solutionEngine.getExecutionContext().getOutputParameter(outputName);
108                 if ( param != null ) {
109                     setOutputValue( outputName, param.getValue() );
110                 }
111             }
112             return true;
113         } else {
114             return false;
115         }
116     }
117
118     public boolean init() {
119         return true;
120     }
121
122 }
123
Popular Tags