summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java
blob: ac91c29fc0e8eb766a7b422cafde788257992fcd (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.c2kernel.lookup.ldap;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.c2kernel.common.ObjectNotFound;
import com.c2kernel.common.PersistencyException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.lookup.Lookup;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.process.Gateway;
import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.property.Property;
import com.c2kernel.utils.Logger;

public class LDAPClusterStorage extends ClusterStorage {
    LDAPPropertyManager ldapStore;

    @Override
	public void open(Authenticator auth) throws PersistencyException {
    	Lookup lookup = Gateway.getLookup();
    	if (lookup instanceof LDAPLookup)
    		ldapStore = ((LDAPLookup)lookup).getPropManager();
    	else
    		throw new PersistencyException("Cannot use LDAP cluster storage without LDAP Lookup");

    }

    @Override
	public void close() throws PersistencyException {
    }

    // introspection
    @Override
	public short queryClusterSupport(String clusterType) {
        if (clusterType.equals(PROPERTY))
            return READWRITE;
        else
            return NONE;
    }

    @Override
	public String getName() {
        return "LDAP Cluster Storage";
    }

    @Override
	public String getId() {
        return "LDAP";
    }

    // retrieve object by path
    @Override
	public C2KLocalObject get(ItemPath thisItem, String path) throws PersistencyException {
    	Logger.msg(6, "LDAPClusterStorage.get() - "+thisItem+"/"+path);
		StringTokenizer tok = new StringTokenizer(path, "/");
		int pathLength = tok.countTokens();
		if (pathLength != 2)
			throw new PersistencyException("Path length was invalid: "+path);
		String type = tok.nextToken();

		String objName = tok.nextToken();
		C2KLocalObject newObj;

		if (type.equals(PROPERTY)) {
            try {
            	Property newProperty = ldapStore.getProperty(thisItem, objName);
                newObj = newProperty;
            } catch (ObjectNotFound ex) {
                throw new PersistencyException("Property "+objName+" not found in "+thisItem);
            }

		}
		else
			throw new PersistencyException("Cluster type "+type+" not supported.");

		return newObj;
    }
    // store object by path
    @Override
	public void put(ItemPath thisItem, C2KLocalObject obj) throws PersistencyException {
    	Logger.msg(6, "LDAPClusterStorage.put() - "+thisItem+"/"+ClusterStorage.getPath(obj));

		String type = obj.getClusterType();

		if (type.equals(PROPERTY)) {
			try {
                ldapStore.setProperty(thisItem, (Property)obj);
            } catch (Exception e1) {
                Logger.error(e1);
                throw new PersistencyException("LDAPClusterStorage - could not write property");
            }
		}
		else
			throw new PersistencyException("Cluster type "+type+" not supported.");

    }
    // delete cluster
    @Override
	public void delete(ItemPath thisItem, String path) throws PersistencyException {
		StringTokenizer tok = new StringTokenizer(path, "/");
		int pathLength = tok.countTokens();
		if (pathLength != 2)
			throw new PersistencyException("Path length was invalid: "+path);
		String type = tok.nextToken();

		if (type.equals(PROPERTY)) {
			try {
                ldapStore.deleteProperty(thisItem, tok.nextToken());
            } catch (Exception e1) {
                Logger.error(e1);
                throw new PersistencyException("LDAPClusterStorage - could not delete property");
            }
		}
		else
			throw new PersistencyException("Cluster type "+type+" not supported.");

    }

    /* navigation */

    // directory listing
    @Override
	public String[] getClusterContents(ItemPath thisItem, String path) throws PersistencyException {
    	Logger.msg(6, "LDAPClusterStorage.getClusterContents() - "+thisItem+"/"+path);
        StringTokenizer tok = new StringTokenizer(path, "/");
        int pathLength = tok.countTokens();
        if (pathLength > 1)
            return new String[0];

        String type = getClusterType(path);

        try {
        	if (type.equals(PROPERTY))
        		return ldapStore.getPropertyNames(thisItem);
        	else
        		if (type.equals("")) { // root query
        			String[] allClusters = new String[0];
        			ArrayList<String> clusterList = new ArrayList<String>();
        			if (ldapStore.hasProperties(thisItem))
        				clusterList.add(PROPERTY);
        			allClusters = clusterList.toArray(allClusters);
        			return allClusters;
        		}
        		else
        			throw new PersistencyException("Cluster type "+type+" not supported.");
        } catch (ObjectNotFound e) {
        	throw new PersistencyException("Item "+thisItem+" does not exist");
        }
    }
}