fcd90aa10c8974d8e2ae916f363cc2aa70a00f4f
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / utils / DbDaoUtil.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.openo.holmes.common.utils;\r
17 \r
18 import io.dropwizard.db.DataSourceFactory;\r
19 import io.dropwizard.jdbi.DBIFactory;\r
20 import io.dropwizard.setup.Environment;\r
21 import javax.inject.Inject;\r
22 import javax.inject.Singleton;\r
23 import lombok.extern.slf4j.Slf4j;\r
24 import org.glassfish.hk2.api.IterableProvider;\r
25 import org.jvnet.hk2.annotations.Service;\r
26 import org.skife.jdbi.v2.DBI;\r
27 import org.skife.jdbi.v2.Handle;\r
28 \r
29 @Singleton\r
30 @Service\r
31 @Slf4j\r
32 public class DbDaoUtil {\r
33 \r
34     private static DBI jdbi;\r
35 \r
36     @Inject\r
37     private static IterableProvider<Environment> environmentProvider;\r
38 \r
39     @Inject\r
40     private static IterableProvider<DataSourceFactory> dataSourceFactoryProvider;\r
41 \r
42     static {\r
43         if (jdbi == null) {\r
44             synchronized (DbDaoUtil.class) {\r
45                 if (jdbi == null) {\r
46                     final DBIFactory factory = new DBIFactory();\r
47                     jdbi = factory\r
48                         .build(environmentProvider.get(), dataSourceFactoryProvider.get(), "mysql");\r
49                 }\r
50             }\r
51         }\r
52     }\r
53 \r
54     public <K> K getDao(Class<K> clazz) {\r
55         try {\r
56             return jdbi.open(clazz);\r
57         } catch (Exception e) {\r
58             log.warn("get object instance of Dao error.", e);\r
59         }\r
60         return null;\r
61     }\r
62 \r
63     public Handle getHandle() {\r
64         try {\r
65             return jdbi.open();\r
66         } catch (Exception e) {\r
67             log.warn("get object instance of Dao error.", e);\r
68         }\r
69         return null;\r
70     }\r
71 \r
72     public void close(Object obj) {\r
73         if (obj != null) {\r
74             try {\r
75                 jdbi.close(obj);\r
76             } catch (Exception e) {\r
77                 log.warn("close jdbi connection error.", e);\r
78             }\r
79         }\r
80     }\r
81 \r
82     public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {\r
83 \r
84         return jdbi.onDemand(daoClazz);\r
85 \r
86     }\r
87 \r
88     public <T> T getJdbiDaoByOpen(Class<T> daoClazz) {\r
89 \r
90         return jdbi.open(daoClazz);\r
91 \r
92     }\r
93 }\r