Introduce lifecycle to runtime beans registrator in config-manager
[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 import javax.annotation.Nullable;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
18 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingScheduledThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.spi.Module;
21
22 /**
23  * This class has two exported interfaces and two runtime beans. Recreation is
24  * triggered by setting Recreate attribute to true.
25  */
26 public class TestingScheduledThreadPoolModule implements Module,
27         TestingScheduledThreadPoolConfigBeanMXBean,
28         RuntimeBeanRegistratorAwareModule,
29         TestingScheduledThreadPoolServiceInterface {
30
31     private final ModuleIdentifier identifier;
32     @Nullable
33     private final AutoCloseable oldCloseable;
34     @Nullable
35     private final TestingScheduledThreadPoolImpl oldInstance;
36
37     private int threadCount = 10;
38     private TestingScheduledThreadPoolImpl instance;
39     private RootRuntimeBeanRegistrator runtimeBeanRegistrator;
40     private boolean recreate;
41
42     public TestingScheduledThreadPoolModule(ModuleIdentifier identifier,
43             @Nullable AutoCloseable oldCloseable,
44             @Nullable TestingScheduledThreadPoolImpl oldInstance) {
45         this.identifier = identifier;
46         this.oldCloseable = oldCloseable;
47         this.oldInstance = oldInstance;
48     }
49
50     @Override
51     public void setRuntimeBeanRegistrator(
52             RootRuntimeBeanRegistrator runtimeBeanRegistrator) {
53         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
54     }
55
56     @Override
57     public void validate() {
58         assertNull(runtimeBeanRegistrator);
59         // check thread count
60         checkState(threadCount > 0,
61                 "Parameter 'ThreadCount' must be greater than 0");
62     }
63
64     @Override
65     public boolean canReuse(final Module oldModule) {
66         return getClass().isInstance(oldModule) && getThreadCount() ==
67                 ((TestingScheduledThreadPoolModule) oldModule).getThreadCount();
68     }
69
70     @Override
71     public int getThreadCount() {
72         return threadCount;
73     }
74
75     @Override
76     public void setThreadCount(int threadCount) {
77         this.threadCount = threadCount;
78     }
79
80     @Override
81     public Closeable getInstance() {
82         assertNotNull(runtimeBeanRegistrator);
83         if (instance == null) {
84             if (oldInstance != null && recreate == false) {
85                 // reuse old instance
86                 instance = oldInstance;
87             }
88             if (instance == null) {
89                 if (oldCloseable != null) {
90                     try {
91                         oldCloseable.close();
92                     } catch (Exception e) {
93                         throw new RuntimeException(e);
94                     }
95                 }
96                 // close old threadpool and esp. unregister runtime beans
97                 instance = new TestingScheduledThreadPoolImpl(
98                         runtimeBeanRegistrator, threadCount);
99             }
100         }
101         return instance;
102     }
103
104     // getters and setters
105     @Override
106     public boolean isRecreate() {
107         return recreate;
108     }
109
110     @Override
111     public void setRecreate(boolean recreate) {
112         this.recreate = recreate;
113     }
114
115     @Override
116     public ModuleIdentifier getIdentifier() {
117         return identifier;
118     }
119
120
121 }