Search in sources :

Example 6 with NativeObject

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);
    }
}
Also used : CarbonDataSource(org.wso2.carbon.ndatasource.core.CarbonDataSource) RDBMSDataSource(org.wso2.carbon.ndatasource.rdbms.RDBMSDataSource) Gson(com.google.gson.Gson) DataSourceManager(org.wso2.carbon.ndatasource.core.DataSourceManager) RDBMSConfiguration(org.wso2.carbon.ndatasource.rdbms.RDBMSConfiguration) DataSource(javax.sql.DataSource) CarbonDataSource(org.wso2.carbon.ndatasource.core.CarbonDataSource) RDBMSDataSource(org.wso2.carbon.ndatasource.rdbms.RDBMSDataSource) NativeObject(org.mozilla.javascript.NativeObject) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) DataSourceException(org.wso2.carbon.ndatasource.common.DataSourceException)

Example 7 with NativeObject

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;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) NativeObject(org.mozilla.javascript.NativeObject) NativeObject(org.mozilla.javascript.NativeObject)

Example 8 with NativeObject

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
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) NativeObject(org.mozilla.javascript.NativeObject) Test(org.testng.annotations.Test)

Example 9 with NativeObject

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 10 with NativeObject

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);
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

NativeObject (org.mozilla.javascript.NativeObject)10 Scriptable (org.mozilla.javascript.Scriptable)5 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 Gson (com.google.gson.Gson)2 ArgumentReader (com.scratchdisk.script.ArgumentReader)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 Function (org.mozilla.javascript.Function)2 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)2 StringArgumentReader (com.scratchdisk.script.StringArgumentReader)1 WeakIdentityHashMap (com.scratchdisk.util.WeakIdentityHashMap)1 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 Cookie (javax.servlet.http.Cookie)1 DataSource (javax.sql.DataSource)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 Test (org.junit.Test)1 Callable (org.mozilla.javascript.Callable)1 Context (org.mozilla.javascript.Context)1 MemberBox (org.mozilla.javascript.MemberBox)1