1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ============LICENSE_END=========================================================
*/
package org.onap.appc.oam.util;
import org.osgi.framework.Bundle;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
*
* Utility Class that splits a given bundleSet into two sets: bundleToStopSet and
* bundleToNotStopSet
*
* The bundleToStopSet is defined as: all bundles which match at least one of
* the stopRegexes but exceptRegexes none of the
*
* The bundleToNotStopSet is defined as all bundles which are not a member of
* the bundleToStopSet
*
*/
public class BundleFilter {
private final Map<String, Bundle> bundleToStopSet;
private final Map<String, Bundle> bundleToNotStopSet;
/**
* BundleFilter a bundle filter
* @param stopRegexes - An array of regular expression used to pick out which bundles are candidates for stopping
* @param exceptRegexes - An array of regular expression used to override which bundles are candidates for stopping
* @param bundles - An array of the bundle to be split into {@link #getBundlesToStop()} {@link #getBundlesToNotStop()}
*/
public BundleFilter(String[] stopRegexes, String[] exceptRegexes, Bundle[] bundles) {
Pattern[] stopPatterns = toPattern(stopRegexes);
Pattern[] exceptPatterns = toPattern(exceptRegexes);
Map<String, Bundle> bundleToStop = new HashMap<>();
Map<String, Bundle> bundleToNotStop = new HashMap<>();
for (Bundle bundle : bundles) {
String symbolicName = bundle.getSymbolicName();
if (isMatch(symbolicName,stopPatterns) && !isMatch(symbolicName,exceptPatterns)) {
bundleToStop.put(symbolicName, bundle);
} else {
bundleToNotStop.put(symbolicName, bundle);
}
}
this.bundleToStopSet = Collections.unmodifiableMap(bundleToStop);
this.bundleToNotStopSet = Collections.unmodifiableMap(bundleToNotStop);
}
/**
* Determines if the value matches any of the regular expressions.
*
* @param value
* - the value that is to be matched
* @param patterns
* - the array of {@link Pattern} to match the value against
* @return boolean true if there is a match
*/
private boolean isMatch(String value,Pattern[] patterns) {
for (Pattern pattern : patterns) {
if (pattern.matcher(value).matches()) {
return true;
}
}
return false;
}
/**
* This method converts an Array of regular expression in String form into a
* Array of {@link Pattern}
*
* @param regex
* - A string array of regular expressions
* @return Pattern Array of compiled regular expressions
*/
private Pattern[] toPattern(String[] regex) {
Pattern[] pattern = new Pattern[regex.length];
for (int i = 0; i < regex.length; i++ ) {
pattern[i] = Pattern.compile(regex[i]);
}
return pattern;
}
/**@return Map of bundles that are to be stopped */
public Map<String, Bundle> getBundlesToStop(){
return bundleToStopSet;
}
/**
*
* @return Map of bundles that are not to be stopped
*/
public Map<String, Bundle> getBundlesToNotStop() {
return bundleToNotStopSet;
}
}
|