001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *                               ***
008 *
009 *   Community License: GPL 3.0
010 *
011 *   This file is free software: you can redistribute it and/or modify
012 *   it under the terms of the GNU General Public License as published
013 *   by the Free Software Foundation, either version 3 of the License,
014 *   or (at your option) any later version.
015 *
016 *   This file is distributed in the hope that it will be useful, but
017 *   WITHOUT ANY WARRANTY; without even the implied warranty of
018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019 *   GNU General Public License for more details.
020 *
021 *   You should have received a copy of the GNU General Public License
022 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
023 *
024 *                               ***
025 *
026 *   Available Commercial License: GraniteDS SLA 1.0
027 *
028 *   This is the appropriate option if you are creating proprietary
029 *   applications and you are not prepared to distribute and share the
030 *   source code of your application under the GPL v3 license.
031 *
032 *   Please visit http://www.granitedataservices.com/license for more
033 *   details.
034 */
035package org.granite.client.tide.impl;
036
037import java.util.concurrent.ExecutionException;
038import java.util.concurrent.Future;
039import java.util.concurrent.TimeUnit;
040import java.util.concurrent.TimeoutException;
041
042import org.granite.client.messaging.channel.ResponseMessageFuture;
043import org.granite.client.tide.server.ComponentListener;
044
045/**
046 * @author William DRAI
047 */
048public class FutureResult<T> implements Future<T> {
049    
050        private ResponseMessageFuture responseMessageFuture;
051    private ComponentListener<T> componentListener;
052    
053    public FutureResult(ResponseMessageFuture responseMessageFuture, ComponentListener<T> componentListener) {
054        this.responseMessageFuture = responseMessageFuture;
055        this.componentListener = componentListener;
056    }
057
058    @Override
059    public boolean cancel(boolean mayInterruptIfRunning) {
060        return responseMessageFuture.cancel();
061    }
062
063    @Override
064    public boolean isCancelled() {
065        return responseMessageFuture.isCancelled();
066    }
067
068    @Override
069    public boolean isDone() {
070        return responseMessageFuture.isDone();
071    }
072
073        @Override
074    public T get() throws InterruptedException, ExecutionException {
075                try {
076                        responseMessageFuture.get();
077                }
078                catch (TimeoutException e) {
079                        throw new InterruptedException("Request timeout: " + e.getMessage());
080                }
081                return componentListener.getResult();
082    }
083        
084    @Override
085    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
086                throw new UnsupportedOperationException("Cannot use timeout on get(), set timeout on request");
087    }
088
089}