1 11 12 package org.eclipse.equinox.internal.app; 13 14 import java.security.AccessController ; 15 import java.util.*; 16 import org.eclipse.equinox.app.IApplicationContext; 17 import org.osgi.framework.*; 18 import org.osgi.service.application.ApplicationDescriptor; 19 import org.osgi.service.application.ApplicationHandle; 20 import org.osgi.service.condpermadmin.BundleSignerCondition; 21 import org.osgi.service.condpermadmin.ConditionInfo; 22 23 26 public class EclipseAppDescriptor extends ApplicationDescriptor { 27 static final String APP_TYPE = "eclipse.application.type"; static final String APP_DEFAULT = "eclipse.application.default"; static final String APP_TYPE_MAIN_THREAD = "main.thread"; static final String APP_TYPE_ANY_THREAD = "any.thread"; static final int FLAG_VISIBLE = 0x01; 32 static final int FLAG_CARD_SINGLETON_GLOGAL = 0x02; 33 static final int FLAG_CARD_SINGLETON_SCOPED = 0x04; 34 static final int FLAG_CARD_UNLIMITED = 0x08; 35 static final int FLAG_CARD_LIMITED = 0x10; 36 static final int FLAG_TYPE_MAIN_THREAD = 0x20; 37 static final int FLAG_TYPE_ANY_THREAD = 0x40; 38 static final int FLAG_DEFAULT_APP = 0x80; 39 private long instanceID = 0; 40 private ServiceRegistration sr; 41 private Boolean locked = Boolean.FALSE; 42 private final EclipseAppContainer appContainer; 43 private final Bundle contributor; 44 private final int flags; 45 private final int cardinality; 46 private final String name; 47 48 protected EclipseAppDescriptor(Bundle contributor, String pid, String name, int flags, int cardinality, EclipseAppContainer appContainer) { 49 super(pid); 50 this.name = name; 51 this.contributor = contributor; 52 this.appContainer = appContainer; 53 this.locked = AppPersistence.isLocked(this) ? Boolean.TRUE : Boolean.FALSE; 54 this.flags = flags; 55 this.cardinality = cardinality; 56 } 57 58 protected Map getPropertiesSpecific(String locale) { 59 return getServiceProperties(); 61 } 62 63 protected ApplicationHandle launchSpecific(Map arguments) throws Exception { 64 if (getLocked().booleanValue()) 66 throw new IllegalStateException ("Cannot launch a locked application."); EclipseAppHandle appHandle = createAppHandle(arguments); 69 try { 70 appContainer.launch(appHandle); 72 } catch (Throwable t) { 73 try { 75 appHandle.destroy(); 76 } catch (Throwable destroyError) { 77 } 79 if (t instanceof Exception ) 80 throw (Exception ) t; 81 throw (Error ) t; 82 } 83 return appHandle; 84 } 85 86 protected synchronized void lockSpecific() { 87 locked = Boolean.TRUE; 88 refreshProperties(); 90 } 91 92 protected synchronized void unlockSpecific() { 93 locked = Boolean.FALSE; 94 refreshProperties(); 96 } 97 98 void refreshProperties() { 99 ServiceRegistration reg = getServiceRegistration(); 100 if (reg != null) 101 try { 102 reg.setProperties(getServiceProperties()); 103 } catch (IllegalStateException e) { 104 } 107 } 108 109 synchronized void setServiceRegistration(ServiceRegistration sr) { 110 this.sr = sr; 111 } 112 113 private synchronized ServiceRegistration getServiceRegistration() { 114 return sr; 115 } 116 117 private synchronized Boolean getLocked() { 118 return locked; 119 } 120 121 124 Hashtable getServiceProperties() { 125 Hashtable props = new Hashtable(10); 126 props.put(ApplicationDescriptor.APPLICATION_PID, getApplicationId()); 127 if (name != null) 128 props.put(ApplicationDescriptor.APPLICATION_NAME, name); 129 props.put(ApplicationDescriptor.APPLICATION_CONTAINER, Activator.PI_APP); 130 props.put(ApplicationDescriptor.APPLICATION_LOCATION, getLocation()); 131 Boolean launchable = appContainer.isLocked(this) == 0 ? Boolean.TRUE : Boolean.FALSE; 132 props.put(ApplicationDescriptor.APPLICATION_LAUNCHABLE, launchable); 133 props.put(ApplicationDescriptor.APPLICATION_LOCKED, getLocked()); 134 Boolean visible = (flags & FLAG_VISIBLE) != 0 ? Boolean.TRUE : Boolean.FALSE; 135 props.put(ApplicationDescriptor.APPLICATION_VISIBLE, visible); 136 props.put(APP_TYPE, getThreadTypeString()); 137 if ((flags & FLAG_DEFAULT_APP) != 0) 138 props.put(APP_DEFAULT, Boolean.TRUE); 139 return props; 140 } 141 142 private String getLocation() { 143 if (contributor == null) 144 return ""; return Activator.getLocation(contributor); 146 } 147 148 151 private EclipseAppHandle createAppHandle(Map arguments) { 152 EclipseAppHandle newAppHandle = new EclipseAppHandle(getInstanceID(), arguments, this); 153 ServiceRegistration appHandleReg = (ServiceRegistration) AccessController.doPrivileged(appContainer.getRegServiceAction(new String [] {ApplicationHandle.class.getName(), IApplicationContext.class.getName()}, newAppHandle, newAppHandle.getServiceProperties())); 154 newAppHandle.setServiceRegistration(appHandleReg); 155 return newAppHandle; 156 } 157 158 EclipseAppContainer getContainerManager() { 159 return appContainer; 160 } 161 162 public boolean matchDNChain(String pattern) { 163 if (contributor == null) 164 return false; 165 return BundleSignerCondition.getCondition(contributor, new ConditionInfo(BundleSignerCondition.class.getName(), new String [] {pattern})).isSatisfied(); 166 } 167 168 protected boolean isLaunchableSpecific() { 169 return true; 170 } 171 172 public synchronized void unregister() { 173 ServiceRegistration temp = sr; 174 if (temp != null) { 175 sr = null; 176 temp.unregister(); 177 } 178 } 179 180 String getThreadTypeString() { 181 if ((flags & FLAG_TYPE_ANY_THREAD) != 0) 182 return APP_TYPE_ANY_THREAD; 183 return APP_TYPE_MAIN_THREAD; 184 } 185 186 int getThreadType() { 187 return flags & (FLAG_TYPE_ANY_THREAD | FLAG_TYPE_MAIN_THREAD); 188 } 189 190 int getCardinalityType() { 191 return flags & (FLAG_CARD_SINGLETON_GLOGAL | FLAG_CARD_SINGLETON_SCOPED | FLAG_CARD_LIMITED | FLAG_CARD_UNLIMITED); 192 } 193 194 int getCardinality() { 195 return cardinality; 196 } 197 198 private synchronized String getInstanceID() { 199 if (instanceID == Long.MAX_VALUE) 201 instanceID = 0; 202 return getApplicationId() + "." + instanceID++; } 205 } 206 | Popular Tags |