1 12 13 package org.eclipse.equinox.internal.app; 14 15 import java.util.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.equinox.app.IApplication; 18 import org.eclipse.equinox.app.IApplicationContext; 19 import org.eclipse.osgi.service.runnable.ApplicationRunnable; 20 import org.eclipse.osgi.service.runnable.StartupMonitor; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.framework.*; 23 import org.osgi.service.application.ApplicationHandle; 24 25 28 public class EclipseAppHandle extends ApplicationHandle implements ApplicationRunnable, IApplicationContext { 29 private static final int FLAG_STARTING = 0x01; 31 private static final int FLAG_ACTIVE = 0x02; 33 private static final int FLAG_STOPPING = 0x04; 35 private static final int FLAG_STOPPED = 0x08; 37 private static final String STARTING = "org.eclipse.equinox.app.starting"; private static final String STOPPED = "org.eclipse.equinox.app.stopped"; private static final String PROP_ECLIPSE_EXITCODE = "eclipse.exitcode"; 41 private volatile ServiceRegistration handleRegistration; 42 private int status = EclipseAppHandle.FLAG_STARTING; 43 private final Map arguments; 44 private Object application; 45 private final Boolean defaultAppInstance; 46 private Object result; 47 private boolean setResult = false; 48 49 52 EclipseAppHandle(String instanceId, Map arguments, EclipseAppDescriptor descriptor) { 53 super(instanceId, descriptor); 54 defaultAppInstance = arguments == null || arguments.get(EclipseAppDescriptor.APP_DEFAULT) == null ? Boolean.FALSE : (Boolean ) arguments.remove(EclipseAppDescriptor.APP_DEFAULT); 55 if (arguments == null) 56 this.arguments = new HashMap(2); 57 else 58 this.arguments = new HashMap(arguments); 59 } 60 61 synchronized public String getState() { 62 switch (status) { 63 case FLAG_STARTING : 64 return STARTING; 65 case FLAG_ACTIVE : 66 return ApplicationHandle.RUNNING; 67 case FLAG_STOPPING : 68 return ApplicationHandle.STOPPING; 69 case FLAG_STOPPED : 70 default : 71 if (handleRegistration == null) 73 throw new IllegalStateException (NLS.bind(Messages.application_error_state_stopped, getInstanceId())); 74 return STOPPED; 75 } 76 } 77 78 protected void destroySpecific() { 79 setAppStatus(EclipseAppHandle.FLAG_STOPPING); 82 IApplication app = getApplication(); 84 if (app != null) 85 app.stop(); 86 setAppStatus(EclipseAppHandle.FLAG_STOPPED); 88 } 89 90 synchronized void setServiceRegistration(ServiceRegistration sr) { 91 this.handleRegistration = sr; 92 } 93 94 ServiceReference getServiceReference() { 95 ServiceRegistration reg = handleRegistration; 96 if (reg == null) 97 return null; 98 try { 99 return reg.getReference(); 100 } catch (IllegalStateException e) { 101 return null; } 103 } 104 107 Dictionary getServiceProperties() { 108 Dictionary props = new Hashtable(6); 109 props.put(ApplicationHandle.APPLICATION_PID, getInstanceId()); 110 props.put(ApplicationHandle.APPLICATION_STATE, getState()); 111 props.put(ApplicationHandle.APPLICATION_DESCRIPTOR, getApplicationDescriptor().getApplicationId()); 112 props.put(EclipseAppDescriptor.APP_TYPE, ((EclipseAppDescriptor) getApplicationDescriptor()).getThreadTypeString()); 113 if (defaultAppInstance.booleanValue()) 114 props.put(EclipseAppDescriptor.APP_DEFAULT, defaultAppInstance); 115 return props; 116 } 117 118 122 private synchronized void setAppStatus(int status) { 123 if (this.status == status) 124 return; 125 if ((status & EclipseAppHandle.FLAG_STARTING) != 0) 126 throw new IllegalArgumentException ("Cannot set app status to ACTIVE"); if ((status & EclipseAppHandle.FLAG_STOPPING) != 0) 129 if ((this.status & (EclipseAppHandle.FLAG_STOPPING | EclipseAppHandle.FLAG_STOPPED)) != 0) 130 return; 131 this.status = status; 133 if (handleRegistration == null) 134 return; 135 handleRegistration.setProperties(getServiceProperties()); 136 if ((this.status & EclipseAppHandle.FLAG_STOPPED) != 0) { 138 ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().unlock(this); 139 handleRegistration.unregister(); 140 handleRegistration = null; 141 } 142 } 143 144 public Map getArguments() { 145 return arguments; 146 } 147 148 public Object run(Object context) throws Exception { 149 if (context != null) { 150 arguments.put(IApplicationContext.APPLICATION_ARGS, context); 152 } else { 153 context = arguments.get(IApplicationContext.APPLICATION_ARGS); 155 if (context == null) { 156 context = CommandLineArgs.getApplicationArgs(); 158 arguments.put(IApplicationContext.APPLICATION_ARGS, context); 159 } 160 } 161 Object tempResult = null; 162 try { 163 Object app; 164 synchronized (this) { 165 application = getConfiguration().createExecutableExtension("run"); app = application; 167 } 168 if (app instanceof IApplication) 169 tempResult = ((IApplication) app).start(this); 170 else 171 tempResult = EclipseAppContainer.callMethod(app, "run", new Class [] {Object .class}, new Object [] {context}); } finally { 173 synchronized (this) { 174 result = tempResult; 175 setResult = true; 176 application = null; 177 notify(); 178 setAppStatus(EclipseAppHandle.FLAG_STOPPED); 180 } 181 } 182 int exitCode = result instanceof Integer ? ((Integer ) result).intValue() : 0; 183 if (isDefault()) 185 System.getProperties().setProperty(PROP_ECLIPSE_EXITCODE, Integer.toString(exitCode)); 188 if (Activator.DEBUG) 189 System.out.println(NLS.bind(Messages.application_returned, (new String [] {getApplicationDescriptor().getApplicationId(), result == null ? "null" : result.toString()}))); return result; 191 } 192 193 public void stop() { 194 try { 195 destroy(); 196 } catch (IllegalStateException e) { 197 } 200 201 } 202 203 public void applicationRunning() { 204 setAppStatus(EclipseAppHandle.FLAG_ACTIVE); 206 final ServiceReference[] monitors = getStartupMonitors(); 208 if (monitors == null) 209 return; 210 SafeRunner.run(new ISafeRunnable() { 211 public void handleException(Throwable e) { 212 } 215 216 public void run() throws Exception { 217 for (int i = 0; i < monitors.length; i++) { 218 StartupMonitor monitor = (StartupMonitor) Activator.getContext().getService(monitors[i]); 219 if (monitor != null) { 220 monitor.applicationRunning(); 221 Activator.getContext().ungetService(monitors[i]); 222 } 223 } 224 } 225 }); 226 } 227 228 private ServiceReference[] getStartupMonitors() { 229 ServiceReference[] refs = null; 232 try { 233 refs = Activator.getContext().getServiceReferences(StartupMonitor.class.getName(), null); 234 } catch (InvalidSyntaxException e) { 235 } 237 if (refs == null || refs.length == 0) 238 return null; 239 Arrays.sort(refs, new Comparator() { 241 public int compare(Object o1, Object o2) { 242 ServiceReference ref1 = (ServiceReference) o1; 245 ServiceReference ref2 = (ServiceReference) o2; 246 Object property = ref1.getProperty(Constants.SERVICE_RANKING); 247 int rank1 = (property instanceof Integer ) ? ((Integer ) property).intValue() : 0; 248 property = ref2.getProperty(Constants.SERVICE_RANKING); 249 int rank2 = (property instanceof Integer ) ? ((Integer ) property).intValue() : 0; 250 if (rank1 != rank2) 251 return rank1 > rank2 ? -1 : 1; 252 long id1 = ((Long ) (ref1.getProperty(Constants.SERVICE_ID))) .longValue(); 254 long id2 = ((Long ) (ref2.getProperty(Constants.SERVICE_ID))) .longValue(); 255 return id2 > id1 ? -1 : 1; 256 } 257 }); 258 return refs; 259 } 260 261 private synchronized IApplication getApplication() { 262 return (IApplication) ((application instanceof IApplication) ? application : null); 263 } 264 265 private IConfigurationElement getConfiguration() { 266 IExtension applicationExtension = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getAppExtension(getApplicationDescriptor().getApplicationId()); 267 if (applicationExtension == null) 268 throw new RuntimeException (NLS.bind(Messages.application_notFound, getApplicationDescriptor().getApplicationId(), ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getAvailableAppsMsg())); 269 IConfigurationElement[] configs = applicationExtension.getConfigurationElements(); 270 if (configs.length == 0) 271 throw new RuntimeException (NLS.bind(Messages.application_invalidExtension, getApplicationDescriptor().getApplicationId())); 272 return configs[0]; 273 } 274 275 public String getBrandingApplication() { 276 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 277 return branding == null ? null : branding.getApplication(); 278 } 279 280 public Bundle getBrandingBundle() { 281 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 282 return branding == null ? null : branding.getDefiningBundle(); 283 284 } 285 286 public String getBrandingDescription() { 287 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 288 return branding == null ? null : branding.getDescription(); 289 290 } 291 292 public String getBrandingId() { 293 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 294 return branding == null ? null : branding.getId(); 295 } 296 297 public String getBrandingName() { 298 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 299 return branding == null ? null : branding.getName(); 300 301 } 302 303 public String getBrandingProperty(String key) { 304 IBranding branding = ((EclipseAppDescriptor) getApplicationDescriptor()).getContainerManager().getBranding(); 305 return branding == null ? null : branding.getProperty(key); 306 } 307 308 boolean isDefault() { 309 return defaultAppInstance.booleanValue(); 310 } 311 312 synchronized Object waitForResult(int timeout) { 313 if (!setResult) 314 try { 315 wait(timeout); } catch (InterruptedException e) { 317 } 319 return result; 320 } 321 } 322 | Popular Tags |