1 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 40 public class PartitionConfigBuilder 41 { 42 43 private final static ContextPartitionConfig[] EMPTY = new ContextPartitionConfig[0]; 44 45 46 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 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 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 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 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 138 buf.setLength( 0 ); 139 140 buf.append( EnvKeys.ATTRIBUTES ).append( id ); 141 142 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 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 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 209 public static ContextPartitionConfig[] getContextPartitionConfigs( Hashtable env ) 210 throws NamingException 211 { 212 String idList = ( String ) env.get( EnvKeys.PARTITIONS ); 213 214 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 } 231 | Popular Tags |