String moduleId = (String) args[0];
int dotIndex = moduleId.lastIndexOf(".");
if (moduleId.length() == dotIndex + 1) {
String msg = "Invalid file path for require method : " + moduleId;
log.error(msg);
throw new ScriptException(msg);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Map<String, ScriptableObject> requiredModules = (Map<String, ScriptableObject>) jaggeryContext.getProperty(
Constants.JAGGERY_REQUIRED_MODULES);
ScriptableObject object = requiredModules.get(moduleId);
if (object != null) {
return object;
}
if (dotIndex == -1) {
object = CommonManager.require(cx, thisObj, args, funObj);
initModule(cx, jaggeryContext, moduleId, object);
} else {
object = (ScriptableObject) cx.newObject(thisObj);
object.setPrototype(thisObj);
object.setParentScope(thisObj);
//sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope()
//object.setParentScope(sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope());
String ext = moduleId.substring(dotIndex + 1);
if (ext.equalsIgnoreCase("json")) {
object = executeScript(jaggeryContext, object, moduleId, true, true, false);
} else if (ext.equalsIgnoreCase("js")) {
object = executeScript(jaggeryContext, object, moduleId, false, true, false);
} else if (ext.equalsIgnoreCase("jag")) {
object = executeScript(jaggeryContext, object, moduleId, false, false, false);
} else {
String msg = "Unsupported file type for require() method : ." + ext;
log.error(msg);
throw new ScriptException(msg);
}
}
requiredModules.put(moduleId, object);
return object;
}