Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / hibernate / HibernateBundleExt.java
1 /**
2  * Copyright 2016 [ZTE] and others.
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.aai.esr.hibernate;
18
19
20 import com.google.common.collect.ImmutableList;
21
22 import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
23 import io.dropwizard.db.DataSourceFactory;
24 import io.dropwizard.hibernate.HibernateBundle;
25 import io.dropwizard.hibernate.SessionFactoryFactory;
26 import io.dropwizard.setup.Bootstrap;
27 import io.dropwizard.setup.Environment;
28 import org.hibernate.SessionFactory;
29 import org.onap.aai.esr.ExtsysAppConfiguration;
30
31
32 public class HibernateBundleExt extends HibernateBundle<ExtsysAppConfiguration> {
33
34   private static final String DEFAULT_NAME = "hibernate";
35
36   private SessionFactory sessionFactory;
37
38   private final ImmutableList<Class<?>> entities;
39   private final SessionFactoryFactory sessionFactoryFactory;
40
41   protected HibernateBundleExt(Class<?> entity, Class<?>... entities) {
42     this(ImmutableList.<Class<?>>builder().add(entity).add(entities).build(),
43         new SessionFactoryFactory());
44   }
45
46   protected HibernateBundleExt(ImmutableList<Class<?>> entities,
47       SessionFactoryFactory sessionFactoryFactory) {
48     super(entities, sessionFactoryFactory);
49     this.entities = entities;
50     this.sessionFactoryFactory = sessionFactoryFactory;
51   }
52
53   public final void initializeExt(Bootstrap<?> bootstrap) {
54     bootstrap.getObjectMapper().registerModule(createHibernate4Module());
55   }
56
57   /**
58    * Override to configure the {@link Hibernate4Module}.
59    */
60   protected Hibernate4Module createHibernate4Module() {
61     return new Hibernate4Module();
62   }
63
64   protected String name() {
65     return DEFAULT_NAME;
66   }
67
68   /**
69    * ext run function.
70    * @param configuration inventory configuration
71    * @param environment environment
72    * @throws Exception Exception
73    */
74   public final void runExt(ExtsysAppConfiguration configuration, Environment environment)
75       throws Exception {
76     final DataSourceFactory dbConfig = getDataSourceFactory(configuration);
77     this.sessionFactory =
78         sessionFactoryFactory.build(this, environment, dbConfig, entities, name());
79
80   }
81
82   public SessionFactory getSessionFactory() {
83     return sessionFactory;
84   }
85
86
87   @Override
88   public DataSourceFactory getDataSourceFactory(ExtsysAppConfiguration configuration) {
89     return configuration.getDataSourceFactory();
90   }
91
92 }