Merge "Added Junits for POLICY-SDK-APP"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / conf / HibernateSession.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.onap.policy.conf;
22
23 /*
24  * 
25  * 
26  * */
27 import java.util.Properties;
28
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.cfg.Configuration;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.controller.PolicyController;
35 import org.onap.policy.rest.jpa.SystemLogDB;
36
37 @SuppressWarnings("deprecation")
38 public class HibernateSession{
39
40         private static final Logger LOGGER      = FlexLogger.getLogger(HibernateSession.class);
41         
42         private static SessionFactory logSessionFactory;
43         
44         static {
45                 try {
46                         Properties prop= new Properties();
47                         prop.setProperty("hibernate.connection.url", PolicyController.getLogdbUrl());
48                         prop.setProperty("hibernate.connection.username", PolicyController.getLogdbUserName());
49                         prop.setProperty("hibernate.connection.password", PolicyController.getLogdbPassword());
50                         prop.setProperty("dialect", PolicyController.getLogdbDialect());
51                         prop.setProperty("hibernate.connection.driver_class", PolicyController.getLogdbDriver());       
52                         prop.setProperty("show_sql", "false");  
53                         logSessionFactory = new Configuration().addPackage("org.onap.policy.*").addProperties(prop)
54                                    .addAnnotatedClass(SystemLogDB.class).buildSessionFactory();
55                 } catch (Exception ex) {
56                         LOGGER.error("Exception Occured while creating Log database Hibernate session"+ex);
57                 }
58         }
59         public static Session getSession(){
60                 return logSessionFactory.openSession();
61         }
62         
63         public static void setSession(SessionFactory logSessionFactory1){
64                 logSessionFactory = logSessionFactory1;
65         }
66
67         private HibernateSession(){
68         }
69 }