1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.ldap.server.configuration;
20
21 import java.beans.PropertyEditor;
22 import java.beans.PropertyEditorSupport;
23 import java.util.Collection;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.naming.NamingEnumeration;
28 import javax.naming.NamingException;
29 import javax.naming.directory.Attribute;
30 import javax.naming.directory.Attributes;
31 import javax.naming.directory.BasicAttributes;
32
33 import org.apache.commons.collections.MultiHashMap;
34 import org.apache.ldap.common.ldif.LdifComposer;
35 import org.apache.ldap.common.ldif.LdifComposerImpl;
36 import org.apache.ldap.common.ldif.LdifParser;
37 import org.apache.ldap.common.ldif.LdifParserImpl;
38 import org.apache.ldap.common.util.MultiMap;
39 import org.apache.ldap.server.jndi.ContextFactoryService;
40
41 /***
42 * A JavaBeans {@link PropertyEditor} that can convert {@link Attributes}
43 * to LDIF string and vice versa. This class is useful when you're going
44 * to configure a {@link ContextFactoryService} with 3rd party containers
45 * such as <a href="http://www.springframework.org/">Spring Framework</a>.
46 *
47 * @author The Apache Directory Project
48 * @version $Rev: 264732 $, $Date: 2005-08-30 04:04:51 -0400 (Tue, 30 Aug 2005) $
49 */
50 public class AttributesPropertyEditor extends PropertyEditorSupport
51 {
52
53 /***
54 * Creates a new instance.
55 */
56 public AttributesPropertyEditor()
57 {
58 super();
59 }
60
61 /***
62 * Creates a new instance with source object.
63 */
64 public AttributesPropertyEditor( Object source )
65 {
66 super( source );
67 }
68
69 /***
70 * Returns LDIF string of {@link Attributes} object.
71 */
72 public String getAsText()
73 {
74 LdifComposer composer = new LdifComposerImpl();
75 MultiMap map = new MultiMap()
76 {
77
78 private final MultiHashMap map = new MultiHashMap();
79
80 public Object remove( Object arg0, Object arg1 )
81 {
82 return map.remove( arg0, arg1 );
83 }
84
85 public int size()
86 {
87 return map.size();
88 }
89
90 public Object get( Object arg0 )
91 {
92 return map.get( arg0 );
93 }
94
95 public boolean containsValue( Object arg0 )
96 {
97 return map.containsValue( arg0 );
98 }
99
100 public Object put( Object arg0, Object arg1 )
101 {
102 return map.put( arg0, arg1 );
103 }
104
105 public Object remove( Object arg0 )
106 {
107 return map.remove( arg0 );
108 }
109
110 public Collection values()
111 {
112 return map.values();
113 }
114
115 public boolean isEmpty()
116 {
117 return map.isEmpty();
118 }
119
120 public boolean containsKey( Object key )
121 {
122 return map.containsKey( key );
123 }
124
125 public void putAll( Map arg0 )
126 {
127 map.putAll( arg0 );
128 }
129
130 public void clear()
131 {
132 map.clear();
133 }
134
135 public Set keySet()
136 {
137 return map.keySet();
138 }
139
140 public Set entrySet()
141 {
142 return map.entrySet();
143 }
144 };
145
146 Attributes attrs = ( Attributes ) getValue();
147 try
148 {
149 NamingEnumeration e = attrs.getAll();
150 while( e.hasMore() )
151 {
152 Attribute attr = ( Attribute ) e.next();
153 NamingEnumeration e2 = attr.getAll();
154 while( e2.hasMoreElements() )
155 {
156 Object value = e2.next();
157 map.put( attr.getID(), value );
158 }
159 }
160
161 return composer.compose( map );
162 }
163 catch( Exception e )
164 {
165 throw new ConfigurationException( e );
166 }
167 }
168
169 /***
170 * Converts the specified LDIF string into {@link Attributes}.
171 */
172 public void setAsText( String text ) throws IllegalArgumentException
173 {
174 if( text == null )
175 {
176 text = "";
177 }
178
179 Attributes attrs = new BasicAttributes( true );
180 LdifParser parser = new LdifParserImpl();
181 try
182 {
183 parser.parse( attrs, text.trim() );
184 setValue( attrs );
185 }
186 catch( NamingException e )
187 {
188 throw ( IllegalArgumentException ) new IllegalArgumentException().initCause( e );
189 }
190 }
191 }