package org.eclipse.equinox.internal.app;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.eclipse.core.runtime.IContributor;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.spi.RegistryContributor;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class Activator implements BundleActivator, ServiceTrackerCustomizer {
public static final String PI_APP = "org.eclipse.equinox.app";
public static boolean DEBUG = false;
private volatile static BundleContext _context;
private volatile static PackageAdmin _packageAdmin;
private volatile static EclipseAppContainer container;
private volatile static ServiceTracker _frameworkLogTracker;
private ServiceTracker registryTracker;
private IExtensionRegistry registry;
@Override
public void start(BundleContext bc) {
_context = bc;
ServiceReference ref = bc.getServiceReference(PackageAdmin.class.getName());
if (ref != null)
_packageAdmin = (PackageAdmin) bc.getService(ref);
_frameworkLogTracker = new ServiceTracker(bc, FrameworkLog.class.getName(), null);
_frameworkLogTracker.open();
getDebugOptions(bc);
processCommandLineArgs(bc);
AppPersistence.start(bc);
registryTracker = new ServiceTracker(bc, IExtensionRegistry.class.getName(), this);
registryTracker.open();
try {
AppCommands.create(bc);
} catch (NoClassDefFoundError e) {
}
}
@Override
public void stop(BundleContext bc) {
try {
AppCommands.destroy(bc);
} catch (NoClassDefFoundError e) {
}
registryTracker.close();
registryTracker = null;
AppPersistence.stop();
if (_frameworkLogTracker != null) {
_frameworkLogTracker.close();
_frameworkLogTracker = null;
}
_packageAdmin = null;
_context = null;
}
private void getDebugOptions(BundleContext context) {
ServiceReference debugRef = context.getServiceReference(DebugOptions.class.getName());
if (debugRef == null)
return;
DebugOptions debugOptions = (DebugOptions) context.getService(debugRef);
DEBUG = debugOptions.getBooleanOption(PI_APP + "/debug", false);
context.ungetService(debugRef);
}
private static EnvironmentInfo getEnvironmentInfo() {
BundleContext bc = Activator.getContext();
if (bc == null)
return null;
ServiceReference infoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
if (infoRef == null)
return null;
EnvironmentInfo envInfo = (EnvironmentInfo) bc.getService(infoRef);
if (envInfo == null)
return null;
bc.ungetService(infoRef);
return envInfo;
}
private void processCommandLineArgs(BundleContext bc) {
EnvironmentInfo envInfo = Activator.getEnvironmentInfo();
if (envInfo != null)
CommandLineArgs.processCommandLine(envInfo);
}
@Override
public Object addingService(ServiceReference reference) {
BundleContext context = _context;
if (context == null)
return null;
Object service = null;
EclipseAppContainer startContainer = null;
synchronized (this) {
if (container != null)
return null;
service = context.getService(reference);
if (registry == null && service instanceof IExtensionRegistry) {
registry = (IExtensionRegistry) service;
container = new EclipseAppContainer(context, registry);
startContainer = container;
}
}
if (startContainer != null) {
startContainer.start();
return service;
}
if (service != null)
context.ungetService(reference);
return null;
}
@Override
public void modifiedService(ServiceReference reference, Object service) {
}
@Override
public void removedService(ServiceReference reference, Object service) {
EclipseAppContainer currentContainer = null;
synchronized (this) {
if (service == registry)
registry = null;
if (container == null)
return;
currentContainer = container;
container = null;
}
if (currentContainer != null)
currentContainer.stop();
}
static void openTracker(final ServiceTracker tracker, final boolean allServices) {
if (System.getSecurityManager() == null)
tracker.open(allServices);
else
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
tracker.open(allServices);
return null;
}
});
}
static Object getService(final ServiceTracker tracker) {
if (System.getSecurityManager() == null)
return tracker.getService();
return AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
return tracker.getService();
}
});
}
static String getLocation(final Bundle bundle) {
if (System.getSecurityManager() == null)
return bundle.getLocation();
return (String) AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
return bundle.getLocation();
}
});
}
static Bundle getBundle(IContributor contributor) {
if (contributor instanceof RegistryContributor) {
try {
long id = Long.parseLong(((RegistryContributor) contributor).getActualId());
BundleContext context = _context;
if (context != null)
return context.getBundle(id);
} catch (NumberFormatException e) {
}
}
PackageAdmin packageAdmin = _packageAdmin;
if (packageAdmin == null)
return null;
Bundle[] bundles = packageAdmin.getBundles(contributor.getName(), null);
if (bundles == null)
return null;
for (Bundle bundle : bundles) {
if ((bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundle;
}
}
return null;
}
static BundleContext getContext() {
return _context;
}
public static EclipseAppContainer getContainer() {
return container;
}
static void log(FrameworkLogEntry entry) {
ServiceTracker frameworkLogTracker = _frameworkLogTracker;
FrameworkLog log = frameworkLogTracker == null ? null : (FrameworkLog) frameworkLogTracker.getService();
if (log != null)
log.log(entry);
}
static void setProperty(String key, String value) {
EnvironmentInfo envInfo = getEnvironmentInfo();
if (envInfo != null)
envInfo.setProperty(key, value);
else
System.getProperties().setProperty(key, value);
}
}