Search in sources :

Example 1 with ScriptEngine

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

the class HeadlessClientFactoryInternal method createImportHookClient.

public static ISessionClient createImportHookClient(final Solution importHookModule, final IXMLImportUserChannel channel) throws Exception {
    final String[] loadException = new String[1];
    // assuming no login and no method args for import hooks
    SessionClient sc = new SessionClient(null, null, null, null, null, importHookModule.getName()) {

        @Override
        protected IActiveSolutionHandler createActiveSolutionHandler() {
            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
            return new LocalActiveSolutionHandler(as, this) {

                @Override
                protected Solution loadSolution(RootObjectMetaData solutionDef) throws RemoteException, RepositoryException {
                    // grab the latest version (-1) not the active one, because the hook was not yet activated.
                    return (Solution) ((IDeveloperRepository) getRepository()).getRootObject(solutionDef.getRootObjectId(), -1);
                }
            };
        }

        @Override
        protected IExecutingEnviroment createScriptEngine() {
            return new ScriptEngine(this) {

                @Override
                public Object executeFunction(Function f, Scriptable scope, Scriptable thisObject, Object[] args, boolean focusEvent, boolean throwException) throws Exception {
                    // always throw exception
                    return super.executeFunction(f, scope, thisObject, args, focusEvent, true);
                }
            };
        }

        @Override
        public void reportError(String msg, Object detail) {
            super.reportError(msg, detail);
            loadException[0] = msg;
            if (detail instanceof JavaScriptException && ((JavaScriptException) detail).getValue() instanceof Scriptable) {
                loadException[0] += " " + Utils.getScriptableString((Scriptable) ((JavaScriptException) detail).getValue());
            }
            if (detail instanceof Exception) {
                loadException[0] += " " + ((Exception) detail).getMessage();
            }
        }
    };
    sc.setUseLoginSolution(false);
    String userName = channel.getImporterUsername();
    if (userName != null) {
        // let the import hook client run with credentials from the logged in user from the admin page.
        sc.getClientInfo().setUserUid(ApplicationServerRegistry.get().getUserManager().getUserUID(sc.getClientID(), userName));
        sc.getClientInfo().setUserName(userName);
    }
    sc.setOutputChannel(channel);
    sc.loadSolution(importHookModule.getName());
    if (loadException[0] != null) {
        sc.shutDown(true);
        throw new RepositoryException(loadException[0]);
    }
    return sc;
}
Also used : RootObjectMetaData(com.servoy.j2db.persistence.RootObjectMetaData) ISessionClient(com.servoy.j2db.ISessionClient) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) LocalActiveSolutionHandler(com.servoy.j2db.LocalActiveSolutionHandler) Scriptable(org.mozilla.javascript.Scriptable) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) JavaScriptException(org.mozilla.javascript.JavaScriptException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JavaScriptException(org.mozilla.javascript.JavaScriptException) Function(org.mozilla.javascript.Function) Solution(com.servoy.j2db.persistence.Solution)

Example 2 with ScriptEngine

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

the class BasicFormController method executeFunction.

@SuppressWarnings("nls")
protected Object executeFunction(Function f, Object[] args, Scriptable scope, Scriptable thisObject, boolean saveData, Object src, boolean testFindMode, boolean focusEvent, String methodKey, boolean executeWhenFieldValidationFailed, boolean useFormAsEventSourceEventually, boolean throwException) throws Exception {
    if (// only run certain methods in find
    !(testFindMode && isInFindMode())) {
        // this is a semi saveData , we do NOT want the record go out of edit(!) and is updated in db
        if (saveData) {
            application.getFoundSetManager().getEditRecordList().prepareForSave(false);
        }
        if (f != null) {
            if (!executeWhenFieldValidationFailed && Boolean.TRUE.equals(application.getRuntimeProperties().get(IServiceProvider.RT_LASTFIELDVALIDATIONFAILED_FLAG))) {
                if (Debug.tracing()) {
                    Debug.trace("Function not executed because a field is marked invalid");
                }
                return null;
            }
            FormAndComponent formAndComponent = getJSApplicationNames(src, f, useFormAsEventSourceEventually);
            try {
                currentFormExecutingFunctionCount.incrementAndGet();
                Object[] newArgs = args;
                if (formAndComponent != null) {
                    // for use of deprecated aplication.getMethodTriggerElementName() and aplication.getMethodTriggerFormName()
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().pushLastNames(formAndComponent);
                    }
                    if (methodKey != null) {
                        // add form event if needed
                        MethodTemplate methodTemplate = MethodTemplate.getTemplate(null, methodKey);
                        if (methodTemplate != null) {
                            MethodArgument[] methodArguments = methodTemplate.getArguments();
                            for (int i = 0; methodArguments != null && i < methodArguments.length; i++) {
                                if (methodArguments[i].getType() == ArgumentType.JSEvent) {
                                    // method template declares an event argument
                                    if (args == null || args.length <= i || args[i] == null) {
                                        // no event argument there yet, insert a form event
                                        JSEvent event = getJSEvent(formAndComponent.src, methodKey);
                                        if (args == null || args.length <= i) {
                                            newArgs = new Object[i + 1];
                                            if (args != null) {
                                                System.arraycopy(args, 0, newArgs, 0, args.length);
                                            }
                                        }
                                        newArgs[i] = event;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                if (newArgs != null && newArgs.length > 0) {
                    for (Object newArg : newArgs) {
                        if (newArg instanceof JSEvent) {
                            JSEvent event = (JSEvent) newArg;
                            if (formScope != null && event.getSource() instanceof IComponent && ((IComponent) event.getSource()).getName() != null) {
                                Object elementScope = formScope.get("elements");
                                if (elementScope instanceof Scriptable) {
                                    Object elementSrc = ((Scriptable) elementScope).get(((IComponent) event.getSource()).getName(), (Scriptable) elementScope);
                                    if (elementSrc != null) {
                                        if (elementSrc instanceof ITwoNativeJavaObject) {
                                            Object scriptable = event.getSource();
                                            if (scriptable instanceof IScriptableProvider) {
                                                scriptable = ((IScriptableProvider) scriptable).getScriptObject();
                                            }
                                            ((ITwoNativeJavaObject) elementSrc).setRealObject(scriptable);
                                        }
                                        event.setSource(elementSrc);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                return application.getScriptEngine().executeFunction(f, scope, thisObject, newArgs, focusEvent, throwException);
            } finally {
                currentFormExecutingFunctionCount.decrementAndGet();
                if (formAndComponent != null) {
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().popLastStackNames(formAndComponent);
                    }
                }
                // after a script clear the unchanged records so that no records keep hanging around.
                if (!focusEvent && !"onRecordEditStopMethodID".equals(methodKey) && !"onRenderMethodID".equals(methodKey) && application.getFoundSetManager() != null) {
                    application.getFoundSetManager().getEditRecordList().removeUnChangedRecords(false, false);
                }
            }
        }
    }
    return null;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) JSEvent(com.servoy.j2db.scripting.JSEvent) MethodArgument(com.servoy.j2db.persistence.MethodArgument) IComponent(com.servoy.j2db.ui.IComponent) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate) Scriptable(org.mozilla.javascript.Scriptable) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider)

Aggregations

ScriptEngine (com.servoy.j2db.scripting.ScriptEngine)2 Scriptable (org.mozilla.javascript.Scriptable)2 ISessionClient (com.servoy.j2db.ISessionClient)1 LocalActiveSolutionHandler (com.servoy.j2db.LocalActiveSolutionHandler)1 MethodArgument (com.servoy.j2db.persistence.MethodArgument)1 MethodTemplate (com.servoy.j2db.persistence.MethodTemplate)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 RootObjectMetaData (com.servoy.j2db.persistence.RootObjectMetaData)1 Solution (com.servoy.j2db.persistence.Solution)1 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)1 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)1 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)1 FormAndComponent (com.servoy.j2db.scripting.JSApplication.FormAndComponent)1 JSEvent (com.servoy.j2db.scripting.JSEvent)1 IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)1 IComponent (com.servoy.j2db.ui.IComponent)1 RemoteException (java.rmi.RemoteException)1 Function (org.mozilla.javascript.Function)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1