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