introduce configs and added heart beat logic

remotes/1712966534876109980/master
Amalka Subasinghe 1 year ago
parent cb5d672583
commit d67acc3d50

@ -75,7 +75,9 @@
org.wso2.carbon.user.api, org.wso2.carbon.user.api,
org.wso2.carbon.user.core.common, org.wso2.carbon.user.core.common,
org.wso2.carbon.user.core.service, org.wso2.carbon.user.core.service,
org.wso2.carbon.user.mgt.common org.wso2.carbon.user.mgt.common,
io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service,
io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal, !io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal,
@ -219,5 +221,10 @@
<artifactId>jaxb-api</artifactId> <artifactId>jaxb-api</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.server.bootup.heartbeat.beacon</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -20,10 +20,12 @@ package io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper;
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.bean.RoleMapping; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.bean.RoleMapping;
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.bean.UserStoreRoleMappingConfig; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.bean.UserStoreRoleMappingConfig;
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal.UserStoreRoleMappingDataHolder; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal.UserStoreRoleMappingDataHolder;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
@ -36,6 +38,8 @@ import java.util.concurrent.TimeUnit;
public class UserStoreRoleMapper implements ServerStartupObserver { public class UserStoreRoleMapper implements ServerStartupObserver {
private static final Log log = LogFactory.getLog(UserStoreRoleMapper.class); private static final Log log = LogFactory.getLog(UserStoreRoleMapper.class);
private UserStoreRoleMappingConfig config = null;
@Override @Override
public void completingServerStartup() { public void completingServerStartup() {
@ -43,6 +47,16 @@ public class UserStoreRoleMapper implements ServerStartupObserver {
@Override @Override
public void completedServerStartup() { public void completedServerStartup() {
config = UserStoreRoleMappingDataHolder.getInstance()
.getUserStoreRoleMappingConfigManager().getUserStoreRoleMappingConfig();
try {
if ((config.isEnabled() &&
UserStoreRoleMappingDataHolder.getInstance().getHeartBeatService().isTaskPartitioningEnabled() &&
UserStoreRoleMappingDataHolder.getInstance().getHeartBeatService().isQualifiedToExecuteTask())
|| (config.isEnabled() &&
!UserStoreRoleMappingDataHolder.getInstance().getHeartBeatService().isTaskPartitioningEnabled())) {
Runnable periodicTask = new Runnable() { Runnable periodicTask = new Runnable() {
public void run() { public void run() {
updateRoleMapping(); updateRoleMapping();
@ -53,7 +67,11 @@ public class UserStoreRoleMapper implements ServerStartupObserver {
ScheduledExecutorService executor = ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor(); Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(periodicTask,30, 60, TimeUnit.SECONDS); executor.scheduleAtFixedRate(periodicTask, config.getInitialDelayInSeconds(), config.getPeriodInSeconds(), TimeUnit.SECONDS);
}
} catch (HeartBeatManagementException e) {
log.error("Error while accessing heart beat service " + e.getMessage());
}
} }
private void updateRoleMapping() { private void updateRoleMapping() {
@ -64,9 +82,6 @@ public class UserStoreRoleMapper implements ServerStartupObserver {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
UserStoreRoleMappingConfig config = UserStoreRoleMappingDataHolder.getInstance()
.getUserStoreRoleMappingConfigManager().getUserStoreRoleMappingConfig();
List<RoleMapping> roleMappings = config.getMappings(); List<RoleMapping> roleMappings = config.getMappings();
if (!roleMappings.isEmpty()) { if (!roleMappings.isEmpty()) {
@ -115,7 +130,7 @@ public class UserStoreRoleMapper implements ServerStartupObserver {
} }
} }
} }
} catch (Exception e) { } catch (UserStoreException e) {
log.error("Error while getting user store..." + e.getMessage()); log.error("Error while getting user store..." + e.getMessage());
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();

@ -27,8 +27,24 @@ import java.util.List;
) )
public class UserStoreRoleMappingConfig { public class UserStoreRoleMappingConfig {
private boolean enabled;
private List<RoleMapping> mappings; private List<RoleMapping> mappings;
private long initialDelayInSeconds;
private long periodInSeconds;
@XmlElement(
name = "enabled"
)
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@XmlElementWrapper( @XmlElementWrapper(
name = "mappings" name = "mappings"
) )
@ -42,4 +58,26 @@ public class UserStoreRoleMappingConfig {
public void setMappings(List<RoleMapping> mappings) { public void setMappings(List<RoleMapping> mappings) {
this.mappings = mappings; this.mappings = mappings;
} }
@XmlElement(
name = "initialDelayInSeconds"
)
public long getInitialDelayInSeconds() {
return initialDelayInSeconds;
}
public void setInitialDelayInSeconds(long initialDelayInSeconds) {
this.initialDelayInSeconds = initialDelayInSeconds;
}
@XmlElement(
name = "periodInSeconds"
)
public long getPeriodInSeconds() {
return periodInSeconds;
}
public void setPeriodInSeconds(long periodInSeconds) {
this.periodInSeconds = periodInSeconds;
}
} }

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal; package io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.internal;
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMappingConfigManager; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMappingConfigManager;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserRealm;
@ -31,7 +32,7 @@ public class UserStoreRoleMappingDataHolder {
private ConfigurationContextService configurationContextService; private ConfigurationContextService configurationContextService;
private RealmService realmService; private RealmService realmService;
private UserStoreRoleMappingConfigManager userStoreRoleMappingConfigManager; private UserStoreRoleMappingConfigManager userStoreRoleMappingConfigManager;
private HeartBeatManagementService heartBeatService;
private static final UserStoreRoleMappingDataHolder thisInstance = new UserStoreRoleMappingDataHolder(); private static final UserStoreRoleMappingDataHolder thisInstance = new UserStoreRoleMappingDataHolder();
private UserStoreRoleMappingDataHolder() {} private UserStoreRoleMappingDataHolder() {}
@ -82,4 +83,12 @@ public class UserStoreRoleMappingDataHolder {
public void setUserStoreRoleMappingConfigManager(UserStoreRoleMappingConfigManager userStoreRoleMappingConfigManager) { public void setUserStoreRoleMappingConfigManager(UserStoreRoleMappingConfigManager userStoreRoleMappingConfigManager) {
this.userStoreRoleMappingConfigManager = userStoreRoleMappingConfigManager; this.userStoreRoleMappingConfigManager = userStoreRoleMappingConfigManager;
} }
public HeartBeatManagementService getHeartBeatService() {
return heartBeatService;
}
public void setHeartBeatService(HeartBeatManagementService heartBeatService) {
this.heartBeatService = heartBeatService;
}
} }

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.in
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMapper; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMapper;
import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMappingConfigManager; import io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper.UserStoreRoleMappingConfigManager;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
@ -43,6 +44,12 @@ import org.wso2.carbon.utils.ConfigurationContextService;
* policy="dynamic" * policy="dynamic"
* bind="setConfigurationContextService" * bind="setConfigurationContextService"
* unbind="unsetConfigurationContextService" * unbind="unsetConfigurationContextService"
* @scr.reference name="entgra.heart.beat.service"
* interface="io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService"
* cardinality="0..1"
* policy="dynamic"
* bind="setHeartBeatService"
* unbind="unsetHeartBeatService"
*/ */
public class UserStoreRoleMappingServiceComponent { public class UserStoreRoleMappingServiceComponent {
@ -109,4 +116,20 @@ public class UserStoreRoleMappingServiceComponent {
} }
UserStoreRoleMappingDataHolder.getInstance().setRealmService(null); UserStoreRoleMappingDataHolder.getInstance().setRealmService(null);
} }
@SuppressWarnings("unused")
protected void setHeartBeatService(HeartBeatManagementService heartBeatService) {
if (log.isDebugEnabled()) {
log.debug("Setting heart beat service");
}
UserStoreRoleMappingDataHolder.getInstance().setHeartBeatService(heartBeatService);
}
@SuppressWarnings("unused")
protected void unsetHeartBeatService(HeartBeatManagementService heartBeatManagementService) {
if (log.isDebugEnabled()) {
log.debug("Removing heart beat service");
}
UserStoreRoleMappingDataHolder.getInstance().setHeartBeatService(null);
}
} }

@ -17,21 +17,18 @@
~ under the License. ~ under the License.
--> -->
<UserStoreRoleMappingConfig> <UserStoreRoleMappingConfig>
<enabled>false</enabled>
<initialDelayInSeconds>30</initialDelayInSeconds>
<periodInSeconds>3600</periodInSeconds>
<mappings> <mappings>
<mapping secondaryRole="ENTGRA.IO/css_galle"> <mapping secondaryRole="ENTGRA.IO/css_galle">
<internalRoles> <internalRoles>
<role>branch_css</role> <role>Internal/branch_css</role>
<role>lb_03</role>
<role>ld_03_01</role>
<role>ld_03_02</role>
</internalRoles> </internalRoles>
</mapping> </mapping>
<mapping secondaryRole="ENTGRA.IO/css_nugegoda"> <mapping secondaryRole="ENTGRA.IO/css_nugegoda">
<internalRoles> <internalRoles>
<role>branch_css</role> <role>Internal/branch_css</role>
<role>lb_05</role>
<role>ld_05_01</role>
<role>ld_05_02</role>
</internalRoles> </internalRoles>
</mapping> </mapping>
</mappings> </mappings>

Loading…
Cancel
Save