BUG-8: migrate newMBeanProxy() -> newMXBeanProxy()
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool;
9
10 import com.google.common.collect.Lists;
11 import java.io.Closeable;
12 import java.util.List;
13 import java.util.concurrent.Executor;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.ScheduledThreadPoolExecutor;
16 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
17 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
18 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.runtimebeans.TestingScheduledRuntimeBean;
19 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
20
21 public class TestingScheduledThreadPoolImpl implements TestingThreadPoolIfc,
22         TestingScheduledThreadPoolIfc, Closeable {
23     private static volatile int numberOfCloseMethodCalls = 0;
24     private final ScheduledThreadPoolExecutor executor;
25     private final RootRuntimeBeanRegistrator runtimeBeanRegistrator;
26
27     public static final List<ScheduledThreadPoolExecutor> allExecutors = Lists
28             .newLinkedList();
29
30     public TestingScheduledThreadPoolImpl(
31             RootRuntimeBeanRegistrator runtimeBeanRegistrator, int corePoolSize) {
32         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
33         executor = new ScheduledThreadPoolExecutor(corePoolSize);
34         allExecutors.add(executor);
35         HierarchicalRuntimeBeanRegistration hierarchicalRuntimeBeanRegistration = runtimeBeanRegistrator
36                 .registerRoot(new TestingScheduledRuntimeBean());
37         hierarchicalRuntimeBeanRegistration.register("a", "b",
38                 new TestingScheduledRuntimeBean());
39     }
40
41     @Override
42     public void close() {
43         numberOfCloseMethodCalls++;
44         runtimeBeanRegistrator.close();
45         executor.shutdown();
46     }
47
48     @Override
49     public ScheduledExecutorService getScheduledExecutor() {
50         return executor;
51     }
52
53     @Override
54     public Executor getExecutor() {
55         return executor;
56     }
57
58     @Override
59     public int getMaxNumberOfThreads() {
60         return executor.getCorePoolSize();
61     }
62
63     public static void cleanUp() {
64         for (ScheduledThreadPoolExecutor executor : allExecutors) {
65             executor.shutdown();
66         }
67         allExecutors.clear();
68         numberOfCloseMethodCalls = 0;
69     }
70
71     public static int getNumberOfCloseMethodCalls() {
72         return numberOfCloseMethodCalls;
73     }
74
75 }