Remove raw references to Map in XSQL
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / TestingScheduledThreadPoolModule.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.io.Closeable;
15
16 import javax.annotation.Nullable;
17
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;
23
24 /**
25  * This class has two exported interfaces and two runtime beans. Recreation is
26  * triggered by setting Recreate attribute to true.
27  */
28 public class TestingScheduledThreadPoolModule implements Module,
29         TestingScheduledThreadPoolConfigBeanMXBean,
30         RuntimeBeanRegistratorAwareModule,
31         TestingScheduledThreadPoolServiceInterface {
32
33     private final ModuleIdentifier identifier;
34     @Nullable
35     private final AutoCloseable oldCloseable;
36     @Nullable
37     private final TestingScheduledThreadPoolImpl oldInstance;
38
39     private int threadCount = 10;
40     private TestingScheduledThreadPoolImpl instance;
41     private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
42     private boolean recreate;
43
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;
50     }
51
52     @Override
53     public void setRuntimeBeanRegistrator(
54             RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
55         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
56     }
57
58     @Override
59     public void validate() {
60         assertNull(runtimeBeanRegistrator);
61         // check thread count
62         checkState(threadCount > 0,
63                 "Parameter 'ThreadCount' must be greater than 0");
64     }
65
66     @Override
67     public int getThreadCount() {
68         return threadCount;
69     }
70
71     @Override
72     public void setThreadCount(int threadCount) {
73         this.threadCount = threadCount;
74     }
75
76     @Override
77     public Closeable getInstance() {
78         assertNotNull(runtimeBeanRegistrator);
79         if (instance == null) {
80             if (oldInstance != null && recreate == false) {
81                 // reuse old instance
82                 instance = oldInstance;
83             }
84             if (instance == null) {
85                 if (oldCloseable != null) {
86                     try {
87                         oldCloseable.close();
88                     } catch (Exception e) {
89                         throw new RuntimeException(e);
90                     }
91                 }
92                 // close old threadpool and esp. unregister runtime beans
93                 instance = new TestingScheduledThreadPoolImpl(
94                         runtimeBeanRegistrator, threadCount);
95             }
96         }
97         return instance;
98     }
99
100     // getters and setters
101     @Override
102     public boolean isRecreate() {
103         return recreate;
104     }
105
106     @Override
107     public void setRecreate(boolean recreate) {
108         this.recreate = recreate;
109     }
110
111     @Override
112     public ModuleIdentifier getIdentifier() {
113         return identifier;
114     }
115
116
117 }