use of org.mozilla.javascript.NativeObject in project jaggery by wso2.
the class DatabaseHostObject method jsConstructor.
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
int argsCount = args.length;
DatabaseHostObject db = new DatabaseHostObject();
//args count 1 for dataSource name
if (argsCount != 1 && argsCount != 3 && argsCount != 4) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string", args[0], true);
}
if (argsCount == 1) {
String dataSourceName = (String) args[0];
DataSourceManager dataSourceManager = new DataSourceManager();
try {
CarbonDataSource carbonDataSource = dataSourceManager.getInstance().getDataSourceRepository().getDataSource(dataSourceName);
DataSource dataSource = (DataSource) carbonDataSource.getDSObject();
db.conn = dataSource.getConnection();
db.context = cx;
return db;
} catch (DataSourceException e) {
log.error("Failed to access datasource " + dataSourceName, e);
} catch (SQLException e) {
log.error("Failed to get connection", e);
}
}
if (!(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "2", "string", args[1], true);
}
if (!(args[2] instanceof String) && !(args[2] instanceof Integer)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "3", "string", args[2], true);
}
NativeObject configs = null;
if (argsCount == 4) {
if (!(args[3] instanceof NativeObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "4", "object", args[3], true);
}
configs = (NativeObject) args[3];
}
String dbUrl = (String) args[0];
RDBMSConfiguration rdbmsConfig = new RDBMSConfiguration();
try {
if (configs != null) {
Gson gson = new Gson();
rdbmsConfig = gson.fromJson(HostObjectUtil.serializeJSON(configs), RDBMSConfiguration.class);
}
if (rdbmsConfig.getDriverClassName() == null || rdbmsConfig.getDriverClassName().equals("")) {
rdbmsConfig.setDriverClassName(getDriverClassName(dbUrl));
}
rdbmsConfig.setUsername((String) args[1]);
rdbmsConfig.setPassword((String) args[2]);
rdbmsConfig.setUrl(dbUrl);
try {
rdbmsDataSource = new RDBMSDataSource(rdbmsConfig);
} catch (DataSourceException e) {
throw new ScriptException(e);
}
db.conn = rdbmsDataSource.getDataSource().getConnection();
db.context = cx;
return db;
} catch (SQLException e) {
String msg = "Error connecting to the database : " + dbUrl;
log.warn(msg, e);
throw new ScriptException(msg, e);
}
}
use of org.mozilla.javascript.NativeObject in project OpenAM by OpenRock.
the class JavaScriptHttpClient method convertRequestData.
private Map convertRequestData(NativeObject requestData) {
HashMap<String, ArrayList<HashMap>> convertedRequestData = new HashMap<String, ArrayList<HashMap>>();
if (requestData != null) {
NativeArray cookies = (NativeArray) NativeObject.getProperty(requestData, "cookies");
ArrayList<HashMap> convertedCookies = new ArrayList<HashMap>();
if (cookies != null) {
Object[] cookieIds = cookies.getIds();
for (Object id : cookieIds) {
NativeObject cookie = (NativeObject) cookies.get((Integer) id, null);
String domain = (String) cookie.get("domain", null);
String field = (String) cookie.get("field", null);
String value = (String) cookie.get("value", null);
convertedCookies.add(convertCookie(domain, field, value));
}
}
convertedRequestData.put("cookies", convertedCookies);
NativeArray headers = (NativeArray) NativeObject.getProperty(requestData, "headers");
ArrayList<HashMap> convertedHeaders = new ArrayList<HashMap>();
if (headers != null) {
Object[] headerIds = headers.getIds();
for (Object id : headerIds) {
NativeObject header = (NativeObject) headers.get((Integer) id, null);
String field = (String) header.get("field", null);
String value = (String) header.get("value", null);
convertedHeaders.add(convertHeader(field, value));
}
}
convertedRequestData.put("headers", convertedHeaders);
}
return convertedRequestData;
}
use of org.mozilla.javascript.NativeObject in project OpenAM by OpenRock.
the class JavaScriptMapFactoryTest method testInvalidObjectKeyThrowsIllegalArgument.
@Test(expectedExceptions = IllegalArgumentException.class)
public void testInvalidObjectKeyThrowsIllegalArgument() {
//given
NativeObject mockNativeObject = mock(NativeObject.class);
Object[] listOfItems = new Object[1];
listOfItems[0] = new Object();
when(mockNativeObject.getIds()).thenReturn(listOfItems);
//when
Map<String, Object> mapRepresentation = JavaScriptMapFactory.javaScriptObjectToMap(mockNativeObject);
//then - caught by exception
}
use of org.mozilla.javascript.NativeObject in project SmartZPN by andforce.
the class PacScriptParser method runScript.
private String runScript(String js, String functionName, Object[] functionParams) {
Context rhino = Context.enter();
rhino.setOptimizationLevel(-1);
try {
Scriptable scope = rhino.initStandardObjects();
rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
Function function = (Function) scope.get(functionName, scope);
Object result = function.call(rhino, scope, scope, functionParams);
if (result instanceof String) {
return (String) result;
} else if (result instanceof NativeJavaObject) {
return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
} else if (result instanceof NativeObject) {
return (String) ((NativeObject) result).getDefaultValue(String.class);
}
return result.toString();
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.NativeObject in project scriptographer by scriptographer.
the class RhinoEngine method createScope.
public Scope createScope() {
Scriptable scope = new NativeObject();
// Sharing the top level scope:
// https://developer.mozilla.org/En/Rhino_documentation/Scopes_and_Contexts
scope.setPrototype(topLevel);
scope.setParentScope(null);
return new RhinoScope(this, scope);
}
Aggregations