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;
43 public void setThreadCount(int threadCount) {
44 this.threadCount = threadCount;
48 public int getThreadCount() {
53 public boolean isTriggerNewInstanceCreation() {
54 return triggerNewInstanceCreation;
58 public void setTriggerNewInstanceCreation(boolean triggerNewInstanceCreation) {
59 this.triggerNewInstanceCreation = triggerNewInstanceCreation;
64 private boolean isReusable() {
65 return oldInstance != null;
69 public void validate() {
70 checkState(threadCount > 0,
71 "Parameter 'threadCount' must be greater than 0");
75 public Closeable getInstance() {
76 if (instance == null) {
77 if (isReusable() && triggerNewInstanceCreation == false) { // simulate
81 // triggerNewInstanceCreation
82 oldInstance.setMaximumNumberOfThreads(threadCount);
83 instance = oldInstance;
85 if (oldCloseable != null) {
88 } catch (Exception e) {
89 throw new RuntimeException(e);
92 instance = new TestingFixedThreadPool(threadCount,
100 public ModuleIdentifier getIdentifier() {