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 Jan 26, 2006
014: * @author James Dixon
015: */
016:
017: package org.pentaho.plugin.core;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.File;
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
027: import org.pentaho.actionsequence.dom.actions.SubActionAction;
028: import org.pentaho.core.runtime.IActionParameter;
029: import org.pentaho.core.runtime.IRuntimeContext;
030: import org.pentaho.core.session.IPentahoSession;
031: import org.pentaho.core.session.UserSession;
032: import org.pentaho.core.solution.ISolutionEngine;
033: import org.pentaho.core.solution.SolutionHelper;
034: import org.pentaho.messages.Messages;
035: import org.pentaho.messages.util.LocaleHelper;
036: import org.pentaho.plugin.ComponentBase;
037:
038: public class SubActionComponent extends ComponentBase {
039:
040: private static final long serialVersionUID = 3557732430102823611L;
041:
042: public Log getLogger() {
043: return LogFactory.getLog(SubActionComponent.class);
044: }
045:
046: protected boolean validateAction() {
047: boolean value = false;
048: SubActionAction subAction = null;
049:
050: if (getActionDefinition() instanceof SubActionAction) {
051: subAction = (SubActionAction) getActionDefinition();
052:
053: if (subAction.getAction() != IActionInputValueProvider.NULL_INPUT
054: && subAction.getPath() != IActionInputValueProvider.NULL_INPUT
055: && subAction.getSolution() != IActionInputValueProvider.NULL_INPUT) {
056: value = true;
057: }
058: } else {
059: error(Messages
060: .getErrorString(
061: "ComponentBase.ERROR_0001_UNKNOWN_ACTION_TYPE", getActionDefinition().getElement().asXML())); //$NON-NLS-1$
062: }
063:
064: return value;
065: }
066:
067: protected boolean validateSystemSettings() {
068: return true;
069: }
070:
071: public void done() {
072: }
073:
074: protected boolean executeAction() throws Throwable {
075: SubActionAction subAction = (SubActionAction) getActionDefinition();
076:
077: String solution = subAction.getSolution().getStringValue();
078: String path = subAction.getPath().getStringValue();
079: String action = subAction.getAction().getStringValue();
080: String actionPath;
081: if (path == null || path.equals("")) { //$NON-NLS-1$
082: actionPath = solution + File.separator + action;
083: } else {
084: actionPath = solution + File.separator + path
085: + File.separator + action;
086: }
087:
088: // see if we are supposed to proxy the session
089: IPentahoSession session = getSession();
090: if (subAction.getSessionProxy() != IActionInputValueProvider.NULL_INPUT) {
091: String sessionName = subAction.getSessionProxy()
092: .getStringValue();
093: // TODO support user-by-user locales
094: session = new UserSession(sessionName, LocaleHelper
095: .getLocale());
096: }
097:
098: // create a parameter provider
099: HashMap parameters = new HashMap();
100: Iterator iterator = getInputNames().iterator();
101: while (iterator.hasNext()) {
102: String inputName = (String) iterator.next();
103: if (!StandardSettings.SOLUTION.equals(inputName)
104: && !StandardSettings.PATH.equals(inputName)
105: && !StandardSettings.ACTION.equals(inputName)) {
106: Object value = getInputValue(inputName);
107: parameters.put(inputName, value);
108: }
109: }
110:
111: parameters
112: .put(
113: StandardSettings.ACTION_URL_COMPONENT,
114: getInputStringValue(StandardSettings.ACTION_URL_COMPONENT));
115:
116: // get the ouptut stream
117: // TODO verify this with MB and JD
118: ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // getDefaultOutputStream();
119:
120: ISolutionEngine solutionEngine = SolutionHelper.execute(
121: getProcessId(), session, actionPath, parameters,
122: outputStream);
123: if (outputStream.size() > 0) {
124: getDefaultOutputStream(null).write(
125: outputStream.toByteArray());
126: }
127:
128: int status = solutionEngine.getStatus();
129: if (status == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
130: // now pass any outputs back
131: Iterator it = this .getOutputNames().iterator();
132: while (it.hasNext()) {
133: String outputName = (String) it.next();
134: IActionParameter param = solutionEngine
135: .getExecutionContext().getOutputParameter(
136: outputName);
137: if (param != null) {
138: setOutputValue(outputName, param.getValue());
139: }
140: }
141: return true;
142: } else {
143: return false;
144: }
145: }
146:
147: public boolean init() {
148: return true;
149: }
150:
151: }
|