blob: b88ec411dfc33b82cf728fb198093d11f8eb591b [file] [log] [blame]
Richard S. Hall930fecc2005-08-16 18:33:34 +00001/*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
Richard S. Hall5a031592005-08-19 19:53:58 +000017package org.apache.felix.shell.impl;
Richard S. Hall930fecc2005-08-16 18:33:34 +000018
19import java.io.PrintStream;
20import java.util.StringTokenizer;
21
Richard S. Hall5a031592005-08-19 19:53:58 +000022import org.apache.felix.shell.Command;
Richard S. Hall930fecc2005-08-16 18:33:34 +000023import org.osgi.framework.*;
24import org.osgi.service.packageadmin.ExportedPackage;
25import org.osgi.service.packageadmin.PackageAdmin;
26
27public class PackagesCommandImpl implements Command
28{
29 private BundleContext m_context = null;
30
31 public PackagesCommandImpl(BundleContext context)
32 {
33 m_context = context;
34 }
35
36 public String getName()
37 {
38 return "packages";
39 }
40
41 public String getUsage()
42 {
43 return "packages [<id> ...]";
44 }
45
46 public String getShortDescription()
47 {
48 return "list exported packages.";
49 }
50
51 public void execute(String s, PrintStream out, PrintStream err)
52 {
53 // Get package admin service.
54 ServiceReference ref = m_context.getServiceReference(
55 org.osgi.service.packageadmin.PackageAdmin.class.getName());
56 if (ref == null)
57 {
58 out.println("PackageAdmin service is unavailable.");
59 return;
60 }
61
62 PackageAdmin pa = (PackageAdmin) m_context.getService(ref);
63 if (pa == null)
64 {
65 out.println("PackageAdmin service is unavailable.");
66 return;
67 }
68
69 // Parse command line.
70 StringTokenizer st = new StringTokenizer(s, " ");
71
72 // Ignore the command name.
73 st.nextToken();
74
75 if (st.hasMoreTokens())
76 {
77 while (st.hasMoreTokens())
78 {
79 String id = st.nextToken();
80 try
81 {
82 long l = Long.parseLong(id);
83 Bundle bundle = m_context.getBundle(l);
84 ExportedPackage[] exports = pa.getExportedPackages(bundle);
85 printExports(out, bundle, exports);
86 }
87 catch (NumberFormatException ex)
88 {
89 err.println("Unable to parse id '" + id + "'.");
90 }
91 catch (Exception ex)
92 {
93 err.println(ex.toString());
94 }
95 }
96 }
97 else
98 {
99 ExportedPackage[] exports = pa.getExportedPackages((Bundle) null);
100 printExports(out, null, exports);
101 }
102 }
103
104 private void printExports(PrintStream out, Bundle target, ExportedPackage[] exports)
105 {
106 if ((exports != null) && (exports.length > 0))
107 {
108 for (int i = 0; i < exports.length; i++)
109 {
110 Bundle bundle = exports[i].getExportingBundle();
111 out.print(Util.getBundleName(bundle));
112 out.println(": " + exports[i]);
113 }
114 }
115 else
116 {
117 out.println(Util.getBundleName(target)
118 + ": No active exported packages.");
119 }
120 }
121}