1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.jndi;
18
19
20 import org.apache.ldap.common.message.LockableAttributeImpl;
21 import org.apache.ldap.common.message.LockableAttributesImpl;
22 import org.apache.ldap.common.name.LdapName;
23 import org.apache.ldap.common.util.ArrayUtils;
24 import org.apache.ldap.common.util.StringTools;
25 import org.apache.ldap.server.ContextPartitionConfig;
26
27 import javax.naming.NamingException;
28 import javax.naming.directory.Attributes;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
31
32
33 /***
34 * A partition configuration builder which produces ContextPartitionConfig
35 * objects from various configuration formats, namely Hashtables.
36 *
37 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38 * @version $Rev$
39 */
40 public class PartitionConfigBuilder
41 {
42 /*** keep this so we do not have create empty ones over and over again */
43 private final static ContextPartitionConfig[] EMPTY = new ContextPartitionConfig[0];
44
45
46 /***
47 * Extracts properties from a Hashtable and builds a configuration bean for
48 * a ContextPartition.
49 *
50 * @param id the id of the partition to extract configs for
51 * @param env the Hastable containing usually JNDI environment settings
52 * @return the extracted configuration object
53 * @throws NamingException if a partition suffix is malformed
54 */
55 public static ContextPartitionConfig getContextPartitionConfig( String id, Hashtable env )
56 throws NamingException
57 {
58 final StringBuffer buf = new StringBuffer();
59
60 final ContextPartitionConfig config = new ContextPartitionConfig();
61
62 final LockableAttributesImpl attrs = new LockableAttributesImpl();
63
64
65
66
67
68 config.setId( id );
69
70 config.setAttributes( attrs );
71
72 buf.append( EnvKeys.SUFFIX ).append( id );
73
74 String suffix = ( String ) env.get( buf.toString() );
75
76 if ( suffix != null )
77 {
78 suffix = new LdapName( suffix ).toString();
79 }
80
81 config.setSuffix( suffix );
82
83
84
85
86
87 buf.setLength( 0 );
88
89 buf.append( EnvKeys.PARTITION_CLASS ).append( id );
90
91 String partitionClass = ( String ) env.get( buf.toString() );
92
93 if ( partitionClass != null )
94 {
95 config.setPartitionClass( partitionClass );
96 }
97
98
99
100
101
102 buf.setLength( 0 );
103
104 buf.append( EnvKeys.PARTITION_PROPERTIES ).append( id );
105
106 String properties = ( String ) env.get( buf.toString() );
107
108 if ( properties != null )
109 {
110 config.setProperties( properties );
111 }
112
113
114
115
116
117 buf.setLength( 0 );
118
119 buf.append( EnvKeys.INDICES ).append( id );
120
121 String indexList = ( ( String ) env.get( buf.toString() ) );
122
123 if ( indexList == null || indexList.trim().length() == 0 )
124 {
125 config.setIndices( ArrayUtils.EMPTY_STRING_ARRAY );
126 }
127 else
128 {
129 indexList = StringTools.deepTrim( indexList );
130
131 config.setIndices( indexList.split( " " ) );
132 }
133
134
135
136
137
138 buf.setLength( 0 );
139
140 buf.append( EnvKeys.ATTRIBUTES ).append( id );
141
142
143
144
145
146
147
148 String keyBase = buf.toString();
149
150 if ( env.containsKey( keyBase ) )
151 {
152 config.setAttributes( ( Attributes ) env.get( keyBase ) );
153
154 return config;
155 }
156
157
158
159
160
161
162 buf.append( "." );
163
164 keyBase = buf.toString();
165
166 for ( Enumeration list = env.keys(); list.hasMoreElements(); )
167 {
168 String attrKey = ( String ) list.nextElement();
169
170 if ( attrKey.startsWith( keyBase ) )
171 {
172 LockableAttributeImpl attr = new LockableAttributeImpl( attrs, attrKey.substring( keyBase.length() ) ) ;
173
174 String valueList = ( String ) env.get( attrKey );
175
176 if ( valueList == null || valueList.trim().length() == 0 )
177 {
178
179 attrs.put( attr );
180
181 continue;
182 }
183
184 valueList = StringTools.deepTrim( valueList );
185
186 String[] values = valueList.split( " " );
187
188 for ( int ii = 0; ii < values.length; ii++ )
189 {
190 attr.add( values[ii] );
191 }
192
193 attrs.put( attr );
194 }
195 }
196
197 return config;
198 }
199
200
201 /***
202 * Extracts properties from a Hashtable and builds a set of configurations
203 * bean for ContextPartitions.
204 *
205 * @param env the Hastable containing usually JNDI environment settings
206 * @return all the extracted configuration objects configured
207 * @throws NamingException if a partition suffix is malformed
208 */
209 public static ContextPartitionConfig[] getContextPartitionConfigs( Hashtable env )
210 throws NamingException
211 {
212 String idList = ( String ) env.get( EnvKeys.PARTITIONS );
213
214
215 if ( idList == null || idList.trim().length() == 0 )
216 {
217 return EMPTY;
218 }
219
220 idList = StringTools.deepTrim( idList );
221 final String[] ids = idList.split( " " );
222 final ContextPartitionConfig[] configs = new ContextPartitionConfig[ids.length];
223 for ( int ii = 0; ii < configs.length; ii++ )
224 {
225 configs[ii] = getContextPartitionConfig( ids[ii], env );
226 }
227
228 return configs;
229 }
230 }