Initial commit for OpenECOMP SDN-C N-C core
[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.openecomp.sdnc.sli.resource.dblib.config.JndiConfiguration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * @version $Revision: 1.6 $
41  * Change Log
42  * Author         Date     Comments
43  * ============== ======== ====================================================
44  * Rich Tabedzki
45  */
46 public abstract class AbstractResourceManagerFactory {
47         private static Logger LOGGER = LoggerFactory.getLogger(AbstractResourceManagerFactory.class);
48
49         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) throws Exception;
50         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager dbResourceManager, String sourceName) throws SQLException ;
51
52         
53         public static AbstractResourceManagerFactory createIntstance() throws FactoryNotDefinedException {
54                 throw new FactoryNotDefinedException("Factory method 'createIntstance' needs to be overriden in DBResourceManagerFactory");
55         }
56
57         public class DBInitTask implements Callable<CachedDataSource> 
58         {
59                 private BaseDBConfiguration config = null;
60                 private Set<DBInitTask> activeTasks;
61                 
62                 public DBInitTask(JndiConfiguration jndiConfig, Set<DBInitTask> tasks){
63                         this.config = jndiConfig;
64                         this.activeTasks = tasks;
65                 }
66
67                 public DBInitTask(JDBCConfiguration jdbcconfig, Set<DBInitTask> tasks) {
68                         this.config = jdbcconfig;
69                         this.activeTasks = tasks;
70                 }
71
72                 public CachedDataSource call() throws Exception {
73                         CachedDataSource ds = null;
74                         try {
75                                 ds = CachedDataSourceFactory.createDataSource(config);
76                                 return ds;
77                         } finally {
78                                 synchronized(activeTasks){
79                                         activeTasks.remove(this);
80                                         if(activeTasks.isEmpty()){
81                                                 final Runnable closure = new Runnable() {
82
83                                                         public void run() {
84                                                                 try {
85                                                                         Thread.sleep(300);
86                                                                 } catch (Exception e) {
87                                                                 }
88                                                                 synchronized(activeTasks){
89                                                                         activeTasks.notifyAll();
90                                                                 }
91                                                         }
92                                                 };
93                                                 if(LOGGER.isDebugEnabled())
94                                                         LOGGER.debug("Completed CachedDataSource.Call and notifyAll from "+ds.getDbConnectionName());
95                                                 Thread worker = new Thread(closure);
96                                                 worker.setDaemon(true);
97                                                 worker.start();
98                                         } else {
99                                                 if(LOGGER.isDebugEnabled())
100                                                         LOGGER.debug("Completed CachedDataSource.Call from "+ds.getDbConnectionName());
101                                         }
102                                 }
103                         }
104                 }
105         }
106 }