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