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;
13 import javax.annotation.Nullable;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.config.api.ModuleIdentifier;
16 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
17 import org.opendaylight.controller.config.spi.Module;
20 public class TestingFixedThreadPoolModule implements
21 TestingFixedThreadPoolConfigMXBean, Module,
22 TestingThreadPoolConfigMXBean, ModifiableThreadPoolServiceInterface {
23 private final AutoCloseable oldCloseable;
24 private final TestingFixedThreadPool oldInstance;
25 private final ModuleIdentifier name;
26 private TestingFixedThreadPool instance;
27 private int threadCount = 0;
28 private boolean triggerNewInstanceCreation;
30 TestingFixedThreadPoolModule(ModuleIdentifier name,
31 @Nullable AutoCloseable oldCloseable,
32 @Nullable TestingFixedThreadPool oldInstance) {
34 this.oldCloseable = oldCloseable;
35 this.oldInstance = oldInstance;
41 public void setThreadCount(int threadCount) {
42 this.threadCount = threadCount;
46 public int getThreadCount() {
51 public boolean isTriggerNewInstanceCreation() {
52 return triggerNewInstanceCreation;
56 public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
57 this.triggerNewInstanceCreation = triggerNewInstanceCreation;
62 private boolean isReusable() {
63 return oldInstance != null;
67 public void validate() {
68 checkState(threadCount > 0,
69 "Parameter 'threadCount' must be greater than 0");
73 public Closeable getInstance() {
74 if (instance == null) {
75 if (isReusable() && triggerNewInstanceCreation == false) { // simulate
79 // triggerNewInstanceCreation
80 oldInstance.setMaximumNumberOfThreads(threadCount);
81 instance = oldInstance;
83 if (oldCloseable != null) {
86 } catch (Exception e) {
87 throw new RuntimeException(e);
90 instance = new TestingFixedThreadPool(threadCount,
98 public ModuleIdentifier getIdentifier() {