1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.io.Serializable;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * <a href="Message.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Michael C. Han
38   *
39   */
40  public class Message implements Serializable {
41  
42      public Object get(String key) {
43          if (_values == null) {
44              return null;
45          }
46          else {
47              return _values.get(key);
48          }
49      }
50  
51      public boolean getBoolean(String key) {
52          boolean value;
53  
54          Object object = get(key);
55  
56          if (object instanceof Boolean) {
57              value = ((Boolean)object).booleanValue();
58          }
59          else {
60              value = GetterUtil.getBoolean((String)object);
61          }
62  
63          return value;
64      }
65  
66      public String getDestination() {
67          return _destination;
68      }
69  
70      public double getDouble(String key) {
71          double value;
72  
73          Object object = get(key);
74  
75          if (object instanceof Number) {
76              value = ((Number)object).doubleValue();
77          }
78          else {
79              value = GetterUtil.getDouble((String)object);
80          }
81  
82          return value;
83      }
84  
85      public int getInteger(String key) {
86          int value;
87  
88          Object object = get(key);
89  
90          if (object instanceof Number) {
91              value = ((Number)object).intValue();
92          }
93          else {
94              value = GetterUtil.getInteger((String)object);
95          }
96  
97          return value;
98      }
99  
100     public long getLong(String key) {
101         long value;
102 
103         Object object = get(key);
104 
105         if (object instanceof Number) {
106             value = ((Number)object).longValue();
107         }
108         else {
109             value = GetterUtil.getLong((String)object);
110         }
111 
112         return value;
113     }
114 
115     public Object getPayload() {
116         return _payload;
117     }
118 
119     public String getResponseDestination() {
120         return _responseDestination;
121     }
122 
123     public String getResponseId() {
124         return _responseId;
125     }
126 
127     public String getString(String key) {
128         return GetterUtil.getString(String.valueOf(get(key)));
129     }
130 
131     public void put(String key, Object value) {
132         if (_values == null) {
133              _values = new HashMap<String, Object>();
134         }
135 
136         _values.put(key, value);
137     }
138 
139     public void setDestination(String destination) {
140         _destination = destination;
141 
142         if (Validator.isNull(_responseDestination)) {
143             _responseDestination =
144                 _destination + _DEFAULT_RESPONSE_DESTINATION_SUFFIX;
145         }
146     }
147 
148     public void setPayload(Object payload) {
149         _payload = payload;
150     }
151 
152     public void setResponseDestination(String responseDestination) {
153         _responseDestination = responseDestination;
154     }
155 
156     public void setResponseId(String responseId) {
157         _responseId = responseId;
158     }
159 
160     public String toString() {
161         StringBuilder sb = new StringBuilder();
162 
163         sb.append("{destination=");
164         sb.append(_destination);
165         sb.append(", responseDestination=");
166         sb.append(_responseDestination);
167         sb.append(", responseId=");
168         sb.append(_responseId);
169         sb.append(", payload=");
170         sb.append(_payload);
171         sb.append(", values=");
172         sb.append(_values);
173         sb.append("}");
174 
175         return sb.toString();
176     }
177 
178     private static final String _DEFAULT_RESPONSE_DESTINATION_SUFFIX =
179         "/response";
180 
181     private String _destination;
182     private String _responseDestination;
183     private String _responseId;
184     private Object _payload;
185     private Map<String, Object> _values;
186 
187 }