package org.thoughtframe.bridge; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.ui.IStartup; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchListener; import org.eclipse.ui.PlatformUI; /** * Wires engine shutdown to the workbench lifecycle. * * The engine is lazy by default: booting it starts Jetty and an embedded search * node, which is slow and shares its data-directory lock with anything else * running (the TUI, another workbench). Nothing in the hello-world capture path * needs it. Set -Dthoughtframe.eclipse.autostart=true to boot at startup anyway. */ public class ThoughtframeStartup implements IStartup { /** System property: boot the engine as soon as the workbench starts. */ protected String fieldAutostartProperty = "thoughtframe.eclipse.autostart"; public String getAutostartProperty() { return fieldAutostartProperty; } public void setAutostartProperty(String inProperty) { fieldAutostartProperty = inProperty; } public void earlyStartup() { PlatformUI.getWorkbench().addWorkbenchListener(new IWorkbenchListener() { public boolean preShutdown(IWorkbench inWorkbench, boolean inForced) { EngineIsland.getInstance().stop(); return true; } public void postShutdown(IWorkbench inWorkbench) { } }); if (Boolean.getBoolean(getAutostartProperty())) { scheduleStart(); } } /** * Boots the engine on a background job so the UI never blocks. This is the * trigger a later rung calls on demand; the view watches {@code isBooting()}. */ protected void scheduleStart() { Job job = new Job("Starting ThoughtFrame engine") { protected IStatus run(IProgressMonitor inMonitor) { inMonitor.beginTask("Starting ThoughtFrame engine", IProgressMonitor.UNKNOWN); try { EngineIsland.getInstance().start(); return Status.OK_STATUS; } catch (Throwable t) { EngineIsland.getInstance().setFailure(t); return new Status(IStatus.ERROR, Constants.PLUGIN_ID, "ThoughtFrame engine failed to start", t); } finally { inMonitor.done(); } } }; job.setUser(false); job.schedule(); } }