1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/**************************************************************************
* 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.common.ObjectNotFoundException;
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.ItemPath;
import com.c2kernel.lookup.RolePath;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.PropertyArrayList;
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();
}
/**
* Params:
* <ol><li>New Agent name</li>
* <li>Description version to use</li>
* <li>Comma-delimited Role names to assign to the agent. Must already exist.</li>
* <li>Initial properties to set in the new Agent</li>
* </ol>
* @see com.c2kernel.lifecycle.instance.predefined.item.CreateItemFromDescription#runActivityLogic(com.c2kernel.lookup.AgentPath, int, int, java.lang.String)
*/
@Override
protected String runActivityLogic(AgentPath agent, ItemPath itemPath,
int transitionID, String requestData) throws InvalidDataException {
String[] input = getDataList(requestData);
String newName = input[0];
String descVer = input[1];
String roles = input[2];
PropertyArrayList initProps =
input.length > 3 ? getInitProperties(input[3]):new PropertyArrayList();
Logger.msg(1, "CreateAgentFromDescription::request() - Starting.");
try {
// check if given roles exist
String[] roleArr = roles.split(",");
for(int i=0; i<roleArr.length; i++) {
RolePath thisRole = Gateway.getLookup().getRolePath(roleArr[i]);
if (!thisRole.exists()) throw new InvalidDataException("Role "+roleArr[i]+" does not exist");
}
// check if the path is already taken
try {
Gateway.getLookup().getAgentPath(newName);
throw new ObjectAlreadyExistsException("The agent name " +newName+ " exists already.", "");
} catch (ObjectNotFoundException ex) { }
// generate new entity key
Logger.msg(6, "CreateItemFromDescription - Requesting new agent path");
AgentPath newAgentPath = new AgentPath(new ItemPath(), newName);
// 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 = factory.createAgent(newAgentPath);
Gateway.getLookupManager().add(newAgentPath);
// initialise it with its properties and workflow
Logger.msg(3, "CreateItemFromDescription - Initializing Item");
newAgent.initialise(
agent.getSystemKey(),
Gateway.getMarshaller().marshall(getNewProperties(itemPath, descVer, initProps, newName, agent)),
Gateway.getMarshaller().marshall(getNewWorkflow(itemPath, descVer)),
Gateway.getMarshaller().marshall(getNewCollections(itemPath, descVer))
);
// add roles if given
for(int i=1; i<input.length; i++) {
newAgent.addRole(input[i]);
}
return requestData;
} catch (Exception e) {
Logger.error(e);
throw new InvalidDataException(e.getMessage(), "");
}
}
}
|