1 11 package org.eclipse.equinox.internal.app; 12 13 import java.util.*; 14 import org.eclipse.equinox.app.IApplicationContext; 15 import org.eclipse.osgi.framework.console.CommandInterpreter; 16 import org.eclipse.osgi.framework.console.CommandProvider; 17 import org.osgi.framework.*; 18 import org.osgi.service.application.*; 19 import org.osgi.util.tracker.ServiceTracker; 20 21 public class AppCommands implements CommandProvider { 22 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)"; 26 private static AppCommands instance; 27 private BundleContext context; 28 private ServiceTracker applicationDescriptors; 29 private ServiceTracker applicationHandles; 30 private ServiceTracker scheduledApplications; 31 private Filter launchableApp; 32 private Filter activeApp; 33 private Filter lockedApp; 34 private ServiceRegistration providerRegistration; 35 36 static synchronized void create(BundleContext context) { 37 if (instance != null) 38 return; 39 instance = new AppCommands(); 40 instance.start(context); 41 } 42 43 static synchronized void destroy(BundleContext context) { 44 if (instance == null) 45 return; 46 instance.stop(context); 47 instance = null; 48 } 49 50 protected AppCommands() { 51 } 53 54 public void start(BundleContext ctx) { 55 this.context = ctx; 56 try { 57 applicationDescriptors = new ServiceTracker(ctx, ApplicationDescriptor.class.getName(), null); 58 applicationDescriptors.open(); 59 applicationHandles = new ServiceTracker(ctx, ApplicationHandle.class.getName(), null); 60 applicationHandles.open(); 61 scheduledApplications = new ServiceTracker(ctx, ScheduledApplication.class.getName(), null); 62 scheduledApplications.open(); 63 launchableApp = ctx.createFilter(LAUNCHABLE_APP_FILTER); 64 activeApp = ctx.createFilter(ACTIVE_APP_FILTER); 65 lockedApp = ctx.createFilter(LOCKED_APP_FILTER); 66 providerRegistration = ctx.registerService(CommandProvider.class.getName(), this, null); 67 } catch (InvalidSyntaxException e) { 68 } 70 } 71 72 public void stop(BundleContext ctx) { 73 providerRegistration.unregister(); 74 if (applicationDescriptors != null) 75 applicationDescriptors.close(); 76 if (applicationHandles != null) 77 applicationHandles.close(); 78 if (scheduledApplications != null) 79 scheduledApplications.close(); 80 } 81 82 public String getHelp() { 83 StringBuffer sb = new StringBuffer (); 84 sb.append("\n---Application Admin Commands---\n"); sb.append("\tactiveApps - lists all running application IDs\n"); sb.append("\tapps - lists all installed application IDs\n"); sb.append("\tlockApp <application id> - locks the specified application ID\n"); sb.append("\tschedApp <application id> <time filter> [true|false] - schedules the specified application id to launch at the specified time filter. Can optionally make the schedule recurring.\n"); sb.append("\tstartApp <application id> - starts the specified application ID\n"); sb.append("\tstopApp <application id> - stops the specified running application ID\n"); sb.append("\tunlockApp <application id> - unlocks the specified application ID\n"); sb.append("\tunschedApp <application id> - unschedules all scheduled applications with the specified application ID\n"); return sb.toString(); 94 } 95 96 private Dictionary getServiceProps(ServiceReference ref) { 97 String [] keys = ref.getPropertyKeys(); 98 Hashtable props = new Hashtable(keys.length); 99 for (int i = 0; i < keys.length; i++) 100 props.put(keys[i], ref.getProperty(keys[i])); 101 return props; 102 } 103 104 public void _apps(CommandInterpreter intp) { 105 ServiceReference[] apps = applicationDescriptors.getServiceReferences(); 106 if (apps == null) { 107 intp.println("No applications found."); return; 109 } 110 for (int i = 0; i < apps.length; i++) { 111 String application = (String ) apps[i].getProperty(ApplicationDescriptor.APPLICATION_PID); 112 intp.print(application); 113 114 if (getApplication(applicationHandles.getServiceReferences(), application, ApplicationHandle.APPLICATION_DESCRIPTOR, true) != null) 115 intp.print(" [running]"); 117 if (getApplication(scheduledApplications.getServiceReferences(), application, ScheduledApplication.APPLICATION_PID, true) != null) 118 intp.print(" [scheduled]"); 120 if (!launchableApp.match(getServiceProps(apps[i]))) 121 intp.print(" [not launchable]"); else 123 intp.print(" [launchable]"); 125 if (lockedApp.match(getServiceProps(apps[i]))) 126 intp.print(" [locked]"); intp.println(); 128 } 129 } 130 131 public void _activeApps(CommandInterpreter intp) { 132 ServiceReference[] active = applicationHandles.getServiceReferences(); 133 if (active == null) { 134 intp.println("No active applications found"); return; 136 } 137 for (int i = 0; i < active.length; i++) { 138 intp.print(active[i].getProperty(ApplicationHandle.APPLICATION_PID)); 139 intp.print(" ["); intp.print(activeApp.match(getServiceProps(active[i])) ? "running" : "stopping"); intp.println("]"); } 143 } 144 145 private ServiceReference getApplication(ServiceReference[] apps, String targetId, String idKey, boolean perfectMatch) { 146 ServiceReference result = null; 147 if (apps != null && targetId != null) 148 for (int i = 0; i < apps.length; i++) { 149 String id = (String ) apps[i].getProperty(idKey); 150 if (perfectMatch) { 151 if (targetId.equals(id)) return apps[i]; 153 } else if (id.indexOf(targetId) >= 0) { 154 if (result != null) { 155 if (!id.equals(result.getProperty(idKey))) 156 return null; 158 } else { 159 result = apps[i]; 160 } 161 } 162 } 163 return result; 164 } 165 166 public void _startApp(CommandInterpreter intp) throws Exception { 167 String appId = intp.nextArgument(); 168 ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false); 169 if (application == null) 170 intp.println("\"" + appId + "\" does not exist or is ambigous."); else { 172 ArrayList argList = new ArrayList(); 173 String arg = null; 174 while ((arg = intp.nextArgument()) != null) 175 argList.add(arg); 176 String [] args = argList.size() == 0 ? null : (String []) argList.toArray(new String [argList.size()]); 177 try { 178 HashMap launchArgs = new HashMap(1); 179 if (args != null) 180 launchArgs.put(IApplicationContext.APPLICATION_ARGS, args); 181 ApplicationDescriptor appDesc = ((ApplicationDescriptor) context.getService(application)); 182 ApplicationHandle handle = appDesc.launch(launchArgs); 183 intp.println("Launched application instance: " + handle.getInstanceId()); } finally { 185 context.ungetService(application); 186 } 187 return; 188 } 189 } 190 191 public void _stopApp(CommandInterpreter intp) throws Exception { 192 String appId = intp.nextArgument(); 193 ServiceReference application = getApplication(applicationHandles.getServiceReferences(), appId, ApplicationHandle.APPLICATION_DESCRIPTOR, false); 194 if (application == null) 195 intp.println("\"" + appId + "\" does not exist, is not running or is ambigous."); else { 197 if (activeApp.match(getServiceProps(application))) { 198 try { 199 ApplicationHandle appHandle = (ApplicationHandle) context.getService(application); 200 appHandle.destroy(); 201 intp.println("Stopped application instance: " + appHandle.getInstanceId()); } finally { 203 context.ungetService(application); 204 } 205 } else { 206 intp.println("Application instance is already stopping: " + application.getProperty(ApplicationHandle.APPLICATION_PID)); } 208 return; 209 } 210 } 211 212 public void _lockApp(CommandInterpreter intp) throws Exception { 213 String appId = intp.nextArgument(); 214 ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false); 215 if (application == null) 216 intp.println("\"" + appId + "\" does not exist or is ambigous."); else { 218 try { 219 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application); 220 appDesc.lock(); 221 intp.println("Locked application: " + appDesc.getApplicationId()); } finally { 223 context.ungetService(application); 224 } 225 return; 226 } 227 } 228 229 public void _unlockApp(CommandInterpreter intp) throws Exception { 230 String appId = intp.nextArgument(); 231 ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false); 232 if (application == null) 233 intp.println("\"" + appId + "\" does not exist or is ambigous."); else { 235 try { 236 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application); 237 appDesc.unlock(); 238 intp.println("Unlocked application: " + appDesc.getApplicationId()); } finally { 240 context.ungetService(application); 241 } 242 return; 243 } 244 } 245 246 public void _schedApp(CommandInterpreter intp) throws Exception { 247 String appId = intp.nextArgument(); 248 ServiceReference application = getApplication(applicationDescriptors.getServiceReferences(), appId, ApplicationDescriptor.APPLICATION_PID, false); 249 if (application == null) 250 intp.println("\"" + appId + "\" does not exist or is ambigous."); else { 252 try { 253 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application); 254 String filter = intp.nextArgument(); 255 boolean recure = Boolean.valueOf(intp.nextArgument()).booleanValue(); 256 appDesc.schedule(null, null, "org/osgi/application/timer", filter, recure); intp.println("Scheduled application: " + appDesc.getApplicationId()); } finally { 259 context.ungetService(application); 260 } 261 return; 262 } 263 } 264 265 public void _unschedApp(CommandInterpreter intp) throws Exception { 266 String appId = intp.nextArgument(); 267 ServiceReference application = getApplication(scheduledApplications.getServiceReferences(), appId, ScheduledApplication.APPLICATION_PID, false); 268 if (application == null) 269 intp.println("\"" + appId + "\" does not exist or is ambigous."); else { 271 try { 272 ScheduledApplication schedApp = (ScheduledApplication) context.getService(application); 273 schedApp.remove(); 274 intp.println("Unscheduled application: " + application.getProperty(ApplicationDescriptor.APPLICATION_PID)); } finally { 276 context.ungetService(application); 277 } 278 } 279 } 280 } 281 | Popular Tags |