2 * Copyright 2016 [ZTE] and others.
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.openo.commontosca.catalog.db.hibernate;
19 import com.google.common.collect.ImmutableList;
21 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;
29 import org.hibernate.SessionFactory;
30 import org.openo.commontosca.catalog.CatalogAppConfiguration;
33 public class HibernateBundleExt extends HibernateBundle<CatalogAppConfiguration> {
35 private static final String DEFAULT_NAME = "hibernate";
37 private SessionFactory sessionFactory;
39 private final ImmutableList<Class<?>> entities;
40 private final SessionFactoryFactory sessionFactoryFactory;
42 protected HibernateBundleExt(Class<?> entity, Class<?>... entities) {
43 this(ImmutableList.<Class<?>>builder().add(entity).add(entities).build(),
44 new SessionFactoryFactory());
47 protected HibernateBundleExt(ImmutableList<Class<?>> entities,
48 SessionFactoryFactory sessionFactoryFactory) {
49 super(entities, sessionFactoryFactory);
50 this.entities = entities;
51 this.sessionFactoryFactory = sessionFactoryFactory;
54 public final void initializeExt(Bootstrap<?> bootstrap) {
55 bootstrap.getObjectMapper().registerModule(createHibernate4Module());
59 * Override to configure the {@link Hibernate4Module}.
61 protected Hibernate4Module createHibernate4Module() {
62 return new Hibernate4Module();
65 protected String name() {
69 public final void runExt(CatalogAppConfiguration configuration, Environment environment)
71 final DataSourceFactory dbConfig = getDataSourceFactory(configuration);
73 sessionFactoryFactory.build(this, environment, dbConfig, entities, name());
77 public SessionFactory getSessionFactory() {
78 return sessionFactory;
83 public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
84 return configuration.getDataSourceFactory();