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;
16 import javax.annotation.Nullable;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
20 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
21 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
22 import org.opendaylight.controller.config.spi.Module;
25 * This class has two exported interfaces and two runtime beans. Recreation is
26 * triggered by setting Recreate attribute to true.
28 public class TestingScheduledThreadPoolModule implements Module,
29 TestingScheduledThreadPoolConfigBeanMXBean,
30 RuntimeBeanRegistratorAwareModule,
31 TestingScheduledThreadPoolServiceInterface {
33 private final ModuleIdentifier identifier;
35 private final AutoCloseable oldCloseable;
37 private final TestingScheduledThreadPoolImpl oldInstance;
39 private int threadCount = 10;
40 private TestingScheduledThreadPoolImpl instance;
41 private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
42 private boolean recreate;
44 public TestingScheduledThreadPoolModule(ModuleIdentifier identifier,
45 @Nullable AutoCloseable oldCloseable,
46 @Nullable TestingScheduledThreadPoolImpl oldInstance) {
47 this.identifier = identifier;
48 this.oldCloseable = oldCloseable;
49 this.oldInstance = oldInstance;
53 public void setRuntimeBeanRegistrator(
54 RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
55 this.runtimeBeanRegistrator = runtimeBeanRegistrator;
59 public void validate() {
60 assertNull(runtimeBeanRegistrator);
62 checkState(threadCount > 0,
63 "Parameter 'ThreadCount' must be greater than 0");
67 public int getThreadCount() {
72 public void setThreadCount(int threadCount) {
73 this.threadCount = threadCount;
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 (Exception e) {
89 throw new RuntimeException(e);
92 // close old threadpool and esp. unregister runtime beans
93 instance = new TestingScheduledThreadPoolImpl(
94 runtimeBeanRegistrator, threadCount);
100 // getters and setters
102 public boolean isRecreate() {
107 public void setRecreate(boolean recreate) {
108 this.recreate = recreate;
112 public ModuleIdentifier getIdentifier() {