1 50 package org.apache.excalibur.configuration; 51 52 import java.util.Hashtable ; 53 54 import javax.naming.Binding ; 55 import javax.naming.Context ; 56 import javax.naming.Name ; 57 import javax.naming.NameParser ; 58 import javax.naming.NamingEnumeration ; 59 import javax.naming.NamingException ; 60 import javax.naming.directory.Attribute ; 61 import javax.naming.directory.Attributes ; 62 import javax.naming.directory.DirContext ; 63 import javax.naming.directory.InitialDirContext ; 64 65 import org.apache.avalon.framework.configuration.Configuration; 66 import org.apache.avalon.framework.configuration.DefaultConfiguration; 67 68 74 public class NamingConfigurationBuilder 75 { 76 private final boolean m_enableNamespaces; 77 78 81 public NamingConfigurationBuilder() 82 { 83 this( false ); 84 } 85 86 93 public NamingConfigurationBuilder( final boolean enableNamespaces ) 94 { 95 m_enableNamespaces = enableNamespaces; 96 } 97 98 101 public Configuration build( final String uri ) throws NamingException 102 { 103 final Hashtable env = new Hashtable (); 104 env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" ); 105 env.put( Context.SECURITY_AUTHENTICATION, "none" ); 106 env.put( Context.PROVIDER_URL, uri ); 107 108 final DirContext context = new InitialDirContext ( env ); 109 110 return build( context ); 111 } 112 113 116 public Configuration build( final Context context ) throws NamingException 117 { 118 final DefaultConfiguration configuration; 119 120 final String absoluteName = context.getNameInNamespace(); 121 final NameParser parser = context.getNameParser( absoluteName ); 122 final Name parsedName = parser.parse( absoluteName ); 123 124 String name = absoluteName; 125 String prefix = ""; 126 final int position = parsedName.size(); 128 if( position > 0 ) 129 { 130 name = parsedName.get( position - 1 ); 131 } 132 133 if( context instanceof DirContext ) 134 { 135 final Attributes attrs = ( (DirContext )context ).getAttributes( "" ); 137 138 final NamingEnumeration attributes = attrs.getAll(); 139 while( attributes.hasMore() ) 140 { 141 final Attribute attribute = (Attribute )attributes.next(); 142 final String id = attribute.getID(); 143 if( name.startsWith( id ) ) 144 { 145 name = (String )attribute.get(); 146 if( m_enableNamespaces ) prefix = id; 147 attrs.remove( id ); 148 break; 149 } 150 } 151 152 configuration = new DefaultConfiguration( name, null, "", prefix ); 153 copyAttributes( attrs, configuration ); 154 } 155 else 156 configuration = new DefaultConfiguration( name, null, "", prefix ); 157 158 final NamingEnumeration bindings = context.listBindings( "" ); 159 while( bindings.hasMore() ) 160 { 161 final Binding binding = (Binding )bindings.next(); 162 final Object object = binding.getObject(); 163 164 if( ( object instanceof Number ) || 165 ( object instanceof String ) ) 166 { 167 configuration.setValue( object.toString() ); 168 } 169 170 if( object instanceof Context ) 171 { 172 final Context child = (Context )object; 173 configuration.addChild( build( child ) ); 174 } 175 } 176 177 return configuration; 178 } 179 180 private void copyAttributes( final Attributes attrs, final DefaultConfiguration configuration ) throws NamingException 181 { 182 final NamingEnumeration attributes = attrs.getAll(); 183 while( attributes.hasMore() ) 184 { 185 final Attribute attribute = (Attribute )attributes.next(); 186 final String attrName = attribute.getID(); 187 final Object attrValue = attribute.get(); 188 configuration.setAttribute( attrName, attrValue.toString() ); 189 } 190 } 191 } 192 | Popular Tags |