1 11 12 package org.eclipse.core.runtime.internal.adaptor; 13 14 import java.lang.reflect.Method ; 15 import java.util.Map ; 16 import org.eclipse.core.runtime.adaptor.EclipseStarter; 17 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 18 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 19 import org.eclipse.osgi.framework.log.FrameworkLog; 20 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 21 import org.eclipse.osgi.internal.profile.Profile; 22 import org.eclipse.osgi.service.runnable.*; 23 import org.osgi.framework.*; 24 25 public class EclipseAppLauncher implements ApplicationLauncher { 26 volatile private ParameterizedRunnable runnable = null; 27 private Object appContext = null; 28 private Semaphore runningLock = new Semaphore(1); 29 private Semaphore waitForAppLock = new Semaphore(0); 30 private BundleContext context; 31 private boolean relaunch = false; 32 private boolean failOnNoDefault = false; 33 private FrameworkLog log; 34 public EclipseAppLauncher(BundleContext context, boolean relaunch, boolean failOnNoDefault, FrameworkLog log) { 35 this.context = context; 36 this.relaunch = relaunch; 37 this.failOnNoDefault = failOnNoDefault; 38 this.log = log; 39 findRunnableService(); 40 } 41 42 45 private void findRunnableService() { 46 String appClass = ParameterizedRunnable.class.getName(); 48 ServiceReference[] runRefs = null; 49 try { 50 runRefs = context.getServiceReferences(ParameterizedRunnable.class.getName(), "(&(objectClass=" + appClass + ")(eclipse.application=*))"); } catch (InvalidSyntaxException e) { 52 } 54 if (runRefs != null && runRefs.length > 0) { 55 runnable = (ParameterizedRunnable) context.getService(runRefs[0]); 57 relaunch = false; 59 waitForAppLock.release(); 60 } 61 } 62 63 68 public Object start(Object defaultContext) throws Exception { 69 if (failOnNoDefault && runnable == null) 72 throw new IllegalStateException (EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_NO_APPLICATION); 73 Object result = null; 74 do { 75 try { 76 result = runApplication(defaultContext); 77 } catch (Exception e) { 78 if (!relaunch || (context.getBundle().getState() & Bundle.ACTIVE) == 0) 79 throw e; 80 if (log != null) 81 log.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, EclipseAdaptorMsg.ECLIPSE_STARTUP_APP_ERROR, 1, e, null)); 82 } 83 } while (relaunch && (context.getBundle().getState() & Bundle.ACTIVE) != 0); 84 return result; 85 } 86 87 91 private Object runApplication(Object defaultContext) throws Exception { 92 waitForAppLock.acquire(); 94 runningLock.acquire(); 97 if (EclipseStarter.debug) { 98 String timeString = FrameworkProperties.getProperty("eclipse.startTime"); long time = timeString == null ? 0L : Long.parseLong(timeString); 100 System.out.println("Starting application: " + (System.currentTimeMillis() - time)); } 102 if (Profile.PROFILE && (Profile.STARTUP || Profile.BENCHMARK)) 103 Profile.logTime("EclipseStarter.run(Object)()", "framework initialized! starting application..."); try { 105 return runnable.run(appContext != null ? appContext : defaultContext); 107 } finally { 108 if (Profile.PROFILE && Profile.STARTUP) 109 Profile.logExit("EclipseStarter.run(Object)()"); runnable = null; 112 appContext = null; 113 runningLock.release(); 114 } 115 } 116 117 public void launch(ParameterizedRunnable app, Object appContext) { 118 waitForAppLock.acquire(-1); if (!runningLock.acquire(-1)) throw new IllegalStateException ("An application is aready running."); this.runnable = app; 122 this.appContext = appContext; 123 waitForAppLock.release(); runningLock.release(); } 126 127 public void shutdown() { 128 if (runningLock.acquire(-1)) 131 return; ParameterizedRunnable currentRunnable = runnable; 133 if (currentRunnable instanceof ApplicationRunnable) { 134 ((ApplicationRunnable)currentRunnable).stop(); 135 runningLock.acquire(60000); } 137 } 138 139 144 public Object reStart(Object argument) throws Exception { 145 ServiceReference ref[] = null; 146 ref = context.getServiceReferences("org.osgi.service.application.ApplicationDescriptor", "(eclipse.application.default=true)"); if (ref != null && ref.length > 0) { 148 Object defaultApp = context.getService(ref[0]); 149 Method launch = defaultApp.getClass().getMethod("launch", new Class [] {Map .class}); launch.invoke(defaultApp, new Object [] {null}); 151 return start(argument); 152 } 153 throw new IllegalStateException (EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_NO_APPLICATION); 154 } 155 } 156
| Popular Tags
|