001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.concurrent;
020    
021    import java.util.concurrent.*;
022    
023    /**
024     * Same concept as the {@link SubjectAwareExecutorService} but additionally supports the
025     * {@link ScheduledExecutorService} interface.
026     */
027    public class SubjectAwareScheduledExecutorService extends SubjectAwareExecutorService implements ScheduledExecutorService {
028    
029        private ScheduledExecutorService targetScheduledExecutorService;
030    
031        public SubjectAwareScheduledExecutorService() {
032        }
033    
034        public SubjectAwareScheduledExecutorService(ScheduledExecutorService target) {
035            setTargetScheduledExecutorService(target);
036        }
037    
038        public ScheduledExecutorService getTargetScheduledExecutorService() {
039            return targetScheduledExecutorService;
040        }
041    
042        public void setTargetScheduledExecutorService(ScheduledExecutorService targetScheduledExecutorService) {
043            super.setTargetExecutorService(targetScheduledExecutorService);
044            this.targetScheduledExecutorService = targetScheduledExecutorService;
045        }
046    
047        @Override
048        public void setTargetExecutor(Executor targetExecutor) {
049            if (!(targetExecutor instanceof ScheduledExecutorService)) {
050                String msg = "The " + getClass().getName() + " implementation only accepts " +
051                        ScheduledExecutorService.class.getName() + " target instances.";
052                throw new IllegalArgumentException(msg);
053            }
054            super.setTargetExecutorService((ScheduledExecutorService) targetExecutor);
055        }
056    
057        @Override
058        public void setTargetExecutorService(ExecutorService targetExecutorService) {
059            if (!(targetExecutorService instanceof ScheduledExecutorService)) {
060                String msg = "The " + getClass().getName() + " implementation only accepts " +
061                        ScheduledExecutorService.class.getName() + " target instances.";
062                throw new IllegalArgumentException(msg);
063            }
064            super.setTargetExecutorService(targetExecutorService);
065        }
066    
067        public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
068            Runnable work = associateWithSubject(command);
069            return this.targetScheduledExecutorService.schedule(work, delay, unit);
070        }
071    
072        public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
073            Callable<V> work = associateWithSubject(callable);
074            return this.targetScheduledExecutorService.schedule(work, delay, unit);
075        }
076    
077        public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
078            Runnable work = associateWithSubject(command);
079            return this.targetScheduledExecutorService.scheduleAtFixedRate(work, initialDelay, period, unit);
080        }
081    
082        public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
083            Runnable work = associateWithSubject(command);
084            return this.targetScheduledExecutorService.scheduleWithFixedDelay(work, initialDelay, delay, unit);
085        }
086    }