001: /*
002: * $Id: Message.java,v 1.11 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.protocol.AbstractAckRequested;
044: import com.sun.xml.ws.rm.protocol.AbstractSequence;
045: import com.sun.xml.ws.rm.protocol.AbstractSequenceAcknowledgement;
046:
047: /**
048: * Message is an abstraction of messages that can be added to WS-RM Sequences.
049: * Each instance wraps a JAX-WS message.
050: */
051: public class Message {
052:
053: /**
054: * The JAX-WS Message wrapped by this instance.
055: */
056: protected com.sun.xml.ws.api.message.Message message;
057:
058: /**
059: * The Sequence to which the message belongs.
060: */
061: protected Sequence sequence = null;
062:
063: /**
064: * The messageNumber of the Message in its Sequence.
065: */
066: protected int messageNumber = 0;
067:
068: /**
069: * Flag which is true if and only if the message is being processed
070: */
071: protected boolean isBusy = false;
072:
073: /**
074: * Flag indicating whether message is delivered/acked.
075: * The meaning differs according to the type of sequence
076: * to which the message belongs. The value must only be
077: * changed using the complete() method, which should only
078: * be invoked by the Sequence containing the message.
079: */
080: protected boolean isComplete = false;
081:
082: /**
083: * For messages belonging to 2-way MEPS, the corresponding message.
084: */
085: protected com.sun.xml.ws.rm.Message relatedMessage = null;
086:
087: /**
088: * Sequence stored when the corresponding com.sun.xml.ws.api.message.Header
089: * is added to the message.
090: */
091: protected AbstractSequence sequenceElement = null;
092:
093: /**
094: * SequenceAcknowledgmentElement stored when the corresponding com.sun.xml.ws.api.message.Header
095: * is added to the message.
096: */
097: protected AbstractSequenceAcknowledgement sequenceAcknowledgementElement = null;
098:
099: /**
100: * SequenceElement stored when the corresponding com.sun.xml.ws.api.message.Header
101: * is added to the message.
102: */
103: protected AbstractAckRequested ackRequestedElement = null;
104:
105: /**
106: * Version of RM spec being used.
107: */
108: protected RMVersion version;
109:
110: /**
111: * When true, indicates that the message is a request message for
112: * a two-way operation. ClientOutboundSequence with anonymous
113: * AcksTo has to handle Acknowledgements differently in this case.
114: */
115: public boolean isTwoWayRequest = false;
116:
117: /**
118: * Set in empty message used to piggyback response
119: * headers on a one-way response.
120: */
121: public boolean isOneWayResponse = false;
122:
123: /**
124: * Instance of TublineHelper used to resend messages.
125: */
126: private MessageSender messageSender;
127:
128: /**
129: * Public ctor takes wrapped JAX-WS message as its argument.
130: */
131: public Message(com.sun.xml.ws.api.message.Message message,
132: RMVersion version) {
133: this .message = message;
134: this .version = version;
135: }
136:
137: /**
138: * Sets the value of the sequence field. Used by Sequence methods when
139: * adding message to the sequence.
140: * @param sequence The sequence.
141: */
142: public void setSequence(Sequence sequence) {
143: this .sequence = sequence;
144: }
145:
146: /**
147: * Gets the Sequence to which the Message belongs.
148: * @return The sequence.
149: */
150: public Sequence getSequence() {
151: return sequence;
152: }
153:
154: /**
155: * Sets the value of the messageNumber field. Used by Sequence methods when
156: * adding message to the sequence.
157: * @param messageNumber The message number.
158: */
159: public void setMessageNumber(int messageNumber) {
160: this .messageNumber = messageNumber;
161: }
162:
163: /**
164: * Returns the value of the messageNumber field
165: * @return The message number.
166: */
167: public int getMessageNumber() {
168: return messageNumber;
169: }
170:
171: /**
172: * For client message, sets the messageSender used to resend messages.
173: */
174: public void setMessageSender(MessageSender messageSender) {
175: this .messageSender = messageSender;
176: }
177:
178: /**
179: * Accessor for the relatedMessage field.
180: *
181: * @return The response corresponding to a request and vice-versa.
182: */
183: public com.sun.xml.ws.rm.Message getRelatedMessage() {
184: return relatedMessage;
185: }
186:
187: /**
188: * Mutator for the relatedMessage field.
189: *
190: * @param mess
191: */
192: public void setRelatedMessage(com.sun.xml.ws.rm.Message mess) {
193: //store the message with a copy of the "inner" com.sun.xml.ws.api.message.Message
194: //since the original one will be consumed
195: mess.copyContents();
196: relatedMessage = mess;
197: }
198:
199: /**
200: * Accessors for isBusy field used with Tubeline implementation.
201: */
202: public boolean getIsBusy() {
203: return isBusy();
204: }
205:
206: public void setIsBusy(boolean value) {
207: isBusy = value;
208: }
209:
210: /**
211: * Get the RM Header Element with the specified name from the underlying
212: * JAX-WS message's HeaderList
213: * @param name The name of the Header to find.
214: */
215: public com.sun.xml.ws.api.message.Header getHeader(String name) {
216: if (message == null || !message.hasHeaders()) {
217: return null;
218: }
219:
220: return message.getHeaders().get(version.getNamespaceURI(),
221: name, true);
222: }
223:
224: /**
225: * Add the specified RM Header element to the underlying JAX-WS message's
226: * <code>HeaderList</code>.
227: *
228: * @param header The <code>Header</code> to add to the <code>HeaderList</code>.
229: */
230: public void addHeader(com.sun.xml.ws.api.message.Header header) {
231: message.getHeaders().add(header);
232: }
233:
234: /**
235: * Determines whether this message is delivered/acked
236: *
237: * @return The value of the isComplete flag
238: */
239: public boolean isComplete() {
240: //synchronized block is redundant.
241: synchronized (sequence) {
242: return isComplete;
243: }
244: }
245:
246: /**
247: * Sets the isComplete field to true, indicating that the message has been acked. Also
248: * discards the stored com.sun.xml.api.message.Message.
249: */
250: public void complete() {
251: //release reference to JAX-WS message.
252: synchronized (sequence) {
253: message = null;
254: isComplete = true;
255: }
256: }
257:
258: /**
259: * Resume processing of the message on this Message's monitor.
260: */
261: public synchronized void resume() {
262:
263: if (!isBusy && !isComplete) {
264: messageSender.send();
265: }
266:
267: }
268:
269: public synchronized boolean isBusy() {
270: return isBusy;
271: }
272:
273: /**
274: * Returns a copy of the wrapped com.sun.xml.ws.api.message.Message.
275: */
276: public com.sun.xml.ws.api.message.Message getCopy() {
277: return message == null ? null : message.copy();
278: }
279:
280: /**
281: * Returns a com.sun.ws.rm.Message whose inner com.sun.xml.ws.api.message.Message is replaced by
282: * a copy of the original one. This message is stored in the relatedMessage field of ClientInboundSequence
283: * messages. A copy needs to be retained rather than the original since the original will already
284: * have been consumed at such time the relatedMessage needs to be resent.
285: *
286: */
287: public void copyContents() {
288: if (message != null) {
289: com.sun.xml.ws.api.message.Message newmessage = message
290: .copy();
291: message = newmessage;
292: }
293: }
294:
295: public String toString() {
296:
297: String ret = Messages.MESSAGE_NUMBER_STRING
298: .format(messageNumber);
299: ret += Messages.SEQUENCE_STRING
300: .format(getSequence() != null ? getSequence().getId()
301: : "null");
302:
303: AbstractSequence sel;
304: AbstractSequenceAcknowledgement sael;
305: AbstractAckRequested ael;
306: if (null != (sel = getSequenceElement())) {
307: ret += sel.toString();
308: }
309:
310: if (null != (sael = getSequenceAcknowledgementElement())) {
311: ret += sael.toString();
312: }
313:
314: if (null != (ael = getAckRequestedElement())) {
315: ret += ael.toString();
316: }
317:
318: return ret;
319:
320: }
321:
322: /* Diagnostic methods store com.sun.xml.ws.protocol.* elements when
323: * corresponding com.sun.xml.ws.api.message.Headers are added to the
324: * message
325: */
326:
327: public AbstractSequenceAcknowledgement getSequenceAcknowledgementElement() {
328: return sequenceAcknowledgementElement;
329: }
330:
331: public void setSequenceAcknowledgementElement(
332: AbstractSequenceAcknowledgement el) {
333: sequenceAcknowledgementElement = el;
334: }
335:
336: public AbstractSequence getSequenceElement() {
337: return sequenceElement;
338: }
339:
340: public void setSequenceElement(AbstractSequence el) {
341: sequenceElement = el;
342: }
343:
344: public AbstractAckRequested getAckRequestedElement() {
345: return ackRequestedElement;
346: }
347:
348: public void setAckRequestedElement(AbstractAckRequested el) {
349: ackRequestedElement = el;
350: }
351:
352: }
|