001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 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
021 package org.granite.client.messaging.messages.responses;
022
023 import java.io.IOException;
024 import java.io.ObjectInput;
025 import java.io.ObjectOutput;
026 import java.util.Iterator;
027 import java.util.Map;
028 import java.util.NoSuchElementException;
029
030 import org.granite.client.messaging.messages.AbstractMessage;
031 import org.granite.client.messaging.messages.ResponseMessage;
032
033 /**
034 * @author Franck WOLFF
035 */
036 public abstract class AbstractResponseMessage extends AbstractMessage implements ResponseMessage {
037
038 private String correlationId;
039 private ResponseMessage next;
040
041 public AbstractResponseMessage() {
042 }
043
044 public AbstractResponseMessage(String clientId, String correlationId) {
045 super(clientId);
046
047 this.correlationId = correlationId;
048 }
049
050 public AbstractResponseMessage(
051 String id,
052 String clientId,
053 long timestamp,
054 long timeToLive,
055 Map<String, Object> headers,
056 String correlationId) {
057
058 super(id, clientId, timestamp, timeToLive, headers);
059
060 this.correlationId = correlationId;
061 }
062
063 public String getCorrelationId() {
064 return correlationId;
065 }
066
067 public void setCorrelationId(String correlationId) {
068 this.correlationId = correlationId;
069 }
070
071 @Override
072 public void setNext(ResponseMessage next) {
073 for (ResponseMessage n = next; n != null; n = n.getNext()) {
074 if (n == this)
075 throw new RuntimeException("Circular chaining to this: " + next);
076 }
077 this.next = next;
078 }
079
080 @Override
081 public ResponseMessage getNext() {
082 return next;
083 }
084
085 @Override
086 public Iterator<ResponseMessage> iterator() {
087
088 final ResponseMessage first = this;
089
090 return new Iterator<ResponseMessage>() {
091
092 private ResponseMessage current = first;
093
094 @Override
095 public boolean hasNext() {
096 return current != null;
097 }
098
099 @Override
100 public ResponseMessage next() {
101 if (current == null)
102 throw new NoSuchElementException();
103 ResponseMessage c = current;
104 current = current.getNext();
105 return c;
106 }
107
108 @Override
109 public void remove() {
110 throw new UnsupportedOperationException();
111 }
112 };
113 }
114
115 @Override
116 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
117 super.readExternal(in);
118
119 this.correlationId = in.readUTF();
120 }
121
122 @Override
123 public void writeExternal(ObjectOutput out) throws IOException {
124 super.writeExternal(out);
125
126 if (correlationId != null)
127 out.writeUTF(correlationId);
128 else
129 out.writeObject(null);
130 }
131
132 @Override
133 protected void copy(AbstractMessage message) {
134 copy((AbstractResponseMessage)message, correlationId);
135 }
136
137 protected void copy(AbstractResponseMessage message, String correlationId) {
138 super.copy(message);
139
140 message.correlationId = correlationId;
141 }
142
143 public ResponseMessage copy(String correlationId) {
144 AbstractResponseMessage message = (AbstractResponseMessage)copy();
145
146 message.correlationId = correlationId;
147
148 return message;
149 }
150
151 @Override
152 public StringBuilder toString(StringBuilder sb) {
153 return super.toString(sb).append("\n correlationId=").append(correlationId);
154 }
155 }