1 11 12 package org.eclipse.equinox.internal.app; 13 14 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 15 import org.eclipse.osgi.service.runnable.ApplicationRunnable; 16 import org.eclipse.osgi.util.NLS; 17 import org.osgi.framework.*; 18 import org.osgi.service.application.ApplicationHandle; 19 import org.osgi.util.tracker.ServiceTracker; 20 import org.osgi.util.tracker.ServiceTrackerCustomizer; 21 22 28 public class DefaultApplicationListener implements ApplicationRunnable, ServiceTrackerCustomizer { 29 private boolean running = true; private EclipseAppHandle launchMainApp; private final ServiceTracker handleTracker; private Object result; 34 public DefaultApplicationListener(EclipseAppHandle defaultApp) { 35 ServiceReference defaultRef = defaultApp.getServiceReference(); 36 if (defaultRef == null) { 37 result = defaultApp.waitForResult(100); 40 handleTracker = null; 41 return; 42 } 43 ServiceTracker defaultAppTracker = new ServiceTracker(Activator.getContext(), defaultRef, this); 44 defaultAppTracker.open(); 45 EclipseAppHandle trackedApp = (EclipseAppHandle) defaultAppTracker.getService(); 46 if (trackedApp == null) { 47 result = defaultApp.waitForResult(100); 50 handleTracker = null; 51 } else { 52 handleTracker = defaultAppTracker; 53 } 54 } 55 56 public Object run(Object context) { 57 if (handleTracker == null) 58 return getResult(); try { 60 while (waitOnRunning()) { 61 EclipseAppHandle mainHandle = getMainHandle(); 62 if (mainHandle != null) { 63 try { 66 mainHandle.run(null); 67 } catch (Exception e) { 68 String message = NLS.bind(Messages.application_error_starting, mainHandle.getInstanceId()); 69 Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, e, null)); 70 } 71 unsetMainHandle(mainHandle); 72 } 73 } 74 } finally { 75 handleTracker.close(); 76 } 77 return getResult(); 78 } 79 80 private synchronized EclipseAppHandle getMainHandle() { 81 return launchMainApp; 82 } 83 84 private synchronized void unsetMainHandle(EclipseAppHandle mainHandle) { 85 if (launchMainApp == mainHandle) 86 launchMainApp = null; 87 } 88 89 private synchronized boolean waitOnRunning() { 90 if (!running) 91 return false; 92 try { 93 wait(100); 94 } catch (InterruptedException e) { 95 } 97 return running; 98 } 99 100 public void stop() { 101 if (handleTracker == null) 102 return; 103 ApplicationHandle handle =(ApplicationHandle) handleTracker.getService(); 105 if (handle != null) { 106 try { 107 handle.destroy(); 108 } catch (Throwable t) { 109 String message = NLS.bind(Messages.application_error_stopping, handle.getInstanceId()); 110 Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, t, null)); 111 } 112 } 113 } 114 115 public Object addingService(ServiceReference reference) { 116 return Activator.getContext().getService(reference); 117 } 118 119 public void modifiedService(ServiceReference reference, Object service) { 120 } 122 123 synchronized public void removedService(ServiceReference reference, Object service) { 124 running = false; 125 result = ((EclipseAppHandle) service).waitForResult(5000); 128 EclipseAppHandle mainHandle = getMainHandle(); 129 if (mainHandle != null) 130 try { 132 mainHandle.destroy(); 133 } catch (Throwable t) { 134 String message = NLS.bind(Messages.application_error_stopping, mainHandle.getInstanceId()); 135 Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, t, null)); 136 } 137 this.notify(); 138 } 139 synchronized void launch(EclipseAppHandle app) { 140 launchMainApp = app; 141 this.notify(); 142 } 143 144 private synchronized Object getResult() { 145 return result; 146 } 147 } 148 | Popular Tags |