Search in sources :

Example 1 with PluginScope

use of com.servoy.j2db.scripting.PluginScope in project servoy-client by Servoy.

the class DebugNGClient method createScriptEngine.

@Override
protected IExecutingEnviroment createScriptEngine() {
    RemoteDebugScriptEngine engine = new RemoteDebugScriptEngine(this);
    WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
    PluginScope scope = (PluginScope) engine.getSolutionScope().get("plugins", engine.getSolutionScope());
    scope.setLocked(false);
    for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
        if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
            scope.put(serviceSpecification.getScriptingName(), scope, new WebServiceScriptable(this, serviceSpecification, engine.getSolutionScope()));
        }
    }
    scope.setLocked(true);
    if (designerCallback != null) {
        designerCallback.addScriptObjects(this, engine.getSolutionScope());
    }
    return engine;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) PluginScope(com.servoy.j2db.scripting.PluginScope)

Example 2 with PluginScope

use of com.servoy.j2db.scripting.PluginScope in project servoy-client by Servoy.

the class NGClient method createScriptEngine.

@Override
protected IExecutingEnviroment createScriptEngine() {
    IExecutingEnviroment scriptEngine = super.createScriptEngine();
    WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
    PluginScope scope = (PluginScope) scriptEngine.getSolutionScope().get("plugins", scriptEngine.getSolutionScope());
    scope.setLocked(false);
    for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
        if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
            scope.put(ClientService.convertToJSName(serviceSpecification.getName()), scope, new WebServiceScriptable(this, serviceSpecification, scriptEngine.getSolutionScope()));
        }
    }
    scope.setLocked(true);
    return scriptEngine;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) PluginScope(com.servoy.j2db.scripting.PluginScope)

Example 3 with PluginScope

use of com.servoy.j2db.scripting.PluginScope in project servoy-client by Servoy.

the class NGClient method doCallCloseSolutionMethod.

private boolean doCallCloseSolutionMethod(boolean force) {
    boolean canClose = super.callCloseSolutionMethod(force);
    // cleanup here before script engine is destroyed
    if (canClose || force) {
        SpecProviderState specProviderState = WebServiceSpecProvider.getSpecProviderState();
        if (specProviderState != null) {
            WebObjectSpecification[] serviceSpecifications = specProviderState.getAllWebComponentSpecifications();
            for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
                WebObjectFunctionDefinition apiFunction = serviceSpecification.getApiFunction("cleanup");
                if (apiFunction != null && getScriptEngine() != null) {
                    final PluginScope scope = (PluginScope) getScriptEngine().getSolutionScope().get("plugins", getScriptEngine().getSolutionScope());
                    if (scope != null) {
                        final Scriptable service = (Scriptable) scope.get(serviceSpecification.getScriptingName(), null);
                        final Object api = service.get(apiFunction.getName(), null);
                        if (api instanceof Function) {
                            Runnable r = new Runnable() {

                                @Override
                                public void run() {
                                    Context context = Context.enter();
                                    try {
                                        ((Function) api).call(context, scope, service, ScriptRuntime.emptyArgs);
                                    } catch (Exception ex) {
                                        Debug.trace(ex);
                                    } finally {
                                        Context.exit();
                                    }
                                }
                            };
                            if (api instanceof WebServiceFunction) {
                                invokeAndWait(r);
                            } else {
                                r.run();
                            }
                        }
                    }
                }
            }
        }
    }
    return canClose;
}
Also used : BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) Context(org.mozilla.javascript.Context) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) Scriptable(org.mozilla.javascript.Scriptable) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) MalformedURLException(java.net.MalformedURLException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Function(org.mozilla.javascript.Function) WebServiceFunction(com.servoy.j2db.server.ngclient.scripting.WebServiceFunction) BrowserFunction(com.servoy.j2db.server.ngclient.property.BrowserFunction) SpecProviderState(org.sablo.specification.SpecProviderState) WebServiceFunction(com.servoy.j2db.server.ngclient.scripting.WebServiceFunction) PluginScope(com.servoy.j2db.scripting.PluginScope) BaseWebObject(org.sablo.BaseWebObject) JSONObject(org.json.JSONObject)

Example 4 with PluginScope

use of com.servoy.j2db.scripting.PluginScope in project servoy-client by Servoy.

the class NGClient method executeMethod.

/*
	 * @see org.sablo.websocket.IServerService#executeMethod(java.lang.String, org.json.JSONObject)
	 */
@Override
public Object executeMethod(String methodName, JSONObject args) throws Exception {
    switch(methodName) {
        case "login":
            try {
                credentials.setUserName(args.optString("username"));
                credentials.setPassword(args.optBoolean("encrypted") ? SecuritySupport.decrypt(Settings.getInstance(), args.optString("password")) : args.optString("password"));
                authenticate(null, null, new Object[] { credentials.getUserName(), credentials.getPassword() });
                if (getClientInfo().getUserUid() != null) {
                    wsSession.getEventDispatcher().postEvent(new Runnable() {

                        public void run() {
                            try {
                                loadSolution(getSolution().getName());
                            } catch (RepositoryException ex) {
                                Debug.error(ex);
                            }
                        }
                    });
                    if (args.optBoolean("remember")) {
                        JSONObject r = new JSONObject();
                        r.put("username", credentials.getUserName());
                        r.put("password", SecuritySupport.encrypt(Settings.getInstance(), credentials.getPassword()));
                        return r;
                    } else
                        return Boolean.TRUE;
                }
            } catch (Exception ex) {
                Debug.error(ex);
            }
            return Boolean.FALSE;
        case "autosave":
            getFoundSetManager().getEditRecordList().stopEditing(false);
            break;
        case "callServerSideApi":
            {
                String serviceScriptingName = args.getString("service");
                PluginScope scope = (PluginScope) getScriptEngine().getSolutionScope().get("plugins", getScriptEngine().getSolutionScope());
                Object service = scope.get(serviceScriptingName, scope);
                if (service instanceof WebServiceScriptable) {
                    WebServiceScriptable webServiceScriptable = (WebServiceScriptable) service;
                    JSONArray methodArguments = args.getJSONArray("args");
                    String serviceMethodName = args.getString("methodName");
                    // apply browser to sablo java value conversion - using server-side-API definition from the service's spec file if available (otherwise use default conversion)
                    // the call to webServiceScriptable.executeScopeFunction will do the java to Rhino one
                    // find spec for method
                    // get specification from plugins scope (which uses getScriptName() of service, then use the getClientService using the real name, to make sure client service is created if needed)
                    WebObjectSpecification serviceSpec = webServiceScriptable.getServiceSpecification();
                    BaseWebObject serviceWebObject = (BaseWebObject) getWebsocketSession().getClientService(serviceSpec.getName());
                    WebObjectFunctionDefinition functionSpec = (serviceSpec != null ? serviceSpec.getInternalApiFunction(serviceMethodName) : null);
                    if (functionSpec == null) {
                        functionSpec = (serviceSpec != null ? serviceSpec.getApiFunction(serviceMethodName) : null);
                    }
                    List<PropertyDescription> argumentPDs = (functionSpec != null ? functionSpec.getParameters() : null);
                    // apply conversion
                    Object[] arrayOfJavaConvertedMethodArgs = new Object[methodArguments.length()];
                    for (int i = 0; i < methodArguments.length(); i++) {
                        arrayOfJavaConvertedMethodArgs[i] = JSONUtils.fromJSON(null, methodArguments.get(i), (argumentPDs != null && argumentPDs.size() > i) ? argumentPDs.get(i) : null, new BrowserConverterContext(serviceWebObject, PushToServerEnum.allow), new ValueReference<Boolean>(false));
                    }
                    Object retVal = webServiceScriptable.executeScopeFunction(functionSpec, arrayOfJavaConvertedMethodArgs);
                    if (functionSpec != null && functionSpec.getReturnType() != null) {
                        // this means that when this return value is sent to client it will be converted to browser JSON correctly - if we give it the type
                        retVal = new TypedData<Object>(retVal, functionSpec.getReturnType());
                    }
                    return retVal;
                } else {
                    Debug.warn("callServerSideApi for unknown service '" + serviceScriptingName + "'");
                }
                break;
            }
    }
    return null;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) TypedData(org.sablo.websocket.TypedData) JSONArray(org.json.JSONArray) RepositoryException(com.servoy.j2db.persistence.RepositoryException) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) MalformedURLException(java.net.MalformedURLException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) BaseWebObject(org.sablo.BaseWebObject) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) JSONObject(org.json.JSONObject) PluginScope(com.servoy.j2db.scripting.PluginScope) BaseWebObject(org.sablo.BaseWebObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext)

Aggregations

PluginScope (com.servoy.j2db.scripting.PluginScope)4 WebServiceScriptable (com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable)4 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)4 ApplicationException (com.servoy.j2db.ApplicationException)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ServoyException (com.servoy.j2db.util.ServoyException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 RemoteException (java.rmi.RemoteException)2 ExecutionException (java.util.concurrent.ExecutionException)2 JSONObject (org.json.JSONObject)2 BaseWebObject (org.sablo.BaseWebObject)2 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)2 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)2 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)1 BrowserFunction (com.servoy.j2db.server.ngclient.property.BrowserFunction)1 WebServiceFunction (com.servoy.j2db.server.ngclient.scripting.WebServiceFunction)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONArray (org.json.JSONArray)1