package it.lorenzoingrilli.icarus.utils;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class Tomcat {
	
	private static final String START = "start";
	private static final String STOP = "stop";
	private static final String QUERY = "Tomcat:type=Connector,*";
	
	public static void restartConnectors() throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, MBeanException, InterruptedException {
		final ObjectName query = new ObjectName(QUERY); 
		for(final MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
			for(ObjectName objectName: server.queryNames(query, null)) {
				server.invoke(objectName, STOP, null, null);				
				/*while(server.queryNames(objectName, null).size()>0) {
					Thread.sleep(250);
				}*/
				server.invoke(objectName, START, null, null);				
			};
		}
	}
	
}
