package ch.goodsolutions.olat.jmx;
import java.lang.management.ManagementFactory;
import java.util.Iterator;
import java.util.List;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.log4j.Logger;
import org.olat.core.configuration.OLATModule;
import org.olat.core.logging.StartupException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.WebappHelper;
import com.anthonyeden.lib.config.Configuration;
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* This software is protected by the OLAT software license.<br>
* Use is subject to license terms.<br>
* See LICENSE.TXT in this distribution for details.
* <p>
* Copyright (c) 2005-2006 by JGS goodsolutions GmbH, Switzerland<br>
* http://www.goodsolutions.ch
*
* All rights reserved.
* <p>
*/
public class JMXModule implements OLATModule {
private Logger log = Tracing.getLogger(JMXModule.class);
private static int port;
private static String user, pass;
public void init(Configuration moduleConfig) {
String enabled = moduleConfig.getChildValue("enableJMXsupport");
if (enabled.equalsIgnoreCase("false") || enabled.equalsIgnoreCase("no")) {
log.info("JMX support disabled.");
return;
}
log.info("JMX support enabled.");
Configuration jmxServer = moduleConfig.getChild("jmxserver");
try {
port = Integer.parseInt(jmxServer.getAttribute("port"));
} catch (NumberFormatException nfe) {
throw new StartupException("Invalid JMX server port. Please fix!");
}
user = jmxServer.getAttribute("user");
pass = jmxServer.getAttribute("pass");
if (user != null && user.length() == 0) user = null;
if (pass != null && pass.length() == 0) pass = null;
// expose MBeans
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
List beans = moduleConfig.getChildren("bean");
for (Iterator iter = beans.iterator(); iter.hasNext();) {
Configuration bean = (Configuration) iter.next();
String clazz = bean.getAttribute("class");
Class beanClass;
try {
beanClass = cl.loadClass(clazz);
ObjectName name = new ObjectName(JMXHelper.buildRegisteredObjectName(beanClass, WebappHelper.getServletContextPath()));
if (!mbs.isRegistered(name))
mbs.registerMBean(beanClass.newInstance(), name);
} catch (Exception e) {
throw new StartupException("Error instantiating JMX bean: ", e);
}
}
}
public void destroy() {
// TODO Auto-generated method stub
}
public static String getPass() {
return pass;
}
public static int getPort() {
return port;
}
public static String getUser() {
return user;
}
}