001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.messaging;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.TransientValue;
020    
021    import java.io.Serializable;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Michael C. Han
029     */
030    public class Message implements Cloneable, Serializable {
031    
032            @Override
033            public Message clone() {
034                    Message message = new Message();
035    
036                    message._destinationName = _destinationName;
037                    message._payload = _payload;
038                    message._response = _response;
039                    message._responseDestinationName = _responseDestinationName;
040                    message._responseId = _responseId;
041    
042                    if (_values != null) {
043                            message._values = new HashMap<String, Object>(_values);
044                    }
045    
046                    return message;
047            }
048    
049            public boolean contains(String key) {
050                    if (_values == null) {
051                            return false;
052                    }
053                    else {
054                            return _values.containsKey(key);
055                    }
056            }
057    
058            public Object get(String key) {
059                    if (_values == null) {
060                            return null;
061                    }
062    
063                    Object value = _values.get(key);
064    
065                    if (value instanceof TransientValue) {
066                            TransientValue<Object> transientValue =
067                                    (TransientValue<Object>)value;
068    
069                            value = transientValue.getValue();
070                    }
071    
072                    return value;
073            }
074    
075            public boolean getBoolean(String key) {
076                    boolean value = false;
077    
078                    Object object = get(key);
079    
080                    if (object instanceof Boolean) {
081                            value = ((Boolean)object).booleanValue();
082                    }
083                    else {
084                            value = GetterUtil.getBoolean((String)object);
085                    }
086    
087                    return value;
088            }
089    
090            public String getDestinationName() {
091                    return _destinationName;
092            }
093    
094            public double getDouble(String key) {
095                    double value = 0;
096    
097                    Object object = get(key);
098    
099                    if (object instanceof Number) {
100                            value = ((Number)object).doubleValue();
101                    }
102                    else {
103                            value = GetterUtil.getDouble((String)object);
104                    }
105    
106                    return value;
107            }
108    
109            public int getInteger(String key) {
110                    int value = 0;
111    
112                    Object object = get(key);
113    
114                    if (object instanceof Number) {
115                            value = ((Number)object).intValue();
116                    }
117                    else {
118                            value = GetterUtil.getInteger((String)object);
119                    }
120    
121                    return value;
122            }
123    
124            public long getLong(String key) {
125                    long value = 0;
126    
127                    Object object = get(key);
128    
129                    if (object instanceof Number) {
130                            value = ((Number)object).longValue();
131                    }
132                    else {
133                            value = GetterUtil.getLong((String)object);
134                    }
135    
136                    return value;
137            }
138    
139            public Object getPayload() {
140                    return _payload;
141            }
142    
143            public Object getResponse() {
144                    return _response;
145            }
146    
147            public String getResponseDestinationName() {
148                    return _responseDestinationName;
149            }
150    
151            public String getResponseId() {
152                    return _responseId;
153            }
154    
155            public String getString(String key) {
156                    return GetterUtil.getString(String.valueOf(get(key)));
157            }
158    
159            public Map<String, Object> getValues() {
160                    return _values;
161            }
162    
163            public void put(String key, Object value) {
164                    if (value == null) {
165                            if (_values != null) {
166                                    _values.remove(key);
167                            }
168    
169                            return;
170                    }
171    
172                    if (_values == null) {
173                            _values = new HashMap<String, Object>();
174                    }
175    
176                    if (!(value instanceof Serializable)) {
177                            value = new TransientValue<Object>(value);
178                    }
179    
180                    _values.put(key, value);
181            }
182    
183            public void remove(String key) {
184                    if (_values != null) {
185                            _values.remove(key);
186                    }
187            }
188    
189            public void setDestinationName(String destinationName) {
190                    _destinationName = destinationName;
191            }
192    
193            public void setPayload(Object payload) {
194                    _payload = payload;
195            }
196    
197            public void setResponse(Object response) {
198                    _response = response;
199            }
200    
201            public void setResponseDestinationName(String responseDestinationName) {
202                    _responseDestinationName = responseDestinationName;
203            }
204    
205            public void setResponseId(String responseId) {
206                    _responseId = responseId;
207            }
208    
209            public void setValues(Map<String, Object> values) {
210                    _values = values;
211            }
212    
213            @Override
214            public String toString() {
215                    StringBundler sb = new StringBundler(11);
216    
217                    sb.append("{destinationName=");
218                    sb.append(_destinationName);
219                    sb.append(", response=");
220                    sb.append(_response);
221                    sb.append(", responseDestinationName=");
222                    sb.append(_responseDestinationName);
223                    sb.append(", responseId=");
224                    sb.append(_responseId);
225                    sb.append(", payload=");
226                    sb.append(_payload);
227                    sb.append(", values=");
228                    sb.append(_values);
229                    sb.append("}");
230    
231                    return sb.toString();
232            }
233    
234            private String _destinationName;
235            private Object _payload;
236            private transient Object _response;
237            private String _responseDestinationName;
238            private String _responseId;
239            private Map<String, Object> _values;
240    
241    }