summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java
blob: 93fcf5c623735192c2e62962a67e5e4d5b225f3c (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
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
package com.c2kernel.lookup.ldap;

import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.InvalidItemPathException;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.NextKeyManager;
import com.c2kernel.process.Gateway;
import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.utils.Logger;
import com.novell.ldap.LDAPEntry;

/**************************************************************************
 *
 * $Revision: 1.2 $
 * $Date: 2005/04/27 13:47:24 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

//    public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/04/27 13:47:24 $ $Author: abranson $";
public class LDAPNextKeyManager implements NextKeyManager {

	protected LDAPAuthManager ldap;
	protected String lastKeyPath;

    public LDAPNextKeyManager() {
    }
    
    @Override
	public void open(Authenticator auth) {
   		this.ldap = (LDAPAuthManager)auth;
        LDAPProperties props = new LDAPProperties(Gateway.getProperties());
        this.lastKeyPath = "cn=last,cn=entity,"+props.mLocalPath;
    }

    @Override
	public synchronized ItemPath generateNextEntityKey()
    throws ObjectCannotBeUpdated, ObjectNotFoundException
    {
        ItemPath lastKey = getLastEntityPath();

        try {
            lastKey.setSysKey(lastKey.getSysKey()+1);
        } catch (InvalidItemPathException ex) {
            throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full.");
        }
        //test that storage is empty for that key
        try {
			if (Gateway.getStorage().getClusterContents(lastKey.getSysKey(), "").length > 0)
				throw new ObjectCannotBeUpdated("NextKeyManager: Storage already contains data for syskey "+lastKey.getSysKey()+
						". Storage is out of sync with nextkey. Please contact an administrator", "");
		} catch (ClusterStorageException e) {
			Logger.error(e);
			throw new ObjectCannotBeUpdated("Could not check storage for prior data for the next generated systemKey: "+e.getMessage());
		}
        
        //set the last key
        writeLastEntityKey(lastKey.getSysKey());

        return lastKey;
    }

    @Override
	public synchronized AgentPath generateNextAgentKey()
    throws ObjectCannotBeUpdated, ObjectNotFoundException {
        ItemPath newEntity = generateNextEntityKey();
        return new AgentPath(newEntity);
    }

    @Override
	public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException {
        LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath);
        LDAPLookupUtils.setAttributeValue(ldap.getAuthObject(), lastKeyEntry,"intsyskey",Integer.toString(sysKey));
    }

    @Override
	public ItemPath getLastEntityPath() throws ObjectNotFoundException
    {
        LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath);
        String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey");
        try {
            int sysKey = Integer.parseInt(lastKey);
            ItemPath sysPath = new ItemPath(sysKey);
            return sysPath;
        } catch (InvalidItemPathException ex) {
            throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full.");
        } catch (NumberFormatException ex) {
            throw new ObjectNotFoundException("Invalid syskey in lastkey.");
        }

    }

	@Override
	public void close() {
		ldap = null;
		lastKeyPath = null;
	}

}