Search in sources :

Example 1 with RunOnServerWizard

use of org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard in project webtools.servertools by eclipse.

the class RunOnServerActionDelegate method getServer.

public IServer getServer(IModule module, IModuleArtifact moduleArtifact, IProgressMonitor monitor) throws CoreException {
    IServer server = ServerCore.getDefaultServer(module);
    // ignore preference if the server doesn't support this mode.
    if (server != null && !ServerUIPlugin.isCompatibleWithLaunchMode(server, launchMode))
        server = null;
    if (server != null && !ServerUtil.containsModule(server, module, monitor)) {
        IServerWorkingCopy wc = server.createWorkingCopy();
        try {
            ServerUtil.modifyModules(wc, new IModule[] { module }, new IModule[0], monitor);
            wc.save(false, monitor);
        } catch (CoreException ce) {
            if (Trace.SEVERE) {
                Trace.trace(Trace.STRING_SEVERE, "Could not add module to server", ce);
            }
            server = null;
        }
    }
    Shell shell;
    if (window != null)
        shell = window.getShell();
    else
        shell = ServerUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow().getShell();
    if (server == null) {
        // try the full wizard
        if (Trace.FINEST) {
            Trace.trace(Trace.STRING_FINEST, "Launching wizard");
        }
        RunOnServerWizard wizard = new RunOnServerWizard(module, launchMode, moduleArtifact, wiz_properties);
        WizardDialog dialog = new WizardDialog(shell, wizard);
        if (dialog.open() == Window.CANCEL) {
            if (monitor != null)
                monitor.setCanceled(true);
            return null;
        }
        try {
            Job.getJobManager().join("org.eclipse.wst.server.ui.family", null);
        } catch (Exception e) {
            if (Trace.WARNING) {
                Trace.trace(Trace.STRING_WARNING, "Error waiting for job", e);
            }
        }
        server = wizard.getServer();
        boolean preferred = wizard.isPreferredServer();
        tasksAndClientShown = true;
        if (client == null || launchableAdapter == null) {
            client = wizard.getSelectedClient();
            launchableAdapter = wizard.getLaunchableAdapter();
        }
        // set preferred server if requested
        if (server != null && preferred) {
            try {
                ServerCore.setDefaultServer(module, server, monitor);
            } catch (CoreException ce) {
                String message = Messages.errorCouldNotSavePreference;
                ErrorDialog.openError(shell, Messages.errorDialogTitle, message, ce.getStatus());
            }
        }
    }
    try {
        Job.getJobManager().join("org.eclipse.wst.server.ui.family", new NullProgressMonitor());
    } catch (Exception e) {
        if (Trace.WARNING) {
            Trace.trace(Trace.STRING_WARNING, "Error waiting for job", e);
        }
    }
    return server;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) RunOnServerWizard(org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 2 with RunOnServerWizard

use of org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard in project webtools.servertools by eclipse.

the class WizardTestCase method testRoS.

public static void testRoS(IModule module) {
    Shell shell = UITestHelper.getShell();
    RunOnServerWizard ros = new RunOnServerWizard(module, ILaunchManager.RUN_MODE, null);
    WizardDialog dialog = new WizardDialog(shell, ros);
    UITestHelper.assertDialog(dialog);
}
Also used : Shell(org.eclipse.swt.widgets.Shell) RunOnServerWizard(org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 3 with RunOnServerWizard

use of org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard in project webtools.servertools by eclipse.

the class RunOnServerActionDelegate method run.

/**
 * Run the resource on a server.
 */
protected void run() {
    final IModuleArtifact[] moduleArtifacts = ServerPlugin.getModuleArtifacts(selection);
    if (moduleArtifacts == null || moduleArtifacts.length == 0 || moduleArtifacts[0] == null) {
        EclipseUtil.openError(Messages.errorNoArtifact);
        if (Trace.FINEST) {
            Trace.trace(Trace.STRING_FINEST, "No module artifact found");
        }
        return;
    }
    Shell shell2 = null;
    if (window != null)
        shell2 = window.getShell();
    else {
        try {
            shell2 = ServerUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow().getShell();
        } catch (Exception e) {
        // ignore
        }
        if (shell2 == null)
            shell2 = Display.getDefault().getActiveShell();
    }
    final Shell shell = shell2;
    final IAdaptable info = new IAdaptable() {

        public Object getAdapter(Class adapter) {
            if (Shell.class.equals(adapter))
                return shell;
            if (String.class.equals(adapter))
                return "user";
            return null;
        }
    };
    // If there is more than 1 moduleArtifact, get a valid ModuleArtifact that we can use for launching
    // TODO The ModuleArtifactComposite should be part of the RunOnServerWizard
    final IModuleArtifact moduleArtifact;
    if (moduleArtifacts.length > 1) {
        ModuleArtifactComposite artifactComposite = new ModuleArtifactComposite(shell, moduleArtifacts, launchMode);
        if (artifactComposite.open() == Window.CANCEL)
            return;
        moduleArtifact = artifactComposite.getSelection();
    } else
        moduleArtifact = moduleArtifacts[0];
    if (moduleArtifact.getModule() == null) {
        // 149425
        EclipseUtil.openError(Messages.errorNoModules);
        if (Trace.FINEST) {
            Trace.trace(Trace.STRING_FINEST, "Module artifact not contained in a module");
        }
        return;
    }
    final IModule module = moduleArtifact.getModule();
    // check for servers with the given start mode
    IServer[] servers = ServerCore.getServers();
    boolean found = false;
    if (servers != null) {
        int size = servers.length;
        for (int i = 0; i < size && !found; i++) {
            if (ServerUIPlugin.isCompatibleWithLaunchMode(servers[i], launchMode)) {
                try {
                    IModule[] parents = servers[i].getRootModules(module, null);
                    if (parents != null && parents.length > 0)
                        found = true;
                } catch (Exception e) {
                // ignore
                }
            }
        }
    }
    if (!found) {
        // no existing server supports the project and start mode!
        // check if there might be another one that can be created
        IServerType[] serverTypes = ServerCore.getServerTypes();
        if (serverTypes != null) {
            int size = serverTypes.length;
            for (int i = 0; i < size && !found; i++) {
                IServerType type = serverTypes[i];
                IModuleType[] moduleTypes = type.getRuntimeType().getModuleTypes();
                if (type.supportsLaunchMode(launchMode) && ServerUtil.isSupportedModule(moduleTypes, module.getModuleType())) {
                    found = true;
                }
            }
        }
        if (!found) {
            EclipseUtil.openError(Messages.errorNoServer);
            if (Trace.FINEST) {
                Trace.trace(Trace.STRING_FINEST, "No server for start mode");
            }
            return;
        }
    }
    if (!ServerUIPlugin.saveEditors())
        return;
    tasksAndClientShown = false;
    IServer server2 = null;
    // initialize its value using the predefined value if one has been given
    client = (IClient) getOverwriteValue(ROS_CLIENT);
    launchableAdapter = (ILaunchableAdapter) getOverwriteValue(ROS_LAUNCHABLE);
    if (Trace.FINEST) {
        Trace.trace(Trace.STRING_FINEST, "Client and launchableAdapter after setting predefined values: launchableAdapter=" + launchableAdapter + " client=" + client);
    }
    try {
        IProgressMonitor monitor = new NullProgressMonitor();
        server2 = getServer(module, moduleArtifact, monitor);
        if (monitor.isCanceled())
            return;
        if (server2 != null) {
            IFolder folder = server2.getServerConfiguration();
            if (folder != null && folder.getProject() != null && !folder.getProject().isOpen())
                folder.getProject().open(monitor);
        }
    } catch (CoreException ce) {
        EclipseUtil.openError(shell, ce.getLocalizedMessage());
        return;
    }
    final IServer server = server2;
    if (Trace.FINEST) {
        Trace.trace(Trace.STRING_FINEST, "Server: " + server);
    }
    if (server == null) {
        EclipseUtil.openError(Messages.errorNoServer);
        if (Trace.SEVERE) {
            Trace.trace(Trace.STRING_SEVERE, "No server found");
        }
        return;
    }
    if (!ServerUIPlugin.promptIfDirty(shell, server))
        return;
    // We need to check if the client and launchable were pre-populated
    if (!tasksAndClientShown) {
        RunOnServerWizard wizard = new RunOnServerWizard(server, launchMode, moduleArtifact, wiz_properties);
        if (wizard.shouldAppear()) {
            WizardDialog dialog = new WizardDialog(shell, wizard);
            if (dialog.open() == Window.CANCEL)
                return;
        } else
            wizard.performFinish();
        // the wizard are used only if the client and launchableAdapter are null
        if (client == null) {
            client = wizard.getSelectedClient();
        }
        if (launchableAdapter == null) {
            launchableAdapter = wizard.getLaunchableAdapter();
        }
    }
    // if there is no client, use a dummy
    if (client == null) {
        client = new IClient() {

            public String getDescription() {
                return Messages.clientDefaultDescription;
            }

            public String getId() {
                return "org.eclipse.wst.server.ui.client.default";
            }

            public String getName() {
                return Messages.clientDefaultName;
            }

            public IStatus launch(IServer server3, Object launchable2, String launchMode3, ILaunch launch) {
                return Status.OK_STATUS;
            }

            public boolean supports(IServer server3, Object launchable2, String launchMode3) {
                return true;
            }
        };
    }
    if (Trace.FINEST) {
        Trace.trace(Trace.STRING_FINEST, "Prior to creating launch client jobs: launchableAdapter=" + launchableAdapter + " client=" + client);
    }
    if (moduleArtifact instanceof ModuleArtifactDelegate) {
        boolean canLoad = false;
        try {
            Class c = Class.forName(moduleArtifact.getClass().getName());
            if (c.newInstance() != null)
                canLoad = true;
        } catch (Throwable t) {
            if (Trace.WARNING) {
                Trace.trace(Trace.STRING_WARNING, "Could not load module artifact delegate class, switching to backup");
            }
        }
        if (canLoad) {
            try {
                IProgressMonitor monitor = new NullProgressMonitor();
                ILaunchConfiguration config = getLaunchConfiguration(server, (ModuleArtifactDelegate) moduleArtifact, launchableAdapter, client, monitor);
                config.launch(launchMode, monitor);
            } catch (CoreException ce) {
                if (Trace.SEVERE) {
                    Trace.trace(Trace.STRING_SEVERE, "Could not launch Run on Server", ce);
                }
            }
            return;
        }
    }
    Thread thread = new Thread("Run on Server") {

        public void run() {
            if (Trace.FINEST) {
                Trace.trace(Trace.STRING_FINEST, "Ready to launch");
            }
            // start server if it's not already started
            // and cue the client to start
            // TODO: get parent hierarchy correct
            IModule[] modules = new IModule[] { module };
            int state = server.getServerState();
            if (state == IServer.STATE_STARTING) {
                LaunchClientJob clientJob = new LaunchClientJob(server, modules, launchMode, moduleArtifact, launchableAdapter, client);
                clientJob.schedule();
            } else if (state == IServer.STATE_STARTED) {
                boolean restart = false;
                String mode = server.getMode();
                IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager();
                boolean disabledBreakpoints = false;
                if (server.getServerRestartState()) {
                    int result = openRestartDialog(shell);
                    if (result == 0) {
                        launchMode = mode;
                        restart = true;
                    } else if (// cancel
                    result == 9)
                        return;
                }
                if (!restart) {
                    if (!ILaunchManager.RUN_MODE.equals(mode) && ILaunchManager.RUN_MODE.equals(launchMode)) {
                        boolean breakpointsOption = false;
                        if (breakpointManager.isEnabled() && ILaunchManager.DEBUG_MODE.equals(mode))
                            breakpointsOption = true;
                        int result = openOptionsDialog(shell, Messages.wizRunOnServerTitle, Messages.dialogModeWarningRun, breakpointsOption);
                        if (result == 0)
                            restart = true;
                        else if (result == 1) {
                            breakpointManager.setEnabled(false);
                            disabledBreakpoints = true;
                            launchMode = mode;
                        } else if (result == 2)
                            launchMode = mode;
                        else
                            // result == 9 // cancel
                            return;
                    } else if (!ILaunchManager.DEBUG_MODE.equals(mode) && ILaunchManager.DEBUG_MODE.equals(launchMode)) {
                        int result = openOptionsDialog(shell, Messages.wizDebugOnServerTitle, Messages.dialogModeWarningDebug, false);
                        if (result == 0)
                            restart = true;
                        else if (result == 1)
                            launchMode = mode;
                        else
                            // result == 9 // cancel
                            return;
                    } else if (!ILaunchManager.PROFILE_MODE.equals(mode) && ILaunchManager.PROFILE_MODE.equals(launchMode)) {
                        boolean breakpointsOption = false;
                        if (breakpointManager.isEnabled() && ILaunchManager.DEBUG_MODE.equals(mode))
                            breakpointsOption = true;
                        int result = openOptionsDialog(shell, Messages.wizProfileOnServerTitle, Messages.dialogModeWarningProfile, breakpointsOption);
                        if (result == 0)
                            restart = true;
                        else if (result == 1) {
                            breakpointManager.setEnabled(false);
                            disabledBreakpoints = true;
                            launchMode = mode;
                        } else if (result == 2)
                            launchMode = mode;
                        else
                            // result == 9 // cancel
                            return;
                    }
                    if (ILaunchManager.DEBUG_MODE.equals(launchMode)) {
                        if (!breakpointManager.isEnabled() && !disabledBreakpoints) {
                            int result = openBreakpointDialog(shell);
                            if (result == 0)
                                breakpointManager.setEnabled(true);
                            else if (result == 1) {
                            // ignore
                            } else
                                // result == 2
                                return;
                        }
                    }
                }
                final LaunchClientJob clientJob = new LaunchClientJob(server, modules, launchMode, moduleArtifact, launchableAdapter, client);
                if (restart) {
                    final IServer server3 = server;
                    server.restart(launchMode, new IServer.IOperationListener() {

                        public void done(IStatus result) {
                            // Only publish if the server requires publish before launching the client.
                            if (server3.shouldPublish()) {
                                server3.publish(IServer.PUBLISH_INCREMENTAL, null, info, new IServer.IOperationListener() {

                                    public void done(IStatus result2) {
                                        if (result2.isOK())
                                            clientJob.schedule();
                                    }
                                });
                            } else {
                                clientJob.schedule();
                            }
                        }
                    });
                } else {
                    // Only publish if the server requires publish before launching the client.
                    if (server.shouldPublish()) {
                        server.publish(IServer.PUBLISH_INCREMENTAL, null, info, new IServer.IOperationListener() {

                            public void done(IStatus result) {
                                if (result.isOK())
                                    clientJob.schedule();
                            }
                        });
                    } else {
                        clientJob.schedule();
                    }
                }
            } else if (state != IServer.STATE_STOPPING) {
                final LaunchClientJob clientJob = new LaunchClientJob(server, modules, launchMode, moduleArtifact, launchableAdapter, client);
                server.start(launchMode, new IServer.IOperationListener() {

                    public void done(IStatus result) {
                        if (result.isOK())
                            clientJob.schedule();
                    }
                });
            }
        }
    };
    thread.setDaemon(true);
    thread.start();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ModuleArtifactDelegate(org.eclipse.wst.server.core.model.ModuleArtifactDelegate) RunOnServerWizard(org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard) IClient(org.eclipse.wst.server.core.internal.IClient) ModuleArtifactComposite(org.eclipse.wst.server.ui.internal.viewers.ModuleArtifactComposite) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

WizardDialog (org.eclipse.jface.wizard.WizardDialog)3 Shell (org.eclipse.swt.widgets.Shell)3 RunOnServerWizard (org.eclipse.wst.server.ui.internal.wizard.RunOnServerWizard)3 IFolder (org.eclipse.core.resources.IFolder)1 IClient (org.eclipse.wst.server.core.internal.IClient)1 ModuleArtifactDelegate (org.eclipse.wst.server.core.model.ModuleArtifactDelegate)1 ModuleArtifactComposite (org.eclipse.wst.server.ui.internal.viewers.ModuleArtifactComposite)1