KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > assembler > classic > xml > DomOpenEjbConfigurationFactory


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: DomOpenEjbConfigurationFactory.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.alt.assembler.classic.xml;
46
47 import java.io.IOException JavaDoc;
48 import java.util.Properties JavaDoc;
49
50 import org.apache.xerces.parsers.DOMParser;
51 import org.openejb.EnvProps;
52 import org.openejb.OpenEJBException;
53 import org.openejb.alt.assembler.classic.OpenEjbConfiguration;
54 import org.openejb.alt.assembler.classic.OpenEjbConfigurationFactory;
55 import org.openejb.util.OpenEJBErrorHandler;
56 import org.openejb.util.SafeProperties;
57 import org.openejb.util.SafeToolkit;
58 import org.w3c.dom.Document JavaDoc;
59 import org.xml.sax.ErrorHandler JavaDoc;
60 import org.xml.sax.SAXException JavaDoc;
61 import org.xml.sax.SAXNotRecognizedException JavaDoc;
62 import org.xml.sax.SAXNotSupportedException JavaDoc;
63 import org.xml.sax.SAXParseException JavaDoc;
64
65
66 /**
67  * Factory for creating an instance of the OpenEjbConfiguration using DOM and and XML configuration file
68  *
69  * <p>
70  * DomOpenEjbConfigurationFactory is the default OpenEjbConfigurationFactory, which
71  * creates an OpenEjbConfiguration object based on XML config files located on the
72  * local system.</p>
73  *
74  * <p>
75  * The OpenEjbConfiguration object structure provides the inforamtion about the
76  * configuration of OpenEJB and the container system and is used by the
77  * org.openejb.alt.assembler.classic.Assembler to build a running unstance of OpenEJB.</p>
78  *
79  * <p>
80  * Other OpenEjbConfigurationFactory implementations can be created that might populate
81  * this object using a different approach. Other usefull implementations might be:<br>
82  * <UL>
83  * <LI>Populating the OpenEjbConfiguration from values in a RDBMS.
84  * <LI>Populating the OpenEjbConfiguration from values in a Properties file.
85  * <LI>Retrieving the OpenEjbConfiguration from a ODBMS.
86  * <LI>Creating the OpenEjbConfiguration using a JavaBeans enabled editing tool or wizard.
87  * </UL>
88  *
89  * <p>
90  * If you are interested in creating alternate an OpenEjbConfigurationFactory to do
91  * any of the above techniques or a new approach, email the
92  * <a HREF="mailto:openejb-dev@exolab.org">OpenEJB Developer list</a> with a description
93  * of the new OpenEjbConfigurationFactory implementation.
94  * </p>
95  *
96  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
97  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
98  * @see org.openejb.spi.Assembler
99  * @see org.openejb.alt.assembler.classic.Assembler
100  * @see org.openejb.alt.assembler.classic.OpenEjbConfiguration
101  * @see org.openejb.alt.assembler.classic.OpenEjbConfigurationFactory
102  */

103 public class DomOpenEjbConfigurationFactory implements OpenEjbConfigurationFactory{
104     
105     private SafeToolkit toolkit = SafeToolkit.getToolkit("DomConfig");
106     private XmlOpenEJBConfiguration config;
107     private String JavaDoc configXml;
108
109     /**
110      * Initializes the {@link org.openejb.alt.assembler.classic.OpenEjbConfiguration} with the
111      * XML config file specified by the value of {@link org.openejb.EnvProps#CONFIGURATION} in
112      * the environment variables used to construct this container system.
113      * @exception OpenEJBException
114      * @param props A Properties object containing the EnvProps#CONFIGURATION entry
115      * @exception OpenEJBException
116      * if there was a problem parsing the XML file, the XML file is invalid or the XML file could not be found.
117      * @see org.openejb.alt.assembler.classic.OpenEjbConfiguration
118      * @see org.openejb.EnvProps#CONFIGURATION
119      */

120     public void init(Properties JavaDoc props) throws OpenEJBException {
121         SafeProperties safeProps = toolkit.getSafeProperties(props);
122         configXml = safeProps.getProperty(EnvProps.CONFIGURATION);
123         new java.io.File JavaDoc(configXml);
124     }
125     
126     public OpenEjbConfiguration getOpenEjbConfiguration() throws OpenEJBException {
127         try {
128             if (config == null) {
129                 config = new XmlOpenEJBConfiguration();
130                 DOMParser parser = new DOMParser();
131                 
132                 parser.setErrorHandler(new XMLErrorHandler());
133                 parser.setFeature("http://xml.org/sax/features/validation", true);
134                 parser.setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", true);
135                 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
136                 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
137                 parser.parse(configXml);
138     
139                 Document JavaDoc document = parser.getDocument();
140                 config.initializeFromDOM(document);
141             }
142
143             return config;
144         } catch (IOException JavaDoc e) {
145             throw new OpenEJBException("Problem creating the OpenEjbConfiguration structure from file "+configXml, e);
146         } catch (SAXNotSupportedException JavaDoc e) {
147             throw new OpenEJBException("Problem creating the OpenEjbConfiguration structure from file "+configXml, e);
148         } catch (SAXNotRecognizedException JavaDoc e) {
149             throw new OpenEJBException("Problem creating the OpenEjbConfiguration structure from file "+configXml, e);
150         } catch (SAXException JavaDoc e) {
151             throw new OpenEJBException("Problem creating the OpenEjbConfiguration structure from file "+configXml, e);
152         }
153     }
154
155 }
156
157 class XMLErrorHandler implements ErrorHandler{
158
159     public void warning (SAXParseException JavaDoc exception) throws SAXException JavaDoc{
160         handleError("warning",exception);
161     }
162
163     public void error (SAXParseException JavaDoc exception) throws SAXException JavaDoc{
164         handleError("error",exception);
165     }
166
167     public void fatalError (SAXParseException JavaDoc exception) throws SAXException JavaDoc{
168         handleError("fatal error",exception);
169     }
170
171     private void handleError(String JavaDoc errorType, SAXParseException JavaDoc e) {
172         OpenEJBErrorHandler.configurationParsingError(errorType, e.getLocalizedMessage(), e.getLineNumber()+"", e.getColumnNumber()+"");
173     }
174 }
175
176
177
Popular Tags