diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/pom.xml b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/pom.xml index b5e71414618..5cb57f3dfbc 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/pom.xml +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/pom.xml @@ -59,6 +59,7 @@ javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, org.wso2.carbon.context, org.wso2.carbon.utils.*, + org.wso2.carbon.ndatasource.core, org.w3c.dom, org.apache.velocity;version="${velocity.version}", org.apache.velocity.app;version="${velocity.version}", @@ -168,6 +169,16 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common + + org.wso2.carbon + org.wso2.carbon.ndatasource.core + + + log4j + log4j + + + diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/HeartBeatBeaconUtils.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/HeartBeatBeaconUtils.java index 8d9a69f7039..9c5b91a93d1 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/HeartBeatBeaconUtils.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/HeartBeatBeaconUtils.java @@ -30,7 +30,6 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.net.InetAddress; -import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Hashtable; @@ -68,7 +67,7 @@ public class HeartBeatBeaconUtils { return (DataSource) InitialContext.doLookup(dataSourceName); } final InitialContext context = new InitialContext(jndiProperties); - return (DataSource) context.lookup(dataSourceName); + return (DataSource) context.doLookup(dataSourceName); } catch (Exception e) { throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } @@ -77,20 +76,10 @@ public class HeartBeatBeaconUtils { public static ServerContext getServerDetails() throws UnknownHostException, SocketException { InetAddress localHost = InetAddress.getLocalHost(); - NetworkInterface ni = NetworkInterface.getByInetAddress(localHost); - byte[] hardwareAddress = ni.getHardwareAddress(); - String[] hexadecimal = new String[hardwareAddress.length]; - for (int i = 0; i < hardwareAddress.length; i++) { - hexadecimal[i] = String.format("%02X", hardwareAddress[i]); - } - String macAddress = String.join("-", hexadecimal); int iotsCorePort = Integer.parseInt(System.getProperty("iot.core.https.port")); - ServerContext ctx = new ServerContext(); ctx.setHostName(localHost.getHostName()); - ctx.setMacAddress(macAddress); ctx.setCarbonServerPort(iotsCorePort); - return ctx; } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/config/HeartBeatBeaconConfig.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/config/HeartBeatBeaconConfig.java index 7c1365a9d63..e2bb9e2feca 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/config/HeartBeatBeaconConfig.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/config/HeartBeatBeaconConfig.java @@ -35,6 +35,7 @@ import java.io.File; @XmlRootElement(name = "HeartBeatBeaconConfig") public class HeartBeatBeaconConfig { + private boolean enabled; private int notifierFrequency; private int notifierDelay; private int serverTimeOutIntervalInSeconds; @@ -44,7 +45,7 @@ public class HeartBeatBeaconConfig { private static HeartBeatBeaconConfig config; private static final String HEART_BEAT_NOTIFIER_CONFIG_PATH = - CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "heart-beat-config.xml"; + CarbonUtils.getCarbonConfigDirPath() + File.separator + "heart-beat-config.xml"; private HeartBeatBeaconConfig() { } @@ -102,6 +103,15 @@ public class HeartBeatBeaconConfig { this.dataSourceConfig = dataSourceConfig; } + @XmlElement(name = "Enable", required = true) + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public static void init() throws HeartBeatBeaconConfigurationException { try { File emailSenderConfig = new File(HEART_BEAT_NOTIFIER_CONFIG_PATH); diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java index 97e1c3428b2..e8d3011be99 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java @@ -167,7 +167,6 @@ public class HeartBeatBeaconDAOFactory { currentConnection.remove(); } - /** * Resolve data source from the data source definition * @@ -190,7 +189,7 @@ public class HeartBeatBeaconDAOFactory { List jndiPropertyList = jndiConfig.getJndiProperties(); if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable<>(); + Hashtable jndiProperties = new Hashtable(); for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/impl/GenericHeartBeatDAOImpl.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/impl/GenericHeartBeatDAOImpl.java index f8ccfee4144..bdc7d6a52fb 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/impl/GenericHeartBeatDAOImpl.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/impl/GenericHeartBeatDAOImpl.java @@ -29,6 +29,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -46,23 +47,21 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO { String uuid = null; try { Connection conn = HeartBeatBeaconDAOFactory.getConnection(); + String serverUUID = UUID.randomUUID().toString(); String sql; - sql = "INSERT INTO SERVER_HEART_BEAT_EVENTS(HOST_NAME, MAC, UUID, SERVER_PORT) VALUES (?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[]{"UUID"}); + sql = "INSERT INTO SERVER_HEART_BEAT_EVENTS(HOST_NAME, UUID, SERVER_PORT) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, ctx.getHostName()); - stmt.setString(2, ctx.getMacAddress()); - stmt.setString(3, UUID.randomUUID().toString()); - stmt.setInt(4, ctx.getCarbonServerPort()); + stmt.setString(2, serverUUID); + stmt.setInt(3, ctx.getCarbonServerPort()); - stmt.executeUpdate(); - ResultSet result = stmt.getGeneratedKeys(); - if (result.next()){ - uuid = result.getString("UUID"); + if(stmt.executeUpdate() > 0){ + uuid = serverUUID; } } catch (SQLException e) { throw new HeartBeatDAOException("Error occurred while persisting server context for : '" + - "mac '" + ctx.getMacAddress() + "' " + + "port '" + ctx.getCarbonServerPort() + "' " + "hostname : '" + ctx.getHostName() + "' ", e); } finally { HeartBeatBeaconDAOUtil.cleanupResources(stmt, null); @@ -97,11 +96,10 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO { String uuid = null; try { Connection conn = HeartBeatBeaconDAOFactory.getConnection(); - String sql = "SELECT UUID FROM SERVER_HEART_BEAT_EVENTS WHERE HOST_NAME = ? AND MAC = ? AND SERVER_PORT = ?"; + String sql = "SELECT UUID FROM SERVER_HEART_BEAT_EVENTS WHERE HOST_NAME = ? AND SERVER_PORT = ?"; stmt = conn.prepareStatement(sql, new String[]{"UUID"}); stmt.setString(1, ctx.getHostName()); - stmt.setString(2, ctx.getMacAddress()); - stmt.setInt(3, ctx.getCarbonServerPort()); + stmt.setInt(2, ctx.getCarbonServerPort()); resultSet = stmt.executeQuery(); if (resultSet.next()){ @@ -109,7 +107,7 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO { } } catch (SQLException e) { throw new HeartBeatDAOException("Error occurred while retrieving meta information for heart beat event from " + - "mac '" + ctx.getMacAddress() + "' " + + "port '" + ctx.getCarbonServerPort() + "' " + "hostname : '" + ctx.getHostName() + "' ", e); } finally { HeartBeatBeaconDAOUtil.cleanupResources(stmt, resultSet); @@ -124,7 +122,7 @@ public class GenericHeartBeatDAOImpl implements HeartBeatDAO { Map ctxList = new HashMap<>(); try { Connection conn = HeartBeatBeaconDAOFactory.getConnection(); - String sql = "SELECT (@row_number:=@row_number + 1) AS IDX, UUID, HOST_NAME, MAC, SERVER_PORT from " + + String sql = "SELECT (@row_number:=@row_number + 1) AS IDX, UUID, HOST_NAME, SERVER_PORT from " + "SERVER_HEART_BEAT_EVENTS, (SELECT @row_number:=-1) AS TEMP " + "WHERE LAST_UPDATED_TIMESTAMP > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? SECOND) " + "ORDER BY UUID"; diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/util/HeartBeatBeaconDAOUtil.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/util/HeartBeatBeaconDAOUtil.java index fa4e291d844..211a10635b3 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/util/HeartBeatBeaconDAOUtil.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dao/util/HeartBeatBeaconDAOUtil.java @@ -85,7 +85,6 @@ public final class HeartBeatBeaconDAOUtil { ctx.setIndex(resultSet.getInt("IDX")); ctx.setUuid(resultSet.getString("UUID")); ctx.setHostName(resultSet.getString("HOST_NAME")); - ctx.setMacAddress(resultSet.getString("MAC")); ctx.setCarbonServerPort(resultSet.getInt("SERVER_PORT")); return ctx; } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dto/ServerContext.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dto/ServerContext.java index 25ea650e55f..bdb5abe76e2 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dto/ServerContext.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/dto/ServerContext.java @@ -21,7 +21,6 @@ package io.entgra.server.bootup.heartbeat.beacon.dto; public class ServerContext { private String hostName; - private String macAddress; private int carbonServerPort; private String uuid; private int index; @@ -34,14 +33,6 @@ public class ServerContext { this.hostName = hostName; } - public String getMacAddress() { - return macAddress; - } - - public void setMacAddress(String macAddress) { - this.macAddress = macAddress; - } - public String getUuid() { return uuid; } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java index 7cc795d4a6e..3435c16601a 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java @@ -27,10 +27,17 @@ import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementServi import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.ndatasource.core.DataSourceService; /** * @scr.component name="io.entgra.server.bootup.heartbeat.beacon.heartbeatBeaconComponent" * immediate="true" + * @scr.reference name="org.wso2.carbon.ndatasource" + * interface="org.wso2.carbon.ndatasource.core.DataSourceService" + * cardinality="1..1" + * policy="dynamic" + * bind="setDataSourceService" + * unbind="unsetDataSourceService" */ public class HeartBeatBeaconComponent { @@ -42,21 +49,24 @@ public class HeartBeatBeaconComponent { if (log.isDebugEnabled()) { log.debug("Initializing email sender core bundle"); } + this.registerHeartBeatServices(componentContext); + //heart beat notifier configuration */ HeartBeatBeaconConfig.init(); - DataSourceConfig dsConfig = HeartBeatBeaconConfig.getInstance().getDataSourceConfig(); - HeartBeatBeaconDAOFactory.init(dsConfig); - this.registerHeartBeatServices(componentContext); + if(HeartBeatBeaconConfig.getInstance().isEnabled()) { + DataSourceConfig dsConfig = HeartBeatBeaconConfig.getInstance().getDataSourceConfig(); + HeartBeatBeaconDAOFactory.init(dsConfig); - //Setting up executors to notify heart beat status */ - HeartBeatInternalUtils.setUpNotifiers(HeartBeatBeaconUtils.getServerDetails()); + //Setting up executors to notify heart beat status */ + HeartBeatInternalUtils.setUpNotifiers(HeartBeatBeaconUtils.getServerDetails()); + } if (log.isDebugEnabled()) { - log.debug("Email sender core bundle has been successfully initialized"); + log.debug("Heart Beat Notifier bundle has been successfully initialized"); } } catch (Throwable e) { - log.error("Error occurred while initializing email sender core bundle", e); + log.error("Error occurred while initializing Heart Beat Notifier bundle", e); } } @@ -74,4 +84,16 @@ public class HeartBeatBeaconComponent { componentContext.getBundleContext().registerService(HeartBeatManagementService.class, heartBeatServiceProvider, null); } + protected void setDataSourceService(DataSourceService dataSourceService) { + /* This is to avoid mobile device management component getting initialized before the underlying datasources + are registered */ + if (log.isDebugEnabled()) { + log.debug("Data source service set to mobile service component"); + } + } + + protected void unsetDataSourceService(DataSourceService dataSourceService) { + //do nothing + } + } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatInternalUtils.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatInternalUtils.java index 1e5debde743..c407348b4f3 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatInternalUtils.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/internal/HeartBeatInternalUtils.java @@ -20,6 +20,7 @@ package io.entgra.server.bootup.heartbeat.beacon.internal; import io.entgra.server.bootup.heartbeat.beacon.config.HeartBeatBeaconConfig; import io.entgra.server.bootup.heartbeat.beacon.HeartBeatBeaconConfigurationException; +import io.entgra.server.bootup.heartbeat.beacon.dao.HeartBeatBeaconDAOFactory; import io.entgra.server.bootup.heartbeat.beacon.dto.HeartBeatEvent; import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext; import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException; @@ -66,7 +67,7 @@ public class HeartBeatInternalUtils { CONFIG.getNotifierFrequency() != 0 ? CONFIG.getNotifierFrequency() : DEFAULT__NOTIFIER_INTERVAL, TimeUnit.SECONDS); } catch (HeartBeatManagementException e) { - throw new HeartBeatBeaconConfigurationException("Error occured while updating initial server context."); + throw new HeartBeatBeaconConfigurationException("Error occured while updating initial server context.", e); } } diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java index 576b7c67dfb..362f97879ec 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java @@ -27,6 +27,7 @@ import io.entgra.server.bootup.heartbeat.beacon.exception.HeartBeatManagementExc import io.entgra.server.bootup.heartbeat.beacon.dto.ServerContext; import io.entgra.server.bootup.heartbeat.beacon.internal.HeartBeatBeaconDataHolder; import org.wso2.carbon.device.mgt.common.ServerCtxInfo; +import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import java.sql.SQLException; import java.util.Map; @@ -39,30 +40,35 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic int hashIndex = -1; ServerContext localServerCtx = null; ServerCtxInfo serverCtxInfo = null; - try { - HeartBeatBeaconDAOFactory.openConnection(); - heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); + if(HeartBeatBeaconConfig.getInstance().isEnabled()) { + try { + HeartBeatBeaconDAOFactory.openConnection(); + heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); - int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds(); - int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew(); - int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew; - String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID(); - Map serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut); - if(!serverCtxMap.isEmpty()) { - localServerCtx = serverCtxMap.get(localServerUUID); - if (localServerCtx != null) { - hashIndex = localServerCtx.getIndex(); - serverCtxInfo = new ServerCtxInfo(serverCtxMap.size(), hashIndex); + int timeOutIntervalInSeconds = HeartBeatBeaconConfig.getInstance().getServerTimeOutIntervalInSeconds(); + int timeSkew = HeartBeatBeaconConfig.getInstance().getTimeSkew(); + int cumilativeTimeOut = timeOutIntervalInSeconds + timeSkew; + String localServerUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID(); + Map serverCtxMap = heartBeatDAO.getActiveServerDetails(cumilativeTimeOut); + if (!serverCtxMap.isEmpty()) { + localServerCtx = serverCtxMap.get(localServerUUID); + if (localServerCtx != null) { + hashIndex = localServerCtx.getIndex(); + serverCtxInfo = new ServerCtxInfo(serverCtxMap.size(), hashIndex); + } } + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the underlying data source"; + throw new HeartBeatManagementException(msg, e); + } catch (HeartBeatDAOException e) { + String msg = "Error occurred while retrieving active server count."; + throw new HeartBeatManagementException(msg, e); + } finally { + HeartBeatBeaconDAOFactory.closeConnection(); } - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the underlying data source"; - throw new HeartBeatManagementException(msg, e); - } catch (HeartBeatDAOException e) { - String msg = "Error occurred while retrieving active server count."; - throw new HeartBeatManagementException(msg, e); - } finally { - HeartBeatBeaconDAOFactory.closeConnection(); + } else { + String msg = "Heart Beat Configuration Disabled. Server Context Information Not available."; + throw new HeartBeatManagementException(msg); } return serverCtxInfo; } @@ -71,22 +77,29 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic public String updateServerContext(ServerContext ctx) throws HeartBeatManagementException { HeartBeatDAO heartBeatDAO; String uuid = null; - try { - HeartBeatBeaconDAOFactory.openConnection(); - heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); + if(HeartBeatBeaconConfig.getInstance().isEnabled()) { + try { + HeartBeatBeaconDAOFactory.beginTransaction(); + heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); - uuid = heartBeatDAO.retrieveExistingServerCtx(ctx); - if(uuid == null){ - uuid = heartBeatDAO.recordServerCtx(ctx); + uuid = heartBeatDAO.retrieveExistingServerCtx(ctx); + if (uuid == null) { + uuid = heartBeatDAO.recordServerCtx(ctx); + HeartBeatBeaconDAOFactory.commitTransaction(); + } + } catch (HeartBeatDAOException e) { + String msg = "Error Occured while retrieving server context."; + throw new HeartBeatManagementException(msg, e); + } catch (TransactionManagementException e) { + HeartBeatBeaconDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating server context. Issue in opening a connection to the underlying data source"; + throw new HeartBeatManagementException(msg, e); + } finally { + HeartBeatBeaconDAOFactory.closeConnection(); } - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the underlying data source"; - throw new HeartBeatManagementException(msg, e); - } catch (HeartBeatDAOException e) { - String msg = "Error Occured while retrieving active server count."; - throw new HeartBeatManagementException(msg, e); - } finally { - HeartBeatBeaconDAOFactory.closeConnection(); + } else { + String msg = "Heart Beat Configuration Disabled. Updating Server Context Failed."; + throw new HeartBeatManagementException(msg); } return uuid; } @@ -96,18 +109,26 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic public boolean recordHeartBeat(HeartBeatEvent event) throws HeartBeatManagementException { HeartBeatDAO heartBeatDAO; boolean operationSuccess = false; - try { - HeartBeatBeaconDAOFactory.openConnection(); - heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); - operationSuccess = heartBeatDAO.recordHeatBeat(event); - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the underlying data source"; - throw new HeartBeatManagementException(msg, e); - } catch (HeartBeatDAOException e) { - String msg = "Error Occured while retrieving active server count."; - throw new HeartBeatManagementException(msg, e); - } finally { - HeartBeatBeaconDAOFactory.closeConnection(); + if (HeartBeatBeaconConfig.getInstance().isEnabled()) { + try { + HeartBeatBeaconDAOFactory.beginTransaction(); + heartBeatDAO = HeartBeatBeaconDAOFactory.getHeartBeatDAO(); + operationSuccess = heartBeatDAO.recordHeatBeat(event); + HeartBeatBeaconDAOFactory.commitTransaction(); + } catch (HeartBeatDAOException e) { + String msg = "Error occurred while recording heart beat."; + throw new HeartBeatManagementException(msg, e); + } catch (TransactionManagementException e) { + HeartBeatBeaconDAOFactory.rollbackTransaction(); + String msg = "Error occurred performing heart beat record transaction. " + + "Transaction rolled back."; + throw new HeartBeatManagementException(msg, e); + } finally { + HeartBeatBeaconDAOFactory.closeConnection(); + } + } else { + String msg = "Heart Beat Configuration Disabled. Recording Heart Beat Failed."; + throw new HeartBeatManagementException(msg); } return operationSuccess; } diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/pom.xml b/features/heartbeat-management/io.entgra.server.heart.beat.feature/pom.xml index c1f137a61df..7f6d79f5261 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/pom.xml +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/pom.xml @@ -87,7 +87,6 @@ - @@ -115,42 +114,6 @@ - - - org.apache.maven.plugins - maven-antrun-plugin - - - - create-heart-beat-mgt-schema - package - - run - - - - - - - - - - - - - - - - - - - - - - - - - org.wso2.maven carbon-p2-plugin diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/conf/heart-beat-config.xml b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/conf/heart-beat-config.xml index f025df04785..8b3aa6a82a9 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/conf/heart-beat-config.xml +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/conf/heart-beat-config.xml @@ -19,9 +19,10 @@ --> - + true + - jdbc/ServerHeartBeat_DS + jdbc/HeartBeat_DS 30 diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/datasources/heart-beat-datasources.xml b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/datasources/heart-beat-datasources.xml index 34c5d011b5d..a3c656b7570 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/datasources/heart-beat-datasources.xml +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/datasources/heart-beat-datasources.xml @@ -16,25 +16,23 @@ ~ specific language governing permissions and limitations ~ under the License. --> - - + org.wso2.carbon.ndatasource.rdbms.RDBMSDataSourceReader - - jdbc/ServerHeartBeat_DS - The datasource used for recording server Heart Beats + HeartBeat_DS + The datasource Server Heart Beat - jdbc/ServerHeartBeat_DS + jdbc/HeartBeat_DS - jdbc:h2:repository/database/HeartBeat_DB;DB_CLOSE_ON_EXIT=FALSE - wso2carbon - wso2carbon - org.h2.Driver + jdbc:mysql://localhost:3306/heart_beat + root + root + com.mysql.jdbc.Driver 50 60000 true diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/h2.sql b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/h2.sql index 570c2f78df2..3f5ad9652e2 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/h2.sql +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/h2.sql @@ -4,7 +4,6 @@ CREATE TABLE IF NOT EXISTS SERVER_HEART_BEAT_EVENTS ( ID INTEGER AUTO_INCREMENT NOT NULL, HOST_NAME VARCHAR(100) NOT NULL, - MAC VARCHAR(100) NOT NULL, UUID VARCHAR(100) NOT NULL, SERVER_PORT INTEGER NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mssql.sql b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mssql.sql index 830aaea26aa..a56b9f981bb 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mssql.sql +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mssql.sql @@ -6,7 +6,6 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[S CREATE TABLE SERVER_HEART_BEAT_EVENTS ( ID INT NOT NULL AUTO_INCREMENT, HOST_NAME VARCHAR(100) NOT NULL, - MAC VARCHAR(100) NOT NULL, UUID VARCHAR(100) NOT NULL, SERVER_PORT INT NOT NULL, LAST_UPDATED_TIMESTAMP DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mysql.sql b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mysql.sql index 570c2f78df2..3f5ad9652e2 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mysql.sql +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/mysql.sql @@ -4,7 +4,6 @@ CREATE TABLE IF NOT EXISTS SERVER_HEART_BEAT_EVENTS ( ID INTEGER AUTO_INCREMENT NOT NULL, HOST_NAME VARCHAR(100) NOT NULL, - MAC VARCHAR(100) NOT NULL, UUID VARCHAR(100) NOT NULL, SERVER_PORT INTEGER NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/oracle.sql b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/oracle.sql index 3f435816f4c..f9ca7a43672 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/oracle.sql +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/oracle.sql @@ -5,7 +5,6 @@ CREATE TABLE SERVER_HEART_BEAT_EVENTS ( ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL, HOST_NAME VARCHAR(100) NOT NULL, - MAC VARCHAR(100) NOT NULL, UUID VARCHAR(100) NOT NULL, SERVER_PORT INTEGER NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/postgresql.sql b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/postgresql.sql index 989ed77954f..85081a385bd 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/postgresql.sql +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/dbscripts/heart-beat/postgresql.sql @@ -4,7 +4,6 @@ CREATE TABLE IF NOT EXISTS SERVER_HEART_BEAT_EVENTS ( ID INTEGER AUTO_INCREMENT NOT NULL, HOST_NAME VARCHAR(100) NOT NULL, - MAC VARCHAR(100) NOT NULL, UUID VARCHAR(100) NOT NULL, SERVER_PORT INTEGER NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/p2.inf b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/p2.inf index 1a0593e92d7..6add3736779 100644 --- a/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/p2.inf +++ b/features/heartbeat-management/io.entgra.server.heart.beat.feature/src/main/resources/p2.inf @@ -1,11 +1,8 @@ instructions.configure = \ -org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/datasources/,target:${installFolder}/../../../conf/datasources/,overwrite:true);\ -org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/conf/heart-beat-config.xml,target:${installFolder}/../../../conf/heart-beat-config.xml,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/datasources/,target:${installFolder}/../../conf/datasources/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/conf/heart-beat-config.xml,target:${installFolder}/../../conf/heart-beat-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/dbscripts/heart-beat/,target:${installFolder}/../../../dbscripts/heart-beat,overwrite:true);\ -org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../../repository/database/);\ -org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/io.entgra.server.heart.beat_${feature.version}/database/,target:${installFolder}/../../../repository/database/,overwrite:true);\ instructions.unconfigure = \ org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../conf/datasources/heart-beat-datasources.xml);\ org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../../dbscripts/heart-beat);\ -org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../database/HeartBeat_DB.h2.db);\