001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.scripting;
021:
022: import java.io.IOException;
023:
024: import org.apache.bsf.BSFEngine;
025: import org.apache.bsf.BSFException;
026: import org.apache.bsf.BSFManager;
027: import org.apache.log4j.Logger;
028:
029: import de.schlund.pfixcore.generator.IHandler;
030: import de.schlund.pfixcore.generator.IWrapper;
031: import de.schlund.pfixcore.workflow.Context;
032:
033: /**
034: *
035: * @author Benjamin Reitzammer <benjamin@schlund.de>
036: *
037: */
038: public class ScriptingIHandler implements IHandler {
039:
040: // ****
041: // NOTE:
042: // ****
043: //
044: // This IHandler has instance variables because it is created new each time and not
045: // controlled by the IHandlerFactory. This may change soon, but for now it works OK.
046:
047: private final static Logger LOG = Logger
048: .getLogger(ScriptingIHandler.class);
049:
050: /**
051: */
052: private BSFEngine bsfEngine = null;
053:
054: /**
055: */
056: private String path = null;
057:
058: /**
059: */
060: private boolean init = false;
061:
062: /**
063: * @exception IllegalArgumentException if the provided path is <code>null</code>,
064: */
065: public ScriptingIHandler(String path) {
066: if (path == null)
067: throw new IllegalArgumentException(
068: "Parameter 'path' is not allowed to be null");
069:
070: this .path = path;
071: }
072:
073: /**
074: */
075: public void handleSubmittedData(Context context, IWrapper wrapper)
076: throws Exception {
077: init();
078: bsfEngine.call(null, "handleSubmittedData", new Object[] {
079: context, wrapper });
080: }
081:
082: /**
083: */
084: public void retrieveCurrentStatus(Context context, IWrapper wrapper)
085: throws Exception {
086: init();
087: bsfEngine.call(null, "retrieveCurrentStatus", new Object[] {
088: context, wrapper });
089: }
090:
091: /**
092: */
093: public boolean prerequisitesMet(Context context) throws Exception {
094: init();
095: return ScriptingUtil.exec(bsfEngine, "prerequisitesMet",
096: new Object[] { context });
097: }
098:
099: /**
100: */
101: public boolean isActive(Context context) throws Exception {
102: init();
103: return ScriptingUtil.exec(bsfEngine, "isActive",
104: new Object[] { context });
105: }
106:
107: /**
108: */
109: public boolean needsData(Context context) throws Exception {
110: init();
111: return ScriptingUtil.exec(bsfEngine, "needsData",
112: new Object[] { context });
113: }
114:
115: /**
116: *
117: */
118: protected void init() throws BSFException, IOException {
119:
120: if (!init) {
121: LOG.debug("Initializing ScriptingIHandler for path: "
122: + path);
123:
124: String lang = BSFManager.getLangFromFilename(path);
125:
126: BSFManager manager = new BSFManager();
127: manager.declareBean("LOG", LOG, Logger.class);
128: manager.exec(lang, path, 0, 0, ScriptingUtil
129: .getScript(path));
130:
131: bsfEngine = manager.loadScriptingEngine(lang);
132:
133: init = true;
134: }
135:
136: }
137:
138: }
|