001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.policy;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.ResourceBundle;
025: import java.util.Set;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.cxf.common.i18n.BundleUtils;
030: import org.apache.cxf.common.i18n.Message;
031: import org.apache.cxf.interceptor.Interceptor;
032: import org.apache.cxf.service.model.BindingFaultInfo;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.apache.cxf.service.model.EndpointInfo;
035: import org.apache.neethi.Assertion;
036: import org.apache.neethi.Policy;
037:
038: /**
039: *
040: */
041: public class EndpointPolicyImpl implements EndpointPolicy {
042:
043: private static final ResourceBundle BUNDLE = BundleUtils
044: .getBundle(EndpointPolicyImpl.class);
045:
046: private Policy policy;
047: private Collection<Assertion> chosenAlternative;
048: private Collection<Assertion> vocabulary;
049: private Collection<Assertion> faultVocabulary;
050: private List<Interceptor> interceptors;
051: private List<Interceptor> faultInterceptors;
052:
053: public Policy getPolicy() {
054: return policy;
055: }
056:
057: public Collection<Assertion> getChosenAlternative() {
058: return chosenAlternative;
059: }
060:
061: public Collection<Assertion> getVocabulary() {
062: return vocabulary;
063: }
064:
065: public Collection<Assertion> getFaultVocabulary() {
066: return faultVocabulary;
067: }
068:
069: public List<Interceptor> getInterceptors() {
070: return interceptors;
071: }
072:
073: public List<Interceptor> getFaultInterceptors() {
074: return faultInterceptors;
075: }
076:
077: void initialise(EndpointInfo ei, boolean isRequestor,
078: PolicyEngineImpl engine, Assertor assertor) {
079: initialisePolicy(ei, engine);
080: chooseAlternative(engine, assertor);
081: initialiseVocabulary(ei, isRequestor, engine);
082: initialiseInterceptors(ei, isRequestor, engine);
083: }
084:
085: void initialisePolicy(EndpointInfo ei, PolicyEngineImpl engine) {
086: policy = engine.getAggregatedServicePolicy(ei.getService());
087: policy = policy.merge(engine.getAggregatedEndpointPolicy(ei));
088: policy = (Policy) policy.normalize(true);
089: }
090:
091: void chooseAlternative(PolicyEngineImpl engine, Assertor assertor) {
092: Collection<Assertion> alternative = engine
093: .getAlternativeSelector().selectAlternative(policy,
094: engine, assertor);
095: if (null == alternative) {
096: throw new PolicyException(new Message("NO_ALTERNATIVE_EXC",
097: BUNDLE));
098: } else {
099: setChosenAlternative(alternative);
100: }
101: }
102:
103: void initialiseVocabulary(EndpointInfo ei, boolean requestor,
104: PolicyEngineImpl engine) {
105: vocabulary = new ArrayList<Assertion>();
106: if (requestor) {
107: faultVocabulary = new ArrayList<Assertion>();
108: }
109:
110: // vocabulary of alternative chosen for endpoint
111:
112: for (Assertion a : getChosenAlternative()) {
113: if (a.isOptional()) {
114: continue;
115: }
116: vocabulary.add(a);
117: if (null != faultVocabulary) {
118: faultVocabulary.add(a);
119: }
120: }
121:
122: // add assertions for specific inbound (in case of a server endpoint) or outbound
123: // (in case of a client endpoint) messages
124:
125: for (BindingOperationInfo boi : ei.getBinding().getOperations()) {
126: Policy p = engine.getAggregatedOperationPolicy(boi);
127: Collection<Assertion> c = engine.getAssertions(p, false);
128: vocabulary.addAll(c);
129: if (null != faultVocabulary) {
130: faultVocabulary.addAll(c);
131: }
132:
133: if (!requestor) {
134: p = engine.getAggregatedMessagePolicy(boi.getInput());
135: vocabulary.addAll(engine.getAssertions(p, false));
136: } else if (null != boi.getOutput()) {
137: p = engine.getAggregatedMessagePolicy(boi.getOutput());
138: vocabulary.addAll(engine.getAssertions(p, false));
139:
140: for (BindingFaultInfo bfi : boi.getFaults()) {
141: p = engine.getAggregatedFaultPolicy(bfi);
142: faultVocabulary.addAll(engine.getAssertions(p,
143: false));
144: }
145: }
146: }
147: }
148:
149: void initialiseInterceptors(EndpointInfo ei, boolean requestor,
150: PolicyEngineImpl engine) {
151: PolicyInterceptorProviderRegistry reg = engine.getBus()
152: .getExtension(PolicyInterceptorProviderRegistry.class);
153: interceptors = new ArrayList<Interceptor>();
154: if (requestor) {
155: faultInterceptors = new ArrayList<Interceptor>();
156: }
157:
158: Set<QName> v = new HashSet<QName>();
159: for (Assertion a : vocabulary) {
160: v.add(a.getName());
161: }
162:
163: for (QName qn : v) {
164: PolicyInterceptorProvider pp = reg.get(qn);
165: if (null != pp) {
166: interceptors.addAll(pp.getInInterceptors());
167: }
168: }
169:
170: if (!requestor) {
171: return;
172: }
173:
174: Set<QName> faultV = new HashSet<QName>();
175: for (Assertion a : faultVocabulary) {
176: faultV.add(a.getName());
177: }
178:
179: for (QName qn : faultV) {
180: PolicyInterceptorProvider pp = reg.get(qn);
181: if (null != pp) {
182: faultInterceptors.addAll(pp.getInFaultInterceptors());
183: }
184: }
185: }
186:
187: // for test
188:
189: void setPolicy(Policy ep) {
190: policy = ep;
191: }
192:
193: void setChosenAlternative(Collection<Assertion> c) {
194: chosenAlternative = c;
195: }
196:
197: void setVocabulary(Collection<Assertion> v) {
198: vocabulary = v;
199: }
200:
201: void setFaultVocabulary(Collection<Assertion> v) {
202: faultVocabulary = v;
203: }
204:
205: void setInterceptors(List<Interceptor> in) {
206: interceptors = in;
207: }
208:
209: void setFaultInterceptors(List<Interceptor> inFault) {
210: faultInterceptors = inFault;
211: }
212:
213: }
|