Add the unit test for ExtsysAppTest.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / common / HibernateSession.java
1 /**
2  * Copyright 2017 ZTE Corporation.
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 package org.onap.aai.esr.common;
17
18 import java.io.File;
19 import java.net.URISyntaxException;
20
21 import org.hibernate.SessionFactory;
22 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
23 import org.hibernate.cfg.Configuration;
24 import org.hibernate.service.ServiceRegistry;
25
26 public class HibernateSession {
27   private static File cfgfile = null;
28
29   private static ServiceRegistry serviceRegistry = null;
30   private static Configuration configuration = null;
31   private static SessionFactory sessionFactory = null;
32   private static String resourcePath;
33
34   /**
35    * Get a hibernate sessionFactory.
36    */
37   public static SessionFactory init() {
38     initConfigure();
39     configuration = new Configuration().configure(cfgfile);
40     configuration.setProperty("hibernate.connection.url", "jdbc:h2:tcp://localhost:8205/"
41         + resourcePath + "db/catalog");
42     serviceRegistry =
43         new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
44     sessionFactory = configuration.buildSessionFactory(serviceRegistry);
45     return sessionFactory;
46   }
47
48   private static void initConfigure() {
49     try {
50       resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
51     } catch (URISyntaxException e1) {
52       e1.printStackTrace();
53     }
54     final String filename = "Hibernate.cfg.xml";
55     cfgfile = new File(resourcePath + filename);
56   }
57
58   /**
59    * Destory a hibernate sessionFactory.
60    */
61   public static void destory() {
62     if (sessionFactory != null && !sessionFactory.isClosed()) {
63       sessionFactory.close();
64     }
65   }
66
67   /**
68    * test.
69    * @param args param
70    */
71   public static void main(String[] args) {
72
73   }
74 }