author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.ws.policy.sourcemodel; |
|
27 |
||
28 |
import com.sun.xml.internal.ws.policy.PolicyConstants; |
|
29 |
import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; |
|
30 |
import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; |
|
31 |
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; |
|
32 |
||
33 |
import java.io.Serializable; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.HashSet; |
|
36 |
import java.util.Map; |
|
37 |
import java.util.Set; |
|
38 |
import javax.xml.namespace.QName; |
|
39 |
||
40 |
/** |
|
41 |
* Wrapper class for possible data that each "assertion" and "assertion parameter content" policy source model node may |
|
42 |
* have attached. |
|
43 |
* <p/> |
|
44 |
* This data, when stored in an 'assertion' model node, is intended to be used as input parameter when creating |
|
45 |
* {@link com.sun.xml.internal.ws.policy.PolicyAssertion} objects via {@link com.sun.xml.internal.ws.policy.spi.PolicyAssertionCreator} |
|
46 |
* implementations. |
|
47 |
* |
|
48 |
* @author Marek Potociar (marek.potociar@sun.com) |
|
49 |
* @author Fabian Ritzmann |
|
50 |
*/ |
|
51 |
public final class AssertionData implements Cloneable, Serializable { |
|
52 |
private static final long serialVersionUID = 4416256070795526315L; |
|
53 |
private static final PolicyLogger LOGGER = PolicyLogger.getLogger(AssertionData.class); |
|
54 |
||
55 |
private final QName name; |
|
56 |
private final String value; |
|
57 |
private final Map<QName, String> attributes; |
|
58 |
private ModelNode.Type type; |
|
59 |
||
60 |
private boolean optional; |
|
61 |
private boolean ignorable; |
|
62 |
||
63 |
/** |
|
64 |
* Constructs assertion data wrapper instance for an assertion that does not |
|
65 |
* contain any value nor any attributes. |
|
66 |
* |
|
67 |
* @param name the FQN of the assertion |
|
68 |
* |
|
69 |
* @throws IllegalArgumentException in case the {@code type} parameter is not |
|
70 |
* {@link ModelNode.Type#ASSERTION ASSERTION} or |
|
71 |
* {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE} |
|
72 |
*/ |
|
73 |
public static AssertionData createAssertionData(final QName name) throws IllegalArgumentException { |
|
74 |
return new AssertionData(name, null, null, ModelNode.Type.ASSERTION, false, false); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Constructs assertion data wrapper instance for an assertion parameter that |
|
79 |
* does not contain any value nor any attributes. |
|
80 |
* |
|
81 |
* @param name the FQN of the assertion parameter |
|
82 |
* |
|
83 |
* @throws IllegalArgumentException in case the {@code type} parameter is not |
|
84 |
* {@link ModelNode.Type#ASSERTION ASSERTION} or |
|
85 |
* {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE} |
|
86 |
*/ |
|
87 |
public static AssertionData createAssertionParameterData(final QName name) throws IllegalArgumentException { |
|
88 |
return new AssertionData(name, null, null, ModelNode.Type.ASSERTION_PARAMETER_NODE, false, false); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Constructs assertion data wrapper instance for an assertion that does |
|
93 |
* contain a value or attributes. |
|
94 |
* |
|
95 |
* @param name the FQN of the assertion |
|
96 |
* @param value a {@link String} representation of model node value |
|
97 |
* @param attributes map of model node's <attribute name, attribute value> pairs |
|
98 |
* @param optional flag indicating whether the assertion is optional or not |
|
99 |
* @param ignorable flag indicating whether the assertion is ignorable or not |
|
100 |
* |
|
101 |
* @throws IllegalArgumentException in case the {@code type} parameter is not |
|
102 |
* {@link ModelNode.Type#ASSERTION ASSERTION} or |
|
103 |
* {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE} |
|
104 |
*/ |
|
105 |
public static AssertionData createAssertionData(final QName name, final String value, final Map<QName, String> attributes, boolean optional, boolean ignorable) throws IllegalArgumentException { |
|
106 |
return new AssertionData(name, value, attributes, ModelNode.Type.ASSERTION, optional, ignorable); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Constructs assertion data wrapper instance for an assertion parameter that |
|
111 |
* contains a value or attributes |
|
112 |
* |
|
113 |
* @param name the FQN of the assertion parameter |
|
114 |
* @param value a {@link String} representation of model node value |
|
115 |
* @param attributes map of model node's <attribute name, attribute value> pairs |
|
116 |
* |
|
117 |
* @throws IllegalArgumentException in case the {@code type} parameter is not |
|
118 |
* {@link ModelNode.Type#ASSERTION ASSERTION} or |
|
119 |
* {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE} |
|
120 |
*/ |
|
121 |
public static AssertionData createAssertionParameterData(final QName name, final String value, final Map<QName, String> attributes) throws IllegalArgumentException { |
|
122 |
return new AssertionData(name, value, attributes, ModelNode.Type.ASSERTION_PARAMETER_NODE, false, false); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Constructs assertion data wrapper instance for an assertion or assertion parameter that contains a value or |
|
127 |
* some attributes. Whether the data wrapper is constructed for assertion or assertion parameter node is distinguished by |
|
128 |
* the supplied {@code type} parameter. |
|
129 |
* |
|
130 |
* @param name the FQN of the assertion or assertion parameter |
|
131 |
* @param value a {@link String} representation of model node value |
|
132 |
* @param attributes map of model node's <attribute name, attribute value> pairs |
|
133 |
* @param type specifies whether the data will belong to the assertion or assertion parameter node. This is |
|
134 |
* a workaround solution that allows us to transfer this information about the owner node to |
|
135 |
* a policy assertion instance factory without actualy having to touch the {@link PolicyAssertionCreator} |
|
136 |
* interface and protected {@link PolicyAssertion} constructors. |
|
137 |
* |
|
138 |
* @throws IllegalArgumentException in case the {@code type} parameter is not |
|
139 |
* {@link ModelNode.Type#ASSERTION ASSERTION} or |
|
140 |
* {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE} |
|
141 |
*/ |
|
142 |
AssertionData(QName name, String value, Map<QName, String> attributes, ModelNode.Type type, boolean optional, boolean ignorable) throws IllegalArgumentException { |
|
143 |
this.name = name; |
|
144 |
this.value = value; |
|
145 |
this.optional = optional; |
|
146 |
this.ignorable = ignorable; |
|
147 |
||
148 |
this.attributes = new HashMap<QName, String>(); |
|
149 |
if (attributes != null && !attributes.isEmpty()) { |
|
150 |
this.attributes.putAll(attributes); |
|
151 |
} |
|
152 |
setModelNodeType(type); |
|
153 |
} |
|
154 |
||
155 |
private void setModelNodeType(final ModelNode.Type type) throws IllegalArgumentException { |
|
156 |
if (type == ModelNode.Type.ASSERTION || type == ModelNode.Type.ASSERTION_PARAMETER_NODE) { |
|
157 |
this.type = type; |
|
158 |
} else { |
|
159 |
throw LOGGER.logSevereException(new IllegalArgumentException( |
|
160 |
LocalizationMessages.WSP_0074_CANNOT_CREATE_ASSERTION_BAD_TYPE(type, ModelNode.Type.ASSERTION, ModelNode.Type.ASSERTION_PARAMETER_NODE))); |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Copy constructor. |
|
166 |
* |
|
167 |
* @param data The instance that is to be copied. |
|
168 |
*/ |
|
169 |
AssertionData(final AssertionData data) { |
|
170 |
this.name = data.name; |
|
171 |
this.value = data.value; |
|
172 |
this.attributes = new HashMap<QName, String>(); |
|
173 |
if (!data.attributes.isEmpty()) { |
|
174 |
this.attributes.putAll(data.attributes); |
|
175 |
} |
|
176 |
this.type = data.type; |
|
177 |
} |
|
178 |
||
179 |
@Override |
|
180 |
protected AssertionData clone() throws CloneNotSupportedException { |
|
181 |
return (AssertionData) super.clone(); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Returns true if the given attribute exists, false otherwise. |
|
186 |
* |
|
187 |
* @param name The name of the attribute. Must not be null. |
|
188 |
* @return True if the given attribute exists, false otherwise. |
|
189 |
*/ |
|
190 |
public boolean containsAttribute(final QName name) { |
|
191 |
synchronized (attributes) { |
|
192 |
return attributes.containsKey(name); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
||
197 |
@Override |
|
198 |
public boolean equals(final Object obj) { |
|
199 |
if (this == obj) { |
|
200 |
return true; |
|
201 |
} |
|
202 |
||
203 |
if (!(obj instanceof AssertionData)) { |
|
204 |
return false; |
|
205 |
} |
|
206 |
||
207 |
boolean result = true; |
|
208 |
final AssertionData that = (AssertionData) obj; |
|
209 |
||
210 |
result = result && this.name.equals(that.name); |
|
211 |
result = result && ((this.value == null) ? that.value == null : this.value.equals(that.value)); |
|
212 |
synchronized (attributes) { |
|
213 |
result = result && this.attributes.equals(that.attributes); |
|
214 |
} |
|
215 |
||
216 |
return result; |
|
217 |
} |
|
218 |
||
219 |
||
220 |
/** |
|
221 |
* Returns the value of the given attribute. Returns null if the attribute |
|
222 |
* does not exist. |
|
223 |
* |
|
224 |
* @param name The name of the attribute. Must not be null. |
|
225 |
* @return The value of the given attribute. Returns null if the attribute |
|
226 |
* does not exist. |
|
227 |
*/ |
|
228 |
public String getAttributeValue(final QName name) { |
|
229 |
synchronized (attributes) { |
|
230 |
return attributes.get(name); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
||
235 |
/** |
|
236 |
* Returns the disconnected map of attributes attached to the assertion. |
|
237 |
* <p/> |
|
238 |
* 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is |
|
239 |
* also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's |
|
240 |
* attributes. |
|
241 |
* |
|
242 |
* @return disconnected map of attributes attached to the assertion. |
|
243 |
*/ |
|
244 |
public Map<QName, String> getAttributes() { |
|
245 |
synchronized (attributes) { |
|
246 |
return new HashMap<QName, String>(attributes); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
||
251 |
/** |
|
252 |
* Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single |
|
253 |
* {@code Map.Entry<attributeName, attributeValue>} element. |
|
254 |
* <p/> |
|
255 |
* 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is |
|
256 |
* also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's |
|
257 |
* attributes. |
|
258 |
* |
|
259 |
* @return disconnected set of attributes attached to the assertion. |
|
260 |
*/ |
|
261 |
public Set<Map.Entry<QName, String>> getAttributesSet() { |
|
262 |
synchronized (attributes) { |
|
263 |
return new HashSet<Map.Entry<QName, String>>(attributes.entrySet()); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
||
268 |
/** |
|
269 |
* Returns the name of the assertion. |
|
270 |
* |
|
271 |
* @return assertion's name |
|
272 |
*/ |
|
273 |
public QName getName() { |
|
274 |
return name; |
|
275 |
} |
|
276 |
||
277 |
||
278 |
/** |
|
279 |
* Returns the value of the assertion. |
|
280 |
* |
|
281 |
* @return assertion's value |
|
282 |
*/ |
|
283 |
public String getValue() { |
|
284 |
return value; |
|
285 |
} |
|
286 |
||
287 |
||
288 |
/** |
|
289 |
* An {@code Object.hashCode()} method override. |
|
290 |
*/ |
|
291 |
@Override |
|
292 |
public int hashCode() { |
|
293 |
int result = 17; |
|
294 |
||
295 |
result = 37 * result + this.name.hashCode(); |
|
296 |
result = 37 * result + ((this.value == null) ? 0 : this.value.hashCode()); |
|
297 |
synchronized (attributes) { |
|
298 |
result = 37 * result + this.attributes.hashCode(); |
|
299 |
} |
|
300 |
return result; |
|
301 |
} |
|
302 |
||
303 |
||
304 |
/** |
|
305 |
* Method specifies whether the assertion data contain proprietary visibility element set to "private" value. |
|
306 |
* |
|
307 |
* @return {@code 'true'} if the attribute is present and set properly (i.e. the node containing this assertion data instance should |
|
308 |
* not be marshaled into generated WSDL documents). Returns {@code false} otherwise. |
|
309 |
*/ |
|
310 |
public boolean isPrivateAttributeSet() { |
|
311 |
return PolicyConstants.VISIBILITY_VALUE_PRIVATE.equals(getAttributeValue(PolicyConstants.VISIBILITY_ATTRIBUTE)); |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Removes the given attribute from the assertion data. |
|
316 |
* |
|
317 |
* @param name The name of the attribute. Must not be null |
|
318 |
* @return The value of the removed attribute. |
|
319 |
*/ |
|
320 |
public String removeAttribute(final QName name) { |
|
321 |
synchronized (attributes) { |
|
322 |
return attributes.remove(name); |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Adds or overwrites an attribute. |
|
328 |
* |
|
329 |
* @param name The name of the attribute. |
|
330 |
* @param value The value of the attribute. |
|
331 |
*/ |
|
332 |
public void setAttribute(final QName name, final String value) { |
|
333 |
synchronized (attributes) { |
|
334 |
attributes.put(name, value); |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Sets the optional attribute. |
|
340 |
* |
|
341 |
* @param value The value of the optional attribute. |
|
342 |
*/ |
|
343 |
public void setOptionalAttribute(final boolean value) { |
|
344 |
optional = value; |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Tests if the optional attribute is set. |
|
349 |
* |
|
350 |
* @return True if optional is set and is true. False otherwise. |
|
351 |
*/ |
|
352 |
public boolean isOptionalAttributeSet() { |
|
353 |
return optional; |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Sets the ignorable attribute. |
|
358 |
* |
|
359 |
* @param value The value of the ignorable attribute. |
|
360 |
*/ |
|
361 |
public void setIgnorableAttribute(final boolean value) { |
|
362 |
ignorable = value; |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Tests if the ignorable attribute is set. |
|
367 |
* |
|
368 |
* @return True if ignorable is set and is true. False otherwise. |
|
369 |
*/ |
|
370 |
public boolean isIgnorableAttributeSet() { |
|
371 |
return ignorable; |
|
372 |
} |
|
373 |
||
374 |
@Override |
|
375 |
public String toString() { |
|
376 |
return toString(0, new StringBuffer()).toString(); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* A helper method that appends indented string representation of this instance to the input string buffer. |
|
381 |
* |
|
382 |
* @param indentLevel indentation level to be used. |
|
383 |
* @param buffer buffer to be used for appending string representation of this instance |
|
384 |
* @return modified buffer containing new string representation of the instance |
|
385 |
*/ |
|
386 |
public StringBuffer toString(final int indentLevel, final StringBuffer buffer) { |
|
387 |
final String indent = PolicyUtils.Text.createIndent(indentLevel); |
|
388 |
final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1); |
|
389 |
final String innerDoubleIndent = PolicyUtils.Text.createIndent(indentLevel + 2); |
|
390 |
||
391 |
buffer.append(indent); |
|
392 |
if (type == ModelNode.Type.ASSERTION) { |
|
393 |
buffer.append("assertion data {"); |
|
394 |
} else { |
|
395 |
buffer.append("assertion parameter data {"); |
|
396 |
} |
|
397 |
buffer.append(PolicyUtils.Text.NEW_LINE); |
|
398 |
||
399 |
buffer.append(innerIndent).append("namespace = '").append(name.getNamespaceURI()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
400 |
buffer.append(innerIndent).append("prefix = '").append(name.getPrefix()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
401 |
buffer.append(innerIndent).append("local name = '").append(name.getLocalPart()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
402 |
buffer.append(innerIndent).append("value = '").append(value).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
403 |
buffer.append(innerIndent).append("optional = '").append(optional).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
404 |
buffer.append(innerIndent).append("ignorable = '").append(ignorable).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
405 |
synchronized (attributes) { |
|
406 |
if (attributes.isEmpty()) { |
|
407 |
buffer.append(innerIndent).append("no attributes"); |
|
408 |
} else { |
|
409 |
||
410 |
buffer.append(innerIndent).append("attributes {").append(PolicyUtils.Text.NEW_LINE); |
|
411 |
for(Map.Entry<QName, String> entry : attributes.entrySet()) { |
|
412 |
final QName aName = entry.getKey(); |
|
413 |
buffer.append(innerDoubleIndent).append("name = '").append(aName.getNamespaceURI()).append(':').append(aName.getLocalPart()); |
|
414 |
buffer.append("', value = '").append(entry.getValue()).append('\'').append(PolicyUtils.Text.NEW_LINE); |
|
415 |
} |
|
416 |
buffer.append(innerIndent).append('}'); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
buffer.append(PolicyUtils.Text.NEW_LINE).append(indent).append('}'); |
|
421 |
||
422 |
return buffer; |
|
423 |
} |
|
424 |
||
425 |
public ModelNode.Type getNodeType() { |
|
426 |
return type; |
|
427 |
} |
|
428 |
||
429 |
} |