Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QueueMapping |
|
| 2.3076923076923075;2.308 |
1 | package com.aurea.maven.plugins.sonic.sdm; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | import org.codehaus.plexus.util.xml.Xpp3Dom; | |
7 | ||
8 | /** | |
9 | * Represents a single queue mapping. Can be either a <code>defaultMapping</code> or a <code>queueMapping</code> | |
10 | * element. | |
11 | * | |
12 | * @author Gerco Dries | |
13 | */ | |
14 | public class QueueMapping { | |
15 | ||
16 | ||
17 | /** | |
18 | * Id of the queues element to map matching queues to. | |
19 | */ | |
20 | private String id; | |
21 | ||
22 | /** | |
23 | * Regular expressions to match queue names for this mapping. | |
24 | */ | |
25 | private final List<String> includes; | |
26 | ||
27 | /** | |
28 | * Regular expressions to match queue names to exclude from this mapping. | |
29 | */ | |
30 | private final List<String> excludes; | |
31 | ||
32 | /** | |
33 | * The parameterSet id for queues matching this mapping | |
34 | */ | |
35 | private String parameterSetId; | |
36 | ||
37 | /** | |
38 | * The value of the SupressJNDI element for in the SDM model for queues matching this mapping. | |
39 | */ | |
40 | 0 | private boolean suppressJNDI = true; |
41 | ||
42 | /** | |
43 | * Create an empty QueueMapping. | |
44 | */ | |
45 | 0 | public QueueMapping() { |
46 | ||
47 | 0 | includes = new ArrayList<String>(); |
48 | 0 | excludes = new ArrayList<String>(); |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Create a new QueueMapping with the specified id and include regular expression. | |
53 | * | |
54 | * @param _id | |
55 | * @param _include | |
56 | */ | |
57 | public QueueMapping(final String _id, final String _include) { | |
58 | ||
59 | 0 | this(); |
60 | 0 | setId(_id); |
61 | 0 | includes.add(_include); |
62 | 0 | } |
63 | ||
64 | /** | |
65 | * Create a new QueueMapping object from the specified dom element. The object is required to have an <code>id</code> | |
66 | * child element. A <code>include</code> child elements are optional and default to <code>.*</code>. The | |
67 | * <code>exclude</code> elements denotes regular expressions for excluding queues from the mapping. | |
68 | * | |
69 | * <pre> | |
70 | * <tt><queueMapping> | |
71 | * <id>some id</id> | |
72 | * <parameterSet>parameter set id</parameterSet> | |
73 | * <suppressJNDI>true or false</suppressJNDI> | |
74 | * <includes> | |
75 | * <include>include regex</include> | |
76 | * <include>another include regex</include> | |
77 | * </includes> | |
78 | * <excludes> | |
79 | * <exclude>exclude regex</exclude> | |
80 | * <exclude>another exclude regex</exclude> | |
81 | * </excludes> | |
82 | * </queueMapping> | |
83 | * </tt> | |
84 | * </pre> | |
85 | * <p> | |
86 | * The name of the enclosing element is not relevant. | |
87 | * <DL> | |
88 | * <DT>Note:</DT> | |
89 | * <DD> | |
90 | * The <code>suppressJNDI</code> element is valid only for SDM 7.6.x, since version 8.0 the JNDI mappings are handled | |
91 | * differently. NVR</DD> | |
92 | * </DL> | |
93 | * | |
94 | * @param _dom | |
95 | * @return | |
96 | */ | |
97 | public QueueMapping(final Xpp3Dom _dom) { | |
98 | ||
99 | 0 | this(); |
100 | ||
101 | 0 | final Xpp3Dom idElement = _dom.getChild("id"); |
102 | 0 | if (idElement == null) { |
103 | 0 | throw new RuntimeException("id element is required in a queueMapping"); |
104 | } | |
105 | 0 | setId(_dom.getChild("id").getValue()); |
106 | ||
107 | 0 | final Xpp3Dom parameterSetElement = _dom.getChild("parameterSet"); |
108 | 0 | if (parameterSetElement != null) { |
109 | 0 | setParameterSetId(parameterSetElement.getValue()); |
110 | } | |
111 | // No longer supported for SDN v8 NVR | |
112 | /* | |
113 | * Xpp3Dom suppressJNDIElement = _dom.getChild("suppressJNDI"); if (suppressJNDIElement != null) { | |
114 | * setSuppressJNDI(Boolean.parseBoolean(suppressJNDIElement.getValue())); } | |
115 | */ | |
116 | 0 | final Xpp3Dom includesElement = _dom.getChild("includes"); |
117 | 0 | if (includesElement != null) { |
118 | 0 | for (final Xpp3Dom includeElement : includesElement.getChildren("include")) { |
119 | 0 | addInclude(includeElement.getValue()); |
120 | } | |
121 | } else { | |
122 | 0 | addInclude(".*"); |
123 | } | |
124 | ||
125 | 0 | final Xpp3Dom excludesElement = _dom.getChild("excludes"); |
126 | 0 | if (excludesElement != null) { |
127 | 0 | for (final Xpp3Dom excludeElement : excludesElement.getChildren("exclude")) { |
128 | 0 | addExclude(excludeElement.getValue()); |
129 | } | |
130 | } | |
131 | 0 | } |
132 | ||
133 | /** | |
134 | * Determine whether the queue name matches this QueueMapping. A queue matches when it matches any | |
135 | * <code>include</code> and none of the <code>exclude</code>s. | |
136 | * | |
137 | * @param _queueName | |
138 | * @return | |
139 | */ | |
140 | public boolean matches(final String _queueName) { | |
141 | ||
142 | // Check excludes first. If the queue matched none of these, check includes. | |
143 | 0 | for (final String exclude : excludes) { |
144 | 0 | if (_queueName.matches(exclude)) { |
145 | 0 | return false; |
146 | } | |
147 | 0 | } |
148 | ||
149 | // If the queue matched any include, report a match. | |
150 | 0 | for (final String include : includes) { |
151 | 0 | if (_queueName.matches(include)) { |
152 | 0 | return true; |
153 | } | |
154 | 0 | } |
155 | ||
156 | // The queue matched none of the includes. | |
157 | 0 | return false; |
158 | } | |
159 | ||
160 | public String getId() { | |
161 | ||
162 | 0 | return id; |
163 | } | |
164 | ||
165 | public void setId(final String _id) { | |
166 | ||
167 | 0 | id = _id; |
168 | 0 | } |
169 | ||
170 | public void addInclude(final String _regex) { | |
171 | ||
172 | 0 | if (_regex == null) { |
173 | 0 | throw new IllegalArgumentException("Regex must not be null"); |
174 | } | |
175 | 0 | includes.add(_regex); |
176 | 0 | } |
177 | ||
178 | public void addExclude(final String _regex) { | |
179 | ||
180 | 0 | if (_regex == null) { |
181 | 0 | throw new IllegalArgumentException("Regex must not be null"); |
182 | } | |
183 | 0 | excludes.add(_regex); |
184 | 0 | } |
185 | ||
186 | public void setParameterSetId(final String _parameterSetId) { | |
187 | ||
188 | 0 | parameterSetId = _parameterSetId; |
189 | 0 | } |
190 | ||
191 | public String getParameterSetId() { | |
192 | ||
193 | 0 | return parameterSetId; |
194 | } | |
195 | ||
196 | public void setSuppressJNDI(final boolean _suppressJNDI) { | |
197 | ||
198 | 0 | suppressJNDI = _suppressJNDI; |
199 | 0 | } |
200 | ||
201 | public boolean isSuppressJNDI() { | |
202 | ||
203 | 0 | return suppressJNDI; |
204 | } | |
205 | ||
206 | @Override | |
207 | public String toString() { | |
208 | ||
209 | 0 | return String.format("id: %s, includes: %s, excludes: %s", getId(), includes, excludes); |
210 | } | |
211 | } |