Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / test / java / org / onap / aai / esr / db / util / HibernateSession.java
1 /**
2  * Copyright 2016 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.db.util;
17
18 import org.hibernate.SessionFactory;
19 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
20 import org.hibernate.cfg.Configuration;
21 import org.hibernate.service.ServiceRegistry;
22
23 import java.io.File;
24 import java.net.URISyntaxException;
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",
41         "jdbc:h2:tcp://localhost:18207/" + resourcePath + "db/extsys");
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 error) {
52       error.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 }