001    /*
002     * Copyright (C) 2009 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.base.Throwables;
021    import com.google.common.collect.ForwardingObject;
022    
023    import java.util.concurrent.ExecutionException;
024    
025    /**
026     * A {@link Service} that forwards all method calls to another service.
027     *
028     * @author Chris Nokleberg
029     * @since 1
030     */
031    @Beta
032    public abstract class ForwardingService extends ForwardingObject
033        implements Service {
034    
035      /** Constructor for use by subclasses. */
036      protected ForwardingService() {}
037    
038      @Override protected abstract Service delegate();
039    
040      @Override public ListenableFuture<State> start() {
041        return delegate().start();
042      }
043    
044      @Override public State state() {
045        return delegate().state();
046      }
047    
048      @Override public ListenableFuture<State> stop() {
049        return delegate().stop();
050      }
051    
052      @Override public State startAndWait() {
053        return delegate().startAndWait();
054      }
055    
056      @Override public State stopAndWait() {
057        return delegate().stopAndWait();
058      }
059    
060      @Override public boolean isRunning() {
061        return delegate().isRunning();
062      }
063    
064      /**
065       * A sensible default implementation of {@link #startAndWait()}, in terms of
066       * {@link #start}. If you override {@link #start}, you may wish to override
067       * {@link #startAndWait()} to forward to this implementation.
068       * @since 9
069       */
070      protected State standardStartAndWait() {
071        try {
072          return Futures.makeUninterruptible(start()).get();
073        } catch (ExecutionException e) {
074          throw Throwables.propagate(e.getCause());
075        }
076      }
077    
078      /**
079       * A sensible default implementation of {@link #stopAndWait()}, in terms of
080       * {@link #stop}. If you override {@link #stop}, you may wish to override
081       * {@link #stopAndWait()} to forward to this implementation.
082       * @since 9
083       */
084      protected State standardStopAndWait() {
085        try {
086          return Futures.makeUninterruptible(stop()).get();
087        } catch (ExecutionException e) {
088          throw Throwables.propagate(e.getCause());
089        }
090      }
091    }