2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool;
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
14 import java.io.Closeable;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
18 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.spi.Module;
23 * This class has two exported interfaces and two runtime beans. Recreation is
24 * triggered by setting Recreate attribute to true.
26 public class TestingScheduledThreadPoolModule implements Module,
27 TestingScheduledThreadPoolConfigBeanMXBean,
28 RuntimeBeanRegistratorAwareModule,
29 TestingScheduledThreadPoolServiceInterface {
31 private final ModuleIdentifier identifier;
33 private final AutoCloseable oldCloseable;
35 private final TestingScheduledThreadPoolImpl oldInstance;
37 private int threadCount = 10;
38 private TestingScheduledThreadPoolImpl instance;
39 private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
40 private boolean recreate;
42 public TestingScheduledThreadPoolModule(ModuleIdentifier identifier,
43 @Nullable AutoCloseable oldCloseable,
44 @Nullable TestingScheduledThreadPoolImpl oldInstance) {
45 this.identifier = identifier;
46 this.oldCloseable = oldCloseable;
47 this.oldInstance = oldInstance;
51 public void setRuntimeBeanRegistrator(
52 RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
53 this.runtimeBeanRegistrator = runtimeBeanRegistrator;
57 public void validate() {
58 assertNull(runtimeBeanRegistrator);
60 checkState(threadCount > 0,
61 "Parameter 'ThreadCount' must be greater than 0");
65 public boolean canReuse(final Module oldModule) {
66 return getClass().isInstance(oldModule) && getThreadCount() ==
67 ((TestingScheduledThreadPoolModule) oldModule).getThreadCount();
71 public int getThreadCount() {
76 public void setThreadCount(int threadCount) {
77 this.threadCount = threadCount;
81 public Closeable getInstance() {
82 assertNotNull(runtimeBeanRegistrator);
83 if (instance == null) {
84 if (oldInstance != null && recreate == false) {
86 instance = oldInstance;
88 if (instance == null) {
89 if (oldCloseable != null) {
92 } catch (Exception e) {
93 throw new RuntimeException(e);
96 // close old threadpool and esp. unregister runtime beans
97 instance = new TestingScheduledThreadPoolImpl(
98 runtimeBeanRegistrator, threadCount);
104 // getters and setters
106 public boolean isRecreate() {
111 public void setRecreate(boolean recreate) {
112 this.recreate = recreate;
116 public ModuleIdentifier getIdentifier() {