001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of Granite Data Services.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or modify
008     *   it under the terms of the GNU Library General Public License as published by
009     *   the Free Software Foundation; either version 2 of the License, or (at your
010     *   option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful, but
013     *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     *   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015     *   for more details.
016     *
017     *   You should have received a copy of the GNU Library General Public License
018     *   along with this library; if not, see <http://www.gnu.org/licenses/>.
019     */
020    package org.granite.client.messaging.messages.responses;
021    
022    import java.io.IOException;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    import java.util.Iterator;
026    import java.util.Map;
027    import java.util.NoSuchElementException;
028    
029    import org.granite.client.messaging.messages.AbstractMessage;
030    import org.granite.client.messaging.messages.ResponseMessage;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public abstract class AbstractResponseMessage extends AbstractMessage implements ResponseMessage {
036    
037            private String correlationId;
038            private ResponseMessage next;
039            
040            public AbstractResponseMessage() {
041            }
042    
043            public AbstractResponseMessage(String clientId, String correlationId) {
044                    super(clientId);
045                    
046                    this.correlationId = correlationId;
047            }
048    
049            public AbstractResponseMessage(
050                    String id,
051                    String clientId,
052                    long timestamp,
053                    long timeToLive,
054                    Map<String, Object> headers,
055                    String correlationId) {
056                    
057                    super(id, clientId, timestamp, timeToLive, headers);
058                    
059                    this.correlationId = correlationId;
060            }
061    
062            public String getCorrelationId() {
063                    return correlationId;
064            }
065    
066            public void setCorrelationId(String correlationId) {
067                    this.correlationId = correlationId;
068            }
069            
070            @Override
071            public void setNext(ResponseMessage next) {
072                    for (ResponseMessage n = next; n != null; n = n.getNext()) {
073                            if (n == this)
074                                    throw new RuntimeException("Circular chaining to this: " + next);
075                    }
076                    this.next = next;
077            }
078    
079            @Override
080            public ResponseMessage getNext() {
081                    return next;
082            }
083            
084            @Override
085            public Iterator<ResponseMessage> iterator() {
086                    
087                    final ResponseMessage first = this;
088                    
089                    return new Iterator<ResponseMessage>() {
090    
091                            private ResponseMessage current = first;
092                            
093                            @Override
094                            public boolean hasNext() {
095                                    return current != null;
096                            }
097    
098                            @Override
099                            public ResponseMessage next() {
100                                    if (current == null)
101                                            throw new NoSuchElementException();
102                                    ResponseMessage c = current;
103                                    current = current.getNext();
104                                    return c;
105                            }
106    
107                            @Override
108                            public void remove() {
109                                    throw new UnsupportedOperationException();
110                            }
111                    };
112            }
113    
114            @Override
115            public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
116                    super.readExternal(in);
117                    
118                    this.correlationId = in.readUTF();
119            }
120    
121            @Override
122            public void writeExternal(ObjectOutput out) throws IOException {
123                    super.writeExternal(out);
124                    
125                    if (correlationId != null)
126                            out.writeUTF(correlationId);
127                    else
128                            out.writeObject(null);
129            }
130    
131            @Override
132            protected void copy(AbstractMessage message) {
133                    copy((AbstractResponseMessage)message, correlationId);
134            }
135    
136            protected void copy(AbstractResponseMessage message, String correlationId) {
137                    super.copy(message);
138                    
139                    message.correlationId = correlationId;
140            }
141    
142            public ResponseMessage copy(String correlationId) {
143                    AbstractResponseMessage message = (AbstractResponseMessage)copy();
144                    
145                    message.correlationId = correlationId;
146                    
147                    return message;
148            }
149    
150            @Override
151            public StringBuilder toString(StringBuilder sb) {
152                    return super.toString(sb).append("\n    correlationId=").append(correlationId);
153            }
154    }