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
|
package com.c2kernel.lookup;
import java.util.ArrayList;
import java.util.Enumeration;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.property.Property;
import com.c2kernel.utils.Logger;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPEntry;
/**************************************************************************
*
* $Revision: 1.3 $
* $Date: 2006/03/03 13:52:21 $
*
* Copyright (C) 2003 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
public class LDAPPropertyManager {
/**
*
*/
protected LDAPLookup ldap;
public LDAPPropertyManager(LDAPLookup ldap) {
super();
this.ldap = ldap;
}
/**
* @param thisEntity - EntityPath of the subject entity
* @return
* @throws ObjectNotFoundException
*/
public boolean hasProperties(EntityPath thisEntity) throws ObjectNotFoundException {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
return entityEntry.getAttribute("cristalprop") != null;
}
/**
* @param thisEntity - EntityPath of the subject entity
* @return array of Property
* @throws ObjectNotFoundException
*/
public String[] getPropertyNames(EntityPath thisEntity) throws ObjectNotFoundException {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
ArrayList propbag = new ArrayList();
LDAPAttribute props = entityEntry.getAttribute("cristalprop");
for (Enumeration e = props.getStringValues(); e.hasMoreElements();) {
String thisProp = (String)e.nextElement();
propbag.add(thisProp.substring(0, thisProp.indexOf(':')));
}
String[] retArr = new String[props.size()];
return (String[])propbag.toArray(retArr);
}
/**
* @param thisEntity - EntityPath of the subject entity
* @param propName - the name of the property to retrieve
* @return String the property value
* @throws ObjectNotFoundException
*/
public String getPropertyValue(EntityPath thisEntity, String name) throws ObjectNotFoundException {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
return getPropertyAttr(entityEntry, name);
}
/**
* @param thisEntity - EntityPath of the subject entity
* @param name - the property name to delete
* @throws ObjectNotFoundException
* @throws ObjectCannotBeUpdated
*/
public void deleteProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException, ObjectCannotBeUpdated {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
String propVal = getPropertyAttr(entityEntry, name);
Logger.msg(6, "LDAPLookupUtils.deleteProperty("+name+") - Deleting property");
LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", name+":"+propVal);
}
/**
* @param thisEntity - EntityPath of the subject entity
* @param prop - the property to store
* @throws ObjectNotFoundException
* @throws ObjectCannotBeUpdated
*/
public void setProperty(EntityPath thisEntity, Property prop) throws ObjectNotFoundException, ObjectCannotBeUpdated {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
try {
String propVal = getPropertyAttr(entityEntry, prop.getName());
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - Removing old value '"+propVal+"'");
LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", prop.getName()+":"+propVal);
} catch (ObjectNotFoundException ex) {
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - creating new property.");
}
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - setting to '"+prop.getValue()+"'");
LDAPLookupUtils.addAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", prop.getName()+":"+prop.getValue());
}
private String getPropertyAttr(LDAPEntry myEntry, String propName) throws ObjectNotFoundException {
// delete existing props
LDAPAttribute props = myEntry.getAttribute("cristalprop");
if (props == null)
throw new ObjectNotFoundException("Property "+propName+" does not exist", "");
String propPrefix = propName+":";
for (Enumeration e = props.getStringValues(); e.hasMoreElements();) {
String val = (String)e.nextElement();
if (val.toLowerCase().startsWith(propPrefix.toLowerCase()))
return val.substring(propPrefix.length());
}
throw new ObjectNotFoundException("Property "+propName+" does not exist", "");
}
}
|