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 final 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 Closeable getInstance() {
73 assertNotNull(runtimeBeanRegistrator);
74 if (instance == null) {
75 if (oldInstance != null && recreate == false) {
77 instance = oldInstance;
79 if (instance == null) {
80 if (oldCloseable != null) {
83 } catch (Exception e) {
84 throw new RuntimeException(e);
87 // close old threadpool and esp. unregister runtime beans
88 instance = new TestingScheduledThreadPoolImpl(
89 runtimeBeanRegistrator, threadCount);
95 // getters and setters
97 public boolean isRecreate() {
102 public void setRecreate(boolean recreate) {
103 this.recreate = recreate;
107 public ModuleIdentifier getIdentifier() {