View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.util.logging;
19  
20  import java.net.URL;
21  // Checkstyle: UseLogger off
22  import org.apache.log4j.xml.DOMConfigurator;
23  //Checkstyle: UseLogger on
24  import org.springframework.beans.factory.BeanNameAware;
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.util.StringUtils;
27  
28  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
29  import ch.elca.el4j.util.codingsupport.Reject;
30  
31  /**
32   * This class is used to load a freely choosable log4j xml configuration file.
33   *
34   * @svnLink $Revision: 4076 $;$Date: 2010-01-06 16:10:41 +0100 (Mi, 06. Jan 2010) $;$Author: swisswheel $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/logging/Log4jXmlLoader.java $
35   *
36   * @author Martin Zeltner (MZE)
37   */
38  public class Log4jXmlLoader implements InitializingBean, BeanNameAware {
39  	/**
40  	 * This is the location of the log4j configuration file.
41  	 */
42  	private String m_configLocation;
43  
44  	/**
45  	 * This is the name of the bean.
46  	 */
47  	private String m_beanName;
48  
49  	/**
50  	 * @return Returns the configLocation.
51  	 */
52  	public String getConfigLocation() {
53  		return m_configLocation;
54  	}
55  
56  	/**
57  	 * @param configLocation
58  	 *            The configLocation to set.
59  	 */
60  	public void setConfigLocation(String configLocation) {
61  		Reject.ifEmpty(configLocation,
62  				"Config location of log4j loader must not be empty!");
63  		this.m_configLocation = configLocation.trim();
64  		System.setProperty("log4j.configuration", configLocation);
65  		URL url = Log4jXmlLoader.class.getClassLoader().getResource(
66  				m_configLocation);
67  		DOMConfigurator.configure(url);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 */
73  	public void afterPropertiesSet() throws Exception {
74  		if (!StringUtils.hasText(getConfigLocation())) {
75  			CoreNotificationHelper.notifyLackingEssentialProperty(
76  					"configLocation", this);
77  		}
78  	}
79  
80  	/**
81  	 * {@inheritDoc}
82  	 */
83  	public void setBeanName(String beanName) {
84  		m_beanName = beanName;
85  	}
86  
87  	/**
88  	 * @return Returns the beanName.
89  	 */
90  	public String getBeanName() {
91  		return m_beanName;
92  	}
93  }