001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.util.jms;
059:
060: import java.util.ArrayList;
061: import java.util.Arrays;
062:
063: import javax.jms.JMSException;
064: import javax.jms.Message;
065: import javax.jms.Queue;
066: import javax.jms.QueueReceiver;
067: import org.apache.wsif.WSIFException;
068: import org.apache.wsif.logging.Trc;
069:
070: /**
071: * Server destination for the JMS2HTTPBridge
072: * @author Mark Whitlock <whitlock@apache.org>
073: */
074: public class JMS2HTTPBridgeDestination extends WSIFJMSDestination {
075: public static final String COLDSTART = "cold";
076: public static final String WARMSTART = "warm";
077:
078: private boolean verbose;
079: private static final ArrayList allStarts = new ArrayList(Arrays
080: .asList(new Object[] { COLDSTART, WARMSTART }));
081:
082: /**
083: * Public constructor.
084: * @param finder used to find JMS objects.
085: * @param altdestName is an alterative JMS provider destination name
086: * @param timeout is the maximum time to wait on a synchronous receive
087: * @param startType is WARMSTART or COLDSTART. Cold means wipe the read queue on startup.
088: */
089: public JMS2HTTPBridgeDestination(WSIFJMSFinder finder,
090: String altDestName, long timeout, String startType,
091: boolean verbose) throws WSIFException {
092: super (finder, altDestName, timeout);
093: Trc.entry(this , finder, altDestName, new Long(timeout),
094: startType, new Boolean(verbose));
095:
096: this .verbose = verbose;
097:
098: // Swap the queues because we're a server not a client.
099: readQ = writeQ;
100: writeQ = null;
101:
102: if (!allStarts.contains(startType))
103: throw new WSIFException(
104: "StartType must either be warm or cold");
105:
106: if (COLDSTART.equals(startType)) {
107: if (verbose)
108: System.out
109: .println("Wiping messages off the read queue");
110: Message msg = null;
111: try {
112: QueueReceiver rec = session.createReceiver(readQ);
113: for (;;) {
114: msg = rec.receive(100);
115: if (msg != null) {
116: if (verbose)
117: System.out
118: .println("Removing an input message");
119: } else
120: break;
121: }
122: } catch (Exception ignored) {
123: Trc.exception(ignored);
124: }
125: }
126: Trc.exit();
127: }
128:
129: /**
130: * Create a listener thread to listen for messages. This waits forever until it gets
131: * an InterruptedException. This listens on the read queue.
132: * @param listener is the JMS message and exception callback interface implementation
133: */
134: public void listen(WSIFJMSListener listener) throws WSIFException {
135: Trc.entry(this , listener);
136: listen(listener, readQ);
137: Trc.exit();
138: }
139:
140: /**
141: * Create a listener thread to listen for messages. This waits forever until it gets
142: * an InterruptedException.
143: * @param listener is the JMS message and exception callback interface implementation
144: * @param queue to listen on
145: */
146: public void listen(WSIFJMSListener listener, Queue queue)
147: throws WSIFException {
148: Trc.entry(this , listener, queue);
149: areWeClosed();
150:
151: try {
152: QueueReceiver qr = session.createReceiver(queue);
153: qr.setMessageListener(listener);
154: connection.setExceptionListener(listener);
155:
156: connection.start();
157:
158: for (int i = 1; !Thread.interrupted(); i++) {
159: Thread.yield();
160: Thread.sleep(5000);
161: if (verbose)
162: System.out.println("Waiting... " + i);
163: }
164: } catch (JMSException je) {
165: Trc.exception(je);
166: throw WSIFJMSConstants.ToWsifException(je);
167: } catch (InterruptedException ignored) {
168: Trc.exception(ignored);
169: System.out.println("Exitting");
170: }
171: Trc.exit();
172: }
173:
174: // Stop anyone overwriting our readQ
175: public void setReplyToQueue() throws WSIFException {
176: Trc.entry(this );
177: Trc.exit();
178: }
179:
180: public void setReplyToQueue(String replyTo) throws WSIFException {
181: Trc.entry(this , replyTo);
182: Trc.exit();
183: }
184:
185: /**
186: * Set the replyTo queue. Special bridge version.
187: * @param replyTo queue.
188: */
189: public void setReplyToQueue(Queue replyTo) throws WSIFException {
190: Trc.entry(this , replyTo);
191: areWeClosed();
192:
193: if (writeQ == null)
194: writeQ = replyTo;
195: else if (!writeQ.equals(replyTo)) {
196:
197: if (sender != null) {
198: try {
199: sender.close();
200: } catch (Exception e) {
201: Trc.exception(e);
202: }
203: sender = null;
204: }
205: writeQ = replyTo;
206: }
207: Trc.exit();
208: }
209: }
|