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.threadpool;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.io.Closeable;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.config.api.ModuleIdentifier;
18 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
19 import org.opendaylight.controller.config.spi.Module;
22 public class TestingFixedThreadPoolModule implements
23 TestingFixedThreadPoolConfigMXBean, Module,
24 TestingThreadPoolConfigMXBean, ModifiableThreadPoolServiceInterface {
25 private final AutoCloseable oldCloseable;
26 private final TestingFixedThreadPool oldInstance;
27 private final ModuleIdentifier name;
28 private TestingFixedThreadPool instance;
29 private int threadCount = 0;
30 private boolean triggerNewInstanceCreation;
32 TestingFixedThreadPoolModule(ModuleIdentifier name,
33 @Nullable AutoCloseable oldCloseable,
34 @Nullable TestingFixedThreadPool oldInstance) {
36 this.oldCloseable = oldCloseable;
37 this.oldInstance = oldInstance;
41 public ModuleIdentifier getName() {
47 public void setThreadCount(int threadCount) {
48 this.threadCount = threadCount;
52 public int getThreadCount() {
57 public boolean isTriggerNewInstanceCreation() {
58 return triggerNewInstanceCreation;
62 public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
63 this.triggerNewInstanceCreation = triggerNewInstanceCreation;
68 private boolean isReusable() {
69 return oldInstance != null;
73 public void validate() {
74 checkState(threadCount > 0,
75 "Parameter 'threadCount' must be greater than 0");
79 public Closeable getInstance() {
80 if (instance == null) {
81 if (isReusable() && triggerNewInstanceCreation == false) { // simulate
85 // triggerNewInstanceCreation
86 oldInstance.setMaximumNumberOfThreads(threadCount);
87 instance = oldInstance;
89 if (oldCloseable != null) {
92 } catch (Exception e) {
93 throw new RuntimeException(e);
96 instance = new TestingFixedThreadPool(threadCount,