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.jndi;
23 import java.sql.SQLException;
24 import java.util.HashSet;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.FutureTask;
32 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
33 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSourceFactory;
34 import org.openecomp.sdnc.sli.resource.dblib.DBResourceManager;
35 import org.openecomp.sdnc.sli.resource.dblib.config.DbConfigPool;
36 import org.openecomp.sdnc.sli.resource.dblib.config.JndiConfiguration;
37 import org.openecomp.sdnc.sli.resource.dblib.factory.AbstractResourceManagerFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * @version $Revision: 1.6 $
44 * Author Date Comments
45 * ============== ======== ====================================================
48 public class JNDIDbResourceManagerFactory extends AbstractResourceManagerFactory {
50 private static Logger LOGGER = LoggerFactory.getLogger(JNDIDbResourceManagerFactory.class);
52 class MyFutureTask extends FutureTask<DBInitTask>
55 public MyFutureTask(Callable<CachedDataSource> result) {
56 super((Callable)result);
61 public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager, String sourceName) throws SQLException
63 // here create the data sources objects
64 JndiConfiguration[] list = dbConfig.getJndiDbSourceArray();
65 CachedDataSource[] cachedDS = new CachedDataSource[1];
67 for(int i=0, max=list.length; i<max; i++){
68 if(!sourceName.equals(list[i].getJndiConnectionName()))
71 JndiConfiguration config = list[i];
72 CachedDataSource dataSource = CachedDataSourceFactory.createDataSource(config);
73 cachedDS[0] = dataSource;
78 public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) /* throws Exception */ {
79 // WSConfigManagement ws = WSConfigManagement.getInstance();
81 ExecutorService threadExecutor = Executors.newFixedThreadPool(2);
82 // here create the data sources objects
83 JndiConfiguration[] list = dbConfig.getJndiDbSourceArray();
84 FutureTask<DBInitTask>[] futures = new MyFutureTask[list.length];
85 final Set<DBInitTask> tasks = new HashSet<DBInitTask>();
86 if(LOGGER.isDebugEnabled())
87 LOGGER.debug("Creating datasources.");
88 for(int i=0, max=list.length; i<max; i++){
89 JndiConfiguration config = list[i];
90 // if(manager.getJndiContextFactoryStr()!=null && manager.getJndiContextFactoryStr().trim().length()>0){
91 // config.setJndiContextFactory(manager.getJndiContextFactoryStr());
93 // if(manager.getJndiURLStr()!=null && manager.getJndiURLStr().trim().length()>0){
94 // config.setJndiURL(manager.getJndiURLStr());
96 DBInitTask task = new DBInitTask(config, tasks);
98 futures[i] = new MyFutureTask(task);
103 for(int i=0, max=list.length; i<max; i++){
104 threadExecutor.execute(futures[i]);
106 // the timeout param is set is seconds.
107 long timeout = ((dbConfig.getTimeout() <= 0) ? 60L : dbConfig.getTimeout());
109 // the timeout param is set is seconds, hence it needs to be multiplied by 1000.
111 if(LOGGER.isDebugEnabled())
112 LOGGER.debug("initDBResourceManager wait completed.");
114 } catch(Exception exc) {
115 LOGGER.error("Failed to initialize JndiCachedDataSource. Reason: ", exc);
118 if(threadExecutor != null){
120 threadExecutor.shutdown();
121 } catch(Exception exc){}
124 CachedDataSource[] cachedDS = new CachedDataSource[futures.length];
126 boolean initialized = false;
127 for(int i=0; i<futures.length; i++){
129 if(futures[i].isDone()){
131 obj = futures[i].get();
132 if(obj instanceof CachedDataSource){
133 cachedDS[i] = (CachedDataSource)obj;
135 LOGGER.info("DataSource "+list[i].getJndiConnectionName()+" initialized successfully");
137 } catch (InterruptedException exc) {
138 LOGGER.error("DataSource "+list[i].getJndiConnectionName()+" initialization failed", exc);
139 } catch (ExecutionException exc) {
140 LOGGER.error("DataSource "+list[i].getJndiConnectionName()+" initialization failed", exc);
141 } catch (Exception exc) {
142 LOGGER.error("DataSource "+list[i].getJndiConnectionName()+" initialization failed", exc);
146 obj = futures[i].get();
147 if(obj instanceof CachedDataSource){
149 LOGGER.error("DataSource "+((CachedDataSource)obj).getDbConnectionName()+" failed");
151 } catch (Exception exc) {
152 LOGGER.error("DataSource "+list[i].getJndiConnectionName()+" initialization failed", exc);
158 new Error("Failed to initialize DB Library.");
163 public static AbstractResourceManagerFactory createIntstance() {
164 return new JNDIDbResourceManagerFactory();