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: * InboundSequence.java
039: *
040: * @author Mike Grogan
041: * Created on November 24, 2005, 9:29 AM
042: *
043: */
044:
045: package com.sun.xml.ws.rm.jaxws.runtime;
046:
047: import com.sun.xml.ws.rm.InvalidMessageNumberException;
048: import com.sun.xml.ws.rm.RMVersion;
049: import com.sun.xml.ws.rm.Sequence;
050: import com.sun.xml.ws.rm.protocol.AbstractAckRequested;
051: import com.sun.xml.ws.rm.protocol.AbstractSequenceAcknowledgement;
052: import com.sun.xml.ws.rm.v200502.Identifier;
053: import com.sun.xml.ws.rm.v200502.SequenceAcknowledgementElement;
054:
055: import javax.xml.bind.Marshaller;
056: import java.net.URI;
057:
058: /**
059: * An <code>InboundSequence</code> represents a sequence of incoming messages. For an
060: * <code>RMDestination</code>, an <code>InboundSequnce</code> consists of all
061: * the requests to a service from a particular proxy. For an <code>RMSource</code>,
062: * an <code>InboundSequence</code> contains all the response messages to requests
063: * in the companion <code>OutboundSequence</code>.
064: */
065: public abstract class InboundSequence extends Sequence {
066:
067: /**
068: * Configuration for this sequence.
069: */
070: protected SequenceConfig config;
071:
072: /**
073: * AcksTo URI. Assigned by ctor.
074: */
075: protected URI acksTo;
076:
077: /**
078: * Companion OutboundSequence
079: */
080: protected OutboundSequence outboundSequence;
081:
082: /**
083: * The SecurityTokenReference obtained from the CreateSequence
084: */
085: private String strId;
086:
087: public InboundSequence() {
088: }
089:
090: public InboundSequence(URI acksTo, SequenceConfig config) {
091:
092: this .acksTo = acksTo;
093: this .config = config;
094: this .rmConstants = config.getRMConstants();
095: }
096:
097: /** Construct a <code>SequenceAcknowlegementElement</code> based on the contents of this sequence.
098: *
099: * @param reqElement The <code>AckRequestedElement</code> to process. May be
100: * null. It is only used to determine its (optional) LastMessage
101: * element. If missing, LastMessage is assumed to be nextIndex - 1.
102: * marshaller The marshaller to be used for construction of the return value.
103: *
104: * TODO - decide whether this needs to be synchronized. It does not
105: * need to be if concurrent modifications only cause messages that
106: * have indeed arrived to be unacknowledged
107: */
108: public synchronized AbstractSequenceAcknowledgement generateSequenceAcknowledgement(
109: AbstractAckRequested reqElement, Marshaller marshaller,
110: boolean generateIsFinal)
111: throws InvalidMessageNumberException {
112:
113: AbstractSequenceAcknowledgement ackElement = null;
114: if (config.getRMVersion() == RMVersion.WSRM10) {
115: ackElement = new SequenceAcknowledgementElement();
116: Identifier id = new Identifier();
117: id.setValue(getId());
118: ((SequenceAcknowledgementElement) ackElement)
119: .setIdentifier(id);
120: } else {
121: ackElement = new com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement();
122: com.sun.xml.ws.rm.v200702.Identifier id = new com.sun.xml.ws.rm.v200702.Identifier();
123: id.setValue(getId());
124: ((com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement) ackElement)
125: .setIdentifier(id);
126: //If the RM version is 1.1 then the SequenceAcknowledgmenet.Final needs to be added
127: //when CloseSequence message is processed
128: if (generateIsFinal) {
129:
130: com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement.Final finalelem = new com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement.Final();
131: ;
132: ((com.sun.xml.ws.rm.v200702.SequenceAcknowledgementElement) ackElement)
133: .setFinal(finalelem);
134: }
135: }
136:
137: if (config != null && config.flowControl) {
138: ackElement.setBufferRemaining(maxMessages - storedMessages);
139: }
140:
141: int maxMessageNumber = 0;
142:
143: //The new WSRM 1.1 spec has no element for MaxMessageNumber in AckRequested
144: //hence commenting this code we will just use the last index of the message
145:
146: /*if (reqElement != null) {
147: maxMessageNumber = (int)(reqElement.getMaxMessageNumber());
148: }*/
149:
150: //if max message number element is not present, use the last
151: //index we know of.
152: if (maxMessageNumber == 0) {
153: maxMessageNumber = nextIndex - 1;
154: }
155:
156: int lower = 1;
157: int current = 1;
158: boolean gap = (get(current) == null);
159:
160: while (current <= maxMessageNumber) {
161: if (gap) {
162: if (get(current) != null) {
163: lower = current;
164: gap = false;
165: }
166: } else {
167: if (get(current) == null) {
168: ackElement.addAckRange(lower, current - 1);
169: gap = true;
170: }
171: }
172: current++;
173: }
174:
175: if (!gap) {
176: ackElement.addAckRange(lower, current - 1);
177: }
178:
179: return ackElement;
180: }
181:
182: /**
183: * Queue up a <code>SequenceAcknowledgement</code> element on companion <code>OutboundSequence</code>
184: * for delivery on next outbound application message.
185: * TODO
186: * Currently only works for replyTo = AcksTo scenarios. Expand functionality to allow AcksTo
187: * to different destination.
188: *
189: *
190: * @param reqElement The <code>AbstractAckRequested</code> to process.
191: * marshaller The marshaller to be used for construction of the return value.
192: */
193: public synchronized void handleAckRequested(
194: AbstractAckRequested reqElement, Marshaller marshaller)
195: throws InvalidMessageNumberException {
196:
197: AbstractSequenceAcknowledgement ackElement = generateSequenceAcknowledgement(
198: reqElement, marshaller, false);
199: outboundSequence.setSequenceAcknowledgement(ackElement);
200: }
201:
202: /**
203: * Accessor for the companion <code>OutboundSequence</code>
204: *
205: * @return The OutboundSequence.
206: */
207: public OutboundSequence getOutboundSequence() {
208: return outboundSequence;
209:
210: }
211:
212: public String getStrId() {
213: return strId;
214: }
215:
216: public void setStrId(String strId) {
217: this .strId = strId;
218: }
219:
220: public SequenceConfig getSequenceConfig() {
221: return config;
222: }
223:
224: public String getSessionId() {
225: return strId != null ? strId : getId();
226: }
227: }
|