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
|
package com.c2kernel.lookup.ldap;
import java.util.ArrayList;
import java.util.Enumeration;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFound;
import com.c2kernel.lookup.ItemPath;
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 final LDAPLookup ldap;
protected final LDAPAuthManager auth;
public LDAPPropertyManager(LDAPLookup ldap, LDAPAuthManager auth) {
super();
this.ldap = ldap;
this.auth = auth;
}
/**
* @param thisItem - EntityPath of the subject entity
* @return
* @throws ObjectNotFound
*/
public boolean hasProperties(ItemPath thisItem) throws ObjectNotFound {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
return entityEntry.getAttribute("cristalprop") != null;
}
/**
* @param thisItem - EntityPath of the subject entity
* @return array of Property
* @throws ObjectNotFound
*/
public String[] getPropertyNames(ItemPath thisItem) throws ObjectNotFound {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
ArrayList<String> propbag = new ArrayList<String>();
LDAPAttribute props = entityEntry.getAttribute("cristalprop");
for (Enumeration<?> e = props.getStringValues(); e.hasMoreElements();) {
String thisProp = (String)e.nextElement();
String thisName = thisProp.substring(0, thisProp.indexOf(':'));
if (thisName.startsWith("!") && thisName.length()>1) thisName = thisName.substring(1);
propbag.add(thisName);
}
String[] retArr = new String[props.size()];
return propbag.toArray(retArr);
}
/**
* @param thisItem - EntityPath of the subject entity
* @param propName - the name of the property to retrieve
* @return The Property object
* @throws ObjectNotFound
*/
public Property getProperty(ItemPath thisItem, String name) throws ObjectNotFound {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
return getProperty(entityEntry, name);
}
/**
* @param thisItem - EntityPath of the subject entity
* @param name - the property name to delete
* @throws ObjectNotFound
* @throws ObjectCannotBeUpdated
*/
public void deleteProperty(ItemPath thisItem, String name) throws ObjectNotFound, ObjectCannotBeUpdated {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
Property prop = getProperty(entityEntry, name);
Logger.msg(6, "LDAPLookupUtils.deleteProperty("+name+") - Deleting property");
LDAPLookupUtils.removeAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
}
private static String getPropertyAttrValue(Property prop) {
return (prop.isMutable()?"":"!")+prop.getName()+":"+prop.getValue();
}
/**
* @param thisItem - EntityPath of the subject entity
* @param prop - the property to store
* @throws ObjectNotFound
* @throws ObjectCannotBeUpdated
*/
public void setProperty(ItemPath thisItem, Property prop) throws ObjectNotFound, ObjectCannotBeUpdated {
LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
try {
Property oldProp = getProperty(entityEntry, prop.getName());
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - Removing old value '"+oldProp.getValue()+"'");
LDAPLookupUtils.removeAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(oldProp));
} catch (ObjectNotFound ex) {
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - creating new property.");
}
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - setting to '"+prop.getValue()+"'");
LDAPLookupUtils.addAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
}
public static Property getProperty(LDAPEntry myEntry, String propName) throws ObjectNotFound {
// delete existing props
LDAPAttribute props = myEntry.getAttribute("cristalprop");
if (props == null)
throw new ObjectNotFound("Property "+propName+" does not exist");
String propPrefix = propName+":";
String roPropPrefix = "!"+propPrefix;
String val = null, name = null; boolean mutable = false;
for (Enumeration<?> e = props.getStringValues(); name==null && e.hasMoreElements();) {
String attrVal = (String)e.nextElement();
if (attrVal.toLowerCase().startsWith(propPrefix.toLowerCase())) {
name = attrVal.substring(0, propPrefix.length()-1);
val = attrVal.substring(propPrefix.length());
mutable = true; break;
}
if (attrVal.toLowerCase().startsWith(roPropPrefix.toLowerCase())) {
name = attrVal.substring(1, roPropPrefix.length()-1);
val = attrVal.substring(roPropPrefix.length());
mutable = false; break;
}
}
if (name == null)
throw new ObjectNotFound("Property "+propName+" does not exist");
Logger.msg(6, "Loaded "+(mutable?"":"Non-")+"Mutable Property: "+name+"="+val);
return new Property(name, val, mutable);
}
}
|