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.nio.intraband.messaging;
016    
017    import com.liferay.portal.kernel.io.Deserializer;
018    import com.liferay.portal.kernel.io.Serializer;
019    import com.liferay.portal.kernel.messaging.Message;
020    
021    import java.io.Externalizable;
022    import java.io.IOException;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    
026    import java.nio.ByteBuffer;
027    
028    import java.util.ArrayList;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class MessageRoutingBag implements Externalizable {
034    
035            public static final String MESSAGE_ROUTING_BAG = "MESSAGE_ROUTING_BAG";
036    
037            public static MessageRoutingBag fromByteArray(byte[] data)
038                    throws ClassNotFoundException {
039    
040                    MessageRoutingBag messageRoutingBag = new MessageRoutingBag();
041    
042                    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(data));
043    
044                    messageRoutingBag._destinationName = deserializer.readString();
045                    messageRoutingBag._messageData = deserializer.readObject();
046                    messageRoutingBag._routingDowncast = deserializer.readBoolean();
047                    messageRoutingBag._routingTrace = deserializer.readObject();
048                    messageRoutingBag._synchronizedBridge = deserializer.readBoolean();
049    
050                    return messageRoutingBag;
051            }
052    
053            public MessageRoutingBag() {
054            }
055    
056            public MessageRoutingBag(Message message, boolean synchronizedBridge) {
057                    _destinationName = message.getDestinationName();
058                    _message = message;
059                    _synchronizedBridge = synchronizedBridge;
060            }
061    
062            public void appendRoutingId(String routingId) {
063                    _routingTrace.add(routingId);
064            }
065    
066            public String getDestinationName() {
067                    return _destinationName;
068            }
069    
070            public Message getMessage() throws ClassNotFoundException {
071                    if (_message == null) {
072                            _message = Message.fromByteArray(_messageData);
073    
074                            _message.put(MESSAGE_ROUTING_BAG, this);
075    
076                            _messageData = null;
077                    }
078    
079                    return _message;
080            }
081    
082            public byte[] getMessageData() {
083                    if (_messageData == null) {
084                            _message.remove(MESSAGE_ROUTING_BAG);
085    
086                            try {
087                                    _messageData = _message.toByteArray();
088                            }
089                            finally {
090                                    _message.put(MESSAGE_ROUTING_BAG, this);
091                                    _message = null;
092                            }
093                    }
094    
095                    return _messageData;
096            }
097    
098            public boolean isRoutingDowncast() {
099                    return _routingDowncast;
100            }
101    
102            public boolean isSynchronizedBridge() {
103                    return _synchronizedBridge;
104            }
105    
106            public boolean isVisited(String routingId) {
107                    return _routingTrace.contains(routingId);
108            }
109    
110            @Override
111            public void readExternal(ObjectInput objectInput)
112                    throws ClassNotFoundException, IOException {
113    
114                    _destinationName = objectInput.readUTF();
115                    _messageData = (byte[])objectInput.readObject();
116                    _routingDowncast = objectInput.readBoolean();
117                    _routingTrace = (ArrayList<String>)objectInput.readObject();
118                    _synchronizedBridge = objectInput.readBoolean();
119            }
120    
121            public void setMessage(Message message) {
122                    _message = message;
123    
124                    message.put(MESSAGE_ROUTING_BAG, this);
125            }
126    
127            public void setRoutingDowncast(boolean routingDowncast) {
128                    _routingDowncast = routingDowncast;
129            }
130    
131            public byte[] toByteArray() {
132                    Serializer serializer = new Serializer();
133    
134                    serializer.writeString(_destinationName);
135                    serializer.writeObject(getMessageData());
136                    serializer.writeBoolean(_routingDowncast);
137                    serializer.writeObject(_routingTrace);
138                    serializer.writeBoolean(_synchronizedBridge);
139    
140                    ByteBuffer byteBuffer = serializer.toByteBuffer();
141    
142                    return byteBuffer.array();
143            }
144    
145            @Override
146            public void writeExternal(ObjectOutput objectOutput) throws IOException {
147                    objectOutput.writeUTF(_destinationName);
148                    objectOutput.writeObject(getMessageData());
149                    objectOutput.writeBoolean(_routingDowncast);
150                    objectOutput.writeObject(_routingTrace);
151                    objectOutput.writeBoolean(_synchronizedBridge);
152            }
153    
154            private String _destinationName;
155            private Message _message;
156            private byte[] _messageData;
157            private boolean _routingDowncast;
158            private ArrayList<String> _routingTrace = new ArrayList<String>();
159            private boolean _synchronizedBridge;
160    
161    }