001: /*
002: * $Id: BsfEventHandler.java,v 1.2 2003/08/19 17:45:22 jonesde Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.event;
026:
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030:
031: import javax.servlet.ServletContext;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.ofbiz.base.util.UtilCache;
036:
037: import com.ibm.bsf.BSFException;
038: import com.ibm.bsf.BSFManager;
039: import com.ibm.bsf.util.IOUtils;
040:
041: /**
042: * BsfEventHandler - BSF Event Handler
043: *
044: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
045: * @version $Revision: 1.2 $
046: * @since 2.2
047: */
048: public class BsfEventHandler implements EventHandler {
049:
050: public static final String module = BsfEventHandler.class.getName();
051: public static UtilCache eventCache = new UtilCache(
052: "webapp.BsfEvents");
053:
054: /**
055: * @see org.ofbiz.content.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
056: */
057: public String invoke(String eventPath, String eventMethod,
058: HttpServletRequest request, HttpServletResponse response)
059: throws EventHandlerException {
060: ServletContext context = (ServletContext) request
061: .getAttribute("servletContext");
062: ClassLoader cl = Thread.currentThread().getContextClassLoader();
063: if (cl == null)
064: cl = this .getClass().getClassLoader();
065:
066: if (context == null)
067: throw new EventHandlerException(
068: "Problem getting ServletContext");
069:
070: try {
071: // create the BSF manager
072: BSFManager bsfManager = new BSFManager();
073: bsfManager.setClassLoader(cl);
074:
075: // expose the event objects to the script
076: bsfManager.declareBean("request", request,
077: HttpServletRequest.class);
078: bsfManager.declareBean("response", response,
079: HttpServletResponse.class);
080:
081: // get the script type
082: String scriptType = BSFManager
083: .getLangFromFilename(eventMethod);
084:
085: // load the script
086: InputStream scriptStream = null;
087: String scriptString = null;
088: String cacheName = null;
089: if (eventPath == null || eventPath.length() == 0) {
090: // we are a resource to be loaded off the classpath
091: cacheName = eventMethod;
092: scriptString = (String) eventCache.get(cacheName);
093: if (scriptString == null) {
094: synchronized (this ) {
095: if (scriptString == null) {
096: scriptStream = cl
097: .getResourceAsStream(eventMethod);
098: scriptString = IOUtils
099: .getStringFromReader(new InputStreamReader(
100: scriptStream));
101: eventCache.put(cacheName, scriptString);
102: }
103: }
104: }
105:
106: } else {
107: // we are a script in the webapp - load by resource
108: cacheName = context.getServletContextName() + ":"
109: + eventPath + eventMethod;
110: scriptString = (String) eventCache.get(cacheName);
111: if (scriptString == null) {
112: synchronized (this ) {
113: if (scriptString == null) {
114: scriptStream = context
115: .getResourceAsStream(eventPath
116: + eventMethod);
117: scriptString = IOUtils
118: .getStringFromReader(new InputStreamReader(
119: scriptStream));
120: eventCache.put(cacheName, scriptString);
121: }
122: }
123: }
124: }
125:
126: // execute the script
127: Object result = bsfManager.eval(scriptType, cacheName, 0,
128: 0, scriptString);
129:
130: // check the result
131: if (result != null && !(result instanceof String)) {
132: throw new EventHandlerException(
133: "Event did not return a String result, it returned a "
134: + result.getClass().getName());
135: }
136:
137: return (String) result;
138:
139: } catch (BSFException e) {
140: throw new EventHandlerException("BSF Error", e);
141: } catch (IOException e) {
142: throw new EventHandlerException("Problems reading script",
143: e);
144: }
145: }
146: }
|