1 25 package org.ofbiz.webapp.event; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 47 public class BsfEventHandler implements EventHandler { 48 49 public static final String module = BsfEventHandler.class.getName(); 50 public static UtilCache eventCache = new UtilCache("webapp.BsfEvents"); 51 52 55 public void init(ServletContext context) throws EventHandlerException { 56 } 57 58 61 public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { 62 ServletContext context = (ServletContext ) request.getAttribute("servletContext"); 63 ClassLoader 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 BSFManager bsfManager = new BSFManager(); 73 bsfManager.setClassLoader(cl); 74 75 bsfManager.declareBean("request", request, HttpServletRequest .class); 77 bsfManager.declareBean("response", response, HttpServletResponse .class); 78 79 String scriptType = BSFManager.getLangFromFilename(eventMethod); 81 82 InputStream scriptStream = null; 84 String scriptString = null; 85 String cacheName = null; 86 if (eventPath == null || eventPath.length() == 0) { 87 cacheName = eventMethod; 89 scriptString = (String ) 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 (scriptStream)); 95 eventCache.put(cacheName, scriptString); 96 } 97 } 98 } 99 100 } else { 101 cacheName = context.getServletContextName() + ":" + eventPath + eventMethod; 103 scriptString = (String ) 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 (scriptStream)); 109 eventCache.put(cacheName, scriptString); 110 } 111 } 112 } 113 } 114 115 Object result = bsfManager.eval(scriptType, cacheName, 0, 0, scriptString); 117 118 if (result != null && !(result instanceof String )) { 120 throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName()); 121 } 122 123 return (String ) result; 124 125 } catch(BSFException e) { 126 throw new EventHandlerException("BSF Error", e); 127 } catch (IOException e) { 128 throw new EventHandlerException("Problems reading script", e); 129 } 130 } 131 } 132 | Popular Tags |