package org.eclipse.equinox.internal.app;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.service.runnable.ApplicationLauncher;
import org.eclipse.osgi.service.runnable.ParameterizedRunnable;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.*;
import org.osgi.service.application.*;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class EclipseAppContainer implements IRegistryEventListener, SynchronousBundleListener, ServiceTrackerCustomizer {
private static final String PI_RUNTIME = "org.eclipse.core.runtime";
private static final String PT_APPLICATIONS = "applications";
private static final String PT_APP_VISIBLE = "visible";
private static final String PT_APP_THREAD = "thread";
private static final String PT_APP_THREAD_ANY = "any";
private static final String PT_APP_CARDINALITY = "cardinality";
private static final String PT_APP_CARDINALITY_SINGLETON_GLOBAL = "singleton-global";
private static final String PT_APP_CARDINALITY_SINGLETON_SCOPED = "singleton-scoped";
private static final String PT_APP_CARDINALITY_UNLIMITED = "*";
private static final String PT_APP_ICON = "icon";
private static final String PT_PRODUCTS = "products";
private static final String EXT_ERROR_APP = "org.eclipse.equinox.app.error";
static final String PROP_PRODUCT = "eclipse.product";
static final String PROP_ECLIPSE_APPLICATION = "eclipse.application";
private static final String PROP_ECLIPSE_APPLICATION_LAUNCH_DEFAULT = "eclipse.application.launchDefault";
static final int NOT_LOCKED = 0;
static final int LOCKED_SINGLETON_GLOBAL_RUNNING = 1;
static final int LOCKED_SINGLETON_GLOBAL_APPS_RUNNING = 2;
static final int LOCKED_SINGLETON_SCOPED_RUNNING = 3;
static final int LOCKED_SINGLETON_LIMITED_RUNNING = 4;
static final int LOCKED_MAIN_THREAD_RUNNING = 5;
final BundleContext context;
private final Object lock = new Object();
final private HashMap<String, EclipseAppDescriptor> apps = new HashMap<>();
final private IExtensionRegistry extensionRegistry;
final private ServiceTracker launcherTracker;
private IBranding branding;
private boolean missingProductReported;
final private Collection<EclipseAppHandle> activeHandles = new ArrayList<>();
private EclipseAppHandle activeMain;
private EclipseAppHandle activeGlobalSingleton;
private EclipseAppHandle activeScopedSingleton;
private HashMap<String, ArrayList<EclipseAppHandle>> activeLimited;
private String defaultAppId;
private DefaultApplicationListener defaultAppListener;
private ParameterizedRunnable defaultMainThreadAppHandle;
private volatile boolean missingApp = false;
private MainApplicationLauncher missingAppLauncher;
public EclipseAppContainer(BundleContext context, IExtensionRegistry extensionRegistry) {
this.context = context;
this.extensionRegistry = extensionRegistry;
launcherTracker = new ServiceTracker(context, ApplicationLauncher.class.getName(), this);
}
void start() {
launcherTracker.open();
extensionRegistry.addListener(this, PI_RUNTIME + '.' + PT_APPLICATIONS);
context.addBundleListener(this);
registerAppDescriptors();
String startDefaultProp = context.getProperty(EclipseAppContainer.PROP_ECLIPSE_APPLICATION_LAUNCH_DEFAULT);
if (startDefaultProp == null || "true".equalsIgnoreCase(startDefaultProp)) {
try {
startDefaultApp(true);
} catch (ApplicationException e) {
Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.ERROR, 0, Messages.application_errorStartDefault, 0, e, null));
}
}
}
void stop() {
stopAllApps();
context.removeBundleListener(this);
extensionRegistry.removeListener(this);
apps.clear();
branding = null;
missingProductReported = false;
launcherTracker.close();
}
private EclipseAppDescriptor getAppDescriptor(String applicationId) {
EclipseAppDescriptor result = null;
synchronized (lock) {
result = apps.get(applicationId);
}
if (result == null) {
registerAppDescriptor(applicationId);
synchronized (lock) {
result = apps.get(applicationId);
}
}
return result;
}
private EclipseAppDescriptor createAppDescriptor(IExtension appExtension) {
if (Activator.DEBUG)
System.out.println("Creating application descriptor: " + appExtension.getUniqueIdentifier());
String iconPath = null;
synchronized (lock) {
EclipseAppDescriptor appDescriptor = apps.get(appExtension.getUniqueIdentifier());
if (appDescriptor != null)
return appDescriptor;
IConfigurationElement[] configs = appExtension.getConfigurationElements();
int flags = EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL | EclipseAppDescriptor.FLAG_VISIBLE | EclipseAppDescriptor.FLAG_TYPE_MAIN_THREAD;
int cardinality = 0;
if (configs.length > 0) {
String sVisible = configs[0].getAttribute(PT_APP_VISIBLE);
if (sVisible != null && !Boolean.valueOf(sVisible).booleanValue())
flags &= ~(EclipseAppDescriptor.FLAG_VISIBLE);
String sThread = configs[0].getAttribute(PT_APP_THREAD);
if (PT_APP_THREAD_ANY.equals(sThread)) {
flags |= EclipseAppDescriptor.FLAG_TYPE_ANY_THREAD;
flags &= ~(EclipseAppDescriptor.FLAG_TYPE_MAIN_THREAD);
}
String sCardinality = configs[0].getAttribute(PT_APP_CARDINALITY);
if (sCardinality != null) {
flags &= ~(EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL);
if (PT_APP_CARDINALITY_SINGLETON_SCOPED.equals(sCardinality))
flags |= EclipseAppDescriptor.FLAG_CARD_SINGLETON_SCOPED;
else if (PT_APP_CARDINALITY_UNLIMITED.equals(sCardinality))
flags |= EclipseAppDescriptor.FLAG_CARD_UNLIMITED;
else if (PT_APP_CARDINALITY_SINGLETON_GLOBAL.equals(sCardinality))
flags |= EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL;
else {
try {
cardinality = Integer.parseInt(sCardinality);
flags |= EclipseAppDescriptor.FLAG_CARD_LIMITED;
} catch (NumberFormatException e) {
flags |= EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL;
}
}
}
String defaultApp = getDefaultAppId();
if (defaultApp != null && defaultApp.equals(appExtension.getUniqueIdentifier()))
flags |= EclipseAppDescriptor.FLAG_DEFAULT_APP;
iconPath = configs[0].getAttribute(PT_APP_ICON);
}
appDescriptor = new EclipseAppDescriptor(Activator.getBundle(appExtension.getContributor()), appExtension.getUniqueIdentifier(), appExtension.getLabel(), iconPath, flags, cardinality, this);
ServiceRegistration sr = (ServiceRegistration) AccessController.doPrivileged(new RegisterService(new String[] {ApplicationDescriptor.class.getName()}, appDescriptor, appDescriptor.getServiceProperties()));
appDescriptor.setServiceRegistration(sr);
apps.put(appExtension.getUniqueIdentifier(), appDescriptor);
return appDescriptor;
}
}
private EclipseAppDescriptor removeAppDescriptor(String applicationId) {
if (Activator.DEBUG)
System.out.println("Removing application descriptor: " + applicationId);
synchronized (lock) {
EclipseAppDescriptor appDescriptor = apps.remove(applicationId);
if (appDescriptor == null)
return null;
appDescriptor.unregister();
return appDescriptor;
}
}
PrivilegedAction getRegServiceAction(String[] serviceClasses, Object serviceObject, Dictionary<String, ?> serviceProps) {
return new RegisterService(serviceClasses, serviceObject, serviceProps);
}
private class RegisterService implements PrivilegedAction {
String[] serviceClasses;
Object serviceObject;
Dictionary<String, ?> serviceProps;
RegisterService(String[] serviceClasses, Object serviceObject, Dictionary<String, ?> serviceProps) {
this.serviceClasses = serviceClasses;
this.serviceObject = serviceObject;
this.serviceProps = serviceProps;
}
@Override
public Object run() {
return context.registerService(serviceClasses, serviceObject, serviceProps);
}
}
void startDefaultApp(boolean delayError) throws ApplicationException {
String applicationId = getDefaultAppId();
EclipseAppDescriptor defaultDesc = null;
Map<String, Object> args = new HashMap<>(2);
args.put(EclipseAppDescriptor.APP_DEFAULT, Boolean.TRUE);
if (applicationId == null && !delayError) {
args.put(ErrorApplication.ERROR_EXCEPTION, new RuntimeException(Messages.application_noIdFound));
defaultDesc = getAppDescriptor(EXT_ERROR_APP);
} else {
defaultDesc = getAppDescriptor(applicationId);
if (defaultDesc == null && !delayError) {
args.put(ErrorApplication.ERROR_EXCEPTION, new RuntimeException(NLS.bind(Messages.application_notFound, applicationId, getAvailableAppsMsg())));
defaultDesc = getAppDescriptor(EXT_ERROR_APP);
}
}
if (delayError && defaultDesc == null) {
missingApp = true;
return;
}
if (defaultDesc != null)
defaultDesc.launch(args);
else
throw new ApplicationException(ApplicationException.APPLICATION_INTERNAL_ERROR, Messages.application_noIdFound);
}
private void registerAppDescriptors() {
IExtension[] availableApps = getAvailableAppExtensions();
for (IExtension availableApp : availableApps) {
createAppDescriptor(availableApp);
}
}
private void registerAppDescriptor(String applicationId) {
IExtension appExtension = getAppExtension(applicationId);
if (appExtension != null)
createAppDescriptor(appExtension);
}
private IExtension[] getAvailableAppExtensions() {
IExtensionPoint point = extensionRegistry.getExtensionPoint(PI_RUNTIME + '.' + PT_APPLICATIONS);
if (point == null)
return new IExtension[0];
return point.getExtensions();
}
String getAvailableAppsMsg() {
IExtension[] availableApps = getAvailableAppExtensions();
String availableAppsMsg = "<NONE>";
if (availableApps.length != 0) {
availableAppsMsg = availableApps[0].getUniqueIdentifier();
for (int i = 1; i < availableApps.length; i++)
availableAppsMsg = availableAppsMsg + ", " + availableApps[i].getUniqueIdentifier();
}
return availableAppsMsg;
}
IExtension getAppExtension(String applicationId) {
return extensionRegistry.getExtension(PI_RUNTIME, PT_APPLICATIONS, applicationId);
}
void launch(EclipseAppHandle appHandle) throws Exception {
boolean isDefault = appHandle.isDefault();
if (((EclipseAppDescriptor) appHandle.getApplicationDescriptor()).getThreadType() == EclipseAppDescriptor.FLAG_TYPE_MAIN_THREAD) {
DefaultApplicationListener curDefaultApplicationListener = null;
MainApplicationLauncher curMissingAppLauncher = null;
ApplicationLauncher appLauncher = null;
synchronized (this) {
appLauncher = (ApplicationLauncher) launcherTracker.getService();
if (appLauncher == null) {
if (isDefault) {
defaultMainThreadAppHandle = appHandle;
return;
}
throw new ApplicationException(ApplicationException.APPLICATION_INTERNAL_ERROR, NLS.bind(Messages.application_error_noMainThread, appHandle.getInstanceId()));
}
curDefaultApplicationListener = defaultAppListener;
curMissingAppLauncher = missingAppLauncher;
}
if (curDefaultApplicationListener != null)
curDefaultApplicationListener.launch(appHandle);
else if (curMissingAppLauncher != null)
curMissingAppLauncher.launch(appHandle);
else
appLauncher.launch(appHandle, appHandle.getArguments().get(IApplicationContext.APPLICATION_ARGS));
} else {
if (isDefault) {
DefaultApplicationListener curDefaultApplicationListener = null;
MainApplicationLauncher curMissingAppLauncher = null;
ApplicationLauncher appLauncher = null;
synchronized (this) {
appLauncher = (ApplicationLauncher) launcherTracker.getService();
if (defaultAppListener == null)
defaultAppListener = new DefaultApplicationListener(appHandle);
curDefaultApplicationListener = defaultAppListener;
if (appLauncher == null) {
defaultMainThreadAppHandle = curDefaultApplicationListener;
return;
}
curMissingAppLauncher = missingAppLauncher;
}
if (curMissingAppLauncher != null)
curMissingAppLauncher.launch(curDefaultApplicationListener);
else
appLauncher.launch(curDefaultApplicationListener, null);
} else {
AnyThreadAppLauncher.launchEclipseApplication(appHandle);
}
}
}
@Override
public void bundleChanged(BundleEvent event) {
if ((BundleEvent.STOPPING & event.getType()) == 0 || event.getBundle().getBundleId() != 0)
return;
stopAllApps();
}
private void stopAllApps() {
try {
ServiceReference[] runningRefs = context.getServiceReferences(ApplicationHandle.class.getName(), "(!(application.state=STOPPING))");
if (runningRefs != null)
for (ServiceReference runningRef : runningRefs) {
ApplicationHandle handle = (ApplicationHandle) context.getService(runningRef);
try {
if (handle != null)
handle.destroy();
} catch (Throwable t) {
String message = NLS.bind(Messages.application_error_stopping, handle.getInstanceId());
Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, t, null));
} finally {
if (handle != null) {
context.ungetService(runningRef);
}
}
}
} catch (InvalidSyntaxException e) {
}
}
private String getDefaultAppId() {
if (defaultAppId != null)
return defaultAppId;
defaultAppId = CommandLineArgs.getApplication();
if (defaultAppId != null)
return defaultAppId;
defaultAppId = context.getProperty(EclipseAppContainer.PROP_ECLIPSE_APPLICATION);
if (defaultAppId != null)
return defaultAppId;
defaultAppId = getBranding() == null ? null : getBranding().getApplication();
return defaultAppId;
}
public IBranding getBranding() {
if (branding != null)
return branding;
String productId = CommandLineArgs.getProduct();
if (productId == null) {
if (context == null)
return null;
productId = context.getProperty(PROP_PRODUCT);
if (productId == null)
return null;
}
IConfigurationElement[] entries = extensionRegistry.getConfigurationElementsFor(PI_RUNTIME, PT_PRODUCTS, productId);
if (entries.length > 0) {
branding = new ProductExtensionBranding(productId, entries[0]);
return branding;
}
IConfigurationElement[] elements = extensionRegistry.getConfigurationElementsFor(PI_RUNTIME, PT_PRODUCTS);
List<FrameworkLogEntry> logEntries = null;
for (IConfigurationElement element : elements) {
if (element.getName().equalsIgnoreCase("provider")) {
try {
Object provider = element.createExecutableExtension("run");
Object[] products = (Object[]) EclipseAppContainer.callMethod(provider, "getProducts", null, null);
if (products != null)
for (Object product : products) {
if (productId.equalsIgnoreCase((String) EclipseAppContainer.callMethod(product, "getId", null, null))) {
branding = new ProviderExtensionBranding(product);
return branding;
}
}
} catch (CoreException e) {
if (logEntries == null)
logEntries = new ArrayList<>(3);
logEntries.add(new FrameworkLogEntry(Activator.PI_APP, NLS.bind(Messages.provider_invalid, element.getParent().toString()), 0, e, null));
}
}
}
if (logEntries != null)
Activator.log(new FrameworkLogEntry(Activator.PI_APP, Messages.provider_invalid_general, 0, null, logEntries.toArray(new FrameworkLogEntry[logEntries.size()])));
if (!missingProductReported) {
Activator.log(new FrameworkLogEntry(Activator.PI_APP, NLS.bind(Messages.product_notFound, productId), 0, null, null));
missingProductReported = true;
}
return null;
}
private void refreshAppDescriptors() {
synchronized (lock) {
for (Iterator<EclipseAppDescriptor> allApps = apps.values().iterator(); allApps.hasNext();)
allApps.next().refreshProperties();
}
}
void lock(EclipseAppHandle appHandle) throws ApplicationException {
EclipseAppDescriptor eclipseApp = (EclipseAppDescriptor) appHandle.getApplicationDescriptor();
synchronized (lock) {
switch (isLocked(eclipseApp)) {
case NOT_LOCKED :
break;
case LOCKED_SINGLETON_GLOBAL_RUNNING :
throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, NLS.bind(Messages.singleton_running, activeGlobalSingleton.getInstanceId()));
case LOCKED_SINGLETON_GLOBAL_APPS_RUNNING :
throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, Messages.apps_running);
case LOCKED_SINGLETON_SCOPED_RUNNING :
throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, NLS.bind(Messages.singleton_running, activeScopedSingleton.getInstanceId()));
case LOCKED_SINGLETON_LIMITED_RUNNING :
throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, NLS.bind(Messages.max_running, eclipseApp.getApplicationId()));
case LOCKED_MAIN_THREAD_RUNNING :
throw new ApplicationException(ApplicationException.APPLICATION_NOT_LAUNCHABLE, NLS.bind(Messages.main_running, activeMain.getInstanceId()));
default :
break;
}
switch (eclipseApp.getCardinalityType()) {
case EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL :
activeGlobalSingleton = appHandle;
break;
case EclipseAppDescriptor.FLAG_CARD_SINGLETON_SCOPED :
activeScopedSingleton = appHandle;
break;
case EclipseAppDescriptor.FLAG_CARD_LIMITED :
if (activeLimited == null)
activeLimited = new HashMap<>(3);
ArrayList<EclipseAppHandle> limited = activeLimited.get(eclipseApp.getApplicationId());
if (limited == null) {
limited = new ArrayList<>(eclipseApp.getCardinality());
activeLimited.put(eclipseApp.getApplicationId(), limited);
}
limited.add(appHandle);
break;
case EclipseAppDescriptor.FLAG_CARD_UNLIMITED :
break;
default :
break;
}
if (eclipseApp.getThreadType() == EclipseAppDescriptor.FLAG_TYPE_MAIN_THREAD)
activeMain = appHandle;
activeHandles.add(appHandle);
refreshAppDescriptors();
}
}
void unlock(EclipseAppHandle appHandle) {
synchronized (lock) {
if (activeGlobalSingleton == appHandle)
activeGlobalSingleton = null;
else if (activeScopedSingleton == appHandle)
activeScopedSingleton = null;
else if (((EclipseAppDescriptor) appHandle.getApplicationDescriptor()).getCardinalityType() == EclipseAppDescriptor.FLAG_CARD_LIMITED) {
if (activeLimited != null) {
ArrayList<EclipseAppHandle> limited = activeLimited.get(((EclipseAppDescriptor) appHandle.getApplicationDescriptor()).getApplicationId());
if (limited != null)
limited.remove(appHandle);
}
}
if (activeMain == appHandle)
activeMain = null;
if (activeHandles.remove(appHandle))
refreshAppDescriptors();
}
}
int isLocked(EclipseAppDescriptor eclipseApp) {
synchronized (lock) {
if (activeGlobalSingleton != null)
return LOCKED_SINGLETON_GLOBAL_RUNNING;
switch (eclipseApp.getCardinalityType()) {
case EclipseAppDescriptor.FLAG_CARD_SINGLETON_GLOGAL :
if (activeHandles.size() > 0)
return LOCKED_SINGLETON_GLOBAL_APPS_RUNNING;
break;
case EclipseAppDescriptor.FLAG_CARD_SINGLETON_SCOPED :
if (activeScopedSingleton != null)
return LOCKED_SINGLETON_SCOPED_RUNNING;
break;
case EclipseAppDescriptor.FLAG_CARD_LIMITED :
if (activeLimited != null) {
ArrayList<EclipseAppHandle> limited = activeLimited.get(eclipseApp.getApplicationId());
if (limited != null && limited.size() >= eclipseApp.getCardinality())
return LOCKED_SINGLETON_LIMITED_RUNNING;
}
break;
case EclipseAppDescriptor.FLAG_CARD_UNLIMITED :
break;
default :
break;
}
if (eclipseApp.getThreadType() == EclipseAppDescriptor.FLAG_TYPE_MAIN_THREAD && activeMain != null)
return LOCKED_MAIN_THREAD_RUNNING;
return NOT_LOCKED;
}
}
static Object callMethod(Object obj, String methodName, Class<?>[] argTypes, Object[] args) {
try {
return callMethodWithException(obj, methodName, argTypes, args);
} catch (Throwable t) {
Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.ERROR, 0, "Error in invoking method.", 0, t, null));
}
return null;
}
static Object callMethodWithException(Object obj, String methodName, Class<?>[] argTypes, Object[] args) throws Exception {
try {
Method method = obj.getClass().getMethod(methodName, argTypes);
return method.invoke(obj, args);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof Error)
throw (Error) e.getTargetException();
if (e.getTargetException() instanceof Exception)
throw (Exception) e.getTargetException();
throw e;
}
}
@Override
public Object addingService(ServiceReference reference) {
ApplicationLauncher appLauncher;
ParameterizedRunnable appRunnable;
synchronized (this) {
appLauncher = (ApplicationLauncher) context.getService(reference);
appRunnable = defaultMainThreadAppHandle;
defaultMainThreadAppHandle = null;
if (appRunnable == null && missingApp) {
missingAppLauncher = new MainApplicationLauncher(this);
appRunnable = missingAppLauncher;
missingApp = false;
}
}
if (appRunnable != null)
appLauncher.launch(appRunnable, appRunnable instanceof EclipseAppHandle ? ((EclipseAppHandle) appRunnable).getArguments().get(IApplicationContext.APPLICATION_ARGS) : null);
return appLauncher;
}
@Override
public void modifiedService(ServiceReference reference, Object service) {
}
@Override
public void removedService(ServiceReference reference, Object service) {
}
@Override
public void added(IExtension[] extensions) {
for (IExtension extension : extensions) {
createAppDescriptor(extension);
}
}
@Override
public void added(IExtensionPoint[] extensionPoints) {
}
@Override
public void removed(IExtension[] extensions) {
for (IExtension extension : extensions) {
removeAppDescriptor(extension.getUniqueIdentifier());
}
}
@Override
public void removed(IExtensionPoint[] extensionPoints) {
}
}