1 11 12 package org.eclipse.equinox.internal.app; 13 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 import org.eclipse.core.runtime.IContributor; 17 import org.eclipse.core.runtime.IExtensionRegistry; 18 import org.eclipse.core.runtime.spi.RegistryContributor; 19 import org.eclipse.osgi.framework.log.FrameworkLog; 20 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 21 import org.eclipse.osgi.service.debug.DebugOptions; 22 import org.eclipse.osgi.service.environment.EnvironmentInfo; 23 import org.osgi.framework.*; 24 import org.osgi.service.packageadmin.PackageAdmin; 25 import org.osgi.util.tracker.ServiceTracker; 26 import org.osgi.util.tracker.ServiceTrackerCustomizer; 27 28 public class Activator implements BundleActivator, ServiceTrackerCustomizer { 29 public static final String PI_APP = "org.eclipse.equinox.app"; public static boolean DEBUG = false; 31 private static BundleContext context; 32 private static PackageAdmin packageAdmin; 35 private static EclipseAppContainer container; 36 private static ServiceTracker frameworkLog; 38 private ServiceTracker registryTracker; 40 private IExtensionRegistry registry; 41 42 public void start(BundleContext bc) { 43 context = bc; 44 ServiceReference ref = context.getServiceReference(PackageAdmin.class.getName()); 46 if (ref != null) 47 packageAdmin = (PackageAdmin) context.getService(ref); 48 getDebugOptions(context); 49 processCommandLineArgs(); 50 AppPersistence.start(context); 52 registryTracker = new ServiceTracker(context, IExtensionRegistry.class.getName(), this); 54 registryTracker.open(); 55 try { 57 AppCommands.create(context); 58 } catch (NoClassDefFoundError e) { 59 } 61 } 62 63 public void stop(BundleContext bc) { 64 try { 66 AppCommands.destroy(context); 67 } catch (NoClassDefFoundError e) { 68 } 70 registryTracker.close(); 72 registryTracker = null; 73 AppPersistence.stop(); 75 if (frameworkLog != null) { 76 frameworkLog.close(); 77 frameworkLog = null; 78 } 79 packageAdmin = null; context = null; 81 } 82 83 private void getDebugOptions(BundleContext context) { 84 ServiceReference debugRef = context.getServiceReference(DebugOptions.class.getName()); 85 if (debugRef == null) 86 return; 87 DebugOptions debugOptions = (DebugOptions) context.getService(debugRef); 88 DEBUG = debugOptions.getBooleanOption(PI_APP + "/debug", false); context.ungetService(debugRef); 90 } 91 92 private void processCommandLineArgs() { 93 ServiceReference infoRef = context.getServiceReference(EnvironmentInfo.class.getName()); 94 if (infoRef == null) 95 return; 96 EnvironmentInfo envInfo = (EnvironmentInfo) context.getService(infoRef); 97 if (envInfo == null) 98 return; 99 String [] args = envInfo.getNonFrameworkArgs(); 100 context.ungetService(infoRef); 101 CommandLineArgs.processCommandLine(args); 102 } 103 104 public Object addingService(ServiceReference reference) { 105 if (container != null) 106 return null; Object service = context.getService(reference); 108 if (registry == null && service instanceof IExtensionRegistry) { 109 registry = (IExtensionRegistry) service; 110 container = new EclipseAppContainer(context, registry); 112 container.start(); 113 return service; 114 } 115 context.ungetService(reference); 117 return null; 118 } 119 120 public void modifiedService(ServiceReference reference, Object service) { 121 } 123 124 public void removedService(ServiceReference reference, Object service) { 125 if (service == registry) 127 registry = null; 128 if (container == null) 129 return; container.stop(); 132 container = null; 133 } 134 135 static void openTracker(final ServiceTracker tracker, final boolean allServices) { 137 if (System.getSecurityManager() == null) 138 tracker.open(allServices); 139 else 140 AccessController.doPrivileged(new PrivilegedAction () { 141 public Object run() { 142 tracker.open(allServices); 143 return null; 144 } 145 }); 146 } 147 148 static Object getService(final ServiceTracker tracker) { 150 if (System.getSecurityManager() == null) 151 return tracker.getService(); 152 return AccessController.doPrivileged(new PrivilegedAction () { 153 public Object run() { 154 return tracker.getService(); 155 } 156 }); 157 } 158 159 static String getLocation(final Bundle bundle) { 161 if (System.getSecurityManager() == null) 162 return bundle.getLocation(); 163 return (String ) AccessController.doPrivileged(new PrivilegedAction () { 164 public Object run() { 165 return bundle.getLocation(); 166 } 167 }); 168 } 169 170 static Bundle getBundle(IContributor contributor) { 172 if (contributor instanceof RegistryContributor) { 173 try { 174 long id = Long.parseLong(((RegistryContributor) contributor).getActualId()); 175 if (context != null) 176 return context.getBundle(id); 177 } catch (NumberFormatException e) { 178 } 180 } 181 if (packageAdmin == null) 182 return null; 183 Bundle[] bundles = packageAdmin.getBundles(contributor.getName(), null); 184 if (bundles == null) 185 return null; 186 for (int i = 0; i < bundles.length; i++) 188 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) 189 return bundles[i]; 190 return null; 191 } 192 193 static BundleContext getContext() { 194 return context; 195 } 196 197 public static EclipseAppContainer getContainer() { 198 return container; 199 } 200 201 static void log(FrameworkLogEntry entry) { 202 BundleContext bc = context; 203 if (bc == null) 204 return; 205 ServiceReference ref = null; 206 try { 207 ref = bc.getServiceReference(FrameworkLog.class.getName()); 208 if (ref == null) 209 return; 210 FrameworkLog log = (FrameworkLog) context.getService(ref); 211 if (log != null) 212 log.log(entry); 213 } finally { 214 if (ref != null) 215 bc.ungetService(ref); 216 } 217 } 218 } 219 | Popular Tags |