Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / db / connection / ConnectionUtil.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vnfsdk.marketplace.db.connection;
18
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import org.apache.ibatis.io.Resources;
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * This class is the session factory for the database to be used in the Driver Manager.
31  * <br/>
32  * 
33  * @author
34  * @version  
35  */
36 public class ConnectionUtil {
37
38     private static SqlSessionFactory sqlSessionFactory;
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionUtil.class);
41     
42     /**
43      * Get the DB session for the myBaties.    
44      * Constructor<br/>
45      * <p>
46      * </p>
47      * 
48      * @since   
49      */
50     private ConnectionUtil() {
51         
52     }
53
54     static {
55         InputStream inputStream;
56         try {
57             inputStream = Resources.getResourceAsStream("mybatis/configuration/configuration.xml");
58             if(null == sqlSessionFactory) {
59                 LOGGER.error("begin generate");
60                 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
61                 LOGGER.error("end generate = {}" , sqlSessionFactory);
62             }
63         } catch(FileNotFoundException ex) {
64             LOGGER.error("File Not Found Exception caught", ex);
65
66         } catch(IOException ex) {
67             LOGGER.error("IO Exception caught", ex);
68         } catch(Exception ex) {
69             LOGGER.error("some exception", ex);
70         }
71     }
72
73     public static SqlSessionFactory getSession() {
74         return sqlSessionFactory;
75     }
76 }