blob: 303f8311c0db0a8db35093b21888bba8921c10b3 [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.Dictionary;
21import java.util.StringTokenizer;
22
Richard S. Hall5a031592005-08-19 19:53:58 +000023import org.apache.felix.shell.Command;
Richard S. Hall930fecc2005-08-16 18:33:34 +000024import org.osgi.framework.*;
25import org.osgi.service.startlevel.StartLevel;
26
27public class PsCommandImpl implements Command
28{
29 private BundleContext m_context = null;
30
31 public PsCommandImpl(BundleContext context)
32 {
33 m_context = context;
34 }
35
36 public String getName()
37 {
38 return "ps";
39 }
40
41 public String getUsage()
42 {
43 return "ps [-l | -u]";
44 }
45
46 public String getShortDescription()
47 {
48 return "list installed bundles.";
49 }
50
51 public void execute(String s, PrintStream out, PrintStream err)
52 {
53 // Get start level service.
54 ServiceReference ref = m_context.getServiceReference(
55 org.osgi.service.startlevel.StartLevel.class.getName());
56 StartLevel sl = null;
57 if (ref != null)
58 {
59 sl = (StartLevel) m_context.getService(ref);
60 }
61
62 if (sl == null)
63 {
64 out.println("StartLevel service is unavailable.");
65 }
66
67 // Parse command line.
68 StringTokenizer st = new StringTokenizer(s, " ");
69
70 // Ignore the command name.
71 st.nextToken();
72
73 // Check for optional argument.
74 boolean showLoc = false;
75 boolean showUpdate = false;
76 if (st.countTokens() >= 1)
77 {
78 while (st.hasMoreTokens())
79 {
80 String token = st.nextToken().trim();
81 if (token.equals("-l"))
82 {
83 showLoc = true;
84 }
85 else if (token.equals("-u"))
86 {
87 showUpdate = true;
88 }
89 }
90 }
91 Bundle[] bundles = m_context.getBundles();
92 if (bundles != null)
93 {
94 // Display active start level.
95 if (sl != null)
96 {
97 out.println("START LEVEL " + sl.getStartLevel());
98 }
99
100 // Print column headers.
101 String msg = " Name";
102 if (showLoc)
103 {
104 msg = " Location";
105 }
106 else if (showUpdate)
107 {
108 msg = " Update location";
109 }
110 String level = (sl == null) ? "" : " Level ";
111 out.println(" ID " + " State " + level + msg);
112 for (int i = 0; i < bundles.length; i++)
113 {
114 // Get the bundle name or location.
115 String name = (String)
116 bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
117 if (showLoc)
118 {
119 name = bundles[i].getLocation();
120 }
121 else if (showUpdate)
122 {
123 Dictionary dict = bundles[i].getHeaders();
124 name = (String) dict.get(Constants.BUNDLE_UPDATELOCATION);
125 if (name == null)
126 {
127 name = bundles[i].getLocation();
128 }
129 }
130 // Show bundle version if not showing location.
131 String version = (String)
132 bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
133 name = (!showLoc && !showUpdate && (version != null))
134 ? name + " (" + version + ")" : name;
135 long l = bundles[i].getBundleId();
136 String id = String.valueOf(l);
137 if (sl == null)
138 {
139 level = "1";
140 }
141 else
142 {
143 level = String.valueOf(sl.getBundleStartLevel(bundles[i]));
144 }
145 while (level.length() < 5)
146 {
147 level = " " + level;
148 }
149 while (id.length() < 4)
150 {
151 id = " " + id;
152 }
153 out.println("[" + id + "] ["
154 + getStateString(bundles[i].getState())
155 + "] [" + level + "] " + name);
156 }
157 }
158 else
159 {
160 out.println("There are no installed bundles.");
161 }
162 }
163
164 public String getStateString(int i)
165 {
166 if (i == Bundle.ACTIVE)
167 return "Active ";
168 else if (i == Bundle.INSTALLED)
169 return "Installed ";
170 else if (i == Bundle.RESOLVED)
171 return "Resolved ";
172 else if (i == Bundle.STARTING)
173 return "Starting ";
174 else if (i == Bundle.STOPPING)
175 return "Stopping ";
176 return "Unknown ";
177 }
178}