summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent
diff options
context:
space:
mode:
authorogattaz <olivier@gattaz.com>2014-07-07 10:59:14 +0200
committerogattaz <olivier@gattaz.com>2014-07-07 10:59:14 +0200
commit6772bfb46b72d859c316a9f6573d0c6be477ad5c (patch)
treedafa343584216685e68b6edae37570eb97cf9d0c /src/main/java/com/c2kernel/lifecycle/instance/predefined/agent
parent2fd193d7936084de91eae46e8c2763914d87ab71 (diff)
parent0b689a787288f5a4ba568157905c3a0577f83821 (diff)
Merge branch 'master' of ssh://dev.cccs.uwe.ac.uk:22/var/git/cristal-kernel
Diffstat (limited to 'src/main/java/com/c2kernel/lifecycle/instance/predefined/agent')
-rw-r--r--src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/AgentPredefinedStepContainer.java29
-rw-r--r--src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java95
-rw-r--r--src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java68
-rw-r--r--src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java58
-rw-r--r--src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentRoles.java72
5 files changed, 322 insertions, 0 deletions
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..f311dc1
--- /dev/null
+++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java
@@ -0,0 +1,95 @@
+/**************************************************************************
+ * 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 com.c2kernel.common.AccessRightsException;
+import com.c2kernel.common.InvalidDataException;
+import com.c2kernel.common.ObjectAlreadyExistsException;
+import com.c2kernel.entity.CorbaServer;
+import com.c2kernel.entity.agent.ActiveEntity;
+import com.c2kernel.lifecycle.instance.predefined.item.CreateItemFromDescription;
+import com.c2kernel.lookup.AgentPath;
+import com.c2kernel.lookup.DomainPath;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.utils.Logger;
+
+/**************************************************************************
+ *
+ * @author $Author: abranson $ $Date: 2005/10/13 08:13:58 $
+ * @version $Revision: 1.47 $
+ **************************************************************************/
+public class CreateAgentFromDescription extends CreateItemFromDescription
+{
+ 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];
+ String wfDefName = null;
+ int wfDefVer = -1;
+ if (input.length > 2) // override wf
+ wfDefName = input[2];
+
+ Logger.msg(1, "CreateAgentFromDescription::request() - Starting.");
+
+ 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 agent name " +newName+ " exists already.", "");
+
+ // generate new entity key
+ Logger.msg(6, "CreateItemFromDescription - Requesting new sysKey");
+ AgentPath newAgentPath = Gateway.getNextKeyManager().generateNextAgentKey();
+
+ // 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", "");
+ ActiveEntity newAgent = (ActiveEntity)factory.createEntity(newAgentPath);
+ Gateway.getLookupManager().add(newAgentPath);
+
+
+ // initialise it with its properties and workflow
+
+ Logger.msg(3, "CreateItemFromDescription - Initializing Item");
+
+ newAgent.initialise(
+ agent.getSysKey(),
+ Gateway.getMarshaller().marshall(getNewProperties(itemSysKey, newName, agent)),
+ Gateway.getMarshaller().marshall(getNewWorkflow(itemSysKey, wfDefName, wfDefVer)),
+ Gateway.getMarshaller().marshall(getNewCollections(itemSysKey))
+ );
+
+ // add its domain path
+ Logger.msg(3, "CreateItemFromDescription - Creating "+context);
+ context.setEntity(newAgentPath);
+ Gateway.getLookupManager().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..0630f6c
--- /dev/null
+++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/RemoveAgent.java
@@ -0,0 +1,68 @@
+package com.c2kernel.lifecycle.instance.predefined.agent;
+
+import com.c2kernel.common.CannotManageException;
+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.", "");
+ } catch (CannotManageException e) {
+ throw new InvalidDataException("Tried to alter roles in a non-server process.", "");
+ }
+ }
+
+ //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.getLookupManager().delete(targetAgent);
+ } catch (Exception 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..09fdefe
--- /dev/null
+++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/SetAgentPassword.java
@@ -0,0 +1,58 @@
+package com.c2kernel.lifecycle.instance.predefined.agent;
+
+import java.security.NoSuchAlgorithmException;
+
+import com.c2kernel.common.CannotManageException;
+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.getLookupManager().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.", "");
+ } catch (CannotManageException e) {
+ throw new InvalidDataException("Cannot set agent password in a non-server process.", "");
+ }
+
+ 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<RolePath> requestedRoles = new ArrayList<RolePath>();
+ for (int i=0; i<params.length; i++)
+ try {
+ requestedRoles.add(Gateway.getLookup().getRolePath(params[i]));
+ } catch (ObjectNotFoundException e) {
+ throw new InvalidDataException("Role "+params[i]+" not found", "");
+ }
+
+ ArrayList<RolePath> rolesToRemove = new ArrayList<RolePath>();
+ 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;
+ }
+
+}