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
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    public int getTotal() {
86      return this.total;
87    }
88  
89    public int getActive() {
90      return this.active;
91    }
92  
93    public int getActiveFragment() {
94      return this.activeFragment;
95    }
96  
97    public int getResolved() {
98      return this.resolved;
99    }
100 
101   public int getInstalled() {
102     return this.installed;
103   }
104 
105   public int getIgnored() {
106     return ignored;
107   }
108 
109   /**
110    * @return true if no bundles are in "installed" or "resolved" state.
111    */
112   public boolean isAllBundlesRunning() {
113     return getInstalled() + getResolved() == 0;
114   }
115 
116   /**
117    * @param symbolicName Bundle symbolic name
118    * @return true if the given bundle is contained in the bundle list
119    */
120   public boolean containsBundle(String symbolicName) {
121     return bundleSymbolicNames.contains(symbolicName);
122   }
123 
124   /**
125    * Checks if a bundle with the given pattern exists in the bundle list.
126    * @param symbolicNamePattern Bundle symbolic name pattern
127    * @return Bundle name if a bundle was found, null otherwise
128    */
129   public String getMatchingBundle(Pattern symbolicNamePattern) {
130     for (String bundleSymbolicName : bundleSymbolicNames) {
131       if (symbolicNamePattern.matcher(bundleSymbolicName).matches()) {
132         return bundleSymbolicName;
133       }
134     }
135     return null;
136   }
137 
138 }