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.jmx;
19  
20  import javax.management.MBeanServer;
21  import javax.management.ObjectName;
22  
23  import org.springframework.beans.factory.DisposableBean;
24  import org.springframework.beans.factory.FactoryBean;
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.util.StringUtils;
27  
28  import com.sun.jdmk.comm.HtmlAdaptorServer;
29  
30  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
31  
32  /**
33   * This class creates a HtmlAdapter for an MBeanServer.
34   *
35   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/HtmlAdapterFactoryBean.java $
36   *
37   * @author Raphael Boog (RBO)
38   */
39  public class HtmlAdapterFactoryBean
40  	implements FactoryBean, InitializingBean, DisposableBean {
41  	
42  	/**
43  	 * The counter on the number of created HtmlAdaptorServers.
44  	 */
45  	private static int s_counter = 1;
46  
47  	/**
48  	 * The default port.
49  	 */
50  	private static final int DEFAULT_PORT = 9092;
51  
52  	/**
53  	 * The instance counter of this object.
54  	 */
55  	private int m_instanceCounter = 1;
56  
57  	/**
58  	 * The HtmlAdaptorServer which is created by this FactoryBean.
59  	 */
60  	private HtmlAdaptorServer m_htmlAdaptorServer;
61  
62  	/**
63  	 * The MBean Server this HtmlAdaptorServer is registered at.
64  	 */
65  	private MBeanServer m_server;
66  	
67  	/**
68  	 * Whether to ignore InstanceAlreadyExistsExceptions or not.
69  	 */
70  	private boolean m_ignoreInstanceAlreadyExistsException = false;
71  	
72  	/**
73  	 * Is the path to the used stylesheet. Default is set to
74  	 * <code>etc/jmx/stylesheet.css</code>.
75  	 */
76  	private String m_stylesheetPath = "etc/jmx/stylesheet.css";
77  
78  	/**
79  	 * The port member.
80  	 */
81  	private int m_port = DEFAULT_PORT;
82  
83  	/**
84  	 * The name of the ObjectName of the HtmlAdaptorServer.
85  	 */
86  	private String m_name = null;
87  	
88  	/**
89  	 * ObjectName of the html parser.
90  	 */
91  	private String m_htmlParserName = null;
92  
93  	/**
94  	 * {@inheritDoc}
95  	 */
96  	public Object getObject() throws Exception {
97  		return m_htmlAdaptorServer;
98  	}
99  
100 	/**
101 	 * {@inheritDoc}
102 	 */
103 	public Class getObjectType() {
104 		return HtmlAdaptorServer.class;
105 	}
106 
107 	/**
108 	 * {@inheritDoc}
109 	 */
110 	public boolean isSingleton() {
111 		return true;
112 	}
113 
114 	/**
115 	 * Creating an HtmlAdaptorServer and register it at the MBean Server.
116 	 *
117 	 * @throws Exception
118 	 *             In case the object name is invalid or the HtmlAdaptorServer
119 	 *             could not be registered at the MBean Server.
120 	 */
121 	public void afterPropertiesSet() throws Exception {
122 		CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
123 			getServer(), "server", this);
124 		CoreNotificationHelper.notifyIfEssentialPropertyIsEmpty(
125 			getStylesheetPath(), "stylesheetPath", this);
126 
127 		/**
128 		 * Increase instance counter.
129 		 */
130 		setInstanceCounter();
131 		
132 		if (!StringUtils.hasText(m_htmlParserName)) {
133 			m_htmlParserName = "HtmlAdapter:name=HtmlParser" + getInstanceCounter();
134 		}
135 		ObjectName htmlParserObjectName = new ObjectName(m_htmlParserName);
136 		if (!getServer().isRegistered(htmlParserObjectName) || !m_ignoreInstanceAlreadyExistsException) {
137 			
138 			/**
139 			 * Setup css html parser.
140 			 */
141 			CssHtmlParser htmlParser = new CssHtmlParser(getStylesheetPath());
142 			
143 			getServer().registerMBean(htmlParser, htmlParserObjectName);
144 		}
145 		
146 		if (!StringUtils.hasText(m_name)) {
147 			m_name = "HtmlAdapter:name=HtmlAdapter" + getInstanceCounter();
148 		}
149 		ObjectName htmlAdapterServerObjectName = new ObjectName(getName());
150 		if (!getServer().isRegistered(htmlAdapterServerObjectName) || !m_ignoreInstanceAlreadyExistsException) {
151 			
152 			/**
153 			 * Setup html adapter server.
154 			 */
155 			m_htmlAdaptorServer = new HtmlAdaptorServer();
156 			m_htmlAdaptorServer.setPort(m_port);
157 			
158 			getServer().registerMBean(m_htmlAdaptorServer, htmlAdapterServerObjectName);
159 			
160 			/**
161 			 * Set css html parser in html adaptor server.
162 			 */
163 			m_htmlAdaptorServer.setParser(htmlParserObjectName);
164 			m_htmlAdaptorServer.start();
165 		}
166 	}
167 
168 	/**
169 	 * The getter method for the instanceCounter member.
170 	 *
171 	 * @return The instanceCounter member
172 	 */
173 	public int getInstanceCounter() {
174 		return m_instanceCounter;
175 	}
176 
177 	/**
178 	 * Save the class variable s_counter to an instance member and increment the
179 	 * class variable by 1.
180 	 */
181 	public void setInstanceCounter() {
182 		synchronized (HtmlAdapterFactoryBean.class) {
183 			m_instanceCounter = s_counter;
184 			s_counter++;
185 		}
186 	}
187 
188 	/**
189 	 * The getter method for the port member.
190 	 *
191 	 * @return The port of the HtmlAdaptorServer.
192 	 */
193 	public int getPort() {
194 		return m_port;
195 	}
196 
197 	/**
198 	 * The setter method for the port member.
199 	 *
200 	 * @param port
201 	 *            The port to set.
202 	 */
203 	public void setPort(int port) {
204 		this.m_port = port;
205 	}
206 
207 	/**
208 	 * The getter method for the MBean Server.
209 	 *
210 	 * @return The MBean Server this HtmlAdaptorServer is registered at.
211 	 */
212 	public MBeanServer getServer() {
213 		return m_server;
214 	}
215 
216 	/**
217 	 * The setter method for the MBean Server.
218 	 *
219 	 * @param mBeanServer
220 	 *            The MBean Server to set.
221 	 */
222 	public void setServer(MBeanServer mBeanServer) {
223 		this.m_server = mBeanServer;
224 	}
225 	
226 	/**
227 	 * @return    whether to ignore InstanceAlreadyExistsExceptions or not
228 	 */
229 	public boolean isIgnoreInstanceAlreadyExistsException() {
230 		return m_ignoreInstanceAlreadyExistsException;
231 	}
232 	
233 	/**
234 	 * @param ignoreInstanceAlreadyExistsException    whether to ignore InstanceAlreadyExistsExceptions or not
235 	 */
236 	public void setIgnoreInstanceAlreadyExistsException(boolean ignoreInstanceAlreadyExistsException) {
237 		m_ignoreInstanceAlreadyExistsException = ignoreInstanceAlreadyExistsException;
238 	}
239 
240 	/**
241 	 * The getter method for the name of the ObjectName.
242 	 *
243 	 * @return The name of the ObjectName.
244 	 */
245 	public String getName() {
246 		return m_name;
247 	}
248 
249 	/**
250 	 * The setter method for the name of the ObjectName.
251 	 *
252 	 * @param name
253 	 *            The name of the ObjectName to set.
254 	 */
255 	public void setName(String name) {
256 		m_name = name;
257 	}
258 	
259 	/**
260 	 * @return Returns the htmlParserName.
261 	 */
262 	public String getHtmlParserName() {
263 		return m_htmlParserName;
264 	}
265 
266 	/**
267 	 * @param htmlParserName The htmlParserName to set.
268 	 */
269 	public void setHtmlParserName(String htmlParserName) {
270 		m_htmlParserName = htmlParserName;
271 	}
272 
273 	/**
274 	 * @return Returns the stylesheetPath.
275 	 */
276 	public String getStylesheetPath() {
277 		return m_stylesheetPath;
278 	}
279 
280 	/**
281 	 * @param stylesheetPath The stylesheetPath to set.
282 	 */
283 	public void setStylesheetPath(String stylesheetPath) {
284 		m_stylesheetPath = stylesheetPath;
285 	}
286 
287 	/**
288 	 * The getter method for the HtmlAdaptorServer.
289 	 *
290 	 * @return The HtmlAdaptorServer.
291 	 */
292 	public HtmlAdaptorServer getHtmlAdaptorServer() {
293 		return m_htmlAdaptorServer;
294 	}
295 
296 	/**
297 	 * {@inheritDoc}
298 	 */
299 	public void destroy() throws Exception {
300 		// Try to stop the AdaptorServer
301 		if (m_htmlAdaptorServer != null) {
302 			m_htmlAdaptorServer.stop();
303 			getServer().unregisterMBean(new ObjectName(m_htmlParserName));
304 			getServer().unregisterMBean(new ObjectName(getName()));
305 		}
306 	}
307 }