GroupCache.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import java.sql.Connection;
  2. import java.util.HashMap;
  3. import java.util.Locale;
  4. import java.util.Map;
  5. import com.cognos.CAM_AAA.authentication.UnrecoverableException;
  6. public class GroupCache
  7. {
  8. private final AccountCache accountCache;
  9. private final Connection connection;
  10. private final Map< String, Group > groups = new HashMap< String, Group >();
  11. public GroupCache(final ConnectionManager connectionManager)
  12. {
  13. this.connection = connectionManager.getConnection();
  14. this.accountCache = connectionManager.getAccountCache();
  15. }
  16. protected Group createGroup(final String groupID) throws UnrecoverableException
  17. {
  18. final Group group = new Group(groupID);
  19. this.setGroupProperties(group);
  20. this.groups.put(groupID, group);
  21. return group;
  22. }
  23. public synchronized Group findGroup(final String groupID) throws UnrecoverableException
  24. {
  25. Group group = this.groups.get(groupID);
  26. if (null == group)
  27. group = this.createGroup(groupID);
  28. return group;
  29. }
  30. protected void setGroupProperties(final Group group) throws UnrecoverableException
  31. {
  32. final String groupIDStr = group.getObjectID();
  33. final Integer groupID = Integer.parseInt(groupIDStr.substring(2));
  34. //Select GROUPNAME & USERID and exclude any users with a tenantId not public or available in the group.
  35. final Object[][] data = QueryUtil.query(this.connection, "SELECT g.GROUPNAME, g.USERID FROM GROUPS g INNER JOIN USERS u ON g.USERID=u.USERID WHERE GROUPID = ? AND (u.TENANT = '' OR u.TENANT = g.TENANT)", groupID);
  36. if (0 < data.length)
  37. group.addName(Locale.getDefault(), (String) data[0][0]);
  38. for (int i = 0; i < data.length; ++i)
  39. {
  40. final Object[] row = data[i];
  41. group.addMember(this.accountCache.findAccount("u:" + String.valueOf(row[1])));
  42. }
  43. }
  44. }