20759491b88ab9a7d10b42cda4a5cd73eb59bd39
[vfc/nfvo/catalog.git] /
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.openo.commontosca.catalog.db.hibernate;
18
19 import com.google.common.collect.ImmutableList;
20
21 import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
22
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
29 import org.hibernate.SessionFactory;
30 import org.openo.commontosca.catalog.CatalogAppConfiguration;
31
32
33 public class HibernateBundleExt extends HibernateBundle<CatalogAppConfiguration> {
34
35   private static final String DEFAULT_NAME = "hibernate";
36
37   private SessionFactory sessionFactory;
38
39   private final ImmutableList<Class<?>> entities;
40   private final SessionFactoryFactory sessionFactoryFactory;
41
42   protected HibernateBundleExt(Class<?> entity, Class<?>... entities) {
43     this(ImmutableList.<Class<?>>builder().add(entity).add(entities).build(),
44         new SessionFactoryFactory());
45   }
46
47   protected HibernateBundleExt(ImmutableList<Class<?>> entities,
48       SessionFactoryFactory sessionFactoryFactory) {
49     super(entities, sessionFactoryFactory);
50     this.entities = entities;
51     this.sessionFactoryFactory = sessionFactoryFactory;
52   }
53
54   public final void initializeExt(Bootstrap<?> bootstrap) {
55     bootstrap.getObjectMapper().registerModule(createHibernate4Module());
56   }
57
58   /**
59    * Override to configure the {@link Hibernate4Module}.
60    */
61   protected Hibernate4Module createHibernate4Module() {
62     return new Hibernate4Module();
63   }
64
65   protected String name() {
66     return DEFAULT_NAME;
67   }
68
69   public final void runExt(CatalogAppConfiguration configuration, Environment environment)
70       throws Exception {
71     final DataSourceFactory dbConfig = getDataSourceFactory(configuration);
72     this.sessionFactory =
73         sessionFactoryFactory.build(this, environment, dbConfig, entities, name());
74
75   }
76
77   public SessionFactory getSessionFactory() {
78     return sessionFactory;
79   }
80
81
82   @Override
83   public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
84     return configuration.getDataSourceFactory();
85   }
86
87 }