2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.oam.util;
27 import org.osgi.framework.Bundle;
29 import java.util.Collections;
30 import java.util.HashMap;
32 import java.util.regex.Pattern;
38 * Utility Class that splits a given bundleSet into two sets: bundleToStopSet and
41 * The bundleToStopSet is defined as: all bundles which match at least one of
42 * the stopRegexes but exceptRegexes none of the
44 * The bundleToNotStopSet is defined as all bundles which are not a member of
50 private final Map<String, Bundle> bundleToStopSet;
51 private final Map<String, Bundle> bundleToNotStopSet;
55 * BundleFilter a bundle filter
56 * @param stopRegexes - An array of regular expression used to pick out which bundles are candidates for stopping
57 * @param exceptRegexes - An array of regular expression used to override which bundles are candidates for stopping
58 * @param bundles - An array of the bundle to be split into {@link #getBundlesToStop()} {@link #getBundlesToNotStop()}
60 BundleFilter(String[] stopRegexes, String[] exceptRegexes, Bundle[] bundles) {
62 Pattern[] stopPatterns = toPattern(stopRegexes);
63 Pattern[] exceptPatterns = toPattern(exceptRegexes);
65 Map<String, Bundle> bundleToStop = new HashMap<>();
66 Map<String, Bundle> bundleToNotStop = new HashMap<>();
68 for (Bundle bundle : bundles) {
69 String symbolicName = bundle.getSymbolicName();
70 if (isMatch(symbolicName,stopPatterns) && !isMatch(symbolicName,exceptPatterns)) {
71 bundleToStop.put(symbolicName, bundle);
73 bundleToNotStop.put(symbolicName, bundle);
77 this.bundleToStopSet = Collections.unmodifiableMap(bundleToStop);
78 this.bundleToNotStopSet = Collections.unmodifiableMap(bundleToNotStop);
82 * Determines if the value matches any of the regular expressions.
85 * - the value that is to be matched
87 * - the array of {@link Pattern} to match the value against
88 * @return boolean true if there is a match
90 private boolean isMatch(String value,Pattern[] patterns) {
91 for (Pattern pattern : patterns) {
92 if (pattern.matcher(value).matches()) {
100 * This method converts an Array of regular expression in String form into a
101 * Array of {@link Pattern}
104 * - A string array of regular expressions
105 * @return Pattern Array of compiled regular expressions
107 private Pattern[] toPattern(String[] regex) {
108 Pattern[] pattern = new Pattern[regex.length];
109 for (int i = 0; i < regex.length; i++ ) {
110 pattern[i] = Pattern.compile(regex[i]);
116 /**@return Map of bundles that are to be stopped */
117 Map<String, Bundle> getBundlesToStop(){
118 return bundleToStopSet;
123 * @return Map of bundles that are not to be stopped
125 Map<String, Bundle> getBundlesToNotStop() {
126 return bundleToNotStopSet;