001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy;
038:
039: import com.sun.xml.ws.policy.privateutil.LocalizationMessages;
040: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
041: import com.sun.xml.ws.policy.privateutil.PolicyUtils;
042: import java.util.Collection;
043: import java.util.LinkedList;
044: import java.util.List;
045:
046: /**
047: * A policy scope is a collection of equally ranked elements or subjects that
048: * hold policies
049: */
050: final class PolicyScope {
051: private static final PolicyLogger LOGGER = PolicyLogger
052: .getLogger(PolicyScope.class);
053:
054: private final List<PolicySubject> subjects = new LinkedList<PolicySubject>();
055:
056: PolicyScope(final List<PolicySubject> initialSubjects) {
057: if (initialSubjects != null && !initialSubjects.isEmpty()) {
058: this .subjects.addAll(initialSubjects);
059: }
060: }
061:
062: void attach(final PolicySubject subject) {
063: if (subject == null) {
064: throw LOGGER
065: .logSevereException(new IllegalArgumentException(
066: LocalizationMessages
067: .WSP_0020_SUBJECT_PARAM_MUST_NOT_BE_NULL()));
068: }
069:
070: subjects.add(subject);
071: }
072:
073: void dettachAllSubjects() {
074: subjects.clear();
075: }
076:
077: /**
078: * Returns all policies of the scope merged into one policy
079: *
080: * @return effective policy of the scope
081: */
082: Policy getEffectivePolicy(final PolicyMerger merger)
083: throws PolicyException {
084: final LinkedList<Policy> policies = new LinkedList<Policy>();
085: for (PolicySubject subject : subjects) {
086: policies.add(subject.getEffectivePolicy(merger));
087: }
088: return merger.merge(policies);
089: }
090:
091: /**
092: * Returns policies of the scope merged into one policy.
093: *<p/>
094: * Only policies with vocabulary containing the namespaces provided are merged into effective policy.
095: *
096: * @return effective policy with respect to the provided namespaces.
097: */
098: Policy getEffectivePolicy(final Collection<String> namespaces,
099: final PolicyMerger merger) throws PolicyException {
100: final LinkedList<Policy> policies = new LinkedList<Policy>();
101: for (PolicySubject subject : subjects) {
102: policies
103: .add(subject.getEffectivePolicy(namespaces, merger));
104: }
105: return merger.merge(policies);
106: }
107:
108: /**
109: * Returns all subjects contained by this scope
110: *
111: * @return The subjects contained by this scope
112: */
113: Collection<PolicySubject> getPolicySubjects() {
114: return this .subjects;
115: }
116:
117: /**
118: * An {@code Object.toString()} method override.
119: */
120: public String toString() {
121: return toString(0, new StringBuffer()).toString();
122: }
123:
124: /**
125: * A helper method that appends indented string representation of this instance to the input string buffer.
126: *
127: * @param indentLevel indentation level to be used.
128: * @param buffer buffer to be used for appending string representation of this instance
129: * @return modified buffer containing new string representation of the instance
130: */
131: StringBuffer toString(final int indentLevel,
132: final StringBuffer buffer) {
133: final String indent = PolicyUtils.Text
134: .createIndent(indentLevel);
135:
136: buffer.append(indent).append("policy scope {").append(
137: PolicyUtils.Text.NEW_LINE);
138: for (PolicySubject policySubject : subjects) {
139: policySubject.toString(indentLevel + 1, buffer).append(
140: PolicyUtils.Text.NEW_LINE);
141: }
142: buffer.append(indent).append('}');
143:
144: return buffer;
145: }
146: }
|