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