]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AttributeDictionary.java
99e5edb6292dc821b0f6d5fd167179f6681f4b19
[lgpl/argeo-commons.git] / AttributeDictionary.java
1 package org.argeo.osgi.useradmin;
2
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;
8 import java.util.List;
9
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;
15
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;
21
22 public AttributeDictionary(Attributes attributes, List<String> attrFilter,
23 Boolean includeFilter) {
24 this.attributes = attributes;
25 this.attrFilter = attrFilter;
26 this.includeFilter = includeFilter;
27 try {
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);
35 }
36 } catch (NamingException e) {
37 throw new ArgeoUserAdminException(
38 "Cannot initialise attribute dictionary", e);
39 }
40 }
41
42 @Override
43 public int size() {
44 return effectiveKeys.size();
45 }
46
47 @Override
48 public boolean isEmpty() {
49 return effectiveKeys.size() == 0;
50 }
51
52 @Override
53 public Enumeration<String> keys() {
54 return Collections.enumeration(effectiveKeys);
55 }
56
57 @Override
58 public Enumeration<Object> elements() {
59 final Iterator<String> it = effectiveKeys.iterator();
60 return new Enumeration<Object>() {
61
62 @Override
63 public boolean hasMoreElements() {
64 return it.hasNext();
65 }
66
67 @Override
68 public Object nextElement() {
69 String key = it.next();
70 try {
71 return attributes.get(key).get();
72 } catch (NamingException e) {
73 throw new ArgeoUserAdminException(
74 "Cannot get value for key " + key, e);
75 }
76 }
77
78 };
79 }
80
81 @Override
82 public Object get(Object key) {
83 try {
84 Attribute attr = attributes.get(key.toString());
85 if (attr == null)
86 return null;
87 return attr.get();
88 } catch (NamingException e) {
89 throw new ArgeoUserAdminException("Cannot get value for attribute "
90 + key, e);
91 }
92 }
93
94 @Override
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[]");
98
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");
103
104 try {
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();
111 else
112 return null;
113 } catch (NamingException e) {
114 throw new ArgeoUserAdminException("Cannot get value for attribute "
115 + key, e);
116 }
117 }
118
119 @Override
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");
125
126 try {
127 Attribute attr = attributes.remove(key.toString());
128 if (attr != null)
129 return attr.get();
130 else
131 return null;
132 } catch (NamingException e) {
133 throw new ArgeoUserAdminException("Cannot remove attribute " + key,
134 e);
135 }
136 }
137 }