2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdnc.sli.resource.dblib.jdbc;
24 import java.sql.SQLException;
25 import java.util.Arrays;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.FutureTask;
35 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
36 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSourceFactory;
37 import org.openecomp.sdnc.sli.resource.dblib.DBResourceManager;
38 import org.openecomp.sdnc.sli.resource.dblib.DataSourceComparator;
39 import org.openecomp.sdnc.sli.resource.dblib.config.DbConfigPool;
40 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
41 import org.openecomp.sdnc.sli.resource.dblib.factory.AbstractResourceManagerFactory;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * @version $Revision: 1.6 $
48 * Author Date Comments
49 * ============== ======== ====================================================
52 public class JdbcDbResourceManagerFactory extends AbstractResourceManagerFactory {
53 private static Logger LOGGER = LoggerFactory.getLogger(JdbcDbResourceManagerFactory.class );
54 private JdbcDbResourceManagerFactory(){
58 class MyFutureTask extends FutureTask<CachedDataSource>
61 public MyFutureTask(Callable<CachedDataSource> result) {
67 public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager, String sourceName) throws SQLException
69 // here create the data sources objects
70 JDBCConfiguration[] list = dbConfig.getJDBCbSourceArray();
71 CachedDataSource[] cachedDS = new CachedDataSource[1];
73 for(int i=0, max=list.length; i<max; i++){
74 if(!sourceName.equals(list[i].getDbConnectionName()))
77 JDBCConfiguration config = list[i];
78 CachedDataSource dataSource = CachedDataSourceFactory.createDataSource(config);
79 cachedDS[0] = dataSource;
84 public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) /* throws Exception */ {
86 ExecutorService threadExecutor = Executors.newFixedThreadPool(2);
87 // here create the data sources objects
88 JDBCConfiguration[] list = dbConfig.getJDBCbSourceArray();
90 MyFutureTask[] futures = new MyFutureTask[list.length];
91 final Set<DBInitTask> tasks = new HashSet<DBInitTask>();
92 if(LOGGER.isDebugEnabled()) {
93 LOGGER.debug("Creating " + list.length + " datasources.");
96 for(int i=0, max=list.length; i<max; i++){
97 JDBCConfiguration config = list[i];
99 DBInitTask task = new DBInitTask(config, tasks);
101 futures[i] = new MyFutureTask(task);
106 for(int i=0, max=list.length; i<max; i++){
107 if(LOGGER.isDebugEnabled())
108 LOGGER.debug("Starting executor tasks.");
109 threadExecutor.execute(futures[i]);
111 // the timeout param is set is seconds.
112 long timeout = ((dbConfig.getTimeout() <= 0) ? 60L : dbConfig.getTimeout());
113 LOGGER.debug("Timeout set to " +timeout+" seconds");
115 // the timeout param is set is seconds, hence it needs to be multiplied by 1000.
117 if(LOGGER.isDebugEnabled())
118 LOGGER.debug("initDBResourceManager wait completed.");
120 } catch(Exception exc) {
121 LOGGER.error("Failed to initialize JndiCachedDataSource. Reason: ", exc);
124 if(threadExecutor != null){
126 threadExecutor.shutdown();
127 } catch(Exception exc){}
130 CachedDataSource[] cachedDS = new CachedDataSource[futures.length];
132 boolean initialized = false;
133 for(int i=0; i<futures.length; i++){
135 if(futures[i].isDone()){
137 obj = futures[i].get();
138 if(obj instanceof CachedDataSource){
139 cachedDS[i] = (CachedDataSource)obj;
140 initialized |= cachedDS[i].isInitialized();
141 if(cachedDS[i].isInitialized())
142 LOGGER.info("DataSource "+list[i].getDbConnectionName()+" initialized successfully");
144 LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed");
147 LOGGER.warn("DataSource " + i + " initialization failed. Returned object is null");
149 LOGGER.warn("DataSource " + i + " initialization failed. Returned object is " + obj.getClass().getName());
152 } catch (InterruptedException exc) {
153 LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc);
154 } catch (ExecutionException exc) {
155 LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc);
156 } catch (Exception exc) {
157 LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc);
161 obj = futures[i].get();
162 if(obj instanceof CachedDataSource){
163 LOGGER.warn("DataSource "+((CachedDataSource)obj).getDbConnectionName()+" failed");
166 LOGGER.warn("DataSource " + i + " initialization failed. Returned object is null");
168 LOGGER.warn("DataSource " + i + " initialization failed. Returned object is " + obj.getClass().getName());
171 } catch (Exception exc) {
172 LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc);
178 new Error("Failed to initialize DB Library.");
183 public static AbstractResourceManagerFactory createIntstance() {
184 return new JdbcDbResourceManagerFactory();