43b9333ea48f5b687bc6ec8d455a597becd07962
[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 io.dropwizard.ConfiguredBundle;
20 import io.dropwizard.setup.Bootstrap;
21 import io.dropwizard.setup.Environment;
22
23 import org.openo.commontosca.catalog.CatalogAppConfiguration;
24 import org.openo.commontosca.catalog.db.dao.DaoManager;
25 import org.openo.commontosca.catalog.db.entity.NodeTemplateData;
26 import org.openo.commontosca.catalog.db.entity.PackageData;
27 import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;
28 import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class HibernateBundleAgent implements ConfiguredBundle<CatalogAppConfiguration> {
33
34   private final HibernateBundleExt bundle = new HibernateBundleExt(ServiceTemplateData.class,
35       PackageData.class, NodeTemplateData.class, ServiceTemplateMappingData.class);
36   private static final Logger LOGGER = LoggerFactory.getLogger(HibernateBundleAgent.class);
37
38   @Override
39   public void run(final CatalogAppConfiguration configuration, final Environment environment)
40       throws Exception {
41     Thread thread = new Thread(new Runnable() {
42       int retry = 0;
43       boolean flag = true;
44
45       public void run() {
46         while (retry < 1000) {
47           LOGGER.info("init hibernateBundle.retry:" + retry);
48           retry++;
49           try {
50             bundle.runExt(configuration, environment);
51           } catch (Exception e1) {
52             flag = false;
53             LOGGER.warn(
54                 "init hibernateBundle failed, sleep 15S and try again.errorMsg:" + e1.getMessage());
55             threadSleep(15000);
56           }
57           if (flag) {
58             LOGGER.info("init hibernateBundle success!");
59             initDao();
60             break;
61           }
62         }
63       }
64     });
65     thread.setName("init hibernateBundle");
66     thread.start();
67   }
68
69   private void initDao() {
70     DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
71   }
72
73   private void threadSleep(int second) {
74     LOGGER.info("start sleep ....");
75     try {
76       Thread.sleep(second);
77     } catch (InterruptedException error) {
78       LOGGER.error("thread sleep error.errorMsg:" + error.getMessage());
79     }
80     LOGGER.info("sleep end .");
81   }
82
83   @Override
84   public void initialize(Bootstrap<?> bootstrap) {
85     bundle.initializeExt(bootstrap);
86   }
87   //
88   // public SessionFactory getSessionFactory() {
89   // return bundle.getSessionFactory();
90   // }
91 }