Skip to content

Commit

Permalink
Issue #513
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrojdeCTL committed Sep 28, 2018
1 parent dbc2aab commit de1a912
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ public class PropertyNames {

public static final String MDW_LISTENER_KAFKA = "mdw.listener.kafka";

public static final String MDW_USERGROUP_MONITOR_INTERVAL = "mdw.usergroupmonitor.interval";

}
4 changes: 4 additions & 0 deletions mdw-hub/src/com/centurylink/mdw/hub/MdwMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.centurylink.mdw.startup.StartupService;
import com.centurylink.mdw.timer.startup.AssetImportMonitor;
import com.centurylink.mdw.timer.startup.TimerTaskRegistration;
import com.centurylink.mdw.timer.startup.UserGroupMonitor;
import com.centurylink.mdw.util.StringHelper;
import com.centurylink.mdw.util.log.LoggerUtil;
import com.centurylink.mdw.util.log.StandardLogger;
Expand Down Expand Up @@ -141,6 +142,9 @@ public void startup(String container, String deployPath, String contextPath) {
logger.info("Initialize " + AssetImportMonitor.class.getName());
(new AssetImportMonitor()).onStartup();

logger.info("Initialize " + UserGroupMonitor.class.getName());
(new UserGroupMonitor()).onStartup();

logger.info("MDW initialization completed after " + (System.currentTimeMillis() - before) + " ms");
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.centurylink.mdw.service.data.task;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -25,18 +28,23 @@
import com.centurylink.mdw.cache.PreloadableCache;
import com.centurylink.mdw.dataaccess.DataAccessException;
import com.centurylink.mdw.dataaccess.DatabaseAccess;
import com.centurylink.mdw.dataaccess.DbAccess;
import com.centurylink.mdw.model.user.Role;
import com.centurylink.mdw.model.user.User;
import com.centurylink.mdw.model.user.Workgroup;
import com.centurylink.mdw.service.data.user.UserDataAccess;
import com.centurylink.mdw.util.log.LoggerUtil;
import com.centurylink.mdw.util.log.StandardLogger;
import com.centurylink.mdw.util.timer.CodeTimer;

/**
* Cache for MDW users/groups/roles (plus group relationships for tasks).
*/
public class UserGroupCache implements PreloadableCache {

private static UserGroupCache instance;
private static UserGroupCache instance;

private static StandardLogger logger = LoggerUtil.getStandardLogger();

// these are loaded initially (and users are shallow)
private List<User> users;
Expand All @@ -49,17 +57,22 @@ public class UserGroupCache implements PreloadableCache {
private volatile Map<String,User> usersByCuid = new HashMap<String,User>(); // includes group-roles and attributes
private volatile Map<Long,User> usersById = new HashMap<Long,User>(); // TODO: remove usages
private volatile Map<String,Workgroup> groupsByName = new HashMap<String,Workgroup>(); // includes (shallow) users
private volatile Map<String,Role> rolesByName = new HashMap<String,Role>();
private volatile Map<String,Role> rolesByName = new HashMap<String,Role>();

// used to determine if a user/group/role change has happened in another instance of cluster
private long lastUserSync = 0;

public void initialize(Map<String,String> params) {
}

public void loadCache() throws CachingException {
instance = this;
instance = this;
lastUserSync = System.currentTimeMillis();
load();
}

public void clearCache() {
public void clearCache() {
lastUserSync = System.currentTimeMillis();
synchronized(usersByCuid) {
usersByCuid.clear();
usersById.clear();
Expand Down Expand Up @@ -91,31 +104,37 @@ public static Workgroup getWorkgroup(String groupName) throws CachingException {
public static void set(Workgroup workgroup) {
instance.workgroups.remove(workgroup);
instance.workgroups.add(workgroup);
Collections.sort(instance.workgroups); // in case new or name changed
Collections.sort(instance.workgroups); // in case new or name changed
instance.updateLastUpdateValue();
instance.clearCache();
}

public static void remove(Workgroup workgroup) {
instance.workgroups.remove(workgroup);
instance.workgroups.remove(workgroup);
instance.updateLastUpdateValue();
instance.clearCache();
}

/**
* Clears only the relationships (not the base data).
* Clears only the relationships (not the base data).
* Should only be called if there were data changes performed in DB
*/
public static void clear() {
public static void clear() {
instance.updateLastUpdateValue();
instance.clearCache();
}

public static void set(Role role) {
instance.roles.remove(role);
instance.roles.add(role);
Collections.sort(instance.roles); // in case new or name changed
Collections.sort(instance.roles); // in case new or name changed
instance.updateLastUpdateValue();
instance.clearCache();
}

public static void remove(Role role) {
instance.roles.remove(role);
instance.roles.remove(role);
instance.updateLastUpdateValue();
instance.clearCache();
}

Expand Down Expand Up @@ -187,14 +206,20 @@ private static boolean isMatch(User user, String prefix) {
public static void set(User user) {
instance.users.remove(user);
instance.users.add(user);
Collections.sort(instance.users); // in case new or name changed
Collections.sort(instance.users); // in case new or name changed
instance.updateLastUpdateValue();
instance.clearCache();
}

public static void remove(User user) {
instance.users.remove(user);
instance.users.remove(user);
instance.updateLastUpdateValue();
instance.clearCache();
}

public static long getLastUserSync() {
return instance.lastUserSync;
}

public static List<String> getUserAttributeNames() {
return instance.userAttributeNames;
Expand Down Expand Up @@ -371,5 +396,47 @@ private User loadUser(Long id) throws CachingException {
catch (Exception ex) {
throw new CachingException(ex.getMessage(), ex);
}
}

private void updateLastUpdateValue() {
String select = "select value from value where name = ? and owner_type = ? and owner_id = ?";
try (DbAccess dbAccess = new DbAccess(); PreparedStatement stmt = dbAccess.getConnection().prepareStatement(select)) {
stmt.setString(1, "LastUserGroupChange");
stmt.setString(2, "UserGroupAdmin");
stmt.setString(3, "0");
try (ResultSet rs = stmt.executeQuery()) {
Timestamp currentDate = new Timestamp(System.currentTimeMillis());
if (rs.next()) {
String update = "update value set value = ?, mod_dt = ? where name = ? and owner_type = ? and owner_id = ?";
try (PreparedStatement updateStmt = dbAccess.getConnection().prepareStatement(update)) {
updateStmt.setString(1, ((Long)currentDate.getTime()).toString());
updateStmt.setTimestamp(2, currentDate);
updateStmt.setString(3, "LastUserGroupChange");
updateStmt.setString(4, "UserGroupAdmin");
updateStmt.setString(5, "0");
updateStmt.executeUpdate();
}
}
else {
String insert = "insert into value (value, name, owner_type, owner_id, create_dt, create_usr, mod_dt, mod_usr, comments) "
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement insertStmt = dbAccess.getConnection().prepareStatement(insert)) {
insertStmt.setString(1, ((Long)currentDate.getTime()).toString());
insertStmt.setString(2, "LastUserGroupChange");
insertStmt.setString(3, "UserGroupAdmin");
insertStmt.setString(4, "0");
insertStmt.setTimestamp(5, currentDate);
insertStmt.setString(6, "MDWEngine");
insertStmt.setTimestamp(7, currentDate);
insertStmt.setString(8, "MDWEngine");
insertStmt.setString(9, "Represents the last time user/group/role changes were made in MDW Hub");
insertStmt.executeUpdate();
}
}
}
}
catch (Exception e) {
logger.severeException("Exception attempting to update last user group change timestamp in Values table", e);
}
}
}

0 comments on commit de1a912

Please sign in to comment.