throw new EventHandlerException("Problem getting ServletContext");
}
try {
// create the BSF manager
BSFManager bsfManager = new BSFManager();
bsfManager.setClassLoader(cl);
// expose the event objects to the script
bsfManager.declareBean("request", request, HttpServletRequest.class);
bsfManager.declareBean("response", response, HttpServletResponse.class);
// get the script type
String scriptType = BSFManager.getLangFromFilename(event.invoke);
// load the script
InputStream scriptStream = null;
String scriptString = null;
String cacheName = null;
if (UtilValidate.isEmpty(event.path)) {
// we are a resource to be loaded off the classpath
cacheName = event.invoke;
scriptString = eventCache.get(cacheName);
if (scriptString == null) {
synchronized(eventCache) {
if (scriptString == null) {
if (Debug.verboseOn()) {
Debug.logVerbose("Loading BSF Script at location: " + cacheName, module);
}
URL scriptUrl = FlexibleLocation.resolveLocation(cacheName);
if (scriptUrl == null) {
throw new EventHandlerException("BSF script not found at location [" + cacheName + "]");
}
scriptStream = scriptUrl.openStream();
scriptString = IOUtils.getStringFromReader(new InputStreamReader(scriptStream));
scriptStream.close();
eventCache.put(cacheName, scriptString);
}
}
}
} else {
// we are a script in the webapp - load by resource
cacheName = context.getServletContextName() + ":" + event.path + event.invoke;
scriptString = eventCache.get(cacheName);
if (scriptString == null) {
synchronized(eventCache) {
if (scriptString == null) {
scriptStream = context.getResourceAsStream(event.path + event.invoke);
if (scriptStream == null) {
throw new EventHandlerException("Could not find BSF script file in webapp context: " + event.path + event.invoke);
}
scriptString = IOUtils.getStringFromReader(new InputStreamReader(scriptStream));
scriptStream.close();
eventCache.put(cacheName, scriptString);
}
}
}
}
// execute the script
Object result = bsfManager.eval(scriptType, cacheName, 0, 0, scriptString);
// check the result
if (result != null && !(result instanceof String)) {
throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName());
}