486d0ccc19a5ad8aa272fc6455aae516a5d696c2
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 ONAP
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.onap.ccsdk.sli.core.dblib.factory;
22
23
24 import org.onap.ccsdk.sli.core.dblib.CachedDataSource;
25 import org.onap.ccsdk.sli.core.dblib.CachedDataSourceFactory;
26 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
27 import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration;
28 import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool;
29 import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.sql.SQLException;
34 import java.util.Set;
35 import java.util.concurrent.Callable;
36
37 /**
38  * @version $Revision: 1.6 $
39  * Change Log
40  * Author         Date     Comments
41  * ============== ======== ====================================================
42  * Rich Tabedzki
43  */
44 public abstract class AbstractResourceManagerFactory {
45         private static Logger LOGGER = LoggerFactory.getLogger(AbstractResourceManagerFactory.class);
46
47         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) throws Exception;
48         public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager dbResourceManager, String sourceName) throws SQLException ;
49
50
51         public static AbstractResourceManagerFactory createIntstance() throws FactoryNotDefinedException {
52                 throw new FactoryNotDefinedException("Factory method 'createIntstance' needs to be overriden in DBResourceManagerFactory");
53         }
54
55         public class DBInitTask implements Callable<CachedDataSource>
56         {
57                 private BaseDBConfiguration config = null;
58                 private Set<DBInitTask> activeTasks;
59
60                 public DBInitTask(JDBCConfiguration jdbcconfig, Set<DBInitTask> tasks) {
61                         this.config = jdbcconfig;
62                         this.activeTasks = tasks;
63                 }
64
65                 public CachedDataSource call() throws Exception {
66                         CachedDataSource ds = null;
67                         try {
68                                 ds = CachedDataSourceFactory.createDataSource(config);
69                                 return ds;
70                         } finally {
71                                 synchronized(activeTasks) {
72                                         activeTasks.remove(this);
73                                         if (activeTasks.isEmpty()) {
74                                                 final Runnable closure = new Runnable() {
75
76                                                         public void run() {
77                                                                 try {
78                                                                         Thread.sleep(300);
79                                                                 } catch (Exception e) {
80                                                                 }
81                                                                 synchronized(activeTasks) {
82                                                                         activeTasks.notifyAll();
83                                                                 }
84                                                         }
85                                                 };
86                                                 if (LOGGER.isDebugEnabled()) {
87                                                         LOGGER.debug("Completed CachedDataSource.Call and notifyAll from " + (ds != null ? ds
88                                                                         .getDbConnectionName() : null));
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 }