1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.tooling.commons.packmgr.httpaction;
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.regex.Pattern;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30
31
32
33
34 final class BundleStatusParser {
35
36 private final List<Pattern> bundleStatusWhitelistBundleNames;
37
38 BundleStatusParser(List<Pattern> bundleStatusWhitelistBundleNames) {
39 this.bundleStatusWhitelistBundleNames = bundleStatusWhitelistBundleNames;
40 }
41
42 BundleStatus parse(String jsonString) {
43 JSONObject json = new JSONObject(jsonString);
44
45 String statusLine = json.getString("status");
46
47
48 int total = 0;
49 int active = 0;
50 int activeFragment = 0;
51 int resolved = 0;
52 int installed = 0;
53 int ignored = 0;
54
55
56 Set<String> bundleSymbolicNames = new HashSet<>();
57 JSONArray data = json.getJSONArray("data");
58 for (int i = 0; i < data.length(); i++) {
59 JSONObject item = data.getJSONObject(i);
60
61 String symbolicName = item.optString("symbolicName");
62 String state = item.optString("state");
63 boolean fragment = item.optBoolean("fragment");
64 boolean whitelisted = isWhitelisted(symbolicName);
65
66 total++;
67 if (fragment) {
68 activeFragment++;
69 }
70 else if (isActive(state)) {
71 active++;
72 }
73 else if (isResolved(state)) {
74 if (whitelisted) {
75 ignored++;
76 }
77 else {
78 resolved++;
79 }
80 }
81 else if (isInstalled(state)) {
82 if (whitelisted) {
83 ignored++;
84 }
85 else {
86 installed++;
87 }
88 }
89
90 if (StringUtils.isNotBlank(symbolicName) && !whitelisted) {
91 bundleSymbolicNames.add(symbolicName);
92 }
93 }
94
95 return new BundleStatus(
96 statusLine,
97 total, active, activeFragment, resolved, installed, ignored,
98 bundleSymbolicNames);
99 }
100
101 private boolean isActive(String actual) {
102 return StringUtils.equalsIgnoreCase(actual, "Active");
103 }
104
105 private boolean isResolved(String actual) {
106 return StringUtils.equalsIgnoreCase(actual, "Resolved");
107 }
108
109 private boolean isInstalled(String actual) {
110 return StringUtils.equalsIgnoreCase(actual, "Installed");
111 }
112
113 private boolean isWhitelisted(String symbolicName) {
114 for (Pattern pattern : bundleStatusWhitelistBundleNames) {
115 if (pattern.matcher(symbolicName).matches()) {
116 return true;
117 }
118 }
119 return false;
120 }
121
122 }