package org.eclipse.equinox.internal.app;
import java.util.*;
import java.util.Map.Entry;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.*;
import org.osgi.service.application.*;
import org.osgi.util.tracker.ServiceTracker;
public class AppCommands implements CommandProvider {
private final static String LAUNCHABLE_APP_FILTER = "(&(application.locked=false)(application.launchable=true)(application.visible=true))";
private final static String ACTIVE_APP_FILTER = "(!(application.state=STOPPING))";
private final static String LOCKED_APP_FILTER = "(application.locked=true)";
private final static String NEW_LINE = "\r\n";
private final static String TAB = "\t";
private Map<String, String[]> commandsHelp = null;
private static AppCommands instance;
private BundleContext context;
private ServiceTracker applicationDescriptors;
private ServiceTracker applicationHandles;
private ServiceTracker scheduledApplications;
private Filter launchableApp;
private Filter activeApp;
private Filter lockedApp;
private ServiceRegistration providerRegistration;
static synchronized void create(BundleContext context) {
if (instance != null)
return;
instance = new AppCommands();
instance.start(context);
}
static synchronized void destroy(BundleContext context) {
if (instance == null)
return;
instance.stop(context);
instance = null;
}
protected AppCommands() {
}
public void start(BundleContext ctx) {
this.context = ctx;
try {
applicationDescriptors = new ServiceTracker(ctx, ApplicationDescriptor.class.getName(), null);
applicationDescriptors.open();
applicationHandles = new ServiceTracker(ctx, ApplicationHandle.class.getName(), null);
applicationHandles.open();
scheduledApplications = new ServiceTracker(ctx, ScheduledApplication.class.getName(), null);
scheduledApplications.open();
launchableApp = ctx.createFilter(LAUNCHABLE_APP_FILTER);
activeApp = ctx.createFilter(ACTIVE_APP_FILTER);
lockedApp = ctx.createFilter(LOCKED_APP_FILTER);
providerRegistration = ctx.registerService(CommandProvider.class.getName(), this, null);
} catch (InvalidSyntaxException e) {
}
}
public void stop(BundleContext ctx) {
providerRegistration.unregister();
if (applicationDescriptors != null)
applicationDescriptors.close();
if (applicationHandles != null)
applicationHandles.close();
if (scheduledApplications != null)
scheduledApplications.close();
}
@Override
public String getHelp() {
return getHelp(null);
}
private String getHelp(String commandName) {
StringBuffer sb = new StringBuffer();
if (commandsHelp == null) {
initializeCommandsHelp();
}
if (commandName != null) {
if (commandsHelp.containsKey(commandName)) {
addCommand(commandName, commandsHelp.get(commandName), sb);
}
return sb.toString();
}
addHeader(Messages.console_help_app_commands_header, sb);
Iterator<Entry<String, String[]>> i = commandsHelp.entrySet().iterator();
while (i.hasNext()) {
Entry<String, String[]> entry = i.next();
String command = entry.getKey();
String[] attributes = entry.getValue();
addCommand(command, attributes, sb);
}
return sb.toString();
}
private void initializeCommandsHelp() {
commandsHelp = new LinkedHashMap<>();
commandsHelp.put("activeApps", new String[] {Messages.console_help_activeapps_description});
commandsHelp.put("apps", new String[] {Messages.console_help_apps_description});
commandsHelp.put("lockApp", new String[] {Messages.console_help_arguments, Messages.console_help_lockapp_description});
commandsHelp.put("schedApp", new String[] {Messages.console_help_schedapp_arguments, Messages.console_help_schedapp_description});
commandsHelp.put("startApp", new String[] {Messages.console_help_arguments, Messages.console_help_startapp_description});
commandsHelp.put("stopApp", new String[] {Messages.console_help_arguments, Messages.console_help_stopapp_description});
commandsHelp.put("unlockApp", new String[] {Messages.console_help_arguments, Messages.console_help_unlockapp_description});
commandsHelp.put("unschedApp", new String[] {Messages.console_help_arguments, Messages.console_help_unschedapp_description});
}
private void addHeader(String header, StringBuffer help) {
help.append("---");
help.append(header);
help.append("---");
help.append(NEW_LINE);
}
private void addCommand(String command, String description, StringBuffer help) {
help.append(TAB);
help.append(command);
help.append(" - ");
help.append(description);
help.append(NEW_LINE);
}
private void addCommand(String command, String parameters, String description, StringBuffer help) {
help.append(TAB);
help.append(command);
help.append(" ");
help.append(parameters);
help.append(" - ");
help.append(description);
help.append(NEW_LINE);
}
private void addCommand(String command, String[] attributes, StringBuffer help) {
if (attributes.length == 1) {
addCommand(command, attributes[0], help);
} else if (attributes.length == 2) {
addCommand(command, attributes[0], attributes[1], help);
}
}
private Dictionary<String, Object> getServiceProps(ServiceReference ref) {
String[] keys = ref.getPropertyKeys();
Hashtable<String, Object> props = new Hashtable<>(keys.length);
for (String key : keys) {
props.put(key, ref.getProperty(key));
}
return props;
}
public void _apps(CommandInterpreter intp) {
ServiceReference[] apps = applicationDescriptors.getServiceReferences();
if (apps == null) {
intp.println("No applications found.");
return;
}
for (ServiceReference app : apps) {
String application = (String) app.getProperty(ApplicationDescriptor.APPLICATION_PID);
intp.print(application);
if (getApplication(applicationHandles.getServiceReferences(), application, ApplicationHandle.APPLICATION_DESCRIPTOR, true) != null)
intp.print(" [running]");
if (getApplication(scheduledApplications.getServiceReferences(), application, ScheduledApplication.APPLICATION_PID, true) != null)
intp.print(" [scheduled]");
if (!launchableApp.match(getServiceProps(app))) {
intp.print(" [not launchable]");
} else {
intp.print(" [launchable]");
}
if (lockedApp.match(getServiceProps(app))) {
intp.print(" [locked]");
}
intp.println();
}
}
public void _activeApps(CommandInterpreter intp) {
ServiceReference[] active = applicationHandles.getServiceReferences();
if (active == null) {
intp.println("No active applications found");
return;
}
for (ServiceReference r : active) {
intp.print(r.getProperty(ApplicationHandle.APPLICATION_PID));
intp.print(" [");
intp.print(activeApp.match(getServiceProps(r)) ? "running" : "stopping");
intp.println("]");
}
}
private ServiceReference getApplication(ServiceReference[] apps, String targetId, String idKey, boolean perfectMatch) {
if (apps == null || targetId == null)
return null;
ServiceReference result = null;
boolean ambigous = false;
for (ServiceReference app : apps) {
String id = (String) app.getProperty(idKey);
if (targetId.equals(id)) {
return app;
}
if (perfectMatch)
continue;
if (id.contains(targetId)) {
if (result != null)
ambigous = true;
result = app;
}
}
return ambigous ? null : result;
}
public void _startApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist or is ambigous.");
else {
ArrayList<String> argList = new ArrayList<>();
String arg = null;
while ((arg = intp.nextArgument()) != null)
argList.add(arg);
String[] args = argList.size() == 0 ? null : (String[]) argList.toArray(new String[argList.size()]);
try {
HashMap<String, Object> launchArgs = new HashMap<>(1);
if (args != null)
launchArgs.put(IApplicationContext.APPLICATION_ARGS, args);
ApplicationDescriptor appDesc = (context.<ApplicationDescriptor> getService(application));
ApplicationHandle handle = appDesc.launch(launchArgs);
intp.println("Launched application instance: " + handle.getInstanceId());
} finally {
context.ungetService(application);
}
return;
}
}
public void _stopApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(applicationHandles.getServiceReferences(), appId, ApplicationHandle.APPLICATION_PID, false);
if (application == null)
application = getApplication(applicationHandles.getServiceReferences(), appId, ApplicationHandle.APPLICATION_DESCRIPTOR, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist, is not running or is ambigous.");
else {
if (activeApp.match(getServiceProps(application))) {
try {
ApplicationHandle appHandle = (ApplicationHandle) context.getService(application);
appHandle.destroy();
intp.println("Stopped application instance: " + appHandle.getInstanceId());
} finally {
context.ungetService(application);
}
} else {
intp.println("Application instance is already stopping: " + application.getProperty(ApplicationHandle.APPLICATION_PID));
}
return;
}
}
public void _lockApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist or is ambigous.");
else {
try {
ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
appDesc.lock();
intp.println("Locked application: " + appDesc.getApplicationId());
} finally {
context.ungetService(application);
}
return;
}
}
public void _unlockApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist or is ambigous.");
else {
try {
ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
appDesc.unlock();
intp.println("Unlocked application: " + appDesc.getApplicationId());
} finally {
context.ungetService(application);
}
return;
}
}
public void _schedApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist or is ambigous.");
else {
try {
ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
String filter = intp.nextArgument();
boolean recure = Boolean.valueOf(intp.nextArgument()).booleanValue();
appDesc.schedule(null, null, "org/osgi/application/timer", filter, recure);
intp.println("Scheduled application: " + appDesc.getApplicationId());
} finally {
context.ungetService(application);
}
return;
}
}
public void _unschedApp(CommandInterpreter intp) throws Exception {
String appId = intp.nextArgument();
ServiceReference application = getApplication(scheduledApplications.getServiceReferences(), appId, ScheduledApplication.APPLICATION_PID, false);
if (application == null)
intp.println("\"" + appId + "\" does not exist or is ambigous.");
else {
try {
ScheduledApplication schedApp = (ScheduledApplication) context.getService(application);
schedApp.remove();
intp.println("Unscheduled application: " + application.getProperty(ApplicationDescriptor.APPLICATION_PID));
} finally {
context.ungetService(application);
}
}
}
public Object _help(CommandInterpreter intp) {
String commandName = intp.nextArgument();
if (commandName == null) {
return Boolean.FALSE;
}
String help = getHelp(commandName);
if (help.length() > 0) {
return help;
}
return Boolean.FALSE;
}
}