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.SQLFeatureNotSupportedException;
24 import java.util.Properties;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
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;
39 * @version $Revision: 1.2 $
41 * Author Date Comments
42 * ============== ======== ====================================================
45 public class JndiCachedDataSource extends CachedDataSource
47 private static Logger LOGGER = LoggerFactory.getLogger(JndiCachedDataSource.class);
51 * @throws SAXException
52 * @throws ScpTblUpdateError
54 public JndiCachedDataSource(BaseDBConfiguration xmlElem) throws DBConfigException
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();
65 if(jdbcConfig.getConnTimeout() > 0){
66 this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
68 if(jdbcConfig.getRequestTimeout() > 0){
69 this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
72 super.setDbConnectionName(jdbcConfig.getJndiConnectionName());
74 if(jndiContextFactoryStr == null || jndiContextFactoryStr.length() == 0)
76 // throw new DBConfigException("The jndi configuration is incomplete: jndiContextFactory");
78 if(jndiURLStr == null || jndiContextFactoryStr.length() == 0)
80 // throw new ScpTblUpdateError("The jndi configuration is incomplete: jndiURL");
82 if(jndiSourceStr == null || jndiSourceStr.length() == 0)
84 throw new DBConfigException("The jndi configuration is incomplete: jndiSource");
87 Properties env = new Properties();
91 if(jndiContextFactoryStr != null && jndiContextFactoryStr.length() != 0){
92 env.put(Context.INITIAL_CONTEXT_FACTORY, jndiContextFactoryStr);
93 ctx = new InitialContext(env);
95 ctx = new InitialContext();
97 ds = (javax.sql.DataSource) ctx.lookup (jndiSourceStr);
100 this.initialized = false;
101 LOGGER.error("AS_CONF_ERROR: Failed to initialize DataSource <"+getDbConnectionName()+"> using JNDI <"+jndiSourceStr+">");
104 this.initialized = true;
105 LOGGER.info("JndiCachedDataSource <"+getDbConnectionName()+"> configured successfully.");
108 } catch (NamingException exc) {
109 this.initialized = false;
110 LOGGER.error("AS_CONF_ERROR" + exc.getMessage());
112 } catch(Throwable exc) {
113 this.initialized = false;
114 LOGGER.error("AS_CONF_ERROR: " + exc.getMessage());
118 public static JndiCachedDataSource createInstance(BaseDBConfiguration config) {
119 return new JndiCachedDataSource(config);
122 public String toString(){
123 return getDbConnectionName();
126 public java.util.logging.Logger getParentLogger()
127 throws SQLFeatureNotSupportedException {
128 // TODO Auto-generated method stub