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