|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
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; |
|
27 |
|
28 import java.util.Collection; |
|
29 import java.util.Iterator; |
|
30 import java.util.Map; |
|
31 import java.util.Set; |
|
32 import javax.xml.namespace.QName; |
|
33 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; |
|
34 import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData; |
|
35 import com.sun.xml.internal.ws.policy.sourcemodel.ModelNode; |
|
36 |
|
37 /** |
|
38 * Base class for any policy assertion implementations. It defines the common |
|
39 * interface and provides some default implentation for common policy assertion |
|
40 * functionality. |
|
41 * <p/> |
|
42 * NOTE: Assertion implementers should not extend this class directly. {@link SimpleAssertion} |
|
43 * or {@link ComplexAssertion} should be used as a base class instead. |
|
44 * |
|
45 * @author Marek Potociar (marek.potociar at sun.com) |
|
46 * @author Fabian Ritzmann |
|
47 */ |
|
48 public abstract class PolicyAssertion { |
|
49 |
|
50 private final AssertionData data; |
|
51 private AssertionSet parameters; |
|
52 private NestedPolicy nestedPolicy; // TODO: remove |
|
53 |
|
54 protected PolicyAssertion() { |
|
55 this.data = AssertionData.createAssertionData(null); |
|
56 this.parameters = AssertionSet.createAssertionSet(null); |
|
57 } |
|
58 |
|
59 /** |
|
60 * Creates generic assertionand stores the data specified in input parameters |
|
61 * |
|
62 * @param assertionData assertion creation data specifying the details of newly created assertion. May be {@code null}. |
|
63 * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}. |
|
64 * @param nestedAlternative assertion set specifying nested policy alternative. May be {@code null}. |
|
65 * |
|
66 * @deprecated Non-abstract assertion types should derive from {@link SimpleAssertion} |
|
67 * or {@link ComplexAssertion} instead. {@link Policy} class will not provide support for |
|
68 * nested policy alternatives in the future. This responsibility is delegated to |
|
69 * {@link ComplexAssertion} class instead. |
|
70 */ |
|
71 @Deprecated |
|
72 protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters, final AssertionSet nestedAlternative) { |
|
73 this.data = assertionData; |
|
74 if (nestedAlternative != null) { |
|
75 this.nestedPolicy = NestedPolicy.createNestedPolicy(nestedAlternative); |
|
76 } |
|
77 |
|
78 this.parameters = AssertionSet.createAssertionSet(assertionParameters); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Creates generic assertionand stores the data specified in input parameters |
|
83 * |
|
84 * @param assertionData assertion creation data specifying the details of newly created assertion |
|
85 * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}. |
|
86 */ |
|
87 protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters) { |
|
88 if (assertionData == null) { |
|
89 this.data = AssertionData.createAssertionData(null); |
|
90 } else { |
|
91 this.data = assertionData; |
|
92 } |
|
93 this.parameters = AssertionSet.createAssertionSet(assertionParameters); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Returns the fully qualified name of the assertion. |
|
98 * |
|
99 * @return assertion's fully qualified name. |
|
100 */ |
|
101 public final QName getName() { |
|
102 return data.getName(); |
|
103 } |
|
104 |
|
105 /** |
|
106 * Returns the value of the assertion - the character data content contained in the assertion element representation. |
|
107 * |
|
108 * @return assertion's value. May return {@code null} if there is no value set for the assertion. |
|
109 */ |
|
110 public final String getValue() { |
|
111 return data.getValue(); |
|
112 } |
|
113 |
|
114 /** |
|
115 * Method specifies whether the assertion is otpional or not. |
|
116 * <p/> |
|
117 * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:optional} attribute |
|
118 * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}. |
|
119 * |
|
120 * @return {@code 'true'} if the assertion is optional. Returns {@code false} otherwise. |
|
121 */ |
|
122 public boolean isOptional() { |
|
123 return data.isOptionalAttributeSet(); |
|
124 } |
|
125 |
|
126 /** |
|
127 * Method specifies whether the assertion is ignorable or not. |
|
128 * <p/> |
|
129 * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:Ignorable} attribute |
|
130 * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}. |
|
131 * |
|
132 * @return {@code 'true'} if the assertion is ignorable. Returns {@code false} otherwise. |
|
133 */ |
|
134 public boolean isIgnorable() { |
|
135 return data.isIgnorableAttributeSet(); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Method specifies whether the assertion is private or not. This is specified by our proprietary visibility element. |
|
140 * |
|
141 * @return {@code 'true'} if the assertion is marked as private (i.e. should not be marshalled int generated WSDL documents). Returns {@code false} otherwise. |
|
142 */ |
|
143 public final boolean isPrivate() { |
|
144 return data.isPrivateAttributeSet(); |
|
145 } |
|
146 |
|
147 /** |
|
148 * Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single |
|
149 * {@code Map.Entry<attributeName, attributeValue>} element. |
|
150 * <p/> |
|
151 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is |
|
152 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's |
|
153 * attributes. |
|
154 * |
|
155 * @return disconected set of attributes attached to the assertion. |
|
156 */ |
|
157 public final Set<Map.Entry<QName, String>> getAttributesSet() { |
|
158 return data.getAttributesSet(); |
|
159 } |
|
160 |
|
161 /** |
|
162 * Returns the disconnected map of attributes attached to the assertion. |
|
163 * <p/> |
|
164 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is |
|
165 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's |
|
166 * attributes. |
|
167 * |
|
168 * @return disconnected map of attributes attached to the assertion. |
|
169 */ |
|
170 public final Map<QName, String> getAttributes() { |
|
171 return data.getAttributes(); |
|
172 } |
|
173 |
|
174 /** |
|
175 * Returns the value of an attribute. Returns null if an attribute with the given name does not exist. |
|
176 * |
|
177 * @param name The fully qualified name of the attribute |
|
178 * @return The value of the attribute. Returns {@code null} if there is no such attribute or if it's value is null. |
|
179 */ |
|
180 public final String getAttributeValue(final QName name) { |
|
181 return data.getAttributeValue(name); |
|
182 } |
|
183 |
|
184 /** |
|
185 * Returns the boolean information whether this assertion contains any parameters. |
|
186 * |
|
187 * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise. |
|
188 * |
|
189 * @deprecated Use hasParameters() instead |
|
190 */ |
|
191 @Deprecated |
|
192 public final boolean hasNestedAssertions() { |
|
193 // TODO: remove |
|
194 return !parameters.isEmpty(); |
|
195 } |
|
196 |
|
197 /** |
|
198 * Returns the boolean information whether this assertion contains any parameters. |
|
199 * |
|
200 * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise. |
|
201 */ |
|
202 public final boolean hasParameters() { |
|
203 return !parameters.isEmpty(); |
|
204 } |
|
205 |
|
206 /** |
|
207 * Returns the assertion's parameter collection iterator. |
|
208 * |
|
209 * @return the assertion's parameter collection iterator. |
|
210 * |
|
211 * @deprecated Use getNestedParametersIterator() instead |
|
212 */ |
|
213 @Deprecated |
|
214 public final Iterator<PolicyAssertion> getNestedAssertionsIterator() { |
|
215 // TODO: remove |
|
216 return parameters.iterator(); |
|
217 } |
|
218 |
|
219 /** |
|
220 * Returns the assertion's parameter collection iterator. |
|
221 * |
|
222 * @return the assertion's parameter collection iterator. |
|
223 */ |
|
224 public final Iterator<PolicyAssertion> getParametersIterator() { |
|
225 return parameters.iterator(); |
|
226 } |
|
227 |
|
228 boolean isParameter() { |
|
229 return data.getNodeType() == ModelNode.Type.ASSERTION_PARAMETER_NODE; |
|
230 } |
|
231 |
|
232 /** |
|
233 * Returns the boolean information whether this assertion contains nested policy. |
|
234 * |
|
235 * @return {@code true} if the assertion contains child (nested) policy. Returns {@code false} otherwise. |
|
236 */ |
|
237 public boolean hasNestedPolicy() { |
|
238 // TODO: make abstract |
|
239 return getNestedPolicy() != null; |
|
240 } |
|
241 |
|
242 /** |
|
243 * Returns the nested policy if any. |
|
244 * |
|
245 * @return the nested policy if the assertion contains a nested policy. Returns {@code null} otherwise. |
|
246 */ |
|
247 public NestedPolicy getNestedPolicy() { |
|
248 // TODO: make abstract |
|
249 return nestedPolicy; |
|
250 } |
|
251 |
|
252 /** |
|
253 * Casts the assertion to the implementation type. Returns null if that is not |
|
254 * possible. |
|
255 * |
|
256 * @param <T> The implementation type of the assertion. |
|
257 * @param type The implementation type of the assertion. May not be null. |
|
258 * @return The instance of the implementation type. Null otherwise. |
|
259 */ |
|
260 public <T extends PolicyAssertion> T getImplementation(Class<T> type) { |
|
261 if (type.isAssignableFrom(this.getClass())) { |
|
262 return type.cast(this); |
|
263 } |
|
264 else { |
|
265 return null; |
|
266 } |
|
267 } |
|
268 |
|
269 /** |
|
270 * An {@code Object.toString()} method override. |
|
271 */ |
|
272 @Override |
|
273 public String toString() { |
|
274 return toString(0, new StringBuffer()).toString(); |
|
275 } |
|
276 |
|
277 /** |
|
278 * A helper method that appends indented string representation of this instance to the input string buffer. |
|
279 * |
|
280 * @param indentLevel indentation level to be used. |
|
281 * @param buffer buffer to be used for appending string representation of this instance |
|
282 * @return modified buffer containing new string representation of the instance |
|
283 */ |
|
284 protected StringBuffer toString(final int indentLevel, final StringBuffer buffer) { |
|
285 final String indent = PolicyUtils.Text.createIndent(indentLevel); |
|
286 final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1); |
|
287 |
|
288 buffer.append(indent).append("Assertion[").append(this.getClass().getName()).append("] {").append(PolicyUtils.Text.NEW_LINE); |
|
289 data.toString(indentLevel + 1, buffer); |
|
290 buffer.append(PolicyUtils.Text.NEW_LINE); |
|
291 |
|
292 if (hasParameters()) { |
|
293 buffer.append(innerIndent).append("parameters {").append(PolicyUtils.Text.NEW_LINE); |
|
294 for (PolicyAssertion parameter : parameters) { |
|
295 parameter.toString(indentLevel + 2, buffer).append(PolicyUtils.Text.NEW_LINE); |
|
296 } |
|
297 buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE); |
|
298 } else { |
|
299 buffer.append(innerIndent).append("no parameters").append(PolicyUtils.Text.NEW_LINE); |
|
300 } |
|
301 |
|
302 if (hasNestedPolicy()) { |
|
303 getNestedPolicy().toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE); |
|
304 } else { |
|
305 buffer.append(innerIndent).append("no nested policy").append(PolicyUtils.Text.NEW_LINE); |
|
306 } |
|
307 |
|
308 buffer.append(indent).append('}'); |
|
309 |
|
310 return buffer; |
|
311 } |
|
312 |
|
313 /** |
|
314 * Checks whether this policy alternative is compatible with the provided policy alternative. |
|
315 * |
|
316 * @param assertion policy alternative used for compatibility test |
|
317 * @param mode compatibility mode to be used |
|
318 * @return {@code true} if the two policy alternatives are compatible, {@code false} otherwise |
|
319 */ |
|
320 boolean isCompatibleWith(final PolicyAssertion assertion, PolicyIntersector.CompatibilityMode mode) { |
|
321 boolean result = this.data.getName().equals(assertion.data.getName()) && (this.hasNestedPolicy() == assertion.hasNestedPolicy()); |
|
322 |
|
323 if (result && this.hasNestedPolicy()) { |
|
324 result = this.getNestedPolicy().getAssertionSet().isCompatibleWith(assertion.getNestedPolicy().getAssertionSet(), mode); |
|
325 } |
|
326 |
|
327 return result; |
|
328 } |
|
329 |
|
330 /** |
|
331 * An {@code Object.equals(Object obj)} method override. |
|
332 */ |
|
333 @Override |
|
334 public boolean equals(final Object obj) { |
|
335 if (this == obj) { |
|
336 return true; |
|
337 } |
|
338 |
|
339 if (!(obj instanceof PolicyAssertion)) { |
|
340 return false; |
|
341 } |
|
342 |
|
343 final PolicyAssertion that = (PolicyAssertion) obj; |
|
344 boolean result = true; |
|
345 |
|
346 result = result && this.data.equals(that.data); |
|
347 result = result && this.parameters.equals(that.parameters); |
|
348 result = result && ((this.getNestedPolicy() == null) ? ((that.getNestedPolicy() == null) ? true : false) : this.getNestedPolicy().equals(that.getNestedPolicy())); |
|
349 |
|
350 return result; |
|
351 } |
|
352 |
|
353 /** |
|
354 * An {@code Object.hashCode()} method override. |
|
355 */ |
|
356 @Override |
|
357 public int hashCode() { |
|
358 int result = 17; |
|
359 |
|
360 result = 37 * result + data.hashCode(); |
|
361 result = 37 * result + ((hasParameters()) ? 17 : 0); |
|
362 result = 37 * result + ((hasNestedPolicy()) ? 17 : 0); |
|
363 |
|
364 return result; |
|
365 } |
|
366 } |