001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.config;
016:
017: import net.sf.ehcache.CacheException;
018: import net.sf.ehcache.util.ClassLoaderUtil;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import javax.xml.parsers.SAXParser;
023: import javax.xml.parsers.SAXParserFactory;
024: import java.io.BufferedInputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.net.URL;
030:
031: /**
032: * A utility class which configures beans from XML, using reflection.
033: *
034: * @author Greg Luck
035: * @version $Id: ConfigurationFactory.java 519 2007-07-27 07:11:45Z gregluck $
036: */
037: public final class ConfigurationFactory {
038: private static final Log LOG = LogFactory
039: .getLog(ConfigurationFactory.class.getName());
040:
041: private static final String DEFAULT_CLASSPATH_CONFIGURATION_FILE = "/ehcache.xml";
042: private static final String FAILSAFE_CLASSPATH_CONFIGURATION_FILE = "/ehcache-failsafe.xml";
043:
044: /**
045: * Constructor.
046: */
047: private ConfigurationFactory() {
048:
049: }
050:
051: /**
052: * Configures a bean from an XML file.
053: */
054: public static Configuration parseConfiguration(final File file)
055: throws CacheException {
056: if (file == null) {
057: throw new CacheException(
058: "Attempt to configure ehcache from null file.");
059: }
060:
061: if (LOG.isDebugEnabled()) {
062: LOG.debug("Configuring ehcache from file: "
063: + file.toString());
064: }
065: Configuration configuration = null;
066: InputStream input = null;
067: try {
068: input = new BufferedInputStream(new FileInputStream(file));
069: configuration = parseConfiguration(input);
070: } catch (Exception e) {
071: throw new CacheException("Error configuring from " + file
072: + ". Initial cause was " + e.getMessage(), e);
073: } finally {
074: try {
075: if (input != null) {
076: input.close();
077: }
078: } catch (IOException e) {
079: LOG
080: .error("IOException while closing configuration input stream. Error was "
081: + e.getMessage());
082: }
083: }
084: return configuration;
085: }
086:
087: /**
088: * Configures a bean from an XML file available as an URL.
089: */
090: public static Configuration parseConfiguration(final URL url)
091: throws CacheException {
092: if (LOG.isDebugEnabled()) {
093: LOG.debug("Configuring ehcache from URL: " + url);
094: }
095: Configuration configuration;
096: InputStream input = null;
097: try {
098: input = url.openStream();
099: configuration = parseConfiguration(input);
100: } catch (Exception e) {
101: throw new CacheException("Error configuring from " + url
102: + ". Initial cause was " + e.getMessage(), e);
103: } finally {
104: try {
105: if (input != null) {
106: input.close();
107: }
108: } catch (IOException e) {
109: LOG
110: .error("IOException while closing configuration input stream. Error was "
111: + e.getMessage());
112: }
113: }
114: return configuration;
115: }
116:
117: /**
118: * Configures a bean from an XML file in the classpath.
119: */
120: public static Configuration parseConfiguration()
121: throws CacheException {
122: ClassLoader standardClassloader = ClassLoaderUtil
123: .getStandardClassLoader();
124: URL url = null;
125: if (standardClassloader != null) {
126: url = standardClassloader
127: .getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE);
128: }
129: if (url == null) {
130: url = ConfigurationFactory.class
131: .getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE);
132: }
133: if (url != null) {
134: if (LOG.isDebugEnabled()) {
135: LOG
136: .debug("Configuring ehcache from ehcache.xml found in the classpath: "
137: + url);
138: }
139: } else {
140: url = ConfigurationFactory.class
141: .getResource(FAILSAFE_CLASSPATH_CONFIGURATION_FILE);
142: if (LOG.isWarnEnabled()) {
143: LOG
144: .warn("No configuration found. Configuring ehcache from ehcache-failsafe.xml "
145: + " found in the classpath: " + url);
146: }
147: }
148: return parseConfiguration(url);
149: }
150:
151: /**
152: * Configures a bean from an XML input stream.
153: */
154: public static Configuration parseConfiguration(
155: final InputStream inputStream) throws CacheException {
156: if (LOG.isDebugEnabled()) {
157: LOG.debug("Configuring ehcache from InputStream");
158: }
159: Configuration configuration = new Configuration();
160: try {
161: final SAXParser parser = SAXParserFactory.newInstance()
162: .newSAXParser();
163: final BeanHandler handler = new BeanHandler(configuration);
164: parser.parse(inputStream, handler);
165: } catch (Exception e) {
166: throw new CacheException(
167: "Error configuring from input stream. Initial cause was "
168: + e.getMessage(), e);
169: }
170: return configuration;
171: }
172:
173: }
|