1 /** 2 * Copyright 2003-2006 Greg Luck 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 package net.sf.ehcache.config; 18 19 import net.sf.ehcache.CacheException; 20 import net.sf.ehcache.util.ClassLoaderUtil; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.xml.parsers.SAXParser; 25 import javax.xml.parsers.SAXParserFactory; 26 import java.io.BufferedInputStream; 27 import java.io.File; 28 import java.io.FileInputStream; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.net.URL; 32 33 /** 34 * A utility class which configures beans from XML, using reflection. 35 * 36 * @author Greg Luck 37 * @version $Id: ConfigurationFactory.java 52 2006-04-24 14:50:03Z gregluck $ 38 */ 39 public final class ConfigurationFactory { 40 private static final Log LOG = LogFactory.getLog(ConfigurationFactory.class.getName()); 41 42 private static final String DEFAULT_CLASSPATH_CONFIGURATION_FILE = "/ehcache.xml"; 43 private static final String FAILSAFE_CLASSPATH_CONFIGURATION_FILE = "/ehcache-failsafe.xml"; 44 45 /** 46 * Constructor. 47 */ 48 private ConfigurationFactory() { 49 50 } 51 52 /** 53 * Configures a bean from an XML file. 54 */ 55 public static Configuration parseConfiguration(final File file) throws CacheException { 56 if (file == null) { 57 throw new CacheException("Attempt to configure ehcache from null file."); 58 } 59 60 if (LOG.isDebugEnabled()) { 61 LOG.debug("Configuring ehcache from file: " + file.toString()); 62 } 63 Configuration configuration = null; 64 InputStream input = null; 65 try { 66 input = new BufferedInputStream(new FileInputStream(file)); 67 configuration = parseConfiguration(input); 68 } catch (Exception e) { 69 throw new CacheException("Error configuring from " + file + ". Initial cause was " + e.getMessage(), e); 70 } finally { 71 try { 72 if (input != null) { 73 input.close(); 74 } 75 } catch (IOException e) { 76 LOG.error("IOException while closing configuration input stream. Error was " + e.getMessage()); 77 } 78 } 79 return configuration; 80 } 81 82 /** 83 * Configures a bean from an XML file available as an URL. 84 */ 85 public static Configuration parseConfiguration(final URL url) throws CacheException { 86 if (LOG.isDebugEnabled()) { 87 LOG.debug("Configuring ehcache from URL: " + url); 88 } 89 Configuration configuration; 90 InputStream input = null; 91 try { 92 input = url.openStream(); 93 configuration = parseConfiguration(input); 94 } catch (Exception e) { 95 throw new CacheException("Error configuring from " + url + ". Initial cause was " + e.getMessage(), e); 96 } finally { 97 try { 98 if (input != null) { 99 input.close(); 100 } 101 } catch (IOException e) { 102 LOG.error("IOException while closing configuration input stream. Error was " + e.getMessage()); 103 } 104 } 105 return configuration; 106 } 107 108 /** 109 * Configures a bean from an XML file in the classpath. 110 */ 111 public static Configuration parseConfiguration() throws CacheException { 112 ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); 113 URL url = null; 114 if (standardClassloader != null) { 115 url = standardClassloader.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); 116 } 117 if (url == null) { 118 url = ConfigurationFactory.class.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); 119 } 120 if (url != null) { 121 if (LOG.isDebugEnabled()) { 122 LOG.debug("Configuring ehcache from ehcache.xml found in the classpath: " + url); 123 } 124 } else { 125 url = ConfigurationFactory.class.getResource(FAILSAFE_CLASSPATH_CONFIGURATION_FILE); 126 if (LOG.isWarnEnabled()) { 127 LOG.warn("No configuration found. Configuring ehcache from ehcache-failsafe.xml " 128 + " found in the classpath: " + url); 129 } 130 } 131 return parseConfiguration(url); 132 } 133 134 /** 135 * Configures a bean from an XML input stream. 136 */ 137 public static Configuration parseConfiguration(final InputStream inputStream) throws CacheException { 138 if (LOG.isDebugEnabled()) { 139 LOG.debug("Configuring ehcache from InputStream"); 140 } 141 Configuration configuration = new Configuration(); 142 try { 143 final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 144 final BeanHandler handler = new BeanHandler(configuration); 145 parser.parse(inputStream, handler); 146 } catch (Exception e) { 147 throw new CacheException("Error configuring from input stream. Initial cause was " + e.getMessage(), e); 148 } 149 return configuration; 150 } 151 152 }