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    
020    import java.io.Serializable;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Michael C. Han
028     */
029    public class Message implements Cloneable, Serializable {
030    
031            @Override
032            public Message clone() {
033                    Message message = new Message();
034    
035                    message._destinationName = _destinationName;
036                    message._payload = _payload;
037                    message._response = _response;
038                    message._responseDestinationName = _responseDestinationName;
039                    message._responseId = _responseId;
040    
041                    if (_values != null) {
042                            message._values = new HashMap<String, Object>(_values);
043                    }
044    
045                    return message;
046            }
047    
048            public boolean contains(String key) {
049                    if (_values == null) {
050                            return false;
051                    }
052                    else {
053                            return _values.containsKey(key);
054                    }
055            }
056    
057            public Object get(String key) {
058                    if (_values == null) {
059                            return null;
060                    }
061                    else {
062                            return _values.get(key);
063                    }
064            }
065    
066            public boolean getBoolean(String key) {
067                    boolean value = false;
068    
069                    Object object = get(key);
070    
071                    if (object instanceof Boolean) {
072                            value = ((Boolean)object).booleanValue();
073                    }
074                    else {
075                            value = GetterUtil.getBoolean((String)object);
076                    }
077    
078                    return value;
079            }
080    
081            public String getDestinationName() {
082                    return _destinationName;
083            }
084    
085            public double getDouble(String key) {
086                    double value = 0;
087    
088                    Object object = get(key);
089    
090                    if (object instanceof Number) {
091                            value = ((Number)object).doubleValue();
092                    }
093                    else {
094                            value = GetterUtil.getDouble((String)object);
095                    }
096    
097                    return value;
098            }
099    
100            public int getInteger(String key) {
101                    int value = 0;
102    
103                    Object object = get(key);
104    
105                    if (object instanceof Number) {
106                            value = ((Number)object).intValue();
107                    }
108                    else {
109                            value = GetterUtil.getInteger((String)object);
110                    }
111    
112                    return value;
113            }
114    
115            public long getLong(String key) {
116                    long value = 0;
117    
118                    Object object = get(key);
119    
120                    if (object instanceof Number) {
121                            value = ((Number)object).longValue();
122                    }
123                    else {
124                            value = GetterUtil.getLong((String)object);
125                    }
126    
127                    return value;
128            }
129    
130            public Object getPayload() {
131                    return _payload;
132            }
133    
134            public Object getResponse() {
135                    return _response;
136            }
137    
138            public String getResponseDestinationName() {
139                    return _responseDestinationName;
140            }
141    
142            public String getResponseId() {
143                    return _responseId;
144            }
145    
146            public String getString(String key) {
147                    return GetterUtil.getString(String.valueOf(get(key)));
148            }
149    
150            public Map<String, Object> getValues() {
151                    return _values;
152            }
153    
154            public void put(String key, Object value) {
155                    if (_values == null) {
156                             _values = new HashMap<String, Object>();
157                    }
158    
159                    _values.put(key, value);
160            }
161    
162            public void remove(String key) {
163                    if (_values != null) {
164                            _values.remove(key);
165                    }
166            }
167    
168            public void setDestinationName(String destinationName) {
169                    _destinationName = destinationName;
170            }
171    
172            public void setPayload(Object payload) {
173                    _payload = payload;
174            }
175    
176            public void setResponse(Object response) {
177                    _response = response;
178            }
179    
180            public void setResponseDestinationName(String responseDestinationName) {
181                    _responseDestinationName = responseDestinationName;
182            }
183    
184            public void setResponseId(String responseId) {
185                    _responseId = responseId;
186            }
187    
188            public void setValues(Map<String, Object> values) {
189                    _values = values;
190            }
191    
192            @Override
193            public String toString() {
194                    StringBundler sb = new StringBundler(11);
195    
196                    sb.append("{destinationName=");
197                    sb.append(_destinationName);
198                    sb.append(", response=");
199                    sb.append(_response);
200                    sb.append(", responseDestinationName=");
201                    sb.append(_responseDestinationName);
202                    sb.append(", responseId=");
203                    sb.append(_responseId);
204                    sb.append(", payload=");
205                    sb.append(_payload);
206                    sb.append(", values=");
207                    sb.append(_values);
208                    sb.append("}");
209    
210                    return sb.toString();
211            }
212    
213            private String _destinationName;
214            private Object _payload;
215            private transient Object _response;
216            private String _responseDestinationName;
217            private String _responseId;
218            private Map<String, Object> _values;
219    
220    }