1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.core.entry;
21
22 import java.beans.PropertyEditor;
23 import java.beans.PropertyEditorSupport;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.util.Map;
28
29 import javax.naming.NamingEnumeration;
30 import javax.naming.NamingException;
31 import javax.naming.directory.Attribute;
32 import javax.naming.directory.Attributes;
33 import javax.naming.directory.BasicAttributes;
34
35 import org.apache.commons.collections.map.MultiValueMap;
36 import org.apache.directory.shared.ldap.ldif.LdifComposer;
37 import org.apache.directory.shared.ldap.ldif.LdifComposerImpl;
38 import org.apache.directory.shared.ldap.ldif.LdifReader;
39 import org.apache.directory.shared.ldap.util.StringTools;
40
41
42 /**
43 * A JavaBeans {@link PropertyEditor} that can convert {@link ServerEntry} to
44 * LDIF string and vice versa. This class is useful when you're going to
45 * configure a {@link org.apache.directory.server.core.DirectoryService} with 3rd party containers such as <a
46 * href="http://www.springframework.org/">Spring Framework</a>.
47 *
48 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49 * @version $Rev: 582462 $, $Date: 2007-10-06 08:48:35 +0200 (Sat, 06 Oct 2007) $
50 */
51 public class ServerEntryPropertyEditor extends PropertyEditorSupport
52 {
53
54 /**
55 * Creates a new instance.
56 */
57 public ServerEntryPropertyEditor()
58 {
59 super();
60 }
61
62 /**
63 * Creates a new instance with source object.
64 */
65 public ServerEntryPropertyEditor( Object source )
66 {
67 super( source );
68 }
69
70 /**
71 * Returns LDIF string of {@link Attributes} object.
72 */
73 public String getAsText()
74 {
75 LdifComposer composer = new LdifComposerImpl();
76 Map<String, Object> map = new MultiValueMap();
77
78 Attributes attrs = (Attributes) getValue();
79
80 try
81 {
82 NamingEnumeration<? extends Attribute> e = attrs.getAll();
83
84 while ( e.hasMore() )
85 {
86 Attribute attr = e.next();
87 NamingEnumeration<? extends Object> e2 = attr.getAll();
88
89 while ( e2.hasMoreElements() )
90 {
91 Object value = e2.next();
92 map.put( attr.getID(), value );
93 }
94 }
95
96 return composer.compose( map );
97 }
98 catch ( Exception e )
99 {
100 return null;
101 }
102 }
103
104 /**
105 * Read an entry (without DN)
106 *
107 * @param text
108 * The ldif format file
109 * @return An Attributes.
110 */
111 private Attributes readEntry( String text )
112 {
113 StringReader strIn = new StringReader( text );
114 BufferedReader in = new BufferedReader( strIn );
115
116 String line = null;
117 Attributes attributes = new BasicAttributes( true );
118
119 try
120 {
121 while ( ( line = in.readLine() ) != null )
122 {
123 if ( line.length() == 0 )
124 {
125 continue;
126 }
127
128 String addedLine = line.trim();
129
130 if ( StringTools.isEmpty( addedLine ) )
131 {
132 continue;
133 }
134
135 Attribute attribute = LdifReader.parseAttributeValue( addedLine );
136 Attribute oldAttribute = attributes.get( attribute.getID() );
137
138 if ( oldAttribute != null )
139 {
140 try
141 {
142 oldAttribute.add( attribute.get() );
143 attributes.put( oldAttribute );
144 }
145 catch (NamingException ne)
146 {
147 // Do nothing
148 }
149 }
150 else
151 {
152 attributes.put( attribute );
153 }
154 }
155 }
156 catch (IOException ioe)
157 {
158 // Do nothing : we can't reach this point !
159 }
160
161 return attributes;
162 }
163
164 /**
165 * Converts the specified LDIF string into {@link Attributes}.
166 */
167 public void setAsText( String text ) throws IllegalArgumentException
168 {
169 if ( text == null )
170 {
171 text = "";
172 }
173
174 setValue( readEntry( text ) );
175 }
176 }