From 3743d182d99dbed9d2be84dc357f6839ffe4d2ec Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 18 Sep 2014 10:39:05 +0200 Subject: Hierarchical Roles. Fixes #199 --- .../c2kernel/entity/agent/AgentImplementation.java | 4 +- .../com/c2kernel/entity/imports/ImportAgent.java | 4 +- .../com/c2kernel/entity/imports/ImportRole.java | 28 ++++++++++- .../com/c2kernel/lifecycle/instance/Activity.java | 48 +++++++++++++------ .../agent/CreateAgentFromDescription.java | 2 +- .../instance/predefined/agent/RemoveAgent.java | 2 +- .../instance/predefined/agent/SetAgentRoles.java | 4 +- .../instance/predefined/server/RemoveRole.java | 9 ++-- src/main/java/com/c2kernel/lookup/Lookup.java | 2 + .../java/com/c2kernel/lookup/LookupManager.java | 10 +++- src/main/java/com/c2kernel/lookup/RolePath.java | 56 ++++++++++------------ src/main/java/com/c2kernel/process/Bootstrap.java | 32 ++++++------- .../java/com/c2kernel/process/module/Module.java | 5 +- 13 files changed, 126 insertions(+), 80 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java b/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java index 0406387..c0f0dd0 100644 --- a/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java +++ b/src/main/java/com/c2kernel/entity/agent/AgentImplementation.java @@ -60,7 +60,7 @@ public class AgentImplementation extends ItemImplementation implements public void addRole(String roleName) throws CannotManageException, ObjectNotFoundException { RolePath newRole = Gateway.getLookup().getRolePath(roleName); try { - newRole.addAgent(mAgentPath); + Gateway.getLookupManager().addRole(mAgentPath, newRole); } catch (ObjectCannotBeUpdated ex) { throw new CannotManageException("Could not update role"); } @@ -70,7 +70,7 @@ public class AgentImplementation extends ItemImplementation implements public void removeRole(String roleName) throws CannotManageException, ObjectNotFoundException { RolePath rolePath = Gateway.getLookup().getRolePath(roleName); try { - rolePath.removeAgent(mAgentPath); + Gateway.getLookupManager().removeRole(mAgentPath, rolePath); } catch (ObjectCannotBeUpdated ex) { throw new CannotManageException("Could not update role"); } diff --git a/src/main/java/com/c2kernel/entity/imports/ImportAgent.java b/src/main/java/com/c2kernel/entity/imports/ImportAgent.java index ac9911e..2bbd307 100644 --- a/src/main/java/com/c2kernel/entity/imports/ImportAgent.java +++ b/src/main/java/com/c2kernel/entity/imports/ImportAgent.java @@ -50,9 +50,9 @@ public class ImportAgent extends ModuleImport implements java.io.Serializable { try { thisRole = Gateway.getLookup().getRolePath(role); } catch (ObjectNotFoundException ex) { - throw new ObjectNotFoundException("Role "+role+" does not exist."); + throw new ObjectNotFoundException("Role "+role+" does not exist.", ""); } - thisRole.addAgent(newAgent); + Gateway.getLookupManager().addRole(newAgent, thisRole); } } diff --git a/src/main/java/com/c2kernel/entity/imports/ImportRole.java b/src/main/java/com/c2kernel/entity/imports/ImportRole.java index dc8f351..975c18b 100644 --- a/src/main/java/com/c2kernel/entity/imports/ImportRole.java +++ b/src/main/java/com/c2kernel/entity/imports/ImportRole.java @@ -1,9 +1,14 @@ package com.c2kernel.entity.imports; +import java.util.Iterator; + import com.c2kernel.common.CannotManageException; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.Path; +import com.c2kernel.lookup.RolePath; import com.c2kernel.process.Gateway; import com.c2kernel.process.module.ModuleImport; @@ -15,8 +20,27 @@ public class ImportRole extends ModuleImport { } @Override - public void create(AgentPath agentPath, boolean reset) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException { - Gateway.getLookupManager().createRole(name, jobList); + public void create(AgentPath agentPath, boolean reset) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { + RolePath parent = new RolePath(); + if (name.indexOf('/') > -1) { + String[] roleComp = name.split("/"); + for (int i=0; i childIter = parent.getChildren(); + boolean found = false; + while (childIter.hasNext()) { + RolePath childRole = (RolePath)childIter.next(); + if (childRole.getName().equals(roleComp[i])) { + parent = childRole; + found = true; + break; + } + } + if (!found) throw new ObjectNotFoundException("Parent role "+roleComp[i]+" was not found", ""); + } + name = roleComp[roleComp.length-1]; + } + RolePath newRole = new RolePath(parent, name, jobList); + if (!newRole.exists()) Gateway.getLookupManager().createRole(newRole); } public boolean hasJobList() { diff --git a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java index 95e0ede..3dd94b0 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java @@ -1,5 +1,6 @@ package com.c2kernel.lifecycle.instance; import java.util.ArrayList; +import java.util.Iterator; import java.util.Map; import java.util.Vector; @@ -21,6 +22,7 @@ import com.c2kernel.lifecycle.instance.stateMachine.Transition; import com.c2kernel.lookup.AgentPath; import com.c2kernel.lookup.InvalidAgentPathException; import com.c2kernel.lookup.ItemPath; +import com.c2kernel.lookup.Path; import com.c2kernel.lookup.RolePath; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.outcome.Outcome; @@ -203,7 +205,16 @@ public class Activity extends WfVertex DateUtility.setToNow(mStateDate); //refresh all the job lists - pushJobsToAgents(itemPath); + String agentRole = getCurrentAgentRole(); + if (agentRole != null && agentRole.length()>0) { + try { + RolePath myRole = Gateway.getLookup().getRolePath(agentRole); + pushJobsToAgents(itemPath, myRole); + } catch (ObjectNotFoundException ex) { // non-existent role + Logger.msg(7, "Activity.pushJobsToAgents() - Activity role '"+agentRole+" not found."); + } + } + return outcome; } @@ -451,22 +462,29 @@ public class Activity extends WfVertex } return jobs; } - - public void pushJobsToAgents(ItemPath itemPath) - { - String agentRole = getCurrentAgentRole(); - if (agentRole == null || agentRole.length()==0) return; - RolePath myRole; - try { - myRole = Gateway.getLookup().getRolePath(agentRole); - } catch (ObjectNotFoundException ex) { // non-existent role - Logger.msg(7, "Activity.pushJobsToAgents() - Activity role '"+agentRole+" not found."); - return; - } - if (myRole.hasJobList()) - new JobPusher(this, itemPath, myRole).start(); + public void pushJobsToAgents(ItemPath itemPath) { + String agentRole = getCurrentAgentRole(); + if (agentRole != null && agentRole.length()>0) { + try { + RolePath myRole = Gateway.getLookup().getRolePath(agentRole); + pushJobsToAgents(itemPath, myRole); + } catch (ObjectNotFoundException ex) { // non-existent role + Logger.msg(7, "Activity.pushJobsToAgents() - Activity role '"+agentRole+" not found."); + } + } + } + + public void pushJobsToAgents(ItemPath itemPath, RolePath role) + { + if (role.hasJobList()) + new JobPusher(this, itemPath, role).start(); + Iterator childRoles = role.getChildren(); + while (childRoles.hasNext()) { + RolePath childRole = (RolePath)childRoles.next(); + pushJobsToAgents(itemPath, childRole); + } } /** diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java index 520f70f..7683143 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java @@ -57,7 +57,7 @@ public class CreateAgentFromDescription extends CreateItemFromDescription throw new InvalidDataException("Agent should have at least one Role defined on creation"); // check if given roles exist for(int i=1; i 0) throw new InvalidDataException("Cannot remove role. "+agents.length+" agents still hold it.", ""); try { - lookup.removeRole(agent, thisRole); + lookup.delete(thisRole); } catch (ObjectCannotBeUpdated e) { Logger.error(e); throw new InvalidDataException("Role "+params[0]+" could not be removed.", ""); - } catch (ObjectNotFoundException e) { - throw new InvalidDataException("Role "+params[0]+" not found.", ""); } return requestData; diff --git a/src/main/java/com/c2kernel/lookup/Lookup.java b/src/main/java/com/c2kernel/lookup/Lookup.java index 87dec1d..6627ea4 100644 --- a/src/main/java/com/c2kernel/lookup/Lookup.java +++ b/src/main/java/com/c2kernel/lookup/Lookup.java @@ -137,6 +137,8 @@ public interface Lookup { public RolePath[] getRoles(AgentPath agentPath); /** + * Returns all of the Agents in this centre who hold this role (including sub-roles) + * * @param agentPath * @param role * @return diff --git a/src/main/java/com/c2kernel/lookup/LookupManager.java b/src/main/java/com/c2kernel/lookup/LookupManager.java index ce1268e..a8ce41b 100644 --- a/src/main/java/com/c2kernel/lookup/LookupManager.java +++ b/src/main/java/com/c2kernel/lookup/LookupManager.java @@ -40,16 +40,20 @@ public interface LookupManager extends Lookup { // Role and agent management /** + * Creates a new Role in this centre + * * @param role * @param hasJobList * @return * @throws ObjectAlreadyExistsException * @throws ObjectCannotBeUpdated */ - public RolePath createRole(String role, boolean hasJobList) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated; + public RolePath createRole(RolePath role) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated; /** - * @param agent + * Adds the given Agent to the given Role, if they both exist. + * + * @param agent - the * @param rolePath * @throws ObjectCannotBeUpdated * @throws ObjectNotFoundException @@ -57,6 +61,8 @@ public interface LookupManager extends Lookup { public void addRole(AgentPath agent, RolePath rolePath) throws ObjectCannotBeUpdated, ObjectNotFoundException; /** + * Removes the given Agent from the given Role. Does not delete the Role. + * * @param agent * @param role * @throws ObjectCannotBeUpdated diff --git a/src/main/java/com/c2kernel/lookup/RolePath.java b/src/main/java/com/c2kernel/lookup/RolePath.java index 9f9c597..e82c7ad 100644 --- a/src/main/java/com/c2kernel/lookup/RolePath.java +++ b/src/main/java/com/c2kernel/lookup/RolePath.java @@ -10,14 +10,12 @@ package com.c2kernel.lookup; -import java.util.ArrayList; import java.util.Iterator; import com.c2kernel.common.CannotManageException; import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; -import com.c2kernel.utils.Logger; @@ -32,13 +30,31 @@ public class RolePath extends DomainPath */ private boolean hasJobList = false; + + public RolePath() { + super("agent"); + } - public RolePath(String roleName) { - super(new DomainPath("agent"), roleName); + @Override + public RolePath getParent() { + try { + if (mPath.length > 2) + return Gateway.getLookup().getRolePath(mPath[mPath.length-2]); + } catch (ObjectNotFoundException ex) { } + return null; + } + + public RolePath(RolePath parent, String roleName) { + super(parent, roleName); + } + + public RolePath(String[] path, boolean jobList) { + super(path); + hasJobList = jobList; } - public RolePath(String roleName, boolean jobList) { - this(roleName); + public RolePath(RolePath parent, String roleName, boolean jobList) { + this(parent, roleName); hasJobList = jobList; } @@ -56,7 +72,6 @@ public class RolePath extends DomainPath */ public void setHasJobList(boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated, CannotManageException { this.hasJobList = hasJobList; - Gateway.getLookupManager().setHasJobList(this, hasJobList); } @@ -64,30 +79,9 @@ public class RolePath extends DomainPath protected void checkType() { mType = CONTEXT; } - - public Iterator getChildren() { - AgentPath[] agents = getAgentsWithRole(); - ArrayList children = new ArrayList(agents.length); - for (int i = 0; i < agents.length; i++) - children.add(i, agents[i]); - return children.iterator(); - } - - public AgentPath[] getAgentsWithRole() { - try { - return Gateway.getLookup().getAgents(this); - } catch (ObjectNotFoundException ex) { - Logger.error("Cannot retrieve agent list. Role "+getName()+" does not exist in LDAP"); - return new AgentPath[0]; - } - } - - public void addAgent(AgentPath agent) throws ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { - Gateway.getLookupManager().addRole(agent, this); - } - - public void removeAgent(AgentPath agent) throws ObjectCannotBeUpdated, CannotManageException, ObjectNotFoundException { - Gateway.getLookupManager().removeRole(agent, this); + + public Iterator getChildren() { + return Gateway.getLookup().getChildren(this); } @Override diff --git a/src/main/java/com/c2kernel/process/Bootstrap.java b/src/main/java/com/c2kernel/process/Bootstrap.java index 0f06616..e269e09 100644 --- a/src/main/java/com/c2kernel/process/Bootstrap.java +++ b/src/main/java/com/c2kernel/process/Bootstrap.java @@ -281,7 +281,7 @@ public class Bootstrap /************************************************************************** * Checks for the existence of the admin users so you can use Cristal **************************************************************************/ - private static void checkAgent(String name, String pass, String role, UUID uuid, boolean joblist) throws Exception { + private static void checkAgent(String name, String pass, RolePath rolePath, UUID uuid) throws Exception { Logger.msg(1, "Bootstrap.checkAgent() - Checking for existence of '"+name+"' user."); LookupManager lookup = Gateway.getLookupManager(); @@ -293,13 +293,6 @@ public class Bootstrap Logger.msg("Bootstrap.checkAgent() - User '"+name+"' not found. Creating."); - RolePath rolePath; - try { - rolePath = lookup.getRolePath(role); - } catch (ObjectNotFoundException ex) { - rolePath = lookup.createRole(role, joblist); - } - try { AgentPath agentPath = new AgentPath(new ItemPath(uuid), name); agentPath.setPassword(pass); @@ -307,8 +300,8 @@ public class Bootstrap lookup.add(agentPath); // assign admin role - Logger.msg("Bootstrap.checkAgent() - Assigning role '"+role+"'"); - rolePath.addAgent(agentPath); + Logger.msg("Bootstrap.checkAgent() - Assigning role '"+rolePath.getName()+"'"); + Gateway.getLookupManager().addRole(agentPath, rolePath); Gateway.getStorage().put(agentPath, new Property("Name", name, true), null); Gateway.getStorage().put(agentPath, new Property("Type", "Agent", false), null); systemAgents.put(name, Gateway.getProxyManager().getAgentProxy(agentPath)); @@ -322,16 +315,23 @@ public class Bootstrap * */ public static void checkAdminAgents() throws Exception { - // check for administrative user + + // check for administrative user & admin role String adminPassword = Gateway.getProperties().getString("AdminPassword", "admin12345"); - + RolePath rootRole = new RolePath(); + if (!rootRole.exists()) Gateway.getLookupManager().createRole(rootRole); + RolePath adminRole = new RolePath(rootRole, "Admin", false); + if (!adminRole.exists()) Gateway.getLookupManager().createRole(adminRole); + // check for import user - checkAgent("system", adminPassword, "Admin", new UUID(0, 0), false); + checkAgent("system", adminPassword, adminRole, new UUID(0, 0)); - checkAgent("admin", adminPassword, "Admin", new UUID(0, 1), false); + checkAgent("admin", adminPassword, adminRole, new UUID(0, 1)); - // check for local usercode user - checkAgent(InetAddress.getLocalHost().getHostName(), "uc", "UserCode", UUID.randomUUID(), true); + // check for local usercode user & role + RolePath usercodeRole = new RolePath(rootRole, "UserCode", true); + if (!usercodeRole.exists()) Gateway.getLookupManager().createRole(usercodeRole); + checkAgent(InetAddress.getLocalHost().getHostName(), "uc", usercodeRole, UUID.randomUUID()); } public static void createServerItem() throws Exception { diff --git a/src/main/java/com/c2kernel/process/module/Module.java b/src/main/java/com/c2kernel/process/module/Module.java index 2f4592f..9a54944 100644 --- a/src/main/java/com/c2kernel/process/module/Module.java +++ b/src/main/java/com/c2kernel/process/module/Module.java @@ -99,10 +99,13 @@ public class Module extends ImportItem { for (ImportRole thisRole : imports.getRoles()) { RolePath rolePath; try { - rolePath = Gateway.getLookup().getRolePath(thisRole.name); + String roleName = thisRole.name; + if (roleName.indexOf('/') > -1) roleName = roleName.substring(roleName.indexOf('/')+1); + rolePath = Gateway.getLookup().getRolePath(roleName); if (rolePath.hasJobList() != thisRole.hasJobList()) { Logger.msg("Module.importAll() - Role '"+thisRole.name+"' has incorrect joblist settings. Correcting."); rolePath.setHasJobList(thisRole.hasJobList()); + Gateway.getLookupManager().createRole(rolePath); } } catch (ObjectNotFoundException ex) { Logger.msg("Module.importAll() - Role '"+thisRole.name+"' not found. Creating."); -- cgit v1.2.3