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: import java.util.Properties;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.bsf.BSFEngine;
028: import org.apache.bsf.BSFException;
029: import org.apache.bsf.BSFManager;
030: import org.apache.log4j.Logger;
031:
032: import de.schlund.pfixcore.workflow.Context;
033: import de.schlund.pfixcore.workflow.State;
034: import de.schlund.pfixxml.PfixServletRequest;
035: import de.schlund.pfixxml.ResultDocument;
036:
037: /**
038: * This class creates a <code>BSFEngine</code>-instance for every request, to
039: * make the use of instance properties, crossing request-boundaries,
040: * inside the script impossible.
041: *
042: * @author Benjamin Reitzammer <benjamin@schlund.de>
043: */
044: public class ScriptingState implements State {
045:
046: public static final String PROP_SCRIPT_PATH = "SCRIPTINGSTATE_SRC_PATH";
047: private static final String REQATTR_BSFENG = "SCRIPTINGSTATE_REQUEST_BSFENGINE";
048: private static final Logger LOG = Logger
049: .getLogger(ScriptingState.class);
050:
051: /**
052: */
053: public ScriptingState() {
054: }
055:
056: /**
057: *
058: */
059: public boolean isAccessible(Context context, PfixServletRequest preq)
060: throws Exception {
061: return ScriptingUtil.exec(getEngine(context, preq),
062: "isAccessible", new Object[] { context, preq });
063: }
064:
065: /**
066: *
067: */
068: public boolean needsData(Context context, PfixServletRequest preq)
069: throws Exception {
070: return ScriptingUtil.exec(getEngine(context, preq),
071: "needsData", new Object[] { context, preq });
072: }
073:
074: /**
075: *
076: */
077: public ResultDocument getDocument(Context context,
078: PfixServletRequest preq) throws Exception {
079: return (ResultDocument) getEngine(context, preq).call(null,
080: "getDocument", new Object[] { context, preq });
081: }
082:
083: // ============ private Helper methods ============
084:
085: /**
086: * @exception IllegalStateException if the properties for the current page
087: * request don't contain a property, that denotes the
088: */
089: protected BSFEngine getEngine(Context context,
090: PfixServletRequest preq) throws BSFException, IOException {
091:
092: String path = getScriptPath(context);
093: BSFEngine bsfengine = null;
094:
095: HttpServletRequest req = preq.getRequest();
096:
097: bsfengine = (BSFEngine) req.getAttribute(REQATTR_BSFENG);
098:
099: // load the BSFEngine only once per request
100: if (bsfengine == null || !ScriptingUtil.isCachedCurrent(path)) {
101:
102: LOG.debug("Initializing ScriptingState with script path: "
103: + path);
104:
105: String lang = BSFManager.getLangFromFilename(path);
106:
107: BSFManager manager = new BSFManager();
108: manager.declareBean("LOG", LOG, Logger.class);
109: manager.exec(lang, path, 0, 0, ScriptingUtil
110: .getScript(path));
111:
112: bsfengine = manager.loadScriptingEngine(lang);
113:
114: req.setAttribute(REQATTR_BSFENG, bsfengine);
115: }
116:
117: return bsfengine;
118: }
119:
120: /**
121: *
122: */
123: public String getScriptPath(Context context) {
124: Properties props = context.getPropertiesForCurrentPageRequest();
125: String path = props
126: .getProperty(ScriptingState.PROP_SCRIPT_PATH);
127:
128: if (path == null) {
129: String pr = context.getCurrentPageRequest().getName();
130: throw new IllegalArgumentException("ScriptingState for "
131: + pr + " has no script path specified");
132: } else {
133: return path;
134: }
135: }
136:
137: }
|