View Javadoc

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.configuration;
21  
22  import org.apache.commons.collections.map.MultiValueMap;
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.shared.ldap.entry.Entry;
25  import org.apache.directory.shared.ldap.entry.EntryAttribute;
26  import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
27  import org.apache.directory.shared.ldap.ldif.LdifComposer;
28  import org.apache.directory.shared.ldap.ldif.LdifComposerImpl;
29  import org.apache.directory.shared.ldap.ldif.LdifReader;
30  import org.apache.directory.shared.ldap.util.AttributeUtils;
31  import org.apache.directory.shared.ldap.util.StringTools;
32  
33  import javax.naming.NamingEnumeration;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import java.beans.PropertyEditor;
38  import java.beans.PropertyEditorSupport;
39  import java.io.BufferedReader;
40  import java.io.IOException;
41  import java.io.StringReader;
42  import java.util.Map;
43  
44  
45  /**
46   * A JavaBeans {@link PropertyEditor} that can convert {@link Attributes} to
47   * LDIF string and vice versa. This class is useful when you're going to
48   * configure a {@link DirectoryService} with 3rd party containers such as <a
49   * href="http://www.springframework.org/">Spring Framework</a>.
50   * 
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   * @version $Rev: 690996 $, $Date: 2008-09-01 17:19:32 +0200 (Mo, 01 Sep 2008) $
53   */
54  public class AttributesPropertyEditor extends PropertyEditorSupport
55  {
56  
57      /**
58       * Creates a new instance.
59       */
60      public AttributesPropertyEditor()
61      {
62          super();
63      }
64  
65      /**
66       * Creates a new instance with source object.
67       */
68      public AttributesPropertyEditor( Object source )
69      {
70          super( source );
71      }
72  
73      /**
74       * Returns LDIF string of {@link Attributes} object.
75       */
76      @SuppressWarnings("deprecation")
77      public String getAsText()
78      {
79          LdifComposer composer = new LdifComposerImpl();
80          Map<String, Object> map = new MultiValueMap();
81  
82          Attributes attrs = (Attributes) getValue();
83          try
84          {
85              NamingEnumeration<? extends Attribute> e = attrs.getAll();
86              while ( e.hasMore() )
87              {
88                  Attribute attr = e.next();
89                  NamingEnumeration<? extends Object> e2 = attr.getAll();
90                  while ( e2.hasMoreElements() )
91                  {
92                      Object value = e2.next();
93                      map.put( attr.getID(), value );
94                  }
95              }
96  
97              return composer.compose( map );
98          }
99          catch ( Exception e )
100         {
101             e.printStackTrace();
102             return null;
103         }
104     }
105 
106     /**
107      * Read an entry (without DN)
108      * 
109      * @param text The ldif format file
110      * @return An entry.
111      */
112     private Entry readEntry( String text )
113     {
114         StringReader strIn = new StringReader( text );
115         BufferedReader in = new BufferedReader( strIn );
116 
117         String line = null;
118         Entry entry = new DefaultClientEntry();
119 
120         try
121         {
122             while ( ( line = in.readLine() ) != null )
123             {
124                 if ( line.length() == 0 )
125                 {
126                     continue;
127                 }
128 
129                 String addedLine = line.trim();
130 
131                 if ( StringTools.isEmpty( addedLine ) )
132                 {
133                     continue;
134                 }
135 
136                 EntryAttribute attribute = AttributeUtils.toClientAttribute( LdifReader.parseAttributeValue( addedLine ) );
137                 EntryAttribute oldAttribute = entry.get( attribute.getId() );
138 
139                 if ( oldAttribute != null )
140                 {
141                     try
142                     {
143                         oldAttribute.add( attribute.get() );
144                         entry.put( oldAttribute );
145                     }
146                     catch (NamingException ne)
147                     {
148                         // Do nothing
149                     }
150                 }
151                 else
152                 {
153                     try
154                     {
155                         entry.put( attribute );
156                     }
157                     catch ( NamingException ne )
158                     {
159                         // Do nothing...
160                     }
161                 }
162             }
163         }
164         catch (IOException ioe)
165         {
166             // Do nothing : we can't reach this point !
167         }
168 
169         return entry;
170     }
171 
172     /**
173      * Converts the specified LDIF string into {@link Attributes}.
174      */
175     public void setAsText( String text ) throws IllegalArgumentException
176     {
177         if ( text == null )
178         {
179             text = "";
180         }
181 
182         Entry entry = readEntry( text );
183         setValue( entry );
184     }
185 }