AbstractResourceManagerFactory Clean Code Hygiene
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / factory / AbstractResourceManagerFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.sli.resource.dblib.factory;
22
23
24 import java.sql.SQLException;
25 import java.util.Set;
26 import java.util.concurrent.Callable;
27
28 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
29 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSourceFactory;
30 import org.openecomp.sdnc.sli.resource.dblib.DBConfigException;
31 import org.openecomp.sdnc.sli.resource.dblib.DBResourceManager;
32 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
33 import org.openecomp.sdnc.sli.resource.dblib.config.DbConfigPool;
34 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * @version $Revision: 1.6 $
40  * Change Log
41  * Author         Date     Comments
42  * ============== ======== ====================================================
43  * Rich Tabedzki
44  */
45 public abstract class AbstractResourceManagerFactory {
46         private static Logger LOGGER = LoggerFactory.getLogger(AbstractResourceManagerFactory.class);
47
48         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) throws Exception;
49         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager dbResourceManager, String sourceName) throws SQLException ;
50
51
52         public static AbstractResourceManagerFactory createIntstance() throws FactoryNotDefinedException {
53                 throw new FactoryNotDefinedException("Factory method 'createIntstance' needs to be overriden in DBResourceManagerFactory");
54         }
55
56         public class DBInitTask implements Callable<CachedDataSource>
57         {
58                 private BaseDBConfiguration config = null;
59                 private Set<DBInitTask> activeTasks;
60
61                 public DBInitTask(JDBCConfiguration jdbcconfig, Set<DBInitTask> tasks) {
62                         this.config = jdbcconfig;
63                         this.activeTasks = tasks;
64                 }
65
66                 public CachedDataSource call() throws Exception {
67                         CachedDataSource ds = null;
68                         try {
69                                 ds = CachedDataSourceFactory.createDataSource(config);
70                                 return ds;
71                         } finally {
72                                 synchronized(activeTasks) {
73                                         activeTasks.remove(this);
74                                         if (activeTasks.isEmpty()) {
75                                                 final Runnable closure = new Runnable() {
76
77                                                         public void run() {
78                                                                 try {
79                                                                         Thread.sleep(300);
80                                                                 } catch (Exception e) {
81                                                                 }
82                                                                 synchronized(activeTasks) {
83                                                                         activeTasks.notifyAll();
84                                                                 }
85                                                         }
86                                                 };
87                                                 if (LOGGER.isDebugEnabled()) {
88                                                         LOGGER.debug("Completed CachedDataSource.Call and notifyAll from " + ds.getDbConnectionName());
89                                                 }
90                                                 Thread worker = new Thread(closure);
91                                                 worker.setDaemon(true);
92                                                 worker.start();
93                                         } else {
94                                                 if (LOGGER.isDebugEnabled()) {
95                                                         if (ds != null) {
96                                                                 LOGGER.debug("Completed CachedDataSource.Call from " + ds.getDbConnectionName());
97                                                         }
98                                                 }
99                                         }
100                                 }
101                         }
102                 }
103         }
104 }