blob: 051155cec803aa443f5750ede4cedfd97e79ec35 [file] [log] [blame]
Thomas Vachuskab0317c62015-04-08 23:58:58 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
18import java.util.Collection;
19import java.util.List;
20import java.util.concurrent.Callable;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.ExecutorService;
23import java.util.concurrent.Future;
24import java.util.concurrent.TimeUnit;
25import java.util.concurrent.TimeoutException;
26
27/**
28 * Executor service wrapper for shared executors with safeguards on shutdown
29 * to prevent inadvertent shutdown.
30 */
31class SharedExecutorService implements ExecutorService {
32
33 private static final String NOT_ALLOWED = "Shutdown of shared executor is not allowed";
34
35 private ExecutorService executor;
36
37 /**
38 * Creates a wrapper for the given executor service.
39 *
40 * @param executor executor service to wrap
41 */
42 SharedExecutorService(ExecutorService executor) {
43 this.executor = executor;
44 }
45
46 /**
47 * Returns the backing executor service.
48 *
49 * @return backing executor service
50 */
51 ExecutorService backingExecutor() {
52 return executor;
53 }
54
55 /**
56 * Swaps the backing executor with a new one and shuts down the old one.
57 *
58 * @param executor new executor service
59 */
60 void setBackingExecutor(ExecutorService executor) {
61 ExecutorService oldExecutor = this.executor;
62 this.executor = executor;
63 oldExecutor.shutdown();
64 }
65
66 @Override
67 public void shutdown() {
68 throw new UnsupportedOperationException(NOT_ALLOWED);
69 }
70
71 @Override
72 public List<Runnable> shutdownNow() {
73 throw new UnsupportedOperationException(NOT_ALLOWED);
74 }
75
76 @Override
77 public boolean isShutdown() {
78 return executor.isShutdown();
79 }
80
81 @Override
82 public boolean isTerminated() {
83 return executor.isTerminated();
84 }
85
86 @Override
87 public boolean awaitTermination(long timeout, TimeUnit unit)
88 throws InterruptedException {
89 return executor.awaitTermination(timeout, unit);
90 }
91
92 @Override
93 public <T> Future<T> submit(Callable<T> task) {
94 return executor.submit(task);
95 }
96
97 @Override
98 public <T> Future<T> submit(Runnable task, T result) {
99 return executor.submit(task, result);
100 }
101
102 @Override
103 public Future<?> submit(Runnable task) {
104 return executor.submit(task);
105 }
106
107 @Override
108 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
109 throws InterruptedException {
110 return executor.invokeAll(tasks);
111 }
112
113 @Override
114 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
115 long timeout, TimeUnit unit)
116 throws InterruptedException {
117 return executor.invokeAll(tasks, timeout, unit);
118 }
119
120 @Override
121 public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
122 throws InterruptedException, ExecutionException {
123 return executor.invokeAny(tasks);
124 }
125
126 @Override
127 public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
128 long timeout, TimeUnit unit)
129 throws InterruptedException, ExecutionException, TimeoutException {
HIGUCHI Yuta1651e982015-09-04 13:45:08 -0700130 return executor.invokeAny(tasks, timeout, unit);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700131 }
132
133 @Override
134 public void execute(Runnable command) {
135 executor.execute(command);
136 }
137
138}