KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > configuration > NamingConfigurationBuilder


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 2002,2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50 package org.apache.excalibur.configuration;
51
52 import java.util.Hashtable JavaDoc;
53
54 import javax.naming.Binding JavaDoc;
55 import javax.naming.Context JavaDoc;
56 import javax.naming.Name JavaDoc;
57 import javax.naming.NameParser JavaDoc;
58 import javax.naming.NamingEnumeration JavaDoc;
59 import javax.naming.NamingException JavaDoc;
60 import javax.naming.directory.Attribute JavaDoc;
61 import javax.naming.directory.Attributes JavaDoc;
62 import javax.naming.directory.DirContext JavaDoc;
63 import javax.naming.directory.InitialDirContext JavaDoc;
64
65 import org.apache.avalon.framework.configuration.Configuration;
66 import org.apache.avalon.framework.configuration.DefaultConfiguration;
67
68 /**
69  * A NamingConfigurationBuilder builds <code>Configuration</code>s from JNDI or
70  * LDAP directory trees.
71  *
72  * @author <a HREF="mailto:mirceatoma@apache.org">Mircea Toma</a>
73  */

74 public class NamingConfigurationBuilder
75 {
76     private final boolean m_enableNamespaces;
77
78     /**
79      * Create a Configuration Builder that ignores namespaces.
80      */

81     public NamingConfigurationBuilder()
82     {
83         this( false );
84     }
85
86     /**
87      * Create a Configuration Builder, specifying a flag that determines
88      * namespace support.
89      *
90      * @param enableNamespaces If <code>true</code>, a configuration with
91      * namespace information is built.
92      */

93     public NamingConfigurationBuilder( final boolean enableNamespaces )
94     {
95         m_enableNamespaces = enableNamespaces;
96     }
97
98     /**
99      * Build a configuration object using an URI
100      */

101     public Configuration build( final String JavaDoc uri ) throws NamingException JavaDoc
102     {
103         final Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc context = new InitialDirContext JavaDoc( env );
109
110         return build( context );
111     }
112
113     /**
114      * Build a configuration object using a naming context.
115      */

116     public Configuration build( final Context JavaDoc context ) throws NamingException JavaDoc
117     {
118         final DefaultConfiguration configuration;
119
120         final String JavaDoc absoluteName = context.getNameInNamespace();
121         final NameParser JavaDoc parser = context.getNameParser( absoluteName );
122         final Name JavaDoc parsedName = parser.parse( absoluteName );
123
124         String JavaDoc name = absoluteName;
125         String JavaDoc prefix = "";
126         //if composite name, use only the relative name.
127
final int position = parsedName.size();
128         if( position > 0 )
129         {
130             name = parsedName.get( position - 1 );
131         }
132
133         if( context instanceof DirContext JavaDoc )
134         {
135             //extract element name, and namespace prefix
136
final Attributes JavaDoc attrs = ( (DirContext JavaDoc)context ).getAttributes( "" );
137
138             final NamingEnumeration JavaDoc attributes = attrs.getAll();
139             while( attributes.hasMore() )
140             {
141                 final Attribute JavaDoc attribute = (Attribute JavaDoc)attributes.next();
142                 final String JavaDoc id = attribute.getID();
143                 if( name.startsWith( id ) )
144                 {
145                     name = (String JavaDoc)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 JavaDoc bindings = context.listBindings( "" );
159         while( bindings.hasMore() )
160         {
161             final Binding JavaDoc binding = (Binding JavaDoc)bindings.next();
162             final Object JavaDoc object = binding.getObject();
163
164             if( ( object instanceof Number JavaDoc ) ||
165                 ( object instanceof String JavaDoc ) )
166             {
167                 configuration.setValue( object.toString() );
168             }
169
170             if( object instanceof Context JavaDoc )
171             {
172                 final Context JavaDoc child = (Context JavaDoc)object;
173                 configuration.addChild( build( child ) );
174             }
175         }
176
177         return configuration;
178     }
179
180     private void copyAttributes( final Attributes JavaDoc attrs, final DefaultConfiguration configuration ) throws NamingException JavaDoc
181     {
182         final NamingEnumeration JavaDoc attributes = attrs.getAll();
183         while( attributes.hasMore() )
184         {
185             final Attribute JavaDoc attribute = (Attribute JavaDoc)attributes.next();
186             final String JavaDoc attrName = attribute.getID();
187             final Object JavaDoc attrValue = attribute.get();
188             configuration.setAttribute( attrName, attrValue.toString() );
189         }
190     }
191 }
192
Popular Tags