use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_get.
public static Scriptable jsFunction_get(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
RegistryHostObject rho = (RegistryHostObject) thisObj;
if (arguments.length == 1) {
if (arguments[0] instanceof String) {
try {
Scriptable hostObject;
Resource resource = rho.registry.get((String) arguments[0]);
if (resource instanceof Collection) {
hostObject = cx.newObject(rho, "Collection", new Object[] { resource });
} else {
hostObject = cx.newObject(rho, "Resource", new Object[] { resource });
}
return hostObject;
} catch (RegistryException e) {
throw new ScriptException("Registry error occurred while executing get() operation", e);
}
} else {
throw new ScriptException("Path argument of method get() should be a string");
}
} else if (arguments.length == 3) {
if (arguments[0] instanceof String && arguments[1] instanceof Number && arguments[2] instanceof Number) {
try {
Collection collection = rho.registry.get((String) arguments[0], ((Number) arguments[1]).intValue(), ((Number) arguments[2]).intValue());
CollectionHostObject cho = (CollectionHostObject) cx.newObject(rho, "Collection", new Object[] { collection });
return cho;
} catch (RegistryException e) {
throw new ScriptException("Registry error occurred while executing get() operation", e);
}
} else {
throw new ScriptException("Invalid argument types for get() method");
}
} else {
throw new ScriptException("Invalid no. of arguments for get() method");
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_getComments.
public static Scriptable jsFunction_getComments(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getComments";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
try {
List<ScriptableObject> commentsArray = new ArrayList<ScriptableObject>();
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
Comment[] comments = registryHostObject.registry.getComments((String) args[0]);
for (Comment comment : comments) {
ScriptableObject commentObj = (ScriptableObject) cx.newObject(thisObj);
commentObj.put("cid", commentObj, comment.getCommentID());
commentObj.put("author", commentObj, comment.getUser());
commentObj.put("content", commentObj, comment.getText());
commentObj.put("created", commentObj, comment.getCreatedTime().getTime());
commentsArray.add(commentObj);
}
return cx.newArray(thisObj, commentsArray.toArray());
} catch (RegistryException e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_getAvgRating.
public static Number jsFunction_getAvgRating(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getAvgRating";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
try {
return registryHostObject.registry.getAverageRating((String) args[0]);
} catch (RegistryException e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_newCollection.
public static Scriptable jsFunction_newCollection(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
RegistryHostObject rho = (RegistryHostObject) thisObj;
if (arguments.length == 0) {
if (rho.registry != null) {
try {
Collection collection = rho.registry.newCollection();
CollectionHostObject cho = (CollectionHostObject) cx.newObject(rho, "Collection", new Object[] { collection });
return cho;
} catch (RegistryException e) {
throw new ScriptException("Error occurred while creating a new Collection", e);
}
} else {
throw new ScriptException("Registry has not initialized");
}
} else {
throw new ScriptException("newCollection() Method doesn't accept arguments");
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppFileManager method getJavaScriptFile.
@Override
public JavaScriptFile getJavaScriptFile(Object object) throws ScriptException {
if (object instanceof String) {
String path = (String) object;
if (path.startsWith(FILE_PATH)) {
return new JavaScriptFileManagerImpl().getJavaScriptFile(path);
}
WebAppFile webAppFile = new WebAppFile(path, context);
webAppFile.setFileManager(this);
return webAppFile;
} else if (object instanceof FileItem) {
UploadedFile uploadedFile = new UploadedFile((FileItem) object);
uploadedFile.setFileManager(this);
return uploadedFile;
} else {
String msg = "Unsupported parameter to the File constructor : " + object.getClass();
log.error(msg);
throw new ScriptException(msg);
}
}
Aggregations