001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.runtime.tool;
046:
047: import com.ibm.bsf.BSFException;
048: import com.ibm.bsf.BSFManager;
049: import com.ibm.bsf.util.ObjectRegistry;
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052: import org.obe.XMLException;
053: import org.obe.client.api.repository.BSFScriptMetaData;
054: import org.obe.client.api.repository.RepositoryException;
055: import org.obe.client.api.tool.ToolInvocation;
056: import org.obe.spi.WorkflowContext;
057: import org.obe.xpdl.model.data.ParameterMode;
058:
059: import java.io.IOException;
060: import java.lang.reflect.InvocationTargetException;
061:
062: /**
063: * A tool agent that executes a BSF-supported script.
064: *
065: * @author Adrian Price
066: * @see <a href="http://jakarta.apache.org/bsf/">Bean Scripting Framework</a>
067: */
068: public class BSFScript extends Script {
069: private static final long serialVersionUID = -8733178487092262357L;
070: public static final String DOC_URL = "http://www-124.ibm.com/developerworks/projects/bsf";
071: private static final Log log = LogFactory.getLog(BSFScript.class);
072:
073: private final BSFScriptMetaData _metadata;
074:
075: public BSFScript(BSFScriptMetaData metadata) {
076: super (metadata);
077: if (!BSFManager.isLanguageRegistered(metadata.getLanguage()))
078: throw new IllegalArgumentException(metadata.getLanguage());
079: _metadata = metadata;
080: }
081:
082: protected int _invokeApplication(ToolInvocation ti)
083: throws InvocationTargetException {
084:
085: try {
086: BSFManager bsfMgr = new BSFManager();
087:
088: // Set up the standard objects.
089: WorkflowContext ctx = getWorkflowContext();
090: bsfMgr.declareBean("log", log, Log.class);
091: bsfMgr.declareBean("obe", ctx, WorkflowContext.class);
092:
093: // Setup the ti.parameters.
094: for (int i = 0; i < ti.parameters.length; i++) {
095: if (ti.parameters[i].getMode() != ParameterMode.OUT) {
096: log.debug("Putting "
097: + ti.parameters[i].getFormalParm().getId()
098: + " = " + ti.parameters[i].getValue()
099: + " into " + _metadata.getLanguage()
100: + " interpreter");
101:
102: Object bean = ti.parameters[i].getValue();
103: bsfMgr.declareBean(ti.parameters[i].getFormalParm()
104: .getId(), bean != null ? bean
105: : new Object(), ti.parameters[i]
106: .getFormalClass());
107: }
108: }
109: // TODO: optimize performance by precompilation, pooling, caching...
110: String source = _metadata.isFile() ? _metadata.getScript()
111: : "<buffer>";
112: String script = readScript();
113:
114: // Execute the script.
115: _status = ACTIVE;
116: bsfMgr.exec(_metadata.getLanguage(), source, 1, 0, script);
117:
118: // Retrieve the results. N.B. In order for INOUT & OUT ti.parameters to
119: // work correctly, the script must set the register the required output
120: // values by calling bsf.registerBean(String name, Object bean);
121: ObjectRegistry registry = bsfMgr.getObjectRegistry();
122: for (int i = 0; i < ti.parameters.length; i++) {
123: if (ti.parameters[i].getMode() != ParameterMode.IN) {
124: log.debug("Getting "
125: + ti.parameters[i].getFormalParm().getId()
126: + " from " + _metadata.getLanguage()
127: + " interpreter");
128:
129: Object value = registry.lookup(ti.parameters[i]
130: .getFormalParm().getId());
131: ti.parameters[i].setValue(value == null ? null
132: : value.toString());
133: }
134: }
135: return 0;
136: } catch (BSFException e) {
137: throw new InvocationTargetException(e);
138: } catch (IOException e) {
139: throw new InvocationTargetException(e);
140: } catch (XMLException e) {
141: throw new InvocationTargetException(e);
142: } catch (RepositoryException e) {
143: throw new InvocationTargetException(e);
144: }
145: }
146: }
|