/
BundlesCommand.java
166 lines (137 loc) · 3.38 KB
/
BundlesCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright 2011 Ken Gilmer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.knapsack.shell.commands;
import org.osgi.framework.Bundle;
import org.sprinkles.Applier;
/**
* Print bundle information.
*
* @author kgilmer
*
*/
public class BundlesCommand extends AbstractKnapsackCommand {
@Override
public String execute() throws Exception {
final StringBuilder sb = new StringBuilder(1024 * 8);
Applier.map(context.getBundles(), new PrintBundleFunction(sb, !arguments.contains("-b")));
return sb.toString();
}
@Override
public String getName() {
return "knapsack-bundles";
}
@Override
public String getUsage() {
return "[-b (brief)]";
}
@Override
public String getDescription() {
return "Get list of OSGi bundles installed in the framework.";
}
public static void appendId(StringBuilder sb, long id) {
sb.append("[");
if (id < 10)
sb.append(" ");
sb.append(id);
sb.append("]");
}
/**
* @param b
* @return The location on filesystem of bundle
*/
public static String getBundleLocation(Bundle b) {
//Remove "file://" prefix
StringBuilder sb = new StringBuilder();
sb.append(b.getLocation().substring(7));
return sb.toString();
}
/**
* @param b
* @return The bundle version as defined in the manifest.
*/
public static String getBundleVersion(Bundle b) {
if (b == null)
return "";
String version = (String) b.getHeaders().get("Bundle-Version");
if (version == null) {
version = "";
}
StringBuilder sb = new StringBuilder();
sb.append(version);
return sb.toString();
}
public static String getBundleLabel(Bundle b) {
if (b == null)
return "";
StringBuilder sb = new StringBuilder();
BundlesCommand.appendId(sb, b.getBundleId());
sb.append(getBundleName(b));
sb.append(" (");
sb.append(BundlesCommand.getBundleVersion(b));
sb.append(")");
return sb.toString();
}
public static String getBundleName(Bundle b) {
if (b == null)
return "[null]";
String name = (String) b.getHeaders().get("Bundle-SymbolicName");
if (name == null) {
name = (String) b.getHeaders().get("Bundle-Name");
}
if (name == null) {
name = "Undefined";
}
if (name.indexOf(";") > -1)
name = name.split(";")[0];
StringBuilder sb = new StringBuilder();
sb.append(name);
return sb.toString();
}
/**
* Return state label as defined in OSGi spec.
*
* @param state
* @return
*/
public static void getStateName(int state, StringBuilder sb) {
String l = null;
switch (state) {
case 0x00000001:
l = "UNINS";
break;
case 0x00000002:
l = "INSTL";
break;
case 0x00000004:
l = "RESOL";
break;
case 0x00000008:
l = "START";
break;
case 0x00000010:
l = " STOP";
break;
case 0x00000020:
l = "ACTIV";
break;
default:
l = "[UNKNOWN STATE: " + state + "]";
break;
}
sb.append(l);
}
}