|
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 com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; |
|
29 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; |
|
30 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; |
|
31 import java.util.Collection; |
|
32 import java.util.LinkedList; |
|
33 import java.util.List; |
|
34 |
|
35 /** |
|
36 * A policy scope is a collection of equally ranked elements or subjects that |
|
37 * hold policies |
|
38 * |
|
39 * @author Fabian Ritzmann |
|
40 */ |
|
41 final class PolicyScope { |
|
42 private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyScope.class); |
|
43 |
|
44 private final List<PolicySubject> subjects = new LinkedList<PolicySubject>(); |
|
45 |
|
46 PolicyScope(final List<PolicySubject> initialSubjects) { |
|
47 if (initialSubjects != null && !initialSubjects.isEmpty()) { |
|
48 this.subjects.addAll(initialSubjects); |
|
49 } |
|
50 } |
|
51 |
|
52 void attach(final PolicySubject subject) { |
|
53 if (subject == null) { |
|
54 throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0020_SUBJECT_PARAM_MUST_NOT_BE_NULL())); |
|
55 } |
|
56 |
|
57 subjects.add(subject); |
|
58 } |
|
59 |
|
60 void dettachAllSubjects() { |
|
61 subjects.clear(); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Returns all policies of the scope merged into one policy |
|
66 * |
|
67 * @return effective policy of the scope |
|
68 */ |
|
69 Policy getEffectivePolicy(final PolicyMerger merger) throws PolicyException { |
|
70 final LinkedList<Policy> policies = new LinkedList<Policy>(); |
|
71 for (PolicySubject subject : subjects) { |
|
72 policies.add(subject.getEffectivePolicy(merger)); |
|
73 } |
|
74 return merger.merge(policies); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Returns all subjects contained by this scope |
|
79 * |
|
80 * @return The subjects contained by this scope |
|
81 */ |
|
82 Collection<PolicySubject> getPolicySubjects() { |
|
83 return this.subjects; |
|
84 } |
|
85 |
|
86 |
|
87 /** |
|
88 * An {@code Object.toString()} method override. |
|
89 */ |
|
90 @Override |
|
91 public String toString() { |
|
92 return toString(0, new StringBuffer()).toString(); |
|
93 } |
|
94 |
|
95 /** |
|
96 * A helper method that appends indented string representation of this instance to the input string buffer. |
|
97 * |
|
98 * @param indentLevel indentation level to be used. |
|
99 * @param buffer buffer to be used for appending string representation of this instance |
|
100 * @return modified buffer containing new string representation of the instance |
|
101 */ |
|
102 StringBuffer toString(final int indentLevel, final StringBuffer buffer) { |
|
103 final String indent = PolicyUtils.Text.createIndent(indentLevel); |
|
104 |
|
105 buffer.append(indent).append("policy scope {").append(PolicyUtils.Text.NEW_LINE); |
|
106 for (PolicySubject policySubject : subjects) { |
|
107 policySubject.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE); |
|
108 } |
|
109 buffer.append(indent).append('}'); |
|
110 |
|
111 return buffer; |
|
112 } |
|
113 } |