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   */
28  public final class BundleStatus {
29  
30    private final String statusLine;
31    private final int total;
32    private final int active;
33    private final int activeFragment;
34    private final int resolved;
35    private final int installed;
36    private final int ignored;
37    private final Set<String> bundleSymbolicNames;
38  
39    BundleStatus(String statusLine, int total, int active, int activeFragment,
40        int resolved, int installed, int ignored,
41        Set<String> bundleSymbolicNames) {
42      this.statusLine = statusLine;
43      this.total = total;
44      this.active = active;
45      this.activeFragment = activeFragment;
46      this.resolved = resolved;
47      this.installed = installed;
48      this.ignored = ignored;
49      this.bundleSymbolicNames = bundleSymbolicNames;
50    }
51  
52    /**
53     * @return Status Line from JSON string
54     * @deprecated Please use {@link #getStatusLineCompact()}
55     */
56    @Deprecated(since = "1.6.0")
57    public String getStatusLine() {
58      return this.statusLine;
59    }
60  
61    /**
62     * @return Compact version of status line.
63     */
64    public String getStatusLineCompact() {
65      StringBuilder sb = new StringBuilder();
66      sb.append(total).append(" total");
67      if (active > 0) {
68        sb.append(", ").append(active).append(" active");
69      }
70      if (activeFragment > 0) {
71        sb.append(", ").append(activeFragment).append(" fragment");
72      }
73      if (resolved > 0) {
74        sb.append(", ").append(resolved).append(" resolved");
75      }
76      if (installed > 0) {
77        sb.append(", ").append(installed).append(" installed");
78      }
79      if (ignored > 0) {
80        sb.append(", ").append(ignored).append(" ignored");
81      }
82      return sb.toString();
83    }
84  
85    /**
86     * @return Total bundles
87     */
88    public int getTotal() {
89      return this.total;
90    }
91  
92    /**
93     * @return Active bundles
94     */
95    public int getActive() {
96      return this.active;
97    }
98  
99    /**
100    * @return Active fragments
101    */
102   public int getActiveFragment() {
103     return this.activeFragment;
104   }
105 
106   /**
107    * @return Resolved bundles
108    */
109   public int getResolved() {
110     return this.resolved;
111   }
112 
113   /**
114    * @return Installed bundles
115    */
116   public int getInstalled() {
117     return this.installed;
118   }
119 
120   /**
121    * @return Ignored bundles
122    */
123   public int getIgnored() {
124     return ignored;
125   }
126 
127   /**
128    * @return true if no bundles are in "installed" or "resolved" state.
129    */
130   public boolean isAllBundlesRunning() {
131     return getInstalled() + getResolved() == 0;
132   }
133 
134   /**
135    * @param symbolicName Bundle symbolic name
136    * @return true if the given bundle is contained in the bundle list
137    */
138   public boolean containsBundle(String symbolicName) {
139     return bundleSymbolicNames.contains(symbolicName);
140   }
141 
142   /**
143    * Checks if a bundle with the given pattern exists in the bundle list.
144    * @param symbolicNamePattern Bundle symbolic name pattern
145    * @return Bundle name if a bundle was found, null otherwise
146    */
147   public String getMatchingBundle(Pattern symbolicNamePattern) {
148     for (String bundleSymbolicName : bundleSymbolicNames) {
149       if (symbolicNamePattern.matcher(bundleSymbolicName).matches()) {
150         return bundleSymbolicName;
151       }
152     }
153     return null;
154   }
155 
156 }