blob: d2c34f1fb6816086491a84fd5ee812a5ef07050a [file] [log] [blame]
Richard S. Hallfbd735b2009-06-11 16:07:20 +00001package aQute.shell.equinox;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7import org.osgi.framework.*;
8import org.osgi.service.command.*;
9import org.osgi.service.component.*;
10import org.osgi.service.log.*;
11import org.osgi.service.packageadmin.*;
12import org.osgi.service.startlevel.*;
13
14public class Equinox implements Converter {
15 BundleContext context;
16 PackageAdmin pka;
17 LogReaderService lrs;
18 StartLevel sls;
19 final static String[] functions = { "active", "bundles", "close", "diag",
20 "exec", "exit", "fork", "gc", "getprop", "headers", "init",
21 "install", "launch", "log", "packages", "packages", "refresh",
22 "services", "setbsl", "setfwsl", "setibsl", "setprop", "shutdown",
23 "sl", "ss", "start", "status", "stop", "uninstall", "update" };
24
25 protected void activate(ComponentContext context) {
26 this.context = context.getBundleContext();
27 Dictionary<String, Object> dict = new Hashtable<String, Object>();
28 dict.put(CommandProcessor.COMMAND_SCOPE, "eqx");
29 dict.put(CommandProcessor.COMMAND_FUNCTION, functions);
30 this.context.registerService( Converter.class.getName(), this, dict);
31 }
32
33 BundleContext getContext() {
34 return context;
35 }
36
37 public void setPka(PackageAdmin pka) {
38 this.pka = pka;
39 }
40
41 public void setLrs(LogReaderService lrs) {
42 this.lrs = lrs;
43 }
44
45 public void setSls(StartLevel sls) {
46 this.sls = sls;
47 }
48
49 /**
50 * - Displays unsatisfied constraints for the specified bundle(s).
51 */
52 public void diag() {
53 }
54
55 /*
56 * active - Displays a list of all bundles currently in the ACTIVE state.
57 */
58 public List<Bundle> active() {
59 List<Bundle> result = new ArrayList<Bundle>();
60 Bundle[] bundles = getContext().getBundles();
61 for (Bundle b : bundles) {
62 if (b.getState() == Bundle.ACTIVE)
63 result.add(b);
64 }
65 return result;
66 }
67
68 /*
69 * getprop { name } - Displays the system properties with the given name, or
70 * all of them.
71 */
72
73 public Object getprop(CharSequence name) {
74 if (name == null)
75 return System.getProperties();
76 else
77 return System.getProperty(name.toString());
78 }
79
80 /**
81 * launch - start the OSGi Framework
82 */
83
84 public void launch() {
85 throw new IllegalStateException("Already running");
86 }
87
88 /**
89 * shutdown - shutdown the OSGi Framework
90 */
91 public void shutdown() throws BundleException {
92 getContext().getBundle().stop();
93 }
94
95 /**
96 * close - shutdown and exit
97 */
98 public void close(CommandSession session) {
99 session.close();
100 }
101
102 /**
103 * exit - exit immediately (System.exit)
104 */
105
106 public void exit(int exitValue) {
107 exit(exitValue);
108 }
109
110 /**
111 * gc - perform a garbage collection
112 */
113 public long gc() {
114 Runtime.getRuntime().gc();
115 return Runtime.getRuntime().freeMemory();
116 }
117
118 /**
119 * init - uninstall all bundles
120 */
121
122 public void init() throws Exception {
123 Bundle bundles[] = getContext().getBundles();
124 for (Bundle b : bundles)
125 if (b.getBundleId() != 0)
126 b.uninstall();
127 }
128
129 /**
130 * setprop <key>=<value> - set the OSGi property
131 */
132 public void setprop(CommandSession session, String key, String value) {
133 session.put(key, value);
134 }
135
136 /**
137 * install - install and optionally start bundle from the given URL
138 *
139 * @throws BundleException
140 */
141
142 public Bundle install(URL url) throws BundleException {
143 return getContext().installBundle(url.toExternalForm());
144 }
145
146 /**
147 * uninstall - uninstall the specified bundle(s)
148 *
149 * @throws BundleException
150 */
151 public void uninstall(Bundle[] bundles) throws BundleException {
152 for (Bundle b : bundles) {
153 b.uninstall();
154 }
155 }
156
157 /**
158 * start - start the specified bundle(s)
159 */
160 public void start(Bundle[] bundles) throws BundleException {
161 for (Bundle b : bundles) {
162 b.start();
163 }
164 }
165
166 /**
167 * stop - stop the specified bundle(s)
168 */
169 public void stop(Bundle[] bundles) throws BundleException {
170 for (Bundle b : bundles) {
171 b.stop();
172 }
173 }
174
175 /**
176 * refresh - refresh the packages of the specified bundles
177 */
178 public void refresh(Bundle[] bundles) throws Exception {
179 if (pka != null)
180 pka.refreshPackages(bundles);
181 else
182 throw new RuntimeException("No PackageAdmin service registered");
183 }
184
185 /**
186 * update - update the specified bundle(s)
187 */
188 public void update(Bundle[] bundles) throws BundleException {
189 for (Bundle b : bundles) {
190 b.update();
191 }
192 }
193
194 /**
195 * status - display installed bundles and registered services
196 */
197 public List<Object> status() throws Exception {
198 List<Object> status = new ArrayList<Object>();
199 status.addAll(Arrays.asList(getContext().getBundles()));
200 status.addAll(Arrays.asList(getContext().getServiceReferences(null,
201 null)));
202 return status;
203 }
204
205 /**
206 * ss - display installed bundles (short status)
207 */
208 public Bundle[] ss() {
209 return getContext().getBundles();
210 }
211
212 /**
213 * services {filter} - display registered service details
214 */
215 public ServiceReference[] services(String filter) throws Exception {
216 return getContext().getServiceReferences(null, filter);
217 }
218
219 /**
220 * packages {<pkgname>|<id>|<location>} - display imported/exported
221 * package details
222 */
223 public ExportedPackage[] packages(Bundle bundle) throws Exception {
224 if (pka != null)
225 return pka.getExportedPackages(bundle);
226 else
227 throw new RuntimeException("No PackageAdmin service registered");
228 }
229
230 public ExportedPackage[] packages(String packageName) throws Exception {
231 if (pka != null)
232 return pka.getExportedPackages(packageName);
233 return null;
234 }
235
236 /**
237 * bundles - display details for all installed bundles
238 */
239 public Bundle[] bundles() {
240 return ss();
241 }
242
243 /**
244 * bundle (<id>|<location>) - display details for the specified bundle(s)
245 */
246
247 /**
248 * headers (<id>|<location>) - print bundle headers
249 */
250
251 @SuppressWarnings("unchecked")
252 public Dictionary headers(Bundle b, String locale) {
253 return b.getHeaders(locale);
254 }
255
256 /**
257 * log (<id>|<location>) - display log entries
258 */
259
260 @SuppressWarnings("unchecked")
261 public Collection<LogEntry> log(Bundle bundle) throws Exception {
262 if (lrs != null)
263 return Collections.list((Enumeration<LogEntry>) lrs.getLog());
264 else
265 throw new RuntimeException("LogReader not available");
266 }
267
268 /**
269 * exec <command> - execute a command in a separate process and wait
270 *
271 * @throws IOException
272 */
273
274 public int exec(Object[] args, boolean fork) throws IOException {
275 StringBuffer sb = new StringBuffer();
276 String del = "";
277 for (Object arg : args) {
278 sb.append(del);
279 sb.append(arg);
280 del = " ";
281 }
282 Process p = Runtime.getRuntime().exec(sb.toString());
283 if (fork) {
284 int c;
285 while ((c = p.getInputStream().read()) > 0)
286 System.out.print(c);
287 }
288 return p.exitValue();
289 }
290
291 /**
292 * fork <command> - execute a command in a separate process
293 */
294
295 public void fork(Object args[]) throws Exception {
296 exec(args, true);
297 }
298
299 /**
300 * sl {(<id>|<location>)} - display the start level for the specified
301 * bundle, or for the framework if no bundle specified
302 */
303 public int sl(Bundle b) throws Exception {
304 if (sls == null)
305 throw new RuntimeException("No StartLevel service registered");
306 if (b == null)
307 return sls.getStartLevel();
308 else
309 return sls.getBundleStartLevel(b);
310 }
311
312 /**
313 * setfwsl <start level> - set the framework start level
314 */
315 public int setfwsl(int n) throws Exception {
316 if (sls == null)
317 throw new RuntimeException("No StartLevel service registered");
318 int old = sls.getStartLevel();
319 sls.setStartLevel(n);
320 return old;
321 }
322
323 /**
324 * setbsl <start level> (<id>|<location>) - set the start level for the
325 * bundle(s)
326 */
327 public int setbsl(Bundle b, int n) throws Exception {
328 if (sls == null)
329 throw new RuntimeException("No StartLevel service registered");
330 int old = sls.getBundleStartLevel(b);
331 sls.setBundleStartLevel(b, n);
332 return old;
333 }
334
335 /**
336 * setibsl <start level> - set the initial bundle start level
337 */
338 public int setibsl(int n) throws Exception {
339 if (sls == null)
340 throw new RuntimeException("No StartLevel service registered");
341 int old = sls.getInitialBundleStartLevel();
342 sls.setInitialBundleStartLevel(n);
343 return old;
344 }
345
346 public Object convert(Class<?> desiredType, Object in) throws Exception {
347 return null;
348 }
349
350 String getLevel(int index) {
351 switch (index) {
352 case LogService.LOG_DEBUG:
353 return "DEBUG";
354 case LogService.LOG_INFO:
355 return "INFO ";
356 case LogService.LOG_WARNING:
357 return "WARNI";
358 case LogService.LOG_ERROR:
359 return "ERROR";
360 default:
361 return "<" + index + ">";
362 }
363 }
364
365 public CharSequence format(Object target, int level, Converter escape) {
366 if (target instanceof LogEntry) {
367 LogEntry entry = (LogEntry) target;
368 switch (level) {
369 case LINE:
370 Formatter f = new Formatter();
371 f.format("%tT %04d %s %s", entry.getTime(), entry.getBundle()
372 .getBundleId(), getLevel(entry.getLevel())+"", entry
373 .getMessage()+"");
374 return f.toString();
375
376 case PART:
377 Formatter f2 = new Formatter();
378 f2.format("%tT %s", entry.getTime(), entry
379 .getMessage());
380 return f2.toString();
381 }
382 }
383 return null;
384 }
385 /**
386 * profilelog - Display & flush the profile log messages
387 */
388
389}