]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/util/naming/AttributesDictionary.java
a6fddb440804801f81eb01b3ca05283f6099cfd9
1 package org
.argeo
.util
.naming
;
3 import java
.util
.Dictionary
;
4 import java
.util
.Enumeration
;
6 import javax
.naming
.NamingEnumeration
;
7 import javax
.naming
.NamingException
;
8 import javax
.naming
.directory
.Attribute
;
9 import javax
.naming
.directory
.Attributes
;
10 import javax
.naming
.directory
.BasicAttribute
;
12 public class AttributesDictionary
extends Dictionary
<String
, Object
> {
13 private final Attributes attributes
;
15 /** The provided attributes is wrapped, not copied. */
16 public AttributesDictionary(Attributes attributes
) {
17 if (attributes
== null)
18 throw new IllegalArgumentException("Attributes cannot be null");
19 this.attributes
= attributes
;
24 return attributes
.size();
28 public boolean isEmpty() {
29 return attributes
.size() == 0;
33 public Enumeration
<String
> keys() {
34 NamingEnumeration
<String
> namingEnumeration
= attributes
.getIDs();
35 return new Enumeration
<String
>() {
38 public boolean hasMoreElements() {
39 return namingEnumeration
.hasMoreElements();
43 public String
nextElement() {
44 return namingEnumeration
.nextElement();
51 public Enumeration
<Object
> elements() {
52 NamingEnumeration
<String
> namingEnumeration
= attributes
.getIDs();
53 return new Enumeration
<Object
>() {
56 public boolean hasMoreElements() {
57 return namingEnumeration
.hasMoreElements();
61 public Object
nextElement() {
62 String key
= namingEnumeration
.nextElement();
70 /** @returns a <code>String</code> or <code>String[]</code> */
71 public Object
get(Object key
) {
74 throw new IllegalArgumentException("Key cannot be null");
75 Attribute attr
= attributes
.get(key
.toString());
79 throw new IllegalStateException("There must be at least one value");
80 else if (attr
.size() == 1) {
81 return attr
.get().toString();
83 String
[] res
= new String
[attr
.size()];
84 for (int i
= 0; i
< attr
.size(); i
++) {
85 Object value
= attr
.get();
87 throw new RuntimeException("Values cannot be null");
88 res
[i
] = attr
.get(i
).toString();
92 } catch (NamingException e
) {
93 throw new RuntimeException("Cannot get value for " + key
, e
);
98 public Object
put(String key
, Object value
) {
100 throw new IllegalArgumentException("Key cannot be null");
102 throw new IllegalArgumentException("Value cannot be null");
104 Object oldValue
= get(key
);
105 Attribute attr
= attributes
.get(key
);
107 attr
= new BasicAttribute(key
);
108 attributes
.put(attr
);
111 if (value
instanceof String
[]) {
112 String
[] values
= (String
[]) value
;
113 // clean additional values
114 for (int i
= values
.length
; i
< attr
.size(); i
++)
117 for (int i
= 0; i
< values
.length
; i
++) {
118 attr
.set(i
, values
[i
]);
121 if (attr
.size() != 1)
122 throw new IllegalArgumentException("Attribute " + key
+ " is multi-valued");
123 attr
.set(0, value
.toString());
129 public Object
remove(Object key
) {
131 throw new IllegalArgumentException("Key cannot be null");
132 Object oldValue
= get(key
);
133 if (oldValue
== null)
135 return attributes
.remove(key
.toString());
139 * Copy the <b>content</b> of an {@link javax.naming.Attributes} to the
140 * provided {@link Dictionary}.
142 public static void copy(Attributes attributes
, Dictionary
<String
, Object
> dictionary
) {
143 AttributesDictionary ad
= new AttributesDictionary(attributes
);
144 Enumeration
<String
> keys
= ad
.keys();
145 while (keys
.hasMoreElements()) {
146 String key
= keys
.nextElement();
147 dictionary
.put(key
, ad
.get(key
));
152 * Copy a {@link Dictionary} into an {@link javax.naming.Attributes}.
154 public static void copy(Dictionary
<String
, Object
> dictionary
, Attributes attributes
) {
155 AttributesDictionary ad
= new AttributesDictionary(attributes
);
156 Enumeration
<String
> keys
= dictionary
.keys();
157 while (keys
.hasMoreElements()) {
158 String key
= keys
.nextElement();
159 ad
.put(key
, dictionary
.get(key
));