summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewAgent.java
blob: baea8ddef40a0a55fb7bb10b268bb43c5769cac9 (plain)
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
package com.c2kernel.lifecycle.instance.predefined.entitycreation;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.agent.ActiveEntity;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.RolePath;
import com.c2kernel.process.Gateway;
import com.c2kernel.process.module.ModuleImport;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.utils.Logger;

public class NewAgent extends ModuleImport implements java.io.Serializable {

    public String password;

    public ArrayList<String> roles = new ArrayList<String>();
    public ArrayList<Property> properties  = new ArrayList<Property>();

    public NewAgent() {
    }

    public NewAgent(String name, String password) {
        this.name = name;
        this.password = password;
    }

	public void create(int agentId) throws ObjectNotFoundException, ObjectCannotBeUpdated, NoSuchAlgorithmException, CannotManageException, ObjectAlreadyExistsException {
        AgentPath newAgent = Gateway.getLDAPLookup().getNextKeyManager().generateNextAgentKey();
        newAgent.setAgentName(name);
        newAgent.setPassword(password);
        ActiveEntity newAgentEnt = (ActiveEntity)Gateway.getCorbaServer().createEntity(newAgent);
        Gateway.getLDAPLookup().add(newAgent);
        // assemble properties
        properties.add(new com.c2kernel.property.Property("Name", name, true));
        properties.add(new com.c2kernel.property.Property("Type", "Agent", false));
        try {
            newAgentEnt.initialise(agentId, Gateway.getMarshaller().marshall(new PropertyArrayList(properties)), null, null);
        } catch (Exception ex) {
            Logger.error(ex);
            throw new CannotManageException("Error initialising new agent");
        }
        for (String role : roles) {
            RolePath thisRole;
            try {
                thisRole = Gateway.getLDAPLookup().getRoleManager().getRolePath(role);
            } catch (ObjectNotFoundException ex) {
                throw new ObjectNotFoundException("Role "+role+" does not exist.");
            }
            thisRole.addAgent(newAgent);
        }

    }
}