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 */
019package org.apache.shiro.concurrent;
020
021import java.util.concurrent.Callable;
022import java.util.concurrent.Executor;
023import java.util.concurrent.ExecutorService;
024import java.util.concurrent.ScheduledExecutorService;
025import java.util.concurrent.ScheduledFuture;
026import java.util.concurrent.TimeUnit;
027
028/**
029 * Same concept as the {@link SubjectAwareExecutorService} but additionally supports the
030 * {@link ScheduledExecutorService} interface.
031 */
032public class SubjectAwareScheduledExecutorService extends SubjectAwareExecutorService implements ScheduledExecutorService {
033
034    private ScheduledExecutorService targetScheduledExecutorService;
035
036    public SubjectAwareScheduledExecutorService() {
037    }
038
039    public SubjectAwareScheduledExecutorService(ScheduledExecutorService target) {
040        setTargetScheduledExecutorService(target);
041    }
042
043    public ScheduledExecutorService getTargetScheduledExecutorService() {
044        return targetScheduledExecutorService;
045    }
046
047    public void setTargetScheduledExecutorService(ScheduledExecutorService targetScheduledExecutorService) {
048        super.setTargetExecutorService(targetScheduledExecutorService);
049        this.targetScheduledExecutorService = targetScheduledExecutorService;
050    }
051
052    @Override
053    public void setTargetExecutor(Executor targetExecutor) {
054        if (!(targetExecutor instanceof ScheduledExecutorService)) {
055            String msg = "The " + getClass().getName() + " implementation only accepts "
056                    + ScheduledExecutorService.class.getName() + " target instances.";
057            throw new IllegalArgumentException(msg);
058        }
059        super.setTargetExecutorService((ScheduledExecutorService) targetExecutor);
060    }
061
062    @Override
063    public void setTargetExecutorService(ExecutorService targetExecutorService) {
064        if (!(targetExecutorService instanceof ScheduledExecutorService)) {
065            String msg = "The " + getClass().getName() + " implementation only accepts "
066                    + ScheduledExecutorService.class.getName() + " target instances.";
067            throw new IllegalArgumentException(msg);
068        }
069        super.setTargetExecutorService(targetExecutorService);
070    }
071
072    public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
073        Runnable work = associateWithSubject(command);
074        return this.targetScheduledExecutorService.schedule(work, delay, unit);
075    }
076
077    public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
078        Callable<V> work = associateWithSubject(callable);
079        return this.targetScheduledExecutorService.schedule(work, delay, unit);
080    }
081
082    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
083        Runnable work = associateWithSubject(command);
084        return this.targetScheduledExecutorService.scheduleAtFixedRate(work, initialDelay, period, unit);
085    }
086
087    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
088        Runnable work = associateWithSubject(command);
089        return this.targetScheduledExecutorService.scheduleWithFixedDelay(work, initialDelay, delay, unit);
090    }
091}