java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ConnectionManagement.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2014 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.jmx;

import java.util.EnumMap;
import java.util.Map;

/**
 * JMX management bean for progress reporting.
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class ConnectionManagement implements ConnectionManagementMBean {

	private final String databaseName;
	private final Map<COUNTER, Integer> counters = new EnumMap(COUNTER.class);

	public ConnectionManagement(String databaseName) {
		this.databaseName = databaseName;
		for (COUNTER c : COUNTER.values()) {
			counters.put(c, 0);
		}
	}

	public enum COUNTER {

		COMMAND,
		RECORD_CURRENT,
		RECORD_TOTAL
	};

	public void incrementCounter(COUNTER counter) {
		synchronized (counters) {
			int old = counters.get(counter);
			counters.put(counter, old + 1);
		}
	}

	public void resetCounter(COUNTER counter) {
		synchronized (counters) {
			counters.put(counter, 0);
		}
	}

	public static void incrementCounter(ConnectionManagement mbean, COUNTER counter) {
		if (mbean != null) {
			mbean.incrementCounter(counter);
		}
	}

	public static void resetCounter(ConnectionManagement mbean, COUNTER counter) {
		if (mbean != null) {
			mbean.resetCounter(counter);
		}
	}

	@Override
	public String getDatabaseName() {
		return databaseName;
	}

	@Override
	public int getCommandCount() {
		synchronized (counters) {
			return counters.get(COUNTER.COMMAND);
		}
	}

	@Override
	public int getCurrentRecordCount() {
		synchronized (counters) {
			return counters.get(COUNTER.RECORD_CURRENT);
		}
	}
	
	@Override
	public int getTotalRecordCount() {
		synchronized (counters) {
			return counters.get(COUNTER.RECORD_TOTAL);
		}
	}
}