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;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    import java.util.concurrent.TimeUnit;
026    
027    import org.granite.client.messaging.channel.Channel;
028    import org.granite.client.messaging.channel.ResponseMessageFuture;
029    import org.granite.client.messaging.messages.requests.InvocationMessage;
030    
031    /**
032     * @author Franck WOLFF
033     */
034    public class RemoteService {
035    
036            private final Channel channel;
037            private final String id;
038    
039            public RemoteService(Channel channel, String id) {
040                    if (channel == null || id == null)
041                            throw new NullPointerException("channel and id cannot be null");
042                    this.channel = channel;
043                    this.id = id;
044            }
045    
046            public Channel getChannel() {
047                    return channel;
048            }
049    
050            public String getId() {
051                    return id;
052            }
053            
054            public RemoteServiceInvocation newInvocation(String method, Object...parameters) {
055                    return new RemoteServiceInvocation(this, method, parameters);
056            }
057            
058            public static interface RemoteServiceInvocationChain {
059                    
060                    RemoteServiceInvocationChain appendInvocation(String method, Object...parameters);
061                    ResponseMessageFuture invoke();
062            }
063            
064            public static class RemoteServiceInvocation implements RemoteServiceInvocationChain {
065                    
066                    private final RemoteService remoteService;
067                    private final InvocationMessage request;
068                    
069                    private long timeToLive = 0L;
070                    private List<ResponseListener> listeners = new ArrayList<ResponseListener>();
071                    
072                    public RemoteServiceInvocation(RemoteService remoteService, String method, Object...parameters) {
073                            if (remoteService == null)
074                                    throw new NullPointerException("remoteService cannot be null");
075                            this.remoteService = remoteService;
076                            this.request = new InvocationMessage(null, remoteService.id, method, parameters);
077                    }
078                    
079                    public RemoteServiceInvocation addListener(ResponseListener listener) {
080                            if (listener != null)
081                                    this.listeners.add(listener);
082                            return this;
083                    }
084                    
085                    public RemoteServiceInvocation addListeners(ResponseListener...listeners) {
086                            if (listeners != null && listeners.length > 0)
087                                    this.listeners.addAll(Arrays.asList(listeners));
088                            return this;
089                    }
090                    
091                    public RemoteServiceInvocation setTimeToLive(long timeToLiveMillis) {
092                            return setTimeToLive(timeToLiveMillis, TimeUnit.MILLISECONDS);
093                    }
094                    
095                    public RemoteServiceInvocation setTimeToLive(long timeToLive, TimeUnit unit) {
096                            if (timeToLive < 0)
097                                    throw new IllegalArgumentException("timeToLive cannot be negative");
098                            if (unit == null)
099                                    throw new NullPointerException("unit cannot be null");
100                            this.timeToLive = unit.toMillis(timeToLive);
101                            return this;
102                    }
103                    
104                    @Override
105                    public RemoteServiceInvocationChain appendInvocation(String method, Object...parameters) {
106                            InvocationMessage message = request;
107                            while (message.getNext() != null)
108                                    message = message.getNext();
109                            message.setNext(new InvocationMessage(null, remoteService.id, method, parameters));
110                            return this;
111                    }
112                    
113                    @Override
114                    public ResponseMessageFuture invoke() {
115                            request.setTimeToLive(timeToLive);
116                            return remoteService.channel.send(request, listeners.toArray(new ResponseListener[listeners.size()]));
117                    }
118            }
119    }