KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > jndi > PartitionConfigBuilder


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc;
28 import javax.naming.directory.Attributes JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
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 JavaDoc id, Hashtable JavaDoc env )
56             throws NamingException JavaDoc
57     {
58         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
59
60         final ContextPartitionConfig config = new ContextPartitionConfig();
61
62         final LockableAttributesImpl attrs = new LockableAttributesImpl();
63
64         // --------------------------------------------------------------------
65
// set id, empty attributes, and lookup the suffix for config
66
// --------------------------------------------------------------------
67

68         config.setId( id );
69
70         config.setAttributes( attrs );
71
72         buf.append( EnvKeys.SUFFIX ).append( id );
73
74         String JavaDoc suffix = ( String JavaDoc ) 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
// set partition class
85
// --------------------------------------------------------------------
86

87         buf.setLength( 0 );
88
89         buf.append( EnvKeys.PARTITION_CLASS ).append( id );
90
91         String JavaDoc partitionClass = ( String JavaDoc ) env.get( buf.toString() );
92
93         if ( partitionClass != null )
94         {
95             config.setPartitionClass( partitionClass );
96         }
97
98         // --------------------------------------------------------------------
99
// set partition properties
100
// --------------------------------------------------------------------
101

102         buf.setLength( 0 );
103
104         buf.append( EnvKeys.PARTITION_PROPERTIES ).append( id );
105
106         String JavaDoc properties = ( String JavaDoc ) env.get( buf.toString() );
107
108         if ( properties != null )
109         {
110             config.setProperties( properties );
111         }
112
113         // --------------------------------------------------------------------
114
// extract index list and set the list of indices in config
115
// --------------------------------------------------------------------
116

117         buf.setLength( 0 );
118
119         buf.append( EnvKeys.INDICES ).append( id );
120
121         String JavaDoc indexList = ( ( String JavaDoc ) 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
// extract attributes and values adding them to the config
136
// --------------------------------------------------------------------
137

138         buf.setLength( 0 );
139
140         buf.append( EnvKeys.ATTRIBUTES ).append( id );
141
142         /*
143          * before going on to extract attributes check to see if the
144          * attributes base plus id has an Attributes object set. Users
145          * can programatically use Attributes objects rather than
146          * wrestle with individual key value pairs.
147          */

148         String JavaDoc keyBase = buf.toString();
149
150         if ( env.containsKey( keyBase ) )
151         {
152             config.setAttributes( ( Attributes JavaDoc ) env.get( keyBase ) );
153
154             return config;
155         }
156
157         /*
158          * looks like the environment attributes were not set programatically
159          * with an Attributes object for the base. So we now add the extra
160          * '.' and go on to try and detect all the keys and their attributes.
161          */

162         buf.append( "." );
163
164         keyBase = buf.toString();
165
166         for ( Enumeration JavaDoc list = env.keys(); list.hasMoreElements(); )
167         {
168             String JavaDoc attrKey = ( String JavaDoc ) list.nextElement();
169
170             if ( attrKey.startsWith( keyBase ) )
171             {
172                 LockableAttributeImpl attr = new LockableAttributeImpl( attrs, attrKey.substring( keyBase.length() ) ) ;
173
174                 String JavaDoc valueList = ( String JavaDoc ) env.get( attrKey );
175
176                 if ( valueList == null || valueList.trim().length() == 0 )
177                 {
178                     // add the empty attribute
179
attrs.put( attr );
180
181                     continue;
182                 }
183
184                 valueList = StringTools.deepTrim( valueList );
185
186                 String JavaDoc[] 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 JavaDoc env )
210             throws NamingException JavaDoc
211     {
212         String JavaDoc idList = ( String JavaDoc ) env.get( EnvKeys.PARTITIONS );
213
214         // return empty array when we got nothin to work with!
215
if ( idList == null || idList.trim().length() == 0 )
216         {
217             return EMPTY;
218         }
219
220         idList = StringTools.deepTrim( idList );
221         final String JavaDoc[] 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 }
231
Popular Tags