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.List;
023: import java.util.ResourceBundle;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
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.common.logging.LogUtils;
032: import org.apache.cxf.interceptor.Interceptor;
033: import org.apache.cxf.service.model.BindingFaultInfo;
034: import org.apache.cxf.service.model.BindingMessageInfo;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.EndpointInfo;
037: import org.apache.cxf.transport.Conduit;
038: import org.apache.cxf.transport.Destination;
039: import org.apache.neethi.Assertion;
040: import org.apache.neethi.Policy;
041:
042: /**
043: *
044: */
045: public class EffectivePolicyImpl implements EffectivePolicy {
046:
047: private static final ResourceBundle BUNDLE = BundleUtils
048: .getBundle(EffectivePolicyImpl.class);
049: private static final Logger LOG = LogUtils
050: .getL7dLogger(EffectivePolicyImpl.class);
051:
052: protected Policy policy;
053: protected Collection<Assertion> chosenAlternative;
054: protected List<Interceptor> interceptors;
055:
056: public Policy getPolicy() {
057: return policy;
058: }
059:
060: public List<Interceptor> getInterceptors() {
061: return interceptors;
062: }
063:
064: public Collection<Assertion> getChosenAlternative() {
065: return chosenAlternative;
066: }
067:
068: void initialise(EndpointPolicyImpl epi, PolicyEngineImpl engine) {
069: policy = epi.getPolicy();
070: chosenAlternative = epi.getChosenAlternative();
071: initialiseInterceptors(engine);
072: }
073:
074: void initialise(EndpointInfo ei, BindingOperationInfo boi,
075: PolicyEngineImpl engine, Assertor assertor,
076: boolean requestor) {
077: initialisePolicy(ei, boi, engine, requestor);
078: chooseAlternative(engine, assertor);
079: initialiseInterceptors(engine);
080: }
081:
082: void initialise(EndpointInfo ei, BindingFaultInfo bfi,
083: PolicyEngineImpl engine, Assertor assertor) {
084: initialisePolicy(ei, bfi, engine);
085: chooseAlternative(engine, assertor);
086: initialiseInterceptors(engine);
087: }
088:
089: void initialisePolicy(EndpointInfo ei, BindingOperationInfo boi,
090: PolicyEngineImpl engine, boolean requestor) {
091: BindingMessageInfo bmi = requestor ? boi.getInput() : boi
092: .getOutput();
093: if (requestor) {
094: policy = engine.getClientEndpointPolicy(ei, (Conduit) null)
095: .getPolicy();
096: } else {
097: policy = engine.getServerEndpointPolicy(ei,
098: (Destination) null).getPolicy();
099: }
100:
101: policy = policy.merge(engine.getAggregatedOperationPolicy(boi));
102: if (null != bmi) {
103: policy = policy.merge(engine
104: .getAggregatedMessagePolicy(bmi));
105: }
106: policy = (Policy) policy.normalize(true);
107: }
108:
109: void initialisePolicy(EndpointInfo ei, BindingFaultInfo bfi,
110: PolicyEngineImpl engine) {
111: BindingOperationInfo boi = bfi.getBindingOperation();
112: policy = engine.getServerEndpointPolicy(ei, (Destination) null)
113: .getPolicy();
114: policy = policy.merge(engine.getAggregatedOperationPolicy(boi));
115: policy = policy.merge(engine.getAggregatedFaultPolicy(bfi));
116: policy = (Policy) policy.normalize(true);
117: }
118:
119: void chooseAlternative(PolicyEngineImpl engine, Assertor assertor) {
120: Collection<Assertion> alternative = engine
121: .getAlternativeSelector().selectAlternative(policy,
122: engine, assertor);
123: if (null == alternative) {
124: PolicyUtils.logPolicy(LOG, Level.FINE,
125: "No alternative supported.", getPolicy());
126: throw new PolicyException(new Message("NO_ALTERNATIVE_EXC",
127: BUNDLE));
128: } else {
129: setChosenAlternative(alternative);
130: }
131: }
132:
133: void initialiseInterceptors(PolicyEngineImpl engine) {
134: PolicyInterceptorProviderRegistry reg = engine.getBus()
135: .getExtension(PolicyInterceptorProviderRegistry.class);
136: List<Interceptor> out = new ArrayList<Interceptor>();
137: for (Assertion a : getChosenAlternative()) {
138: if (a.isOptional()) {
139: continue;
140: }
141: QName qn = a.getName();
142: PolicyInterceptorProvider pp = reg.get(qn);
143: if (null != pp) {
144: out.addAll(pp.getOutInterceptors());
145: }
146: }
147: setInterceptors(out);
148: }
149:
150: // for tests
151:
152: void setPolicy(Policy ep) {
153: policy = ep;
154: }
155:
156: void setChosenAlternative(Collection<Assertion> c) {
157: chosenAlternative = c;
158: }
159:
160: void setInterceptors(List<Interceptor> out) {
161: interceptors = out;
162: }
163:
164: }
|