001: /*
002: * $Id: Sequence.java,v 1.12 2007/09/20 18:47:07 bhaktimehta Exp $
003: */
004:
005: /*
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
007: *
008: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
009: *
010: * The contents of this file are subject to the terms of either the GNU
011: * General Public License Version 2 only ("GPL") or the Common Development
012: * and Distribution License("CDDL") (collectively, the "License"). You
013: * may not use this file except in compliance with the License. You can obtain
014: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
015: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
016: * language governing permissions and limitations under the License.
017: *
018: * When distributing the software, include this License Header Notice in each
019: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
020: * Sun designates this particular file as subject to the "Classpath" exception
021: * as provided by Sun in the GPL Version 2 section of the License file that
022: * accompanied this code. If applicable, add the following below the License
023: * Header, with the fields enclosed by brackets [] replaced by your own
024: * identifying information: "Portions Copyrighted [year]
025: * [name of copyright owner]"
026: *
027: * Contributor(s):
028: *
029: * If you wish your version of this file to be governed by only the CDDL or
030: * only the GPL Version 2, indicate your decision by adding "[Contributor]
031: * elects to include this software in this distribution under the [CDDL or GPL
032: * Version 2] license." If you don't indicate a single choice of license, a
033: * recipient has the option to distribute your version of this file under
034: * either the CDDL, the GPL Version 2 or to extend the choice of license to
035: * its licensees as provided above. However, if you add GPL Version 2 code
036: * and therefore, elected the GPL Version 2 license, then the option applies
037: * only if the new code is made subject to such option by the copyright
038: * holder.
039: */
040:
041: package com.sun.xml.ws.rm;
042:
043: import com.sun.xml.ws.rm.jaxws.runtime.SequenceConfig;
044:
045: import java.util.ArrayList;
046:
047: /**
048: * A Sequence is a sparse array of messages corresponding to an RM Sequence. It
049: * is implemented as an ArrayList with nulls at unfilled indices.
050: */
051:
052: public class Sequence {
053:
054: /**
055: * The sequence identifier.
056: */
057: protected String id;
058:
059: /**
060: * The underlying list of messages
061: */
062: protected ArrayList<Message> list;
063:
064: /**
065: * The smallest unfilled index.
066: */
067: protected int nextIndex = 1;
068:
069: /**
070: * Flag indicates that message with Sequence header containing
071: * Last element has been sent/received. If this is the case,
072: * SequenceAcknowledgements for the sequence may contain acks
073: * for a message is one greater than the index for the last
074: * message in the sequence.
075: */
076: protected boolean last = false;
077:
078: /**
079: * Flag that indicates if the CloseSequence message has been sent/received
080: * If this is the case then the Sequence needs to be closed and no more messages
081: * with that Sequence Id should be accepted
082: */
083: protected boolean closed = false;
084:
085: /**
086: * Maximum number of stored messages. Used for server-side sequences
087: * for which flow control is enabled. The value -1 indicates that
088: * there is no limit.
089: */
090: protected int maxMessages = -1;
091:
092: /**
093: * Number of messages currently being stored awaiting completion.
094: */
095: protected int storedMessages = 0;
096:
097: /**
098: * Last accesse time.
099: */
100: protected long lastActivityTime;
101:
102: protected boolean allowDuplicates;
103:
104: /**
105: * RMConstants associated with each Sequence which
106: * will give information regarding addressing version, JAXBContext etc
107: *
108: */
109: protected RMConstants rmConstants;
110:
111: protected SequenceConfig config;
112:
113: protected int firstKnownGap;
114:
115: /**
116: * Gets the sequence identifier
117: * @return The sequence identifier.
118: */
119: public String getId() {
120: return id;
121: }
122:
123: /**
124: * Sets the sequence identifier.
125: * @param id The sequence identifier.
126: */
127: public void setId(String id) {
128: this .id = id;
129: }
130:
131: public Sequence() {
132:
133: list = new ArrayList<Message>();
134: //fill in 0-th index that will never be used since
135: //messageNumbers are 1-based and we will be keeping
136: //messageNumbers in-sync with indices.
137: list.add(null);
138: allowDuplicates = false;
139: firstKnownGap = 1;
140:
141: resetLastActivityTime();
142:
143: }
144:
145: /**
146: * Accessor for SequenceConfig field.
147: */
148: public SequenceConfig getSequenceConfig() {
149: return config;
150: }
151:
152: /**
153: * Accessor for the nextIndex field.
154: * @return The value of the nextIndex field
155: */
156: public synchronized int getNextIndex() {
157: return nextIndex;
158: }
159:
160: /**
161: * Gets the Message at the specified index.
162: * @param index The index to access
163: * @return The Message at the specified index
164: * @throws InvalidMessageNumberException If the index is larger than the largest
165: * index used.
166: */
167: public synchronized Message get(int index)
168: throws InvalidMessageNumberException {
169:
170: if (index >= nextIndex) {
171: throw new InvalidMessageNumberException(
172: Messages.INVALID_INDEX_MESSAGE.format(index));
173: }
174: return list.get(index);
175: }
176:
177: /**
178: * Adds a Message to the Sequence at a specified indes. The index must be
179: * positive. If the index is larger than the nextIndex field, nulls are inserted
180: * between the largest index used and the index, and the index becomes the new
181: * value of the nextIndex field
182: * @param i The index at which to insert the message.
183: * @param m The message to insert
184: *
185: * @return The new value of nextIndex.
186: */
187: public synchronized int set(int i, Message m)
188: throws InvalidMessageNumberException, BufferFullException,
189: DuplicateMessageException {
190:
191: //record the index and sequence in the message
192: m.setMessageNumber(i);
193: m.setSequence(this );
194:
195: if (i <= 0) {
196: throw new InvalidMessageNumberException();
197: }
198:
199: if (storedMessages == maxMessages) {
200: throw new BufferFullException(this );
201: }
202:
203: if (i < nextIndex) {
204: Message mess = null;
205: if (null != (mess = list.get(i)) && !allowDuplicates) {
206: //Store the original message in the exception so
207: //that exception handling can use it.
208: throw new DuplicateMessageException(mess);
209: }
210:
211: list.set(i, m);
212: } else if (i == nextIndex) {
213: list.add(m);
214: nextIndex++;
215: } else {
216: //fill in nulls between nextIndex an new nextIndex.
217: for (int j = nextIndex; j < i; j++) {
218: list.add(null);
219: }
220: list.add(m);
221: nextIndex = i + 1;
222: }
223:
224: storedMessages++;
225:
226: return i;
227: }
228:
229: /**
230: * Sets the last flag.
231: */
232: public synchronized void setLast() {
233: last = true;
234: }
235:
236: /**
237: * Sets the last flag.
238: */
239: public synchronized void setClosed() {
240: closed = true;
241: }
242:
243: /*
244: * Gets the value of the last flag.
245: *
246: * @return The value of the flag.
247: */
248: public synchronized boolean isLast() {
249: return last;
250: }
251:
252: /*
253: * Gets the value of the last flag.
254: *
255: * @return The value of the flag.
256: */
257: public synchronized boolean isClosed() {
258: return closed;
259: }
260:
261: /////////////////////////////////////////////////////////////////////////////
262: /*
263: * InactivityTimeout management helpers
264: * used by ClientOutboundSequence and ServerInboundSequence
265: *
266: *////////////////////////////////////////////////////////////////////////////
267:
268: /**
269: * Resets lastActivityTime field to current time.
270: */
271: public void resetLastActivityTime() {
272: lastActivityTime = System.currentTimeMillis();
273: }
274:
275: /**
276: * Accessor for lastActivityTime field.
277: *
278: * @return The value of the field.
279: */
280: protected long getLastActivityTime() {
281: return lastActivityTime;
282: }
283:
284: /**
285: * Return value determines whether elapsed time is close enough to
286: * time limit to pull the trigger and send an ackRequested to keep the
287: * sequence alive.
288: *
289: * @param elapsedTime Elapsed time since last reset.
290: * @param timeLimit Maximum time to wait
291: */
292: protected boolean isGettingClose(long elapsedTime, long timeLimit) {
293: //for now
294: return elapsedTime > timeLimit / 2;
295: }
296:
297: }
|