View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.concurrent;
20  
21  import java.util.concurrent.*;
22  
23  /**
24   * Same concept as the {@link SubjectAwareExecutorService} but additionally supports the
25   * {@link ScheduledExecutorService} interface.
26   */
27  public class SubjectAwareScheduledExecutorService extends SubjectAwareExecutorService implements ScheduledExecutorService {
28  
29      private ScheduledExecutorService targetScheduledExecutorService;
30  
31      public SubjectAwareScheduledExecutorService() {
32      }
33  
34      public SubjectAwareScheduledExecutorService(ScheduledExecutorService target) {
35          setTargetScheduledExecutorService(target);
36      }
37  
38      public ScheduledExecutorService getTargetScheduledExecutorService() {
39          return targetScheduledExecutorService;
40      }
41  
42      public void setTargetScheduledExecutorService(ScheduledExecutorService targetScheduledExecutorService) {
43          super.setTargetExecutorService(targetScheduledExecutorService);
44          this.targetScheduledExecutorService = targetScheduledExecutorService;
45      }
46  
47      @Override
48      public void setTargetExecutor(Executor targetExecutor) {
49          if (!(targetExecutor instanceof ScheduledExecutorService)) {
50              String msg = "The " + getClass().getName() + " implementation only accepts " +
51                      ScheduledExecutorService.class.getName() + " target instances.";
52              throw new IllegalArgumentException(msg);
53          }
54          super.setTargetExecutorService((ScheduledExecutorService) targetExecutor);
55      }
56  
57      @Override
58      public void setTargetExecutorService(ExecutorService targetExecutorService) {
59          if (!(targetExecutorService instanceof ScheduledExecutorService)) {
60              String msg = "The " + getClass().getName() + " implementation only accepts " +
61                      ScheduledExecutorService.class.getName() + " target instances.";
62              throw new IllegalArgumentException(msg);
63          }
64          super.setTargetExecutorService(targetExecutorService);
65      }
66  
67      public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
68          Runnable work = associateWithSubject(command);
69          return this.targetScheduledExecutorService.schedule(work, delay, unit);
70      }
71  
72      public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
73          Callable<V> work = associateWithSubject(callable);
74          return this.targetScheduledExecutorService.schedule(work, delay, unit);
75      }
76  
77      public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
78          Runnable work = associateWithSubject(command);
79          return this.targetScheduledExecutorService.scheduleAtFixedRate(work, initialDelay, period, unit);
80      }
81  
82      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
83          Runnable work = associateWithSubject(command);
84          return this.targetScheduledExecutorService.scheduleWithFixedDelay(work, initialDelay, delay, unit);
85      }
86  }