Search in sources :

Example 1 with RunSession

use of aQute.bnd.build.RunSession in project bndtools by bndtools.

the class NativeBndLaunchDelegate method launch.

/*
     * The Eclipse launch interface.
     */
@Override
public void launch(ILaunchConfiguration configuration, String mode, final ILaunch launch, IProgressMonitor m) throws CoreException {
    final IProgressMonitor monitor = m == null ? new NullProgressMonitor() : m;
    Callable<Boolean> isCancelled = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return canceled || monitor.isCanceled();
        }
    };
    Processor p = new Processor();
    try {
        monitor.setTaskName("Detecting if configuration is already launched");
        if (isAlreadyRunning(configuration)) {
            return;
        }
        String target = configuration.getAttribute(LaunchConstants.ATTR_LAUNCH_TARGET, (String) null);
        if (target == null || target.length() == 0) {
            p.error("No target specified in the launch configuration");
            return;
        }
        IResource targetResource = ResourcesPlugin.getWorkspace().getRoot().findMember(target);
        if (targetResource == null) {
            p.error("No actual resource found for " + target);
            return;
        }
        IProject parent = targetResource.getProject();
        if (parent == null) {
            p.error("Not part of a project " + targetResource);
            return;
        }
        Project parentModel = Central.getProject(parent);
        if (parentModel == null) {
            p.error("Cannot locate Bnd project for " + targetResource);
            return;
        }
        Project model;
        if (targetResource.getName().equals(Project.BNDFILE)) {
            model = parentModel;
        } else {
            File file = targetResource.getLocation().toFile();
            if (file == null || !file.isFile()) {
                p.error("No file associated with the entry " + targetResource);
                return;
            }
            model = new Run(parentModel.getWorkspace(), parentModel.getBase(), file);
        }
        monitor.setTaskName("Target is " + model);
        boolean debug = "debug".equals(mode);
        try {
            List<LaunchThread> lts = new ArrayList<LaunchThread>();
            ProjectLauncher projectLauncher = model.getProjectLauncher();
            try {
                List<? extends RunSession> sessions = projectLauncher.getRunSessions();
                if (sessions == null) {
                    projectLauncher.error("This launcher for %s cannot handle the new style", target);
                    return;
                }
                for (RunSession session : sessions) try {
                    monitor.setTaskName("validating session " + session.getLabel());
                    if (!session.validate(isCancelled)) {
                        continue;
                    }
                    LaunchThread lt = new LaunchThread(projectLauncher, session, launch);
                    if (debug) {
                        lt.doDebug(monitor);
                    }
                    if (monitor.isCanceled())
                        return;
                    launch.addProcess(lt);
                    lts.add(lt);
                } catch (Exception e) {
                    projectLauncher.exception(e, "Starting session %s in project %s", session.getName(), model);
                }
            } catch (Exception e) {
                projectLauncher.exception(e, "starting processes");
            } finally {
                p.getInfo(projectLauncher);
            }
            if (!p.isOk()) {
                IStatus status = Central.toStatus(projectLauncher, "Errors detected during the launch");
                IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(status);
                Boolean cont = (Boolean) prompter.handleStatus(status, null);
                if (cont == null || !cont || monitor.isCanceled()) {
                    launch.terminate();
                    return;
                }
            }
            for (LaunchThread lt : lts) {
                lt.start();
            }
        } catch (Exception e) {
            launch.terminate();
            abort("Internal error", e, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
        }
    } catch (Exception e) {
        p.exception(e, "While starting a launch %s", configuration);
    } finally {
        if (!p.isOk()) {
            IStatus status = Central.toStatus(p, "Errors detected during the launch");
            IStatusHandler prompter = new LaunchStatusHandler();
            prompter.handleStatus(status, null);
            launch.terminate();
        }
        monitor.done();
        IO.close(p);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) Processor(aQute.bnd.osgi.Processor) ArrayList(java.util.ArrayList) ProjectLauncher(aQute.bnd.build.ProjectLauncher) Run(aQute.bnd.build.Run) Callable(java.util.concurrent.Callable) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IProject(org.eclipse.core.resources.IProject) Project(aQute.bnd.build.Project) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatusHandler(org.eclipse.debug.core.IStatusHandler) RunSession(aQute.bnd.build.RunSession) File(java.io.File) IResource(org.eclipse.core.resources.IResource) LaunchStatusHandler(bndtools.launch.ui.internal.LaunchStatusHandler)

Example 2 with RunSession

use of aQute.bnd.build.RunSession in project bnd by bndtools.

the class LauncherTest method testSimpleLauncher.

/*
	 * Launches against the agent
	 */
public void testSimpleLauncher() throws Exception {
    Project project = workspace.getProject("p1");
    Run bndrun = new Run(workspace, project.getBase(), project.getFile("one.bndrun"));
    bndrun.setProperty("-runpath", "biz.aQute.remote.launcher");
    bndrun.setProperty("-runbundles", "bsn-1,bsn-2");
    bndrun.setProperty("-runremote", "test");
    final RemoteProjectLauncherPlugin pl = (RemoteProjectLauncherPlugin) bndrun.getProjectLauncher();
    pl.prepare();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger exitCode = new AtomicInteger(-1);
    List<? extends RunSession> sessions = pl.getRunSessions();
    assertEquals(1, sessions.size());
    final RunSession session = sessions.get(0);
    Thread t = new Thread("test-launch") {

        public void run() {
            try {
                exitCode.set(session.launch());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
            }
        }
    };
    t.start();
    Thread.sleep(500);
    for (Bundle b : context.getBundles()) {
        System.out.println(b.getLocation());
    }
    assertEquals(4, context.getBundles().length);
    String p1 = t1.getAbsolutePath();
    System.out.println(p1);
    assertNotNull(context.getBundle(p1));
    assertNotNull(context.getBundle(t2.getAbsolutePath()));
    pl.cancel();
    latch.await();
    assertEquals(-3, exitCode.get());
    bndrun.close();
}
Also used : Project(aQute.bnd.build.Project) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(org.osgi.framework.Bundle) RemoteProjectLauncherPlugin(aQute.remote.plugin.RemoteProjectLauncherPlugin) Run(aQute.bnd.build.Run) CountDownLatch(java.util.concurrent.CountDownLatch) RunSession(aQute.bnd.build.RunSession)

Example 3 with RunSession

use of aQute.bnd.build.RunSession in project bnd by bndtools.

the class LauncherTest method testAgentAndMain.

/*
	 * Launches against the agent& main
	 */
public void testAgentAndMain() throws Exception {
    Project project = workspace.getProject("p1");
    Run bndrun = new Run(workspace, project.getBase(), project.getFile("one.bndrun"));
    bndrun.setProperty("-runpath", "biz.aQute.remote.launcher");
    bndrun.setProperty("-runbundles", "bsn-1,bsn-2");
    bndrun.setProperty("-runremote", "agent,main;agent=1090");
    final RemoteProjectLauncherPlugin pl = (RemoteProjectLauncherPlugin) bndrun.getProjectLauncher();
    pl.prepare();
    List<? extends RunSession> sessions = pl.getRunSessions();
    assertEquals(2, sessions.size());
    RunSession agent = sessions.get(0);
    RunSession main = sessions.get(1);
    CountDownLatch agentLatch = launch(agent);
    CountDownLatch mainLatch = launch(main);
    agent.waitTillStarted(1000);
    main.waitTillStarted(1000);
    Thread.sleep(500);
    agent.cancel();
    main.cancel();
    agentLatch.await();
    mainLatch.await();
    assertEquals(-3, agent.getExitCode());
    assertEquals(-3, main.getExitCode());
    bndrun.close();
}
Also used : Project(aQute.bnd.build.Project) RemoteProjectLauncherPlugin(aQute.remote.plugin.RemoteProjectLauncherPlugin) Run(aQute.bnd.build.Run) RunSession(aQute.bnd.build.RunSession) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Project (aQute.bnd.build.Project)3 Run (aQute.bnd.build.Run)3 RunSession (aQute.bnd.build.RunSession)3 RemoteProjectLauncherPlugin (aQute.remote.plugin.RemoteProjectLauncherPlugin)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ProjectLauncher (aQute.bnd.build.ProjectLauncher)1 Processor (aQute.bnd.osgi.Processor)1 LaunchStatusHandler (bndtools.launch.ui.internal.LaunchStatusHandler)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IStatusHandler (org.eclipse.debug.core.IStatusHandler)1 Bundle (org.osgi.framework.Bundle)1