2 * Copyright (c) 2013, 2017 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, TestingScheduledThreadPoolConfigBeanMXBean,
27 RuntimeBeanRegistratorAwareModule, TestingScheduledThreadPoolServiceInterface {
29 private final ModuleIdentifier identifier;
31 private final AutoCloseable oldCloseable;
33 private final TestingScheduledThreadPoolImpl oldInstance;
35 private int threadCount = 10;
36 private TestingScheduledThreadPoolImpl instance;
37 private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
38 private boolean recreate;
40 public TestingScheduledThreadPoolModule(final ModuleIdentifier identifier,
41 @Nullable final AutoCloseable oldCloseable, @Nullable final TestingScheduledThreadPoolImpl oldInstance) {
42 this.identifier = identifier;
43 this.oldCloseable = oldCloseable;
44 this.oldInstance = oldInstance;
48 public void setRuntimeBeanRegistrator(final RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
49 this.runtimeBeanRegistrator = runtimeBeanRegistrator;
53 public void validate() {
54 assertNull(runtimeBeanRegistrator);
56 checkState(threadCount > 0, "Parameter 'ThreadCount' must be greater than 0");
60 public boolean canReuse(final Module oldModule) {
61 return getClass().isInstance(oldModule)
62 && getThreadCount() == ((TestingScheduledThreadPoolModule) oldModule).getThreadCount();
66 public int getThreadCount() {
71 public void setThreadCount(final int threadCount) {
72 this.threadCount = threadCount;
76 @SuppressWarnings("IllegalCatch")
77 public Closeable getInstance() {
78 assertNotNull(runtimeBeanRegistrator);
79 if (instance == null) {
80 if (oldInstance != null && recreate == false) {
82 instance = oldInstance;
84 if (instance == null) {
85 if (oldCloseable != null) {
88 } catch (final Exception e) {
89 throw new RuntimeException(e);
92 // close old threadpool and esp. unregister runtime beans
93 instance = new TestingScheduledThreadPoolImpl(runtimeBeanRegistrator, threadCount);
99 // getters and setters
101 public boolean isRecreate() {
106 public void setRecreate(final boolean recreate) {
107 this.recreate = recreate;
111 public ModuleIdentifier getIdentifier() {