Search in sources :

Example 1 with IClientHost

use of com.servoy.j2db.dataprocessing.IClientHost in project servoy-client by Servoy.

the class ClientState method closeSolution.

public boolean closeSolution(boolean force, Object[] args) {
    if (solutionRoot.getSolution() == null || isClosing)
        return true;
    try {
        isClosing = true;
        String[] s_args = null;
        // we dont want to open anything again if this was a force close
        if (!force && args != null) {
            s_args = new String[args.length];
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof NativeObject) {
                    StringBuilder sb = new StringBuilder();
                    NativeObject nativeObjectArg = (NativeObject) args[i];
                    Object defaultArg = null;
                    for (Object key : nativeObjectArg.keySet()) {
                        if ("a".equals(key.toString())) {
                            defaultArg = nativeObjectArg.get(key);
                        } else {
                            sb.append('&').append(key).append('=').append(nativeObjectArg.get(key));
                        }
                    }
                    if (defaultArg != null)
                        sb.insert(0, defaultArg.toString());
                    s_args[i] = sb.toString();
                } else {
                    s_args[i] = (args[i] != null && args[i] != Scriptable.NOT_FOUND && args[i] != Undefined.instance ? args[i].toString() : null);
                }
            }
        } else if (!force && args == null) {
            if (getPreferedSolutionNameToLoadOnInit() != null && isInDeveloper())
                s_args = new String[] { getPreferedSolutionNameToLoadOnInit() };
            if (!Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.allowSolutionBrowsing", "true")) && startupArguments != null) {
                if (s_args == null)
                    s_args = startupArguments;
                else {
                    for (String arg : startupArguments) {
                        if ((arg.startsWith("s:") || arg.startsWith("solution:")) && arg.substring(arg.indexOf(":")).equals(getPreferedSolutionNameToLoadOnInit())) {
                            s_args = startupArguments;
                            break;
                        }
                    }
                }
            }
        }
        boolean autoSaveBlocked = false;
        if (// always call stopEditing, also when ERL is not editing so that prepareForSave is always called
        foundSetManager != null) {
            // close solution is not a javaScript stop (do not save edited records if autoSave is off)
            int stopEditing = foundSetManager.getEditRecordList().stopEditing(false);
            if (stopEditing != ISaveConstants.AUTO_SAVE_BLOCKED || foundSetManager.getEditRecordList().getAutoSave() == true) {
                // so the stopEditing was not blocked because autoSave is off
                if (stopEditing != ISaveConstants.STOPPED) {
                    if (force) {
                        // just clean everything when in force mode
                        foundSetManager.getEditRecordList().init();
                    } else {
                        return false;
                    }
                }
            } else {
                // stopEditing was blocked because autoSave is off; this means unsaved changes will not be saved automatically
                // when this solution closes; however we must give the user the opportunity to save unsaved data on his solution close
                // handler - so we will clear edited records only after that method is called - and if the closing of the solution continues
                autoSaveBlocked = true;
            }
        }
        handleArguments(s_args);
        if (!callCloseSolutionMethod(force) && !force) {
            return false;
        }
        solutionClosed = true;
        if (autoSaveBlocked && foundSetManager != null) {
            // clear edited records so that they will not be auto-saved by the operations that follow
            foundSetManager.getEditRecordList().init();
        }
        checkForActiveTransactions(force);
        // formmanager does a savedata on visible form
        // $NON-NLS-1$
        J2DBGlobals.firePropertyChange(this, "solution", solutionRoot.getSolution(), null);
        solutionRoot.clearSecurityAccess();
        // do save after firePropertyChange because that may flush some changes (from undoable cmds)
        saveSolution();
        try {
            solutionRoot.close(getActiveSolutionHandler());
        } catch (Exception e) {
            // ignore any error
            Debug.error(e);
        }
        // Notify server!
        IClientHost ch = getClientHost();
        if (// can be null if failed to init
        ch != null) {
            try {
                if (clientInfo != null) {
                    clientInfo.setOpenSolutionId(-1);
                    ch.pushClientInfo(clientInfo.getClientId(), clientInfo);
                }
            } catch (Exception e1) {
                // incase connection to server is dead
                Debug.error(e1);
            }
        }
        if (foundSetManager != null) {
            // delete any foundsets
            foundSetManager.flushCachedItems();
            foundSetManager.init();
        }
        // inform messages of the closed solution
        refreshI18NMessages(true);
        if (scriptEngine != null) {
            scriptEngine.destroy();
            // delete current script engine
            scriptEngine = null;
        }
        getRuntimeProperties().put(IServiceProvider.RT_JSDATASET_FUNCTIONS, null);
        getRuntimeProperties().put(IServiceProvider.RT_JSFOUNDSET_FUNCTIONS, null);
        getRuntimeProperties().put(IServiceProvider.RT_JSRECORD_FUNCTIONS, null);
        // drop any temp tables for this client
        IDataServer ds = getDataServer();
        if (ds != null) {
            ds.dropTemporaryTable(getClientID(), null, null);
        }
    } catch (Exception ex) {
        Debug.error(ex);
    } finally {
        isClosing = false;
        if (solutionClosed && dataServer instanceof DataServerProxy)
            dataServer = ((DataServerProxy) dataServer).getEnclosingDataServer();
        // just set the solutionClosed boolean to false again here, now the solution should be null.
        solutionClosed = false;
    }
    return true;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) IDataServer(com.servoy.j2db.dataprocessing.IDataServer) IClientHost(com.servoy.j2db.dataprocessing.IClientHost) NativeObject(org.mozilla.javascript.NativeObject) UnmarshalException(java.rmi.UnmarshalException) RhinoException(org.mozilla.javascript.RhinoException) RemoteException(java.rmi.RemoteException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) DataServerProxy(com.servoy.j2db.dataprocessing.DataServerProxy)

Example 2 with IClientHost

use of com.servoy.j2db.dataprocessing.IClientHost in project servoy-client by Servoy.

the class ClientState method registerClient.

protected boolean registerClient(IClient uc) throws Exception {
    String prevClientId = clientInfo.getClientId();
    long t1 = System.currentTimeMillis();
    int counter = 0;
    IClientHost mClientHost = null;
    Object[] retval = null;
    while (counter++ < 10) {
        try {
            mClientHost = getClientHost();
            if (mClientHost != null) {
                retval = mClientHost.register(uc, getClientInfo());
            }
            break;
        } catch (Exception e) {
            if (counter == 10) {
                if (e instanceof RemoteException) {
                    throw (RemoteException) e;
                }
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                }
                throw new RuntimeException(e.getMessage());
            }
            try {
                Thread.sleep(100 * counter);
            } catch (InterruptedException e1) {
            }
        }
    }
    boolean registered = false;
    if (retval != null) {
        registered = ((Integer) retval[1]).intValue() == IClientManager.REGISTER_OK;
        clientInfo.setClientId((String) retval[0]);
        if (Debug.tracing()) {
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.trace("Client (re)registered with id " + clientInfo.getClientId() + ", previous: " + prevClientId);
        }
    }
    if (clientInfo.getClientId() == null) {
        if (mClientHost == null) {
            throw new ApplicationException(ServoyException.InternalCodes.INVALID_RMI_SERVER_CONNECTION);
        } else {
            if (retval != null && ((Integer) retval[1]).intValue() == IClientManager.REGISTER_FAILED_MAINTENANCE_MODE)
                throw new ApplicationException(ServoyException.MAINTENANCE_MODE);
            else
                throw new ApplicationException(ServoyException.NO_LICENSE);
        }
    } else {
        clientInfo.setLoginTimestamp(System.currentTimeMillis());
    }
    long t2 = System.currentTimeMillis();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Debug.trace("Leave registerClient registered:" + registered + " in " + (t2 - t1) + " ms");
    return registered;
}
Also used : IClientHost(com.servoy.j2db.dataprocessing.IClientHost) UnmarshalException(java.rmi.UnmarshalException) RhinoException(org.mozilla.javascript.RhinoException) RemoteException(java.rmi.RemoteException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) NativeObject(org.mozilla.javascript.NativeObject) RemoteException(java.rmi.RemoteException)

Aggregations

IClientHost (com.servoy.j2db.dataprocessing.IClientHost)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ServoyException (com.servoy.j2db.util.ServoyException)2 RemoteException (java.rmi.RemoteException)2 UnmarshalException (java.rmi.UnmarshalException)2 JavaScriptException (org.mozilla.javascript.JavaScriptException)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 DataServerProxy (com.servoy.j2db.dataprocessing.DataServerProxy)1 IDataServer (com.servoy.j2db.dataprocessing.IDataServer)1