All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.welen.jmole.protocols.AbstractIntervalProtocol Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package net.welen.jmole.protocols;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ReflectionException;

import net.welen.jmole.JMole;
import net.welen.jmole.presentation.PresentationInformation;

/*
 * #%L
 * JMole, https://bitbucket.org/awelen/jmole
 * %%
 * Copyright (C) 2015 - 2020 Anders Welén, [email protected]
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

abstract public class AbstractIntervalProtocol extends AbstractProtocol implements Runnable {

	private final static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(AbstractIntervalProtocol.class.getName());
	private static final char SEP = '/';	

	private JMole jmole;
	private Thread collector;
	private boolean stopRequested = false;
	private boolean threadStopped = false;

	@Override
	public void startProtocol(JMole jmole) throws Exception {
		this.jmole = jmole;
		
		if (getInterval() == 0) {
			setInterval(60000L);
		}

		collector = new Thread(this);
		collector.setName("JMole protocol thread");
		collector.start();
		LOG.log(Level.FINE, "JMole protocol thread started: Interval=" + getInterval());
	}

	@Override
	public void stopProtocol() throws Exception {
		LOG.log(Level.FINE, "Stopping protocol thread");
		stopRequested = true;
		collector.interrupt();
		while (!threadStopped) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				LOG.log(Level.FINE, e.getMessage(), e);
			}
		}
		LOG.log(Level.FINE, "Protocol thread stopped");
	}

	@Override
	public void run() {
		stopRequested = false;
		threadStopped = false;
		try {
			while (!stopRequested) {
				try {
					Thread.sleep(getInterval());
				} catch (InterruptedException e) {
					LOG.log(Level.FINE, e.getMessage(), e);
				}

				if (stopRequested) {
					return;
				}

				try {
					handleMeasurements();
				} catch (Exception e) {
					LOG.log(Level.SEVERE, e.getMessage(), e);
				}
				try {
					handleWarnings();
				} catch (Exception e) {
					LOG.log(Level.SEVERE, e.getMessage(), e);
				}
				try {
					handleCriticals();
				} catch (Exception e) {
					LOG.log(Level.SEVERE, e.getMessage(), e);
				}

			}
		} finally {
			threadStopped = true;
		}
	}

	private void handleMeasurements()
			throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException {
		Map presentationInformationMap = new HashMap();
		for (Entry>>> categoryEntry : jmole.collectMeasurements(presentationInformationMap).entrySet()) {
			Iterator>> iter = categoryEntry.getValue().iterator();
			while (iter.hasNext()) {
				for (Entry> nameEntry : iter.next().entrySet()) {
					for (Entry attributeEntry : nameEntry.getValue().entrySet()) {
						String piKey = categoryEntry.getKey() + nameEntry.getKey();
						PresentationInformation presentationInformation = presentationInformationMap.get(piKey);
						if (presentationInformation == null) {
							LOG.severe("No presentation information found for: " + piKey + ", Skipping it");
							continue;
						}
						try {
							if (attributeEntry.getValue() != null) {
								handleMeasurement(categoryEntry.getKey(), nameEntry.getKey(), attributeEntry.getKey(), attributeEntry.getValue(), presentationInformation);
							}
						} catch (Exception e) {
							LOG.log(Level.SEVERE, e.getMessage() + ": " + categoryEntry.getKey() + SEP
									+ nameEntry.getKey() + SEP + attributeEntry.getKey(), e);
						}
					}
				}
			}
		}
	}

	abstract protected long getInterval();
	abstract protected void setInterval(long interval);
	abstract protected void handleMeasurement(String category, String name, String attribute, Object value, PresentationInformation presentationInformation) throws Exception;	
	abstract protected void handleWarnings() throws Exception;
	abstract protected void handleCriticals() throws Exception;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy