1 package org
.argeo
.osgi
.useradmin
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collections
;
5 import java
.util
.Dictionary
;
6 import java
.util
.Enumeration
;
7 import java
.util
.Iterator
;
10 import javax
.naming
.NamingEnumeration
;
11 import javax
.naming
.NamingException
;
12 import javax
.naming
.directory
.Attribute
;
13 import javax
.naming
.directory
.Attributes
;
14 import javax
.naming
.directory
.BasicAttribute
;
16 class AttributeDictionary
extends Dictionary
<String
,Object
> {
17 private final Attributes attributes
;
18 private final List
<String
> effectiveKeys
= new ArrayList
<String
>();
19 private final List
<String
> attrFilter
;
20 private final Boolean includeFilter
;
22 public AttributeDictionary(Attributes attributes
, List
<String
> attrFilter
,
23 Boolean includeFilter
) {
24 this.attributes
= attributes
;
25 this.attrFilter
= attrFilter
;
26 this.includeFilter
= includeFilter
;
28 NamingEnumeration
<String
> ids
= attributes
.getIDs();
29 while (ids
.hasMore()) {
30 String id
= ids
.next();
31 if (includeFilter
&& attrFilter
.contains(id
))
32 effectiveKeys
.add(id
);
33 else if (!includeFilter
&& !attrFilter
.contains(id
))
34 effectiveKeys
.add(id
);
36 } catch (NamingException e
) {
37 throw new ArgeoUserAdminException(
38 "Cannot initialise attribute dictionary", e
);
44 return effectiveKeys
.size();
48 public boolean isEmpty() {
49 return effectiveKeys
.size() == 0;
53 public Enumeration
<String
> keys() {
54 return Collections
.enumeration(effectiveKeys
);
58 public Enumeration
<Object
> elements() {
59 final Iterator
<String
> it
= effectiveKeys
.iterator();
60 return new Enumeration
<Object
>() {
63 public boolean hasMoreElements() {
68 public Object
nextElement() {
69 String key
= it
.next();
71 return attributes
.get(key
).get();
72 } catch (NamingException e
) {
73 throw new ArgeoUserAdminException(
74 "Cannot get value for key " + key
, e
);
82 public Object
get(Object key
) {
84 Attribute attr
= attributes
.get(key
.toString());
88 } catch (NamingException e
) {
89 throw new ArgeoUserAdminException("Cannot get value for attribute "
95 public Object
put(String key
, Object value
) {
96 if (!(value
instanceof String
|| value
instanceof byte[]))
97 throw new IllegalArgumentException("Value must be String or byte[]");
99 if (includeFilter
&& !attrFilter
.contains(key
))
100 throw new IllegalArgumentException("Key " + key
+ " not included");
101 else if (!includeFilter
&& attrFilter
.contains(key
))
102 throw new IllegalArgumentException("Key " + key
+ " excluded");
105 Attribute attribute
= attributes
.get(key
.toString());
106 attribute
= new BasicAttribute(key
.toString());
107 attribute
.add(value
);
108 Attribute previousAttribute
= attributes
.put(attribute
);
109 if (previousAttribute
!= null)
110 return previousAttribute
.get();
113 } catch (NamingException e
) {
114 throw new ArgeoUserAdminException("Cannot get value for attribute "
120 public Object
remove(Object key
) {
121 if (includeFilter
&& !attrFilter
.contains(key
))
122 throw new IllegalArgumentException("Key " + key
+ " not included");
123 else if (!includeFilter
&& attrFilter
.contains(key
))
124 throw new IllegalArgumentException("Key " + key
+ " excluded");
127 Attribute attr
= attributes
.remove(key
.toString());
132 } catch (NamingException e
) {
133 throw new ArgeoUserAdminException("Cannot remove attribute " + key
,