View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.tooling.commons.packmgr.httpaction;
21  
22  import java.util.Set;
23  import java.util.regex.Pattern;
24  
25  /**
26   * Wrapper for Status summary from Web Console Bundles Status info JSON.
27   * @param statusLine Status Line from JSON string
28   * @param total Total bundles
29   * @param active Active bundles
30   * @param activeFragment Active fragment bundles
31   * @param resolved Resolved bundles
32   * @param installed Installed bundles
33   * @param ignored Ignored bundles
34   * @param bundleSymbolicNames Bundle symbolic names
35   */
36  public record BundleStatus(String statusLine, int total, int active, int activeFragment,
37      int resolved, int installed, int ignored,
38      Set<String> bundleSymbolicNames) {
39  
40    /**
41     * @return Compact version of status line.
42     */
43    public String getStatusLineCompact() {
44      StringBuilder sb = new StringBuilder();
45      sb.append(total).append(" total");
46      if (active > 0) {
47        sb.append(", ").append(active).append(" active");
48      }
49      if (activeFragment > 0) {
50        sb.append(", ").append(activeFragment).append(" fragment");
51      }
52      if (resolved > 0) {
53        sb.append(", ").append(resolved).append(" resolved");
54      }
55      if (installed > 0) {
56        sb.append(", ").append(installed).append(" installed");
57      }
58      if (ignored > 0) {
59        sb.append(", ").append(ignored).append(" ignored");
60      }
61      return sb.toString();
62    }
63  
64    /**
65     * @return true if no bundles are in "installed" or "resolved" state.
66     */
67    public boolean isAllBundlesRunning() {
68      return installed() + resolved() == 0;
69    }
70  
71    /**
72     * @param symbolicName Bundle symbolic name
73     * @return true if the given bundle is contained in the bundle list
74     */
75    public boolean containsBundle(String symbolicName) {
76      return bundleSymbolicNames.contains(symbolicName);
77    }
78  
79    /**
80     * Checks if a bundle with the given pattern exists in the bundle list.
81     * @param symbolicNamePattern Bundle symbolic name pattern
82     * @return Bundle name if a bundle was found, null otherwise
83     */
84    public String getMatchingBundle(Pattern symbolicNamePattern) {
85      for (String bundleSymbolicName : bundleSymbolicNames) {
86        if (symbolicNamePattern.matcher(bundleSymbolicName).matches()) {
87          return bundleSymbolicName;
88        }
89      }
90      return null;
91    }
92  
93  }