KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > internal > app > AppCommands


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc LAUNCHABLE_APP_FILTER = "(&(application.locked=false)(application.launchable=true)(application.visible=true))"; //$NON-NLS-1$
23
private final static String JavaDoc ACTIVE_APP_FILTER = "(!(application.state=STOPPING))"; //$NON-NLS-1$
24
private final static String JavaDoc LOCKED_APP_FILTER = "(application.locked=true)"; //$NON-NLS-1$
25

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         // empty
52
}
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             // should not happen.
69
}
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 JavaDoc getHelp() {
83         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
84         sb.append("\n---Application Admin Commands---\n"); //$NON-NLS-1$
85
sb.append("\tactiveApps - lists all running application IDs\n"); //$NON-NLS-1$
86
sb.append("\tapps - lists all installed application IDs\n"); //$NON-NLS-1$
87
sb.append("\tlockApp <application id> - locks the specified application ID\n"); //$NON-NLS-1$
88
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"); //$NON-NLS-1$
89
sb.append("\tstartApp <application id> - starts the specified application ID\n"); //$NON-NLS-1$
90
sb.append("\tstopApp <application id> - stops the specified running application ID\n"); //$NON-NLS-1$
91
sb.append("\tunlockApp <application id> - unlocks the specified application ID\n"); //$NON-NLS-1$
92
sb.append("\tunschedApp <application id> - unschedules all scheduled applications with the specified application ID\n"); //$NON-NLS-1$
93
return sb.toString();
94     }
95
96     private Dictionary getServiceProps(ServiceReference ref) {
97         String JavaDoc[] 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."); //$NON-NLS-1$
108
return;
109         }
110         for (int i = 0; i < apps.length; i++) {
111             String JavaDoc application = (String JavaDoc) 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]"); //$NON-NLS-1$
116

117             if (getApplication(scheduledApplications.getServiceReferences(), application, ScheduledApplication.APPLICATION_PID, true) != null)
118                 intp.print(" [scheduled]"); //$NON-NLS-1$
119

120             if (!launchableApp.match(getServiceProps(apps[i])))
121                 intp.print(" [not launchable]"); //$NON-NLS-1$
122
else
123                 intp.print(" [launchable]"); //$NON-NLS-1$
124

125             if (lockedApp.match(getServiceProps(apps[i])))
126                 intp.print(" [locked]"); //$NON-NLS-1$
127
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"); //$NON-NLS-1$
135
return;
136         }
137         for (int i = 0; i < active.length; i++) {
138             intp.print(active[i].getProperty(ApplicationHandle.APPLICATION_PID));
139             intp.print(" ["); //$NON-NLS-1$
140
intp.print(activeApp.match(getServiceProps(active[i])) ? "running" : "stopping"); //$NON-NLS-1$ //$NON-NLS-2$
141
intp.println("]"); //$NON-NLS-1$
142
}
143     }
144
145     private ServiceReference getApplication(ServiceReference[] apps, String JavaDoc targetId, String JavaDoc idKey, boolean perfectMatch) {
146         ServiceReference result = null;
147         if (apps != null && targetId != null)
148             for (int i = 0; i < apps.length; i++) {
149                 String JavaDoc id = (String JavaDoc) apps[i].getProperty(idKey);
150                 if (perfectMatch) {
151                     if (targetId.equals(id)) // perfect match return it
152
return apps[i];
153                 } else if (id.indexOf(targetId) >= 0) {
154                     if (result != null) {
155                         if (!id.equals(result.getProperty(idKey)))
156                             // this is ambiguous
157
return null;
158                     } else {
159                         result = apps[i];
160                     }
161                 }
162             }
163         return result;
164     }
165
166     public void _startApp(CommandInterpreter intp) throws Exception JavaDoc {
167         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
171
else {
172             ArrayList argList = new ArrayList();
173             String JavaDoc arg = null;
174             while ((arg = intp.nextArgument()) != null)
175                 argList.add(arg);
176             String JavaDoc[] args = argList.size() == 0 ? null : (String JavaDoc[]) argList.toArray(new String JavaDoc[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()); //$NON-NLS-1$
184
} finally {
185                 context.ungetService(application);
186             }
187             return;
188         }
189     }
190
191     public void _stopApp(CommandInterpreter intp) throws Exception JavaDoc {
192         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
196
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()); //$NON-NLS-1$
202
} finally {
203                     context.ungetService(application);
204                 }
205             } else {
206                 intp.println("Application instance is already stopping: " + application.getProperty(ApplicationHandle.APPLICATION_PID)); //$NON-NLS-1$
207
}
208             return;
209         }
210     }
211
212     public void _lockApp(CommandInterpreter intp) throws Exception JavaDoc {
213         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
217
else {
218             try {
219                 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
220                 appDesc.lock();
221                 intp.println("Locked application: " + appDesc.getApplicationId()); //$NON-NLS-1$
222
} finally {
223                 context.ungetService(application);
224             }
225             return;
226         }
227     }
228
229     public void _unlockApp(CommandInterpreter intp) throws Exception JavaDoc {
230         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
234
else {
235             try {
236                 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
237                 appDesc.unlock();
238                 intp.println("Unlocked application: " + appDesc.getApplicationId()); //$NON-NLS-1$
239
} finally {
240                 context.ungetService(application);
241             }
242             return;
243         }
244     }
245
246     public void _schedApp(CommandInterpreter intp) throws Exception JavaDoc {
247         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
251
else {
252             try {
253                 ApplicationDescriptor appDesc = (ApplicationDescriptor) context.getService(application);
254                 String JavaDoc filter = intp.nextArgument();
255                 boolean recure = Boolean.valueOf(intp.nextArgument()).booleanValue();
256                 appDesc.schedule(null, null, "org/osgi/application/timer", filter, recure); //$NON-NLS-1$
257
intp.println("Scheduled application: " + appDesc.getApplicationId()); //$NON-NLS-1$
258
} finally {
259                 context.ungetService(application);
260             }
261             return;
262         }
263     }
264
265     public void _unschedApp(CommandInterpreter intp) throws Exception JavaDoc {
266         String JavaDoc 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."); //$NON-NLS-1$ //$NON-NLS-2$
270
else {
271             try {
272                 ScheduledApplication schedApp = (ScheduledApplication) context.getService(application);
273                 schedApp.remove();
274                 intp.println("Unscheduled application: " + application.getProperty(ApplicationDescriptor.APPLICATION_PID)); //$NON-NLS-1$
275
} finally {
276                 context.ungetService(application);
277             }
278         }
279     }
280 }
281
Popular Tags