Change the package name from openo to onap
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / DbDaoUtil.java
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/DbDaoUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/DbDaoUtil.java
new file mode 100644 (file)
index 0000000..76e11ba
--- /dev/null
@@ -0,0 +1,92 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.onap.holmes.common.utils;\r
+\r
+import io.dropwizard.db.DataSourceFactory;\r
+import io.dropwizard.jdbi.DBIFactory;\r
+import io.dropwizard.setup.Environment;\r
+import javax.annotation.PostConstruct;\r
+import javax.inject.Inject;\r
+import javax.inject.Singleton;\r
+import lombok.extern.slf4j.Slf4j;\r
+import org.jvnet.hk2.annotations.Service;\r
+import org.skife.jdbi.v2.DBI;\r
+import org.skife.jdbi.v2.Handle;\r
+\r
+@Singleton\r
+@Service\r
+@Slf4j\r
+public class DbDaoUtil {\r
+\r
+    private DBI jdbi;\r
+    @Inject\r
+    private Environment environmentProvider;\r
+    @Inject\r
+    private DataSourceFactory dataSourceFactoryProvider;\r
+\r
+    private DBIFactory factory = new DBIFactory();\r
+\r
+    @PostConstruct\r
+    public void init() {\r
+        if (jdbi == null) {\r
+            synchronized (DbDaoUtil.class) {\r
+                if (jdbi == null) {\r
+                    jdbi = factory.build(environmentProvider, dataSourceFactoryProvider, "mysql");\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    public <K> K getDao(Class<K> clazz) {\r
+        try {\r
+            return jdbi.open(clazz);\r
+        } catch (Exception e) {\r
+            log.warn("get object instance of Dao error.", e);\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public Handle getHandle() {\r
+        try {\r
+            return jdbi.open();\r
+        } catch (Exception e) {\r
+            log.warn("get object instance of Dao error.", e);\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public void close(Object obj) {\r
+        if (obj != null) {\r
+            try {\r
+                jdbi.close(obj);\r
+            } catch (Exception e) {\r
+                log.warn("close jdbi connection error.", e);\r
+            }\r
+        }\r
+    }\r
+\r
+    public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {\r
+\r
+        return jdbi.onDemand(daoClazz);\r
+\r
+    }\r
+\r
+    public <T> T getJdbiDaoByOpen(Class<T> daoClazz) {\r
+\r
+        return jdbi.open(daoClazz);\r
+\r
+    }\r
+}\r