Search in sources :

Example 1 with ExecutionArguments

use of org.eclipse.jdt.launching.ExecutionArguments in project webtools.servertools by eclipse.

the class GenericServerLaunchConfigurationDelegate method launch.

/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    IServer server = ServerUtil.getServer(configuration);
    if (server == null) {
        abort(GenericServerCoreMessages.missingServer, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
    }
    // Commented until bug 210859 is resolved
    // if (server.shouldPublish() && ServerCore.isAutoPublishing())
    // server.publish(IServer.PUBLISH_INCREMENTAL, monitor);
    @SuppressWarnings("null") GenericServerBehaviour genericServer = (GenericServerBehaviour) server.loadAdapter(ServerBehaviourDelegate.class, null);
    try {
        genericServer.setupLaunch(launch, mode, monitor);
        if (genericServer.getServer().getServerType().supportsRemoteHosts() && !SocketUtil.isLocalhost(genericServer.getServer().getHost())) {
            // no launch for remote servers
            return;
        }
        String mainTypeName = genericServer.getStartClassName();
        IVMInstall vm = verifyVMInstall(configuration);
        IVMRunner runner = vm.getVMRunner(mode);
        if (runner == null && ILaunchManager.PROFILE_MODE.equals(mode)) {
            runner = vm.getVMRunner(ILaunchManager.RUN_MODE);
        }
        if (runner == null) {
            throw new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, 0, GenericServerCoreMessages.runModeNotSupported, null));
        }
        File workingDir = verifyWorkingDirectory(configuration);
        String workingDirName = null;
        if (workingDir != null)
            workingDirName = workingDir.getAbsolutePath();
        // Program & VM args
        String pgmArgs = getProgramArguments(configuration);
        String vmArgs = getVMArguments(configuration);
        String[] envp = getEnvironment(configuration);
        ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs);
        // VM-specific attributes
        Map vmAttributesMap = getVMSpecificAttributesMap(configuration);
        // Classpath
        String[] classpath = getClasspath(configuration);
        // Create VM config
        VMRunnerConfiguration runConfig = new VMRunnerConfiguration(mainTypeName, classpath);
        runConfig.setProgramArguments(execArgs.getProgramArgumentsArray());
        runConfig.setVMArguments(execArgs.getVMArgumentsArray());
        runConfig.setWorkingDirectory(workingDirName);
        runConfig.setEnvironment(envp);
        runConfig.setVMSpecificAttributesMap(vmAttributesMap);
        // Bootpath
        String[] bootpath = getBootpath(configuration);
        if (bootpath != null && bootpath.length > 0)
            runConfig.setBootClassPath(bootpath);
        setDefaultSourceLocator(launch, configuration);
        if (ILaunchManager.PROFILE_MODE.equals(mode)) {
            try {
                ServerProfilerDelegate.configureProfiling(launch, vm, runConfig, monitor);
            } catch (CoreException ce) {
                genericServer.stopImpl();
                throw ce;
            }
        }
        // Launch the configuration
        genericServer.startPingThread();
        runner.run(runConfig, launch, monitor);
        genericServer.setProcess(launch.getProcesses()[0]);
    } catch (CoreException e) {
        // $NON-NLS-1$
        Trace.trace(Trace.SEVERE, "error launching generic server", e);
        genericServer.terminate();
        throw e;
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IServer(org.eclipse.wst.server.core.IServer) IVMRunner(org.eclipse.jdt.launching.IVMRunner) VMRunnerConfiguration(org.eclipse.jdt.launching.VMRunnerConfiguration) IVMInstall(org.eclipse.jdt.launching.IVMInstall) CoreException(org.eclipse.core.runtime.CoreException) ExecutionArguments(org.eclipse.jdt.launching.ExecutionArguments) ServerBehaviourDelegate(org.eclipse.wst.server.core.model.ServerBehaviourDelegate) File(java.io.File) Map(java.util.Map)

Example 2 with ExecutionArguments

use of org.eclipse.jdt.launching.ExecutionArguments in project linuxtools by eclipse.

the class JavaAppInContainerLaunchDelegate method launch.

/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    // $NON-NLS-1$
    monitor.beginTask(NLS.bind("{0}...", new String[] { configuration.getName() }), 3);
    // check for cancellation
    if (monitor.isCanceled()) {
        return;
    }
    String connectionURI = configuration.getAttribute(JavaLaunchConfigurationConstants.CONNECTION_URI, (String) null);
    String imageID = configuration.getAttribute(JavaLaunchConfigurationConstants.IMAGE_ID, (String) null);
    List<String> extraDirs = configuration.getAttribute(JavaLaunchConfigurationConstants.DIRS, Arrays.asList(new String[0]));
    try {
        DockerConnection conn = (DockerConnection) DockerConnectionManager.getInstance().getConnectionByUri(connectionURI);
        if (conn == null) {
            Display.getDefault().asyncExec(new Runnable() {

                @Override
                public void run() {
                    MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_connection_not_found_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_connection_not_found_text, connectionURI));
                }
            });
            return;
        } else if (!conn.isOpen()) {
            try {
                conn.open(false);
            } catch (DockerException e) {
            }
            if (!conn.isOpen()) {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_connection_not_active_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_connection_not_active_text, connectionURI));
                    }
                });
                return;
            }
        }
        IDockerImage img = conn.getImage(imageID);
        if (img == null) {
            Display.getDefault().asyncExec(new Runnable() {

                @Override
                public void run() {
                    MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_image_not_found_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_image_not_found_text, imageID));
                }
            });
            return;
        }
        // randomized port between 1025 and 65535
        int port = ILaunchManager.DEBUG_MODE.equals(mode) ? (int) ((65535 - 1025) * Math.random()) + 1025 : -1;
        monitor.subTask(Messages.JavaAppInContainerLaunchDelegate_Verifying_launch_attributes____1);
        String mainTypeName = verifyMainTypeName(configuration);
        IVMInstall vm = new ContainerVMInstall(configuration, img, port);
        ContainerVMRunner runner = new ContainerVMRunner(vm);
        File workingDir = verifyWorkingDirectory(configuration);
        String workingDirName = null;
        if (workingDir != null) {
            workingDirName = workingDir.getAbsolutePath();
        }
        runner.setAdditionalDirectories(extraDirs);
        // Environment variables
        String[] envp = getEnvironment(configuration);
        // Program & VM arguments
        String pgmArgs = getProgramArguments(configuration);
        String vmArgs = getVMArguments(configuration);
        ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs);
        // VM-specific attributes
        Map<String, Object> vmAttributesMap = getVMSpecificAttributesMap(configuration);
        // Classpath
        String[] classpath = getClasspath(configuration);
        if (Platform.OS_WIN32.equals(Platform.getOS())) {
            for (int i = 0; i < classpath.length; i++) {
                classpath[i] = UnixFile.convertDOSPathToUnixPath(classpath[i]);
            }
        }
        // Create VM config
        VMRunnerConfiguration runConfig = new VMRunnerConfiguration(mainTypeName, classpath);
        runConfig.setProgramArguments(execArgs.getProgramArgumentsArray());
        runConfig.setEnvironment(envp);
        List<String> finalVMArgs = new ArrayList<>(Arrays.asList(execArgs.getVMArgumentsArray()));
        if (ILaunchManager.DEBUG_MODE.equals(mode)) {
            double version = getJavaVersion(conn, img);
            if (version < 1.5) {
                // $NON-NLS-1$
                finalVMArgs.add("-Xdebug");
                // $NON-NLS-1$
                finalVMArgs.add("-Xnoagent");
            }
            // check if java 1.4 or greater
            if (version < 1.4) {
                // $NON-NLS-1$
                finalVMArgs.add("-Djava.compiler=NONE");
            }
            if (version < 1.5) {
                // $NON-NLS-1$
                finalVMArgs.add("-Xrunjdwp:transport=dt_socket,server=y,address=" + port);
            } else {
                // $NON-NLS-1$
                finalVMArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,address=" + port);
            }
        }
        runConfig.setVMArguments(finalVMArgs.toArray(new String[0]));
        runConfig.setWorkingDirectory(workingDirName);
        runConfig.setVMSpecificAttributesMap(vmAttributesMap);
        // Bootpath
        runConfig.setBootClassPath(getBootpath(configuration));
        // check for cancellation
        if (monitor.isCanceled()) {
            return;
        }
        // stop in main
        prepareStopInMain(configuration);
        // done the verification phase
        monitor.worked(1);
        monitor.subTask(Messages.JavaAppInContainerLaunchDelegate_Creating_source_locator____2);
        // set the default source locator if required
        setDefaultSourceLocator(launch, configuration);
        monitor.worked(1);
        // Launch the configuration - 1 unit of work
        runner.run(runConfig, launch, monitor);
        // check for cancellation
        if (monitor.isCanceled()) {
            return;
        }
        if (ILaunchManager.DEBUG_MODE.equals(mode)) {
            while (runner.getIPAddress() == null || !runner.isListening()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
            IDockerContainerInfo info = runner.getContainerInfo();
            // $NON-NLS-1$
            String configName = info.name().startsWith("/") ? info.name().substring(1) : info.name();
            ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
            ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION);
            ILaunchConfiguration cfgForAttach = type.newInstance(null, configName);
            ILaunchConfigurationWorkingCopy wc = cfgForAttach.getWorkingCopy();
            String ip = runner.getIPAddress();
            // Can we reach it ? Or is it on a different network.
            if (!isListening(ip, port)) {
                // If the daemon is reachable via TCP it should forward traffic.
                if (conn.getSettings() instanceof TCPConnectionSettings) {
                    ip = ((TCPConnectionSettings) conn.getSettings()).getAddr();
                    if (!isListening(ip, port)) {
                        // Try to find some network interface that's listening
                        ip = getIPAddressListening(port);
                        if (!isListening(ip, port)) {
                            ip = null;
                        }
                    }
                } else {
                    ip = null;
                }
            }
            if (ip == null) {
                String imageName = conn.getImage(imageID).repoTags().get(0);
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_session_unreachable_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_session_unreachable_text, new Object[] { imageName, imageID, runner.getIPAddress() }));
                    }
                });
                return;
            }
            Map<String, String> map = new HashMap<>();
            // $NON-NLS-1$
            map.put("hostname", ip);
            // $NON-NLS-1$
            map.put("port", String.valueOf(port));
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, map);
            String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
            wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, projectName);
            wc.doSave();
            DebugUITools.launch(cfgForAttach, ILaunchManager.DEBUG_MODE);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ILaunchManager(org.eclipse.debug.core.ILaunchManager) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) VMRunnerConfiguration(org.eclipse.jdt.launching.VMRunnerConfiguration) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) ILaunchConfigurationType(org.eclipse.debug.core.ILaunchConfigurationType) DockerException(org.eclipse.linuxtools.docker.core.DockerException) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) TCPConnectionSettings(org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings) IVMInstall(org.eclipse.jdt.launching.IVMInstall) ExecutionArguments(org.eclipse.jdt.launching.ExecutionArguments) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) File(java.io.File)

Example 3 with ExecutionArguments

use of org.eclipse.jdt.launching.ExecutionArguments in project liferay-ide by liferay.

the class PortalServerLaunchConfigDelegate method launchServer.

private void launchServer(final IServer server, final ILaunchConfiguration config, final String mode, final ILaunch launch, final IProgressMonitor monitor) throws CoreException {
    final IVMInstall vm = verifyVMInstall(config);
    final IVMRunner runner = vm.getVMRunner(mode) != null ? vm.getVMRunner(mode) : vm.getVMRunner(ILaunchManager.RUN_MODE);
    final File workingDir = verifyWorkingDirectory(config);
    final String workingDirPath = workingDir != null ? workingDir.getAbsolutePath() : null;
    final String progArgs = getProgramArguments(config);
    final String vmArgs = getVMArguments(config);
    final String[] envp = getEnvironment(config);
    final ExecutionArguments execArgs = new ExecutionArguments(vmArgs, progArgs);
    final Map<String, Object> vmAttributesMap = getVMSpecificAttributesMap(config);
    final PortalServerBehavior portalServer = (PortalServerBehavior) server.loadAdapter(PortalServerBehavior.class, monitor);
    final String classToLaunch = portalServer.getClassToLaunch();
    final String[] classpath = getClasspath(config);
    final VMRunnerConfiguration runConfig = new VMRunnerConfiguration(classToLaunch, classpath);
    runConfig.setProgramArguments(execArgs.getProgramArgumentsArray());
    runConfig.setVMArguments(execArgs.getVMArgumentsArray());
    runConfig.setWorkingDirectory(workingDirPath);
    runConfig.setEnvironment(envp);
    runConfig.setVMSpecificAttributesMap(vmAttributesMap);
    final String[] bootpath = getBootpath(config);
    if (ListUtil.isNotEmpty(bootpath)) {
        runConfig.setBootClassPath(bootpath);
    }
    portalServer.launchServer(launch, mode, monitor);
    server.addServerListener(new IServerListener() {

        @Override
        public void serverChanged(ServerEvent event) {
            if ((event.getKind() & ServerEvent.MODULE_CHANGE) > 0) {
                AbstractSourceLookupDirector sourceLocator = (AbstractSourceLookupDirector) launch.getSourceLocator();
                try {
                    final String memento = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String) null);
                    if (memento != null) {
                        sourceLocator.initializeFromMemento(memento);
                    } else {
                        sourceLocator.initializeDefaults(config);
                    }
                } catch (CoreException e) {
                    LiferayServerCore.logError("Could not reinitialize source lookup director", e);
                }
            } else if ((event.getKind() & ServerEvent.SERVER_CHANGE) > 0 && event.getState() == IServer.STATE_STOPPED) {
                server.removeServerListener(this);
            }
        }
    });
    try {
        runner.run(runConfig, launch, monitor);
        portalServer.addProcessListener(launch.getProcesses()[0]);
    } catch (Exception e) {
        portalServer.cleanup();
    }
}
Also used : IVMRunner(org.eclipse.jdt.launching.IVMRunner) VMRunnerConfiguration(org.eclipse.jdt.launching.VMRunnerConfiguration) CoreException(org.eclipse.core.runtime.CoreException) ServerEvent(org.eclipse.wst.server.core.ServerEvent) AbstractSourceLookupDirector(org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector) IVMInstall(org.eclipse.jdt.launching.IVMInstall) CoreException(org.eclipse.core.runtime.CoreException) ExecutionArguments(org.eclipse.jdt.launching.ExecutionArguments) IServerListener(org.eclipse.wst.server.core.IServerListener) File(java.io.File)

Aggregations

File (java.io.File)3 ExecutionArguments (org.eclipse.jdt.launching.ExecutionArguments)3 IVMInstall (org.eclipse.jdt.launching.IVMInstall)3 VMRunnerConfiguration (org.eclipse.jdt.launching.VMRunnerConfiguration)3 CoreException (org.eclipse.core.runtime.CoreException)2 IVMRunner (org.eclipse.jdt.launching.IVMRunner)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Status (org.eclipse.core.runtime.Status)1 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)1 ILaunchConfigurationType (org.eclipse.debug.core.ILaunchConfigurationType)1 ILaunchConfigurationWorkingCopy (org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)1 ILaunchManager (org.eclipse.debug.core.ILaunchManager)1 AbstractSourceLookupDirector (org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector)1 DockerException (org.eclipse.linuxtools.docker.core.DockerException)1 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)1 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)1