Merge "Update .gitreview with onap URL"
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / jndi / JndiCachedDataSource.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.jndi;
22
23 import java.sql.SQLFeatureNotSupportedException;
24 import java.util.Properties;
25
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29
30 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
31 import org.openecomp.sdnc.sli.resource.dblib.DBConfigException;
32 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
33 import org.openecomp.sdnc.sli.resource.dblib.config.JndiConfiguration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.xml.sax.SAXException;
37
38 /**
39  * @version $Revision: 1.2 $
40  * Change Log
41  * Author         Date     Comments
42  * ============== ======== ====================================================
43  * Rich Tabedzki
44  */
45 public class JndiCachedDataSource extends CachedDataSource
46 {
47         private static Logger LOGGER = LoggerFactory.getLogger(JndiCachedDataSource.class);
48         /**
49          * @param alarmLog 
50          * @param jdbcElem
51          * @throws SAXException 
52          * @throws ScpTblUpdateError 
53          */
54         public JndiCachedDataSource(BaseDBConfiguration xmlElem) throws DBConfigException
55         {
56                 super(xmlElem);
57         }
58
59         protected void configure(BaseDBConfiguration xmlElem) throws DBConfigException {
60                 JndiConfiguration jdbcConfig = (JndiConfiguration)xmlElem;
61         String jndiContextFactoryStr = jdbcConfig.getJndiContextFactory(); 
62         String jndiURLStr = jdbcConfig.getJndiURL();
63         String jndiSourceStr = jdbcConfig.getJndiSource();
64         
65         if(jdbcConfig.getConnTimeout() > 0){
66                 this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
67         }
68                 if(jdbcConfig.getRequestTimeout() > 0){
69                         this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
70                 }
71         
72         super.setDbConnectionName(jdbcConfig.getJndiConnectionName());
73         
74         if(jndiContextFactoryStr == null || jndiContextFactoryStr.length() == 0)
75         {
76 //              throw new DBConfigException("The jndi configuration is incomplete: jndiContextFactory");
77         }
78         if(jndiURLStr == null || jndiContextFactoryStr.length() == 0)
79         {
80 //              throw new ScpTblUpdateError("The jndi configuration is incomplete: jndiURL");
81         }
82         if(jndiSourceStr == null || jndiSourceStr.length() == 0)
83         {
84                 throw new DBConfigException("The jndi configuration is incomplete: jndiSource");
85         }
86
87         Properties env = new Properties();
88         Context ctx; 
89                 try
90                 {
91                         if(jndiContextFactoryStr != null && jndiContextFactoryStr.length() != 0){
92                                 env.put(Context.INITIAL_CONTEXT_FACTORY, jndiContextFactoryStr);
93                                 ctx = new InitialContext(env);
94                         } else {
95                                 ctx = new InitialContext();
96                         }
97                         ds = (javax.sql.DataSource) ctx.lookup (jndiSourceStr);
98                         if(ds == null)
99                         {
100                                 this.initialized = false;
101                                 LOGGER.error("AS_CONF_ERROR: Failed to initialize DataSource <"+getDbConnectionName()+"> using JNDI <"+jndiSourceStr+">");
102                                 return;
103                         } else {
104                                 this.initialized = true;
105                                 LOGGER.info("JndiCachedDataSource <"+getDbConnectionName()+"> configured successfully.");
106                                 return;
107                         }
108                 } catch (NamingException exc) {
109                         this.initialized = false;
110                         LOGGER.error("AS_CONF_ERROR" + exc.getMessage());
111
112                 } catch(Throwable exc) {
113                         this.initialized = false;
114                         LOGGER.error("AS_CONF_ERROR: " + exc.getMessage());
115                 }
116     }
117
118         public static JndiCachedDataSource createInstance(BaseDBConfiguration config) {
119                 return new JndiCachedDataSource(config);
120         }
121         
122         public String toString(){
123                 return getDbConnectionName();
124         }
125
126         public java.util.logging.Logger getParentLogger()
127                         throws SQLFeatureNotSupportedException {
128                 // TODO Auto-generated method stub
129                 return null;
130         }
131 }