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 int getThreadCount() {
70 public void setThreadCount(int threadCount) {
71 this.threadCount = threadCount;
75 public Closeable getInstance() {
76 assertNotNull(runtimeBeanRegistrator);
77 if (instance == null) {
78 if (oldInstance != null && recreate == false) {
80 instance = oldInstance;
82 if (instance == null) {
83 if (oldCloseable != null) {
86 } catch (Exception e) {
87 throw new RuntimeException(e);
90 // close old threadpool and esp. unregister runtime beans
91 instance = new TestingScheduledThreadPoolImpl(
92 runtimeBeanRegistrator, threadCount);
98 // getters and setters
100 public boolean isRecreate() {
105 public void setRecreate(boolean recreate) {
106 this.recreate = recreate;
110 public ModuleIdentifier getIdentifier() {