View Javadoc
1   package org.argeo.naming;
2   
3   import java.util.Dictionary;
4   import java.util.Enumeration;
5   
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;
11  
12  public class AttributesDictionary extends Dictionary<String, Object> {
13  	private final Attributes attributes;
14  
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;
20  	}
21  
22  	@Override
23  	public int size() {
24  		return attributes.size();
25  	}
26  
27  	@Override
28  	public boolean isEmpty() {
29  		return attributes.size() == 0;
30  	}
31  
32  	@Override
33  	public Enumeration<String> keys() {
34  		NamingEnumeration<String> namingEnumeration = attributes.getIDs();
35  		return new Enumeration<String>() {
36  
37  			@Override
38  			public boolean hasMoreElements() {
39  				return namingEnumeration.hasMoreElements();
40  			}
41  
42  			@Override
43  			public String nextElement() {
44  				return namingEnumeration.nextElement();
45  			}
46  
47  		};
48  	}
49  
50  	@Override
51  	public Enumeration<Object> elements() {
52  		NamingEnumeration<String> namingEnumeration = attributes.getIDs();
53  		return new Enumeration<Object>() {
54  
55  			@Override
56  			public boolean hasMoreElements() {
57  				return namingEnumeration.hasMoreElements();
58  			}
59  
60  			@Override
61  			public Object nextElement() {
62  				String key = namingEnumeration.nextElement();
63  				return get(key);
64  			}
65  
66  		};
67  	}
68  
69  	@Override
70  	/** @returns a <code>String</code> or <code>String[]</code> */
71  	public Object get(Object key) {
72  		try {
73  			if (key == null)
74  				throw new IllegalArgumentException("Key cannot be null");
75  			Attribute attr = attributes.get(key.toString());
76  			if (attr == null)
77  				return null;
78  			if (attr.size() == 0)
79  				throw new IllegalStateException("There must be at least one value");
80  			else if (attr.size() == 1) {
81  				return attr.get().toString();
82  			} else {// multiple
83  				String[] res = new String[attr.size()];
84  				for (int i = 0; i < attr.size(); i++) {
85  					Object value = attr.get();
86  					if (value == null)
87  						throw new RuntimeException("Values cannot be null");
88  					res[i] = attr.get(i).toString();
89  				}
90  				return res;
91  			}
92  		} catch (NamingException e) {
93  			throw new RuntimeException("Cannot get value for " + key, e);
94  		}
95  	}
96  
97  	@Override
98  	public Object put(String key, Object value) {
99  		if (key == null)
100 			throw new IllegalArgumentException("Key cannot be null");
101 		if (value == null)
102 			throw new IllegalArgumentException("Value cannot be null");
103 
104 		Object oldValue = get(key);
105 		Attribute attr = attributes.get(key);
106 		if (attr == null) {
107 			attr = new BasicAttribute(key);
108 			attributes.put(attr);
109 		}
110 
111 		if (value instanceof String[]) {
112 			String[] values = (String[]) value;
113 			// clean additional values
114 			for (int i = values.length; i < attr.size(); i++)
115 				attr.remove(i);
116 			// set values
117 			for (int i = 0; i < values.length; i++) {
118 				attr.set(i, values[i]);
119 			}
120 		} else {
121 			if (attr.size() > 1)
122 				throw new IllegalArgumentException("Attribute " + key + " is multi-valued");
123 			if (attr.size() == 1) {
124 				try {
125 					if (!attr.get(0).equals(value))
126 						attr.set(0, value.toString());
127 				} catch (NamingException e) {
128 					throw new RuntimeException("Cannot check existing value", e);
129 				}
130 			} else {
131 				attr.add(value.toString());
132 			}
133 		}
134 		return oldValue;
135 	}
136 
137 	@Override
138 	public Object remove(Object key) {
139 		if (key == null)
140 			throw new IllegalArgumentException("Key cannot be null");
141 		Object oldValue = get(key);
142 		if (oldValue == null)
143 			return null;
144 		return attributes.remove(key.toString());
145 	}
146 
147 	/**
148 	 * Copy the <b>content</b> of an {@link Attributes} to the provided
149 	 * {@link Dictionary}.
150 	 */
151 	public static void copy(Attributes attributes, Dictionary<String, Object> dictionary) {
152 		AttributesDictionary ad = new AttributesDictionary(attributes);
153 		Enumeration<String> keys = ad.keys();
154 		while (keys.hasMoreElements()) {
155 			String key = keys.nextElement();
156 			dictionary.put(key, ad.get(key));
157 		}
158 	}
159 
160 	/**
161 	 * Copy a {@link Dictionary} into an {@link Attributes}.
162 	 */
163 	public static void copy(Dictionary<String, Object> dictionary, Attributes attributes) {
164 		AttributesDictionary ad = new AttributesDictionary(attributes);
165 		Enumeration<String> keys = dictionary.keys();
166 		while (keys.hasMoreElements()) {
167 			String key = keys.nextElement();
168 			ad.put(key, dictionary.get(key));
169 		}
170 	}
171 }