From 14e2fe28c2b7ecaae53a10d1aa7aa4b921b6efbc Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 11 Jun 2014 23:38:35 +0200 Subject: Separate PredefinedStepContainers for Agents and Items (and Server Item) --- .../agent/AgentPredefinedStepContainer.java | 29 ++++ .../agent/CreateAgentFromDescription.java | 171 +++++++++++++++++++++ .../instance/predefined/agent/RemoveAgent.java | 65 ++++++++ .../predefined/agent/SetAgentPassword.java | 55 +++++++ .../instance/predefined/agent/SetAgentRoles.java | 72 +++++++++ 5 files changed, 392 insertions(+) create mode 100644 src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/AgentPredefinedStepContainer.java create mode 100644 src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java create mode 100644 src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java create mode 100644 src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java create mode 100644 src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentRoles.java (limited to 'src/main/java/com/c2kernel/lifecycle/instance/predefined/agent') diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/AgentPredefinedStepContainer.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/AgentPredefinedStepContainer.java new file mode 100644 index 0000000..2b9acbc --- /dev/null +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/AgentPredefinedStepContainer.java @@ -0,0 +1,29 @@ +package com.c2kernel.lifecycle.instance.predefined.agent; + +import com.c2kernel.lifecycle.instance.predefined.PredefinedStepContainer; +import com.c2kernel.lifecycle.instance.predefined.item.CreateItemFromDescription; + + +/************************************************************************** + * + * $Revision: 1.2 $ + * $Date: 2005/06/02 10:19:33 $ + * + * Copyright (C) 2003 CERN - European Organization for Nuclear Research + * All rights reserved. + **************************************************************************/ + +// public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/06/02 10:19:33 $ $Author: abranson $"; +public class AgentPredefinedStepContainer extends PredefinedStepContainer { + + + @Override + public void createChildren() + { + super.createChildren(); + predInit("CreateItemFromDescription", "Create a new item using this item as its description", new CreateItemFromDescription()); + predInit("RemoveAgent", "Deletes the Agent", new RemoveAgent()); + predInit("SetAgentPassword", "Changes the Agent's password", new SetAgentPassword()); + predInit("SetAgentRoles", "Sets the roles of the Agent", new SetAgentRoles()); + } +} 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 new file mode 100644 index 0000000..90b9d45 --- /dev/null +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java @@ -0,0 +1,171 @@ +/************************************************************************** + * CreateItemFromDescription + * + * $Workfile$ + * $Revision: 1.47 $ + * $Date: 2005/10/13 08:13:58 $ + * + * Copyright (C) 2001 CERN - European Organization for Nuclear Research + * All rights reserved. + **************************************************************************/ + +package com.c2kernel.lifecycle.instance.predefined.agent; + +import java.util.ArrayList; + +import com.c2kernel.collection.Collection; +import com.c2kernel.collection.CollectionArrayList; +import com.c2kernel.collection.CollectionDescription; +import com.c2kernel.collection.CollectionMember; +import com.c2kernel.common.AccessRightsException; +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectAlreadyExistsException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.CorbaServer; +import com.c2kernel.entity.TraceableEntity; +import com.c2kernel.lifecycle.CompositeActivityDef; +import com.c2kernel.lifecycle.instance.predefined.PredefinedStep; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.DomainPath; +import com.c2kernel.lookup.ItemPath; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.TransactionManager; +import com.c2kernel.process.Gateway; +import com.c2kernel.property.Property; +import com.c2kernel.property.PropertyArrayList; +import com.c2kernel.property.PropertyDescriptionList; +import com.c2kernel.property.PropertyUtility; +import com.c2kernel.utils.LocalObjectLoader; +import com.c2kernel.utils.Logger; + +/************************************************************************** + * + * @author $Author: abranson $ $Date: 2005/10/13 08:13:58 $ + * @version $Revision: 1.47 $ + **************************************************************************/ +public class CreateAgentFromDescription extends PredefinedStep +{ + public CreateAgentFromDescription() + { + super(); + } + + //requestdata is xmlstring + @Override + protected String runActivityLogic(AgentPath agent, int itemSysKey, + int transitionID, String requestData) throws InvalidDataException { + + String[] input = getDataList(requestData); + String newName = input[0]; + String domPath = input[1]; + CompositeActivityDef wfDef; + String wfDefName = null; + int wfDefVer = -1; + if (input.length > 2) // override wf + wfDefName = input[2]; + + PropertyArrayList props = new PropertyArrayList(); + Logger.msg(1, "AddNewItem::request() - Starting."); + TransactionManager storage = Gateway.getStorage(); + + try { + // check if the path is already taken + DomainPath context = new DomainPath(new DomainPath(domPath), newName); + Logger.debug(8,"context "+context.getSysKey()+" "+context.getPath()+" "+context.getString()); + if (context.getSysKey()!=-1) + throw new ObjectAlreadyExistsException("The item name " +newName+ " exists already.", ""); + + // get init objects + + // loop through collections, collecting instantiated descriptions and finding the default workflow def + CollectionArrayList colls = new CollectionArrayList(); + String[] collNames = storage.getClusterContents(itemSysKey, ClusterStorage.COLLECTION); + for (String collName : collNames) { + Collection thisCol = (Collection)storage.get(itemSysKey, ClusterStorage.COLLECTION+"/"+collName, null); + if (thisCol instanceof CollectionDescription) { + CollectionDescription thisDesc = (CollectionDescription)thisCol; + colls.put(thisDesc.newInstance()); + } + else if (thisCol.getName().equalsIgnoreCase("workflow") && wfDefName == null) { + ArrayList members = thisCol.getMembers().list; + // get the first member from the wf collection + CollectionMember wfMember = members.get(0); + wfDefName = wfMember.resolveItem().getName(); + Object wfVerObj = wfMember.getProperties().get("Version"); + try { + wfDefVer = Integer.parseInt(wfVerObj.toString()); + } catch (NumberFormatException ex) { + throw new InvalidDataException("Invalid workflow version number: "+wfVerObj.toString(), ""); + } + } + } + + // load workflow def + if (wfDefName == null) + throw new InvalidDataException("No workflow given or defined", ""); + if (wfDefVer == -1) + throw new InvalidDataException("No workflow def version given",""); + + try { + wfDef = (CompositeActivityDef)LocalObjectLoader.getActDef(wfDefName, wfDefVer); + } catch (ObjectNotFoundException ex) { + throw new InvalidDataException("Workflow def '"+wfDefName+"' item not found", ""); + } catch (ClassCastException ex) { + throw new InvalidDataException("Activity def '"+wfDefName+"' was not Composite", ""); + } + + + // copy properties -- intend to create from propdesc + PropertyDescriptionList pdList = PropertyUtility.getPropertyDescriptionOutcome(itemSysKey); + props = pdList.instanciate(); + // set Name prop or create if not present + boolean foundName = false; + for (Property prop : props.list) { + if (prop.getName().equals("Name")) { + foundName = true; + prop.setValue(newName); + } + } + if (!foundName) props.list.add(new Property("Name", newName, true)); + props.list.add( new Property("Creator", agent.getAgentName(), false)); + + /* ITEM CREATION */ + + // generate new entity key + Logger.msg(6, "CreateItemFromDescription - Requesting new sysKey"); + ItemPath entityPath = Gateway.getNextKeyManager().generateNextEntityKey(); + + // resolve the item factory + Logger.msg(6, "CreateItemFromDescription - Resolving item factory"); + + // create the Item object + Logger.msg(3, "CreateItemFromDescription - Creating Item"); + CorbaServer factory = Gateway.getCorbaServer(); + if (factory == null) throw new AccessRightsException("This process cannot create new Items", ""); + TraceableEntity newItem = (TraceableEntity)factory.createEntity(entityPath); + Gateway.getLookup().add(entityPath); + + + // initialise it with its properties and workflow + + Logger.msg(3, "CreateItemFromDescription - Initializing Item"); + + newItem.initialise( + agent.getSysKey(), + Gateway.getMarshaller().marshall(props), + Gateway.getMarshaller().marshall(wfDef.instantiate()), + Gateway.getMarshaller().marshall(colls) + ); + + // add its domain path + Logger.msg(3, "CreateItemFromDescription - Creating "+context); + context.setEntity(entityPath); + Gateway.getLookup().add(context); + return requestData; + } catch (Exception e) { + Logger.error(e); + throw new InvalidDataException(e.getMessage(), ""); + } + + } +} diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java new file mode 100644 index 0000000..80281cc --- /dev/null +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java @@ -0,0 +1,65 @@ +package com.c2kernel.lifecycle.instance.predefined.agent; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.lifecycle.instance.predefined.PredefinedStep; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.InvalidItemPathException; +import com.c2kernel.lookup.RolePath; +import com.c2kernel.persistency.ClusterStorageException; +import com.c2kernel.process.Gateway; +import com.c2kernel.utils.Logger; + +public class RemoveAgent extends PredefinedStep { + + public RemoveAgent() { + super(); + } + + @Override + protected String runActivityLogic(AgentPath agent, int itemSysKey, + int transitionID, String requestData) throws InvalidDataException { + + Logger.msg(1, "RemoveAgent::request() - Starting."); + + AgentPath targetAgent; + try { + targetAgent = new AgentPath(itemSysKey); + } catch (InvalidItemPathException ex) { + throw new InvalidDataException("Could not resolve syskey "+itemSysKey+" as an Agent."); + } + String agentName = targetAgent.getAgentName(); + + //remove from roles + for (RolePath role: targetAgent.getRoles()) { + try { + role.removeAgent(targetAgent); + } catch (ObjectCannotBeUpdated e) { + Logger.error(e); + throw new InvalidDataException("Error removing "+agentName+" from Role "+role.getName(), ""); + } catch (ObjectNotFoundException e) { + Logger.error(e); + throw new InvalidDataException("Tried to remove "+agentName+" from Role "+role.getName()+" that doesn't exist.", ""); + } + } + + //clear out all storages + try { + Gateway.getStorage().removeCluster(targetAgent.getSysKey(), "", null); + } catch (ClusterStorageException e) { + Logger.error(e); + throw new InvalidDataException("Error deleting storage for "+agentName, ""); + } + + //remove entity path + try { + Gateway.getLookup().delete(targetAgent); + } catch (ObjectCannotBeUpdated e) { + throw new InvalidDataException("Error deleting AgentPath for "+agentName, ""); + } + return requestData; + + } + +} diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java new file mode 100644 index 0000000..102e8e2 --- /dev/null +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java @@ -0,0 +1,55 @@ +package com.c2kernel.lifecycle.instance.predefined.agent; + +import java.security.NoSuchAlgorithmException; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.lifecycle.instance.predefined.PredefinedStep; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.InvalidItemPathException; +import com.c2kernel.process.Gateway; +import com.c2kernel.utils.Logger; + +public class SetAgentPassword extends PredefinedStep { + + public SetAgentPassword() { + super(); + } + + @Override + protected String runActivityLogic(AgentPath agent, int itemSysKey, + int transitionID, String requestData) throws InvalidDataException { + + Logger.msg(1, "SetAgentPassword::request() - Starting."); + + AgentPath targetAgent; + try { + targetAgent = new AgentPath(itemSysKey); + } catch (InvalidItemPathException ex) { + throw new InvalidDataException("Could not resolve syskey "+itemSysKey+" as an Agent."); + } + String agentName = targetAgent.getAgentName(); + + String[] params = getDataList(requestData); + if (params.length!=1) + throw new InvalidDataException("Requires 1 param: new password", ""); + + try { + Gateway.getLookup().setAgentPassword(targetAgent, params[0]); + } catch (ObjectNotFoundException e) { + Logger.error(e); + throw new InvalidDataException("Agent "+agentName+" not found.", ""); + } catch (ObjectCannotBeUpdated e) { + Logger.error(e); + throw new InvalidDataException("Error updating LDAP entry.", ""); + } catch (NoSuchAlgorithmException e) { + Logger.error(e); + throw new InvalidDataException("Cryptographic libraries for password hashing not found.", ""); + } + + params[1] = "REDACTED"; // censor user's password from outcome + return bundleData(params); + } + +} diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentRoles.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentRoles.java new file mode 100644 index 0000000..8bcba30 --- /dev/null +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentRoles.java @@ -0,0 +1,72 @@ +package com.c2kernel.lifecycle.instance.predefined.agent; + +import java.util.ArrayList; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.lifecycle.instance.predefined.PredefinedStep; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.InvalidItemPathException; +import com.c2kernel.lookup.RolePath; +import com.c2kernel.process.Gateway; +import com.c2kernel.utils.Logger; + +public class SetAgentRoles extends PredefinedStep { + + public SetAgentRoles() { + super(); + } + + @Override + protected String runActivityLogic(AgentPath agent, int itemSysKey, + int transitionID, String requestData) throws InvalidDataException { + + Logger.msg(1, "SetAgentRoles::request() - Starting."); + + String[] params = getDataList(requestData); + AgentPath targetAgent; + try { + targetAgent = new AgentPath(itemSysKey); + } catch (InvalidItemPathException ex) { + throw new InvalidDataException("Could not resolve syskey "+itemSysKey+" as an Agent."); + } + + RolePath[] currentRoles = targetAgent.getRoles(); + ArrayList requestedRoles = new ArrayList(); + for (int i=0; i rolesToRemove = new ArrayList(); + for (RolePath existingRole : currentRoles) { // + if (requestedRoles.contains(existingRole)) // if we have it, and it's requested, then it will be kept + requestedRoles.remove(existingRole); // so remove it from request - this will be left with roles to be added + else + rolesToRemove.add(existingRole); // else this role will be removed + } + + // remove roles not in new list + for (RolePath roleToRemove : rolesToRemove) + try { + roleToRemove.removeAgent(targetAgent); + } catch (Exception e) { + Logger.error(e); + throw new InvalidDataException("Error removing role "+roleToRemove.getName(), ""); + } + + // add requested roles we don't already have + for (RolePath roleToAdd : requestedRoles) + try { + roleToAdd.addAgent(targetAgent); + } catch (Exception e) { + Logger.error(e); + throw new InvalidDataException("Error adding role "+roleToAdd.getName(), ""); + } + + return requestData; + } + +} -- cgit v1.2.3