KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > config > XmlConfigurator


1 /*
2  * Copyright 1999,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
18 package org.apache.naming.config;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NameClassPair JavaDoc;
29 import javax.naming.NamingEnumeration JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import org.apache.commons.digester.Digester;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Configure an in-memory JNDI implementation using an XML configuration file.
40  *
41  * @author <a HREF="brett@apache.org">Brett Porter</a>
42  * @version $Id: XmlConfigurator.java,v 1.2 2003/12/01 02:02:45 brett Exp $
43  */

44 public class XmlConfigurator
45 {
46     private static final String JavaDoc COMP_CONTEXT_NAME = "java:comp";
47     private static final String JavaDoc ENV_CONTEXT_NAME = "env";
48     private static final String JavaDoc ROOT_ELEMENT = "naming";
49     private static final String JavaDoc CONTEXT_ELEMENT = ROOT_ELEMENT + "/context";
50     private static final String JavaDoc ENV_ELEMENT = CONTEXT_ELEMENT + "/environment";
51     private static final String JavaDoc RES_ELEMENT = CONTEXT_ELEMENT + "/resource";
52     private static final String JavaDoc RES_PARAM_ELEMENT = RES_ELEMENT + "/parameter";
53     private static Context JavaDoc envContext = null;
54
55     private static final Log LOG = LogFactory.getLog(XmlConfigurator.class);
56
57     /**
58      * Sets up initial context using
59      * <code>org.apache.naming.java.javaURLContextFactory</code>.
60      * <p>
61      * Also creates "env" subcontext in "java:comp" namespace.
62      *
63      * @throws NamingException if a NamingException occurs.
64      */

65     public static synchronized void setupInitialContext() throws NamingException JavaDoc {
66         System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
67         System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
68
69         Context JavaDoc initialContext = new InitialContext JavaDoc();
70         envContext = initialContext.createSubcontext(COMP_CONTEXT_NAME).createSubcontext(ENV_CONTEXT_NAME);
71     }
72
73     /**
74      * Destroys initial context.
75      * <p>
76      * Invokes <code>Context.destroySubcontext(Name)</code> only on top-level
77      * subcontexts.
78      *
79      * @throws NamingException if a NamingException occurs.
80      */

81     public static synchronized void destroyInitialContext() throws NamingException JavaDoc {
82         Context JavaDoc initialContext = new InitialContext JavaDoc();
83         NamingEnumeration JavaDoc contexts = initialContext.list("");
84         while (contexts.hasMore()) {
85             initialContext.destroySubcontext(((NameClassPair JavaDoc) contexts.next()).getName());
86         }
87         envContext = null;
88         initialContext = null;
89     }
90
91     /**
92      * Loads xml configuration data from <code>inputFile</code> into initial context.
93      *
94      * @param inputFile input xml configuration file
95      * @throws NamingException if a NamingException occurs.
96      * @throws ParseException if an error occurs parsing the configuration file.
97      */

98     public static synchronized void loadConfiguration(InputStream JavaDoc inputFile) throws NamingException JavaDoc, ParseException {
99         if (envContext == null)
100         {
101             setupInitialContext();
102         }
103
104         Digester digester = new Digester();
105 // TODO: string constants
106
digester.addObjectCreate(ROOT_ELEMENT, Config.Naming.class);
107         digester.addObjectCreate(CONTEXT_ELEMENT, Config.Context.class);
108         digester.addSetProperties(CONTEXT_ELEMENT);
109         digester.addSetNext(CONTEXT_ELEMENT, "addContext");
110         // TODO: handle context inside context?
111
digester.addObjectCreate(ENV_ELEMENT, Config.Environment.class);
112         digester.addSetProperties(ENV_ELEMENT);
113         digester.addSetNext(ENV_ELEMENT, "addEnvironment");
114         digester.addObjectCreate(RES_ELEMENT, Config.Resource.class);
115         digester.addSetProperties(RES_ELEMENT);
116         digester.addSetNext(RES_ELEMENT, "addResource");
117         digester.addCallMethod(RES_PARAM_ELEMENT + "", "addParameter", 2);
118         digester.addCallParam(RES_PARAM_ELEMENT + "/name", 0);
119         digester.addCallParam(RES_PARAM_ELEMENT + "/value", 1);
120
121         try
122         {
123             Config.Naming naming = (Config.Naming) digester.parse(inputFile);
124             if (naming == null) {
125                 throw new ParseException("Unable to find root element '" + ROOT_ELEMENT + "'");
126             }
127             if (LOG.isDebugEnabled()) {
128                 LOG.debug("XML configuration loaded: " + naming.toString());
129             }
130
131
132             for (Iterator JavaDoc i = naming.getContextList().iterator(); i.hasNext();)
133             {
134                 Config.Context ctx = (Config.Context) i.next();
135                 Context JavaDoc jndiCtx = envContext;
136                 if (ctx.getName() != null)
137                 {
138                     destroyInitialContext();
139                     Context JavaDoc initialContext = new InitialContext JavaDoc();
140                     Name JavaDoc nm = initialContext.getNameParser("").parse(ctx.getName());
141                     envContext = initialContext.createSubcontext(nm.get(0));
142                     jndiCtx = envContext;
143                     for (int k = 1; k < nm.size(); k++) {
144                         jndiCtx = jndiCtx.createSubcontext(nm.get(k));
145                     }
146                 }
147                 precreateSubcontextTree(jndiCtx, naming.generateSortedSubcontextNameSet());
148
149                 for (Iterator JavaDoc j = ctx.getEnvironmentList().iterator(); j.hasNext();)
150                 {
151                     Config.Environment e = (Config.Environment) j.next();
152                     jndiCtx.rebind(e.getName(), e.createValue());
153                 }
154
155                 for (Iterator JavaDoc j = ctx.getResourceList().iterator(); j.hasNext();)
156                 {
157                     Config.Resource r = (Config.Resource) j.next();
158                     jndiCtx.bind(r.getName(), r.createValue());
159                 }
160             }
161         }
162         catch (IOException JavaDoc e)
163         {
164             throw new ParseException("Error reading configuration file", e);
165         }
166         catch (SAXException JavaDoc e)
167         {
168             throw new ParseException("Error reading configuration file", e);
169         }
170     }
171
172     private static void precreateSubcontextTree(Context JavaDoc ctx, Set JavaDoc sortedSubcontextNameSet) throws NamingException JavaDoc
173     {
174         // TODO: don't recreate
175
for (Iterator JavaDoc i = sortedSubcontextNameSet.iterator(); i.hasNext();)
176         {
177             String JavaDoc name = (String JavaDoc) i.next();
178             ctx.createSubcontext(name);
179         }
180     }
181
182 }
183
Popular Tags