}
// Invoke the BSF Script.
private Object serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
if (modelService.location == null || modelService.invoke == null)
throw new GenericServiceException("Cannot locate service to invoke");
// get the DispatchContext from the localName and ServiceDispatcher
DispatchContext dctx = dispatcher.getLocalContext(localName);
// get the classloader to use
ClassLoader cl = null;
if (dctx == null) {
cl = this.getClass().getClassLoader();
} else {
cl = dctx.getClassLoader();
}
String location = this.getLocation(modelService);
// create the manager object and set the classloader
BSFManager mgr = new BSFManager();
mgr.setClassLoader(cl);
mgr.registerBean("dctx", dctx);
mgr.registerBean("context", context);
// pre-load the engine to make sure we were called right
org.apache.bsf.BSFEngine bsfEngine = null;
try {
bsfEngine = mgr.loadScriptingEngine(modelService.engineName);
} catch (BSFException e) {
throw new GenericServiceException("Problems loading org.apache.bsf.BSFEngine: " + modelService.engineName, e);
}
// source the script into a string
String script = scriptCache.get(localName + "_" + location);
if (script == null) {
synchronized (this) {
script = scriptCache.get(localName + "_" + location);
if (script == null) {
URL scriptUrl = UtilURL.fromResource(location, cl);
if (scriptUrl != null) {
try {
HttpClient http = new HttpClient(scriptUrl);
script = http.get();
} catch (HttpClientException e) {
throw new GenericServiceException("Cannot read script from resource", e);
}
} else {
throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
}
if (script == null || script.length() < 2) {
throw new GenericServiceException("Null or empty script");
}
scriptCache.put(localName + "_" + location, script);
}
}
}
// now invoke the script
try {
bsfEngine.exec(location, 0, 0, script);
} catch (BSFException e) {
throw new GenericServiceException("Script invocation error", e);
}
return mgr.lookupBean("response");
}