001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.config;
024:
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.text.MessageFormat;
032:
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.FactoryConfigurationError;
035: import javax.xml.parsers.ParserConfigurationException;
036:
037: import org.w3c.dom.Document;
038: import org.xml.sax.SAXException;
039:
040: /**
041: * Helper class to read XML configuration from file, url or classloader
042: * resource.
043: *
044: * @author Pavel Vlasov
045: * @version $Revision: 1.2 $
046: */
047: public class XmlSource implements Parameterizable {
048:
049: private String prefix;
050:
051: private Class masterClass;
052:
053: private String defaultResourceExtension;
054:
055: /**
056: * Constructor
057: *
058: * @param prefix
059: * Prefix for configuration parameters. Parameter specifying
060: * configruration file would be <prefix>-file, url -
061: * <prefix>-url, resource - <prefix>-resource
062: * @param masterClass
063: * Master class' classloader will be used to load resources and
064: * default resource would be masterClass name with
065: * defaultResourceExtension. If master class is null then this
066: * class classloader will be used and no default resource will be
067: * loaded.
068: * @param defaultResourceExtension
069: * Extension for default resource. If it is null then default
070: * resource will not be loaded.
071: */
072: public XmlSource(String prefix, Class masterClass,
073: String defaultResourceExtension) {
074: this .prefix = prefix;
075: this .masterClass = masterClass;
076: this .defaultResourceExtension = defaultResourceExtension;
077: }
078:
079: private static final String CONFIG_ERROR_MESSAGE = "'{0}-file', "
080: + "'{0}-resource', and '{0}-url' parameters are mutually exclusive";
081:
082: private String resource;
083:
084: private String url;
085:
086: private String file;
087:
088: private Document document;
089:
090: /**
091: * @return parsed XML or null if config not found.
092: * @throws ConfigurationException
093: */
094: public Document getConfigDocument() throws ConfigurationException {
095: if (document == null) {
096: try {
097: InputStream configStream = getStream();
098:
099: if (configStream != null) {
100: document = DocumentBuilderFactory.newInstance()
101: .newDocumentBuilder().parse(configStream);
102: configStream.close();
103: }
104: } catch (IOException e) {
105: throw new ConfigurationException(
106: "Cannot load document, " + e.getMessage(), e);
107: } catch (SAXException e) {
108: throw new ConfigurationException(
109: "Cannot load document, " + e.getMessage(), e);
110: } catch (ParserConfigurationException e) {
111: throw new ConfigurationException(
112: "Cannot load document, " + e.getMessage(), e);
113: } catch (FactoryConfigurationError e) {
114: throw new ConfigurationException(
115: "Cannot load document, " + e.getMessage(), e);
116: }
117: }
118:
119: return document;
120: }
121:
122: /**
123: * @return Stream
124: * @throws FileNotFoundException
125: * @throws IOException
126: * @throws MalformedURLException
127: * @throws ConfigurationException
128: */
129: public InputStream getStream() throws ConfigurationException {
130: try {
131: Class clazz = masterClass == null ? getClass()
132: : masterClass;
133: if (file != null) {
134: return new FileInputStream(file);
135: } else if (url != null) {
136: return new URL(url).openStream();
137: } else if (resource != null) {
138:
139: InputStream configStream = clazz.getClassLoader()
140: .getResourceAsStream(resource);
141: if (configStream == null) {
142: throw new ConfigurationException(
143: "Resource not found: " + resource);
144: }
145:
146: return configStream;
147: } else {
148: if (defaultResourceExtension == null) {
149: return null;
150: }
151:
152: String className = clazz.getName();
153: int idx = className.lastIndexOf('.');
154: String rName = (idx == -1 ? className : className
155: .substring(idx + 1))
156: + defaultResourceExtension;
157: return clazz.getResourceAsStream(rName);
158: }
159: } catch (IOException e) {
160: throw new ConfigurationException("Cannot open stream, "
161: + e.getMessage(), e);
162: }
163: }
164:
165: public boolean setParameter(String name, Object value)
166: throws ConfigurationException {
167: if ((prefix + "-file").equals(name)) {
168: if (resource != null) {
169: throw new ConfigurationException(MessageFormat.format(
170: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
171: }
172: if (url != null) {
173: throw new ConfigurationException(MessageFormat.format(
174: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
175: }
176: file = value.toString();
177: } else if ((prefix + "-resource").equals(name)) {
178: if (file != null) {
179: throw new ConfigurationException(MessageFormat.format(
180: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
181: }
182: if (url != null) {
183: throw new ConfigurationException(MessageFormat.format(
184: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
185: }
186: resource = value.toString();
187: } else if ((prefix + "-url").equals(name)) {
188: if (file != null) {
189: throw new ConfigurationException(MessageFormat.format(
190: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
191: }
192: if (resource != null) {
193: throw new ConfigurationException(MessageFormat.format(
194: CONFIG_ERROR_MESSAGE, new Object[] { prefix }));
195: }
196: url = value.toString();
197: } else {
198: return false;
199: }
200: return true;
201: }
202: }
|