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
     1 /**
     2  * SQL-DK
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package info.globalcode.sql.dk.jmx;
    18 
    19 import java.util.EnumMap;
    20 import java.util.Map;
    21 
    22 /**
    23  * JMX management bean for progress reporting.
    24  *
    25  * @author Ing. František Kučera (frantovo.cz)
    26  */
    27 public class ConnectionManagement implements ConnectionManagementMBean {
    28 
    29 	private final String databaseName;
    30 	private final Map<COUNTER, Integer> counters = new EnumMap(COUNTER.class);
    31 
    32 	public ConnectionManagement(String databaseName) {
    33 		this.databaseName = databaseName;
    34 		for (COUNTER c : COUNTER.values()) {
    35 			counters.put(c, 0);
    36 		}
    37 	}
    38 
    39 	public enum COUNTER {
    40 
    41 		COMMAND,
    42 		RECORD_CURRENT,
    43 		RECORD_TOTAL
    44 	};
    45 
    46 	public void incrementCounter(COUNTER counter) {
    47 		synchronized (counters) {
    48 			int old = counters.get(counter);
    49 			counters.put(counter, old + 1);
    50 		}
    51 	}
    52 
    53 	public void resetCounter(COUNTER counter) {
    54 		synchronized (counters) {
    55 			counters.put(counter, 0);
    56 		}
    57 	}
    58 
    59 	public static void incrementCounter(ConnectionManagement mbean, COUNTER counter) {
    60 		if (mbean != null) {
    61 			mbean.incrementCounter(counter);
    62 		}
    63 	}
    64 
    65 	public static void resetCounter(ConnectionManagement mbean, COUNTER counter) {
    66 		if (mbean != null) {
    67 			mbean.resetCounter(counter);
    68 		}
    69 	}
    70 
    71 	@Override
    72 	public String getDatabaseName() {
    73 		return databaseName;
    74 	}
    75 
    76 	@Override
    77 	public int getCommandCount() {
    78 		synchronized (counters) {
    79 			return counters.get(COUNTER.COMMAND);
    80 		}
    81 	}
    82 
    83 	@Override
    84 	public int getCurrentRecordCount() {
    85 		synchronized (counters) {
    86 			return counters.get(COUNTER.RECORD_CURRENT);
    87 		}
    88 	}
    89 	
    90 	@Override
    91 	public int getTotalRecordCount() {
    92 		synchronized (counters) {
    93 			return counters.get(COUNTER.RECORD_TOTAL);
    94 		}
    95 	}
    96 }