KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > event > BsfEventHandler


1 /*
2  * $Id: BsfEventHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.event;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import com.ibm.bsf.BSFException;
35 import com.ibm.bsf.BSFManager;
36 import com.ibm.bsf.util.IOUtils;
37
38 import org.ofbiz.base.util.cache.UtilCache;
39
40 /**
41  * BsfEventHandler - BSF Event Handler
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 2.2
46  */

47 public class BsfEventHandler implements EventHandler {
48     
49     public static final String JavaDoc module = BsfEventHandler.class.getName();
50     public static UtilCache eventCache = new UtilCache("webapp.BsfEvents");
51
52     /**
53      * @see org.ofbiz.webapp.event.EventHandler#init(javax.servlet.ServletContext)
54      */

55     public void init(ServletContext JavaDoc context) throws EventHandlerException {
56     }
57
58     /**
59      * @see org.ofbiz.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
60      */

61     public String JavaDoc invoke(String JavaDoc eventPath, String JavaDoc eventMethod, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws EventHandlerException {
62         ServletContext JavaDoc context = (ServletContext JavaDoc) request.getAttribute("servletContext");
63         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
64         if (cl == null)
65             cl = this.getClass().getClassLoader();
66         
67         if (context == null)
68             throw new EventHandlerException("Problem getting ServletContext");
69                    
70         try {
71             // create the BSF manager
72
BSFManager bsfManager = new BSFManager();
73             bsfManager.setClassLoader(cl);
74             
75             // expose the event objects to the script
76
bsfManager.declareBean("request", request, HttpServletRequest JavaDoc.class);
77             bsfManager.declareBean("response", response, HttpServletResponse JavaDoc.class);
78             
79             // get the script type
80
String JavaDoc scriptType = BSFManager.getLangFromFilename(eventMethod);
81             
82             // load the script
83
InputStream JavaDoc scriptStream = null;
84             String JavaDoc scriptString = null;
85             String JavaDoc cacheName = null;
86             if (eventPath == null || eventPath.length() == 0) {
87                 // we are a resource to be loaded off the classpath
88
cacheName = eventMethod;
89                 scriptString = (String JavaDoc) eventCache.get(cacheName);
90                 if (scriptString == null) {
91                     synchronized(this) {
92                         if (scriptString == null) {
93                             scriptStream = cl.getResourceAsStream(eventMethod);
94                             scriptString = IOUtils.getStringFromReader(new InputStreamReader JavaDoc(scriptStream));
95                             eventCache.put(cacheName, scriptString);
96                         }
97                     }
98                 }
99                                 
100             } else {
101                 // we are a script in the webapp - load by resource
102
cacheName = context.getServletContextName() + ":" + eventPath + eventMethod;
103                 scriptString = (String JavaDoc) eventCache.get(cacheName);
104                 if (scriptString == null) {
105                     synchronized(this) {
106                         if (scriptString == null) {
107                             scriptStream = context.getResourceAsStream(eventPath + eventMethod);
108                             scriptString = IOUtils.getStringFromReader(new InputStreamReader JavaDoc(scriptStream));
109                             eventCache.put(cacheName, scriptString);
110                         }
111                     }
112                 }
113             }
114                                                                                      
115             // execute the script
116
Object JavaDoc result = bsfManager.eval(scriptType, cacheName, 0, 0, scriptString);
117             
118             // check the result
119
if (result != null && !(result instanceof String JavaDoc)) {
120                 throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName());
121             }
122             
123             return (String JavaDoc) result;
124                                  
125         } catch(BSFException e) {
126             throw new EventHandlerException("BSF Error", e);
127         } catch (IOException JavaDoc e) {
128             throw new EventHandlerException("Problems reading script", e);
129         }
130     }
131 }
132
Popular Tags