| StringResponseMessageListener.java |
1 /**
2 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.kernel.messaging;
24
25 /**
26 * <a href="StringResponseMessageListener.java.html"><b><i>View Source</i></b>
27 * </a>
28 *
29 * @author Brian Wing Shun Chan
30 *
31 */
32 public class StringResponseMessageListener implements MessageListener {
33
34 public StringResponseMessageListener(
35 Destination destination, Destination responseDestination,
36 String responseId, long timeout) {
37
38 _destination = destination;
39 _responseDestination = responseDestination;
40 _responseId = responseId;
41 _timeout = timeout;
42 }
43
44 public synchronized String send(String message) throws MessageBusException {
45 if (message.equals(_EMTPY_MESSAGE)) {
46 StringBuilder sb = new StringBuilder();
47
48 sb.append("{\"lfrResponseDestination\":\"");
49 sb.append(_responseDestination.getName());
50 sb.append("\",\"lfrResponseId\":\"");
51 sb.append(_responseId);
52 sb.append("\"}");
53
54 message = sb.toString();
55 }
56 else {
57 StringBuilder sb = new StringBuilder();
58
59 sb.append(message.substring(0, message.length() - 1));
60 sb.append(",\"lfrResponseDestination\":\"");
61 sb.append(_responseDestination.getName());
62 sb.append("\",\"lfrResponseId\":\"");
63 sb.append(_responseId);
64 sb.append("\"}");
65
66 message = sb.toString();
67 }
68
69 _destination.send(message);
70
71 try {
72 wait(_timeout);
73 }
74 catch (InterruptedException ie) {
75 throw new MessageBusException(
76 "Unable to receive response for request", ie);
77 }
78
79 if (_responseMessage == null) {
80 throw new MessageBusException(
81 "No reply received for request: " + message);
82 }
83
84 return _responseMessage;
85 }
86
87 public void receive(Object message) {
88 throw new UnsupportedOperationException();
89 }
90
91 public synchronized void receive(String message) {
92 if (message.indexOf(
93 "\"lfrResponseId\":\"" + _responseId + "\"") != -1) {
94
95 _responseMessage = message;
96
97 notify();
98 }
99 }
100
101 private static final String _EMTPY_MESSAGE = "{}";
102
103 private Destination _destination;
104 private Destination _responseDestination;
105 private String _responseId;
106 private long _timeout;
107 private String _responseMessage;
108
109 }