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.rm.policy;
019:
020: import java.math.BigInteger;
021: import java.util.Collection;
022:
023: import org.apache.cxf.message.Message;
024: import org.apache.cxf.ws.policy.AssertionInfo;
025: import org.apache.cxf.ws.policy.AssertionInfoMap;
026: import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion;
027: import org.apache.cxf.ws.rm.RMConstants;
028: import org.apache.cxf.ws.rm.policy.RMAssertion.AcknowledgementInterval;
029: import org.apache.cxf.ws.rm.policy.RMAssertion.BaseRetransmissionInterval;
030: import org.apache.cxf.ws.rm.policy.RMAssertion.InactivityTimeout;
031:
032: /**
033: *
034: */
035: public final class PolicyUtils {
036:
037: /**
038: * Prevents instantiation.
039: *
040: */
041: private PolicyUtils() {
042: }
043:
044: /**
045: * Returns an RMAssertion that is compatible with the default value
046: * and all RMAssertions pertaining to the message (can never be null).
047: *
048: * @param rma the default value
049: * @param message the message
050: * @return the compatible RMAssertion
051: */
052: public static RMAssertion getRMAssertion(RMAssertion defaultValue,
053: Message message) {
054: RMAssertion compatible = defaultValue;
055: AssertionInfoMap amap = message.get(AssertionInfoMap.class);
056: if (null != amap) {
057: Collection<AssertionInfo> ais = amap.get(RMConstants
058: .getRMAssertionQName());
059: if (null != ais) {
060:
061: for (AssertionInfo ai : ais) {
062: JaxbAssertion<RMAssertion> ja = getAssertion(ai);
063: RMAssertion rma = ja.getData();
064: compatible = null == defaultValue ? rma
065: : intersect(compatible, rma);
066: }
067: }
068: }
069: return compatible;
070: }
071:
072: public static RMAssertion intersect(RMAssertion a, RMAssertion b) {
073: if (equals(a, b)) {
074: return a;
075: }
076: RMAssertion compatible = new RMAssertion();
077:
078: // use maximum of inactivity timeout
079:
080: BigInteger aval = null;
081: if (null != a.getInactivityTimeout()) {
082: aval = a.getInactivityTimeout().getMilliseconds();
083: }
084: BigInteger bval = null;
085: if (null != b.getInactivityTimeout()) {
086: bval = b.getInactivityTimeout().getMilliseconds();
087: }
088: if (null != aval || null != bval) {
089: InactivityTimeout ia = new RMAssertion.InactivityTimeout();
090: if (null != aval && null != bval) {
091: ia.setMilliseconds(aval.max(bval));
092: } else {
093: ia.setMilliseconds(aval != null ? aval : bval);
094: }
095: compatible.setInactivityTimeout(ia);
096: }
097:
098: // use minimum of base retransmission interval
099:
100: aval = null;
101: if (null != a.getBaseRetransmissionInterval()) {
102: aval = a.getBaseRetransmissionInterval().getMilliseconds();
103: }
104: bval = null;
105: if (null != b.getBaseRetransmissionInterval()) {
106: bval = b.getBaseRetransmissionInterval().getMilliseconds();
107: }
108: if (null != aval || null != bval) {
109: BaseRetransmissionInterval bri = new RMAssertion.BaseRetransmissionInterval();
110: if (null != aval && null != bval) {
111: bri.setMilliseconds(aval.min(bval));
112: } else {
113: bri.setMilliseconds(aval != null ? aval : bval);
114: }
115: compatible.setBaseRetransmissionInterval(bri);
116: }
117:
118: // use minimum of acknowledgement interval
119:
120: aval = null;
121: if (null != a.getAcknowledgementInterval()) {
122: aval = a.getAcknowledgementInterval().getMilliseconds();
123: }
124: bval = null;
125: if (null != b.getAcknowledgementInterval()) {
126: bval = b.getAcknowledgementInterval().getMilliseconds();
127: }
128: if (null != aval || null != bval) {
129: AcknowledgementInterval ai = new RMAssertion.AcknowledgementInterval();
130: if (null != aval && null != bval) {
131: ai.setMilliseconds(aval.min(bval));
132: } else {
133: ai.setMilliseconds(aval != null ? aval : bval);
134: }
135: compatible.setAcknowledgementInterval(ai);
136: }
137:
138: // backoff parameter
139: if (null != a.getExponentialBackoff()
140: || null != b.getExponentialBackoff()) {
141: compatible
142: .setExponentialBackoff(new RMAssertion.ExponentialBackoff());
143: }
144: return compatible;
145: }
146:
147: public static boolean equals(RMAssertion a, RMAssertion b) {
148: if (a == b) {
149: return true;
150: }
151:
152: BigInteger aval = null;
153: if (null != a.getInactivityTimeout()) {
154: aval = a.getInactivityTimeout().getMilliseconds();
155: }
156: BigInteger bval = null;
157: if (null != b.getInactivityTimeout()) {
158: bval = b.getInactivityTimeout().getMilliseconds();
159: }
160: if (!equals(aval, bval)) {
161: return false;
162: }
163:
164: aval = null;
165: if (null != a.getBaseRetransmissionInterval()) {
166: aval = a.getBaseRetransmissionInterval().getMilliseconds();
167: }
168: bval = null;
169: if (null != b.getBaseRetransmissionInterval()) {
170: bval = b.getBaseRetransmissionInterval().getMilliseconds();
171: }
172: if (!equals(aval, bval)) {
173: return false;
174: }
175:
176: aval = null;
177: if (null != a.getAcknowledgementInterval()) {
178: aval = a.getAcknowledgementInterval().getMilliseconds();
179: }
180: bval = null;
181: if (null != b.getAcknowledgementInterval()) {
182: bval = b.getAcknowledgementInterval().getMilliseconds();
183: }
184: if (!equals(aval, bval)) {
185: return false;
186: }
187:
188: return null == a.getExponentialBackoff() ? null == b
189: .getExponentialBackoff() : null != b
190: .getExponentialBackoff();
191: }
192:
193: private static boolean equals(BigInteger aval, BigInteger bval) {
194: if (null != aval) {
195: if (null != bval) {
196: if (!aval.equals(bval)) {
197: return false;
198: }
199: } else {
200: return false;
201: }
202: } else {
203: if (null != bval) {
204: return false;
205: }
206: return true;
207: }
208: return true;
209: }
210:
211: @SuppressWarnings("unchecked")
212: private static JaxbAssertion<RMAssertion> getAssertion(
213: AssertionInfo ai) {
214: return (JaxbAssertion<RMAssertion>) ai.getAssertion();
215: }
216: }
|