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.services.monitoring.notification;
19  
20  import java.lang.reflect.Method;
21  import java.text.MessageFormat;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.util.StringUtils;
26  
27  import ch.elca.el4j.core.exceptions.MisconfigurationRTException;
28  
29  /**
30   * This class is used to notify on events which are core based.
31   *
32   * @svnLink $Revision: 3951 $;$Date: 2009-10-19 17:44:35 +0200 (Mo, 19. Okt 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/monitoring/notification/CoreNotificationHelper.java $
33   *
34   * @author Martin Zeltner (MZE)
35   */
36  public final class CoreNotificationHelper {
37  	/**
38  	 * Private logger of this class.
39  	 */
40  	private static Logger s_logger
41  		= LoggerFactory.getLogger(CoreNotificationHelper.class);
42  	
43  	/**
44  	 * Hide default constructor.
45  	 */
46  	private CoreNotificationHelper() {
47  	}
48  
49  	/**
50  	 * Method to notify that an essential property of a bean is lacking. In
51  	 * every case a <code>MissconfigurationRTException</code> will be thrown.
52  	 *
53  	 * @param propertyName
54  	 *            Is the name of the lacking property.
55  	 * @param concernedBean
56  	 *            Is the bean where the property is missing.
57  	 * @throws MisconfigurationRTException Will be thrown everytime.
58  	 */
59  	public static void notifyLackingEssentialProperty(String propertyName,
60  			Object concernedBean) throws MisconfigurationRTException {
61  		assert propertyName != null && propertyName.length() > 0;
62  		assert concernedBean != null;
63  
64  		Class<?> beanClass = concernedBean.getClass();
65  		String beanClassName = beanClass.getName();
66  		String beanName;
67  		try {
68  			Method m = beanClass.getMethod("getBeanName", (Class[]) null);
69  			beanName = (String) m.invoke(concernedBean, (Object[]) null);
70  		} catch (Exception e) {
71  			// Bean name could not be extracted.
72  			beanName = null;
73  		}
74  
75  		String message = "The property '" + propertyName + "' is required! ";
76  		if (StringUtils.hasText(beanName)) {
77  			message += "Concerned bean is of class '" + beanClassName
78  					+ "' and has name '" + beanName + "'.";
79  		} else {
80  			message += "Concerned bean is of class '" + beanClassName + "'.";
81  		}
82  
83  		notifyMisconfiguration(message);
84  	}
85  	
86  	/**
87  	 * Method to check if an essential property is not <code>null</code>.
88  	 *
89  	 * @param essentialProperty
90  	 *            Is the property which should not be null.
91  	 * @param propertyName
92  	 *            Is the name of the lacking property.
93  	 * @param concernedBean
94  	 *            Is the bean where the property is missing.
95  	 * @throws MisconfigurationRTException
96  	 *             If essential property is misconfigured.
97  	 */
98  	public static void notifyIfEssentialPropertyIsEmpty(
99  		Object essentialProperty, String propertyName, Object concernedBean)
100 		throws MisconfigurationRTException {
101 		if (essentialProperty == null) {
102 			notifyLackingEssentialProperty(propertyName, concernedBean);
103 		}
104 	}
105 	
106 	/**
107 	 * Method to check if an essential property string is has text.
108 	 *
109 	 * @param essentialStringProperty
110 	 *            Is the property which should have text.
111 	 * @param propertyName
112 	 *            Is the name of the lacking property.
113 	 * @param concernedBean
114 	 *            Is the bean where the property is missing.
115 	 * @throws MisconfigurationRTException
116 	 *             If essential property is misconfigured.
117 	 * @see StringUtils#hasText(java.lang.String)
118 	 */
119 	public static void notifyIfEssentialPropertyIsEmpty(
120 		String essentialStringProperty, String propertyName,
121 		Object concernedBean) throws MisconfigurationRTException {
122 		if (!StringUtils.hasText(essentialStringProperty)) {
123 			notifyLackingEssentialProperty(propertyName, concernedBean);
124 		}
125 	}
126 	
127 	/**
128 	 * Method to log that a misconfiguration has occurred.
129 	 * This method will always throw an exception.
130 	 *
131 	 * @param message
132 	 *            Is the message which explains the misconfiguration.
133 	 * @throws MisconfigurationRTException
134 	 *            Will be thrown in every case.
135 	 */
136 	public static void notifyMisconfiguration(String message)
137 		throws MisconfigurationRTException {
138 		s_logger.error(message);
139 		throw new MisconfigurationRTException(message);
140 	}
141 	
142 	/**
143 	 * Logs the message and the Exception cause and translates it to a
144 	 * {@link MisconfigurationRTException}.
145 	 *
146 	 * @param message
147 	 *            describes the misconfiguration.
148 	 * @param cause
149 	 *            the cause exception.
150 	 * @throws MisconfigurationRTException
151 	 *            Will be thrown in every case.
152 	 */
153 	public static void notifyMisconfiguration(String message, Throwable cause)
154 		throws MisconfigurationRTException {
155 		s_logger.error(message, cause);
156 		throw new MisconfigurationRTException(message, cause);
157 	}
158 
159 	/**
160 	 * Replaces place holders in the message with the values provided as
161 	 * parameters.
162 	 *
163 	 * @param message
164 	 *            message that contains place holders.
165 	 * @param parameters
166 	 *            list of parameters that replace the place holders.
167 	 * @throws MisconfigurationRTException
168 	 *            Will be thorwn in every case.
169 	 * @see MessageFormat
170 	 */
171 	public static void notifyMisconfiguration(String message,
172 			Object... parameters) throws MisconfigurationRTException {
173 		String msg = MessageFormat.format(message, parameters);
174 		notifyMisconfiguration(msg);
175 	}
176 }