package org.onap.appc.design.dbervices;
-import java.sql.ResultSet;
-import java.util.ArrayList;
+import static com.google.common.collect.Lists.newArrayList;
-import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
+import java.sql.ResultSet;
+import java.util.List;
import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
+import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
private static final Logger Log = LoggerFactory.getLogger(DbService.class);
private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
- DbLibService dblibSvc = null;
- String errorMsg = null;
+ private DbLibService dbLibSvc = null;
- public DbService() throws Exception {
- DbLibService dblibSvc = null;
+ public DbService() throws DBException {
Log.info("Initializing DbService service");
try
{
- dblibSvc = getDbLibService();
- if (dblibSvc == null) {
+ dbLibSvc = getDbLibService();
+ if (dbLibSvc == null) {
Log.error("Got Exception While getting DB Connection");
- throw new Exception("Got Exception While getting DB Connection");
+ throw new DBException("Got Exception While getting DB Connection");
}
- this.dblibSvc = dblibSvc;
}
catch (Exception e) {
Log.error(e.getMessage());
- throw e;
+ throw new DBException("An error occurred when instantiating DB service", e);
}
}
private static DbLibService getDbLibService() {
- DbLibService dblibSvc = null;
- BundleContext bctx = null;
- ServiceReference sref = null;
+ DbLibService dbLibService = null;
+ BundleContext bundleContext = null;
+ ServiceReference serviceRef = null;
Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
if (bundle != null) {
- bctx = bundle.getBundleContext();
+ bundleContext = bundle.getBundleContext();
}
- if (bctx != null) {
+ if (bundleContext != null) {
Log.debug("Getting bundle Context");
- sref = bctx.getServiceReference(DBLIB_SERVICE);
+ serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
}
- if (sref == null) {
+ if (serviceRef == null) {
Log.warn("Could not find service reference for DBLib service");
} else {
- dblibSvc = (DbLibService) bctx.getService(sref);
- if (dblibSvc == null) {
+ dbLibService = (DbLibService)bundleContext.getService(serviceRef);
+ if (dbLibService == null) {
Log.warn("DBLIB_SERVICE is null");
}
}
- if (dblibSvc == null) {
+ if (dbLibService == null) {
try {
- dblibSvc = new DBResourceManager(System.getProperties());
+ dbLibService = new DBResourceManager(System.getProperties());
} catch (Exception e) {
Log.error("Caught exception trying to create db service", e);
}
- if (dblibSvc == null) {
+ if (dbLibService == null) {
Log.warn("Could not create new DBResourceManager");
}
}
- return (dblibSvc);
+ return dbLibService;
}
- public ResultSet getDBData(String query) throws Exception {
+ public ResultSet getDBData(String query) throws DBException{
ResultSet resultSet;
StringBuilder sqlBuilder = new StringBuilder(query);
Log.info("Query: " + sqlBuilder.toString());
try {
- resultSet = dblibSvc.getData(sqlBuilder.toString(), null, null);
+ resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
} catch (Exception e) {
Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
- throw e;
+ throw new DBException("An error occurred when reading DB data", e);
}
return resultSet;
}
- public ResultSet getDBData(String query, ArrayList<String> paramList) throws Exception {
+ public ResultSet getDBData(String query, List<String> paramList) throws DBException {
ResultSet resultSet;
StringBuilder sqlBuilder = new StringBuilder(query);
Log.info("Query :" + sqlBuilder.toString());
try {
- resultSet = dblibSvc.getData(sqlBuilder.toString(), paramList, null);
- } catch (Exception expObj) {
- Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
- throw expObj;
+ resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
+ } catch (Exception e) {
+ Log.error("query "+sqlBuilder+" :: " + e.getMessage());
+ throw new DBException("An error occurred when reading DB data", e);
}
return resultSet;
}
- public boolean updateDBData(String query, ArrayList<String> paramList) throws Exception {
+ public boolean updateDBData(String query, List<String> paramList) throws DBException{
boolean update;
StringBuilder sqlBuilder = new StringBuilder(query);
Log.info("Query :" + sqlBuilder.toString());
try {
- update = dblibSvc.writeData(sqlBuilder.toString(), paramList, null);
- } catch (Exception expObj) {
- Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
- throw expObj;
+ update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
+ } catch (Exception e) {
+ Log.error("query "+sqlBuilder+" :: " + e.getMessage());
+ throw new DBException("An error occurred when updating DB data", e);
}
return update;
}