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: /*
038: * AcknowledgementHandler.java
039: *
040: * @author Mike Grogan
041: * Created on October 30, 2005, 9:08 AM
042: *
043: */
044:
045: package com.sun.xml.ws.rm.protocol;
046:
047: import com.sun.xml.ws.rm.InvalidMessageNumberException;
048: import com.sun.xml.ws.rm.Message;
049: import com.sun.xml.ws.rm.RMVersion;
050: import com.sun.xml.ws.rm.jaxws.runtime.OutboundSequence;
051: import com.sun.xml.ws.rm.jaxws.runtime.SequenceConfig;
052: import com.sun.xml.ws.rm.v200502.SequenceAcknowledgementElement;
053:
054: import java.math.BigInteger;
055: import java.util.ArrayList;
056: import java.util.List;
057: import java.util.logging.Logger;
058:
059: /**
060: * Utility class that manages the acknowledgement of sequence messages. Methods
061: * return the values for <code>AckRequested</code> protocol requests as well as update
062: * Sequence contents based on <code>SequenceAcknowledgement</code> protocol elements.
063: *
064: */
065: public class AcknowledgementHandler {
066:
067: private static final Logger logger = Logger
068: .getLogger(AcknowledgementHandler.class.getName());
069:
070: /**
071: * Configuration for this sequence.
072: */
073: protected SequenceConfig config;
074:
075: public AcknowledgementHandler(SequenceConfig seqConfig) {
076: this .config = seqConfig;
077: }
078:
079: /**
080: * Mark the messages in the sequence delivered according to the contents
081: * of the specified <code>SequenceAcknowledgement</code> element.
082: *
083: * @param element The <code>SequenceAcknowledgementElement</code>
084: */
085: public void handleAcknowledgement(OutboundSequence sequence,
086: AbstractSequenceAcknowledgement element)
087: throws InvalidMessageNumberException {
088:
089: synchronized (sequence) {
090:
091: List<BigInteger> nacks = null;
092:
093: if (config.getRMVersion() == RMVersion.WSRM10) {
094: sequence
095: .setBufferRemaining(((com.sun.xml.ws.rm.v200502.SequenceAcknowledgementElement) element)
096: .getBufferRemaining());
097: nacks = ((com.sun.xml.ws.rm.v200502.SequenceAcknowledgementElement) element)
098: .getNack();
099: } else {
100: sequence
101: .setBufferRemaining(((com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement) element)
102: .getBufferRemaining());
103: nacks = ((com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement) element)
104: .getNack();
105:
106: }
107:
108: //TODO - error checking
109: //either nacks or ranges must be null or protocol element is malformed.
110: if (nacks != null && !nacks.isEmpty()) {
111:
112: int size = sequence.getNextIndex() + 1;
113: ArrayList<Boolean> list = new ArrayList<Boolean>();
114: for (int i = 1; i < sequence.getNextIndex(); i++) {
115: list.set(i, true);
116: }
117:
118: for (BigInteger big : nacks) {
119: int index = (int) big.longValue();
120: list.set(index, false);
121: }
122:
123: for (int i = 1; i < sequence.getNextIndex(); i++) {
124: Message mess = sequence.get(i);
125: if (list.get(i)) {
126: acknowledgeIfValid(sequence, i);
127: }
128: }
129: } else {
130:
131: switch (config.getRMVersion()) {
132: case WSRM10:
133: List<SequenceAcknowledgementElement.AcknowledgementRange> ranges = ((com.sun.xml.ws.rm.v200502.SequenceAcknowledgementElement) element)
134: .getAcknowledgementRange();
135: for (SequenceAcknowledgementElement.AcknowledgementRange range : ranges) {
136:
137: int lower = range.getLower().intValue();
138: int upper = range.getUpper().intValue();
139:
140: //if a SequenceHeader with Last elemet has been sent, we may
141: //receive acks for that "Message" although one was never stored
142: //at the index.
143: if (sequence.isLast()
144: && upper == sequence.getNextIndex()) {
145: upper--;
146: }
147:
148: for (int i = lower; i <= upper; i++) {
149: acknowledgeIfValid(sequence, i);
150: }
151:
152: }
153: break;
154: case WSRM11:
155: List<com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement.AcknowledgementRange> ranges1 = ((com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement) element)
156: .getAcknowledgementRange();
157: for (com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement.AcknowledgementRange range : ranges1) {
158:
159: int lower = range.getLower().intValue();
160: int upper = range.getUpper().intValue();
161:
162: for (int i = lower; i <= upper; i++) {
163: acknowledgeIfValid(sequence, i);
164: }
165: }
166: break;
167:
168: }
169:
170: }
171: }
172: }
173:
174: /**
175: * We may receive an ack for an unknown message if we are restarting
176: * after a crash or if the RMD is broken. Allow processing to continue
177: * after logging.
178: *
179: */
180: private void acknowledgeIfValid(OutboundSequence seq, int i) {
181: try {
182: Message mess = seq.get(i);
183: if (mess != null) {
184: seq.acknowledge(i);
185: }
186: } catch (InvalidMessageNumberException e) {
187: //this can happen if the sequence has been resurrected
188: //after a restart.
189: logger
190: .fine(com.sun.xml.ws.rm.protocol.Messages.ACKNOWLEDGEMENT_MESSAGE
191: .format(seq.getId(), i));
192:
193: }
194: }
195:
196: }
|