2 * Copyright 2016-2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.aai.esr.hibernate;
20 import com.google.common.collect.ImmutableList;
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;
32 public class HibernateBundleExt extends HibernateBundle<ExtsysAppConfiguration> {
34 private static final String DEFAULT_NAME = "hibernate";
36 private SessionFactory sessionFactory;
38 private final ImmutableList<Class<?>> entities;
39 private final SessionFactoryFactory sessionFactoryFactory;
41 protected HibernateBundleExt(Class<?> entity, Class<?>... entities) {
42 this(ImmutableList.<Class<?>>builder().add(entity).add(entities).build(),
43 new SessionFactoryFactory());
46 protected HibernateBundleExt(ImmutableList<Class<?>> entities,
47 SessionFactoryFactory sessionFactoryFactory) {
48 super(entities, sessionFactoryFactory);
49 this.entities = entities;
50 this.sessionFactoryFactory = sessionFactoryFactory;
53 public final void initializeExt(Bootstrap<?> bootstrap) {
54 bootstrap.getObjectMapper().registerModule(createHibernate4Module());
58 * Override to configure the {@link Hibernate4Module}.
60 protected Hibernate4Module createHibernate4Module() {
61 return new Hibernate4Module();
64 protected String name() {
70 * @param configuration inventory configuration
71 * @param environment environment
72 * @throws Exception Exception
74 public final void runExt(ExtsysAppConfiguration configuration, Environment environment)
76 final DataSourceFactory dbConfig = getDataSourceFactory(configuration);
78 sessionFactoryFactory.build(this, environment, dbConfig, entities, name());
82 public SessionFactory getSessionFactory() {
83 return sessionFactory;
88 public DataSourceFactory getDataSourceFactory(ExtsysAppConfiguration configuration) {
89 return configuration.getDataSourceFactory();