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.requests;
021    
022    import java.io.IOException;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    import java.util.Arrays;
026    import java.util.Iterator;
027    import java.util.Map;
028    import java.util.NoSuchElementException;
029    
030    import org.granite.client.messaging.messages.MessageChain;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public final class InvocationMessage extends AbstractRequestMessage implements MessageChain<InvocationMessage> {
036    
037            private String serviceId = null;
038            private String method = null;
039            private Object[] parameters = null;
040            
041            private InvocationMessage next = null;
042            
043            public InvocationMessage() {
044            }
045    
046            public InvocationMessage(String serviceId, String method, Object[] parameters) {
047                    this(null, serviceId, method, parameters);
048            }
049    
050            public InvocationMessage(String clientId, String serviceId, String method, Object[] parameters) {
051                    super(clientId);
052    
053                    this.serviceId = serviceId;
054                    this.method = method;
055                    this.parameters = parameters;
056            }
057    
058            public InvocationMessage(
059                    String id,
060                    String clientId,
061                    long timestamp,
062                    long timeToLive,
063                    Map<String, Object> headers,
064                    String serviceId,
065                    String method,
066                    Object[] parameters) {
067                    
068                    super(id, clientId, timestamp, timeToLive, headers);
069                    
070                    this.serviceId = serviceId;
071                    this.method = method;
072                    this.parameters = parameters;
073            }
074    
075            @Override
076            public Type getType() {
077                    return Type.INVOCATION;
078            }
079    
080            public String getServiceId() {
081                    return serviceId;
082            }
083    
084            public void setServiceId(String serviceId) {
085                    this.serviceId = serviceId;
086            }
087    
088            public String getMethod() {
089                    return method;
090            }
091    
092            public void setMethod(String method) {
093                    this.method = method;
094            }
095    
096            public Object[] getParameters() {
097                    return parameters;
098            }
099    
100            public void setParameters(Object[] parameters) {
101                    this.parameters = parameters;
102            }
103    
104            @Override
105            public void setNext(InvocationMessage next) {
106                    for (InvocationMessage n = next; n != null; n = n.getNext()) {
107                            if (n == this)
108                                    throw new RuntimeException("Circular chaining to this: " + next);
109                    }
110                    this.next = next;
111            }
112    
113            @Override
114            public InvocationMessage getNext() {
115                    return next;
116            }
117            
118            @Override
119            public Iterator<InvocationMessage> iterator() {
120                    
121                    final InvocationMessage first = this;
122                    
123                    return new Iterator<InvocationMessage>() {
124    
125                            private InvocationMessage current = first;
126                            
127                            @Override
128                            public boolean hasNext() {
129                                    return current != null;
130                            }
131    
132                            @Override
133                            public InvocationMessage next() {
134                                    if (current == null)
135                                            throw new NoSuchElementException();
136                                    InvocationMessage c = current;
137                                    current = current.getNext();
138                                    return c;
139                            }
140    
141                            @Override
142                            public void remove() {
143                                    throw new UnsupportedOperationException();
144                            }
145                    };
146            }
147    
148            @Override
149            public InvocationMessage copy() {
150                    InvocationMessage message = new InvocationMessage();
151    
152                    copy(message);
153                    
154                    message.serviceId = serviceId;
155                    message.method = method;
156                    message.parameters = parameters;
157                    
158                    return message;
159            }
160    
161            @Override
162            public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
163                    super.readExternal(in);
164                    
165                    this.serviceId = in.readUTF();
166                    this.method = in.readUTF();
167                    this.parameters = (Object[])in.readObject();
168            }
169    
170            @Override
171            public void writeExternal(ObjectOutput out) throws IOException {
172                    super.writeExternal(out);
173                    
174                    out.writeUTF(serviceId);
175                    out.writeUTF(method);
176                    out.writeObject(parameters);
177            }
178            
179            @Override
180            public StringBuilder toString(StringBuilder sb) {
181                    return super.toString(sb)
182                            .append("\n    serviceId=").append(serviceId)
183                            .append("\n    method=").append(method)
184                            .append("\n    parameters=").append(parameters == null ? null : Arrays.toString(parameters));
185            }
186    }