001/* 002 * #%L 003 * Netarchivesuite - monitor 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023package dk.netarkivet.monitor.registry; 024 025import java.util.Collections; 026import java.util.HashMap; 027import java.util.HashSet; 028import java.util.Map; 029import java.util.Set; 030 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034import dk.netarkivet.common.distribute.monitorregistry.HostEntry; 035import dk.netarkivet.common.exceptions.ArgumentNotValid; 036 037/** 038 * A registry of known JMX URLs. This class is coded to be thread safe. 039 */ 040public class MonitorRegistry { 041 042 /** A map from host names to known host entries. */ 043 private Map<String, Set<HostEntry>> hostEntries = Collections 044 .synchronizedMap(new HashMap<String, Set<HostEntry>>()); 045 /** The singleton instance. */ 046 private static MonitorRegistry instance; 047 /** The logger for this class. */ 048 private static final Logger log = LoggerFactory.getLogger(MonitorRegistry.class); 049 050 /** 051 * Get the singleton instance. 052 * 053 * @return The singleton instance. 054 */ 055 public static synchronized MonitorRegistry getInstance() { 056 if (instance == null) { 057 instance = new MonitorRegistry(); 058 } 059 return instance; 060 } 061 062 /** 063 * Register a new JMX host entry. 064 * 065 * @param hostEntry The entry to add 066 * @throws ArgumentNotValid if hostEntry is null. 067 */ 068 public synchronized void register(HostEntry hostEntry) { 069 ArgumentNotValid.checkNotNull(hostEntry, "HostEntry hostEntry"); 070 Set<HostEntry> set = hostEntries.get(hostEntry.getName()); 071 if (set == null) { 072 set = Collections.synchronizedSet(new HashSet<HostEntry>()); 073 hostEntries.put(hostEntry.getName(), set); 074 } 075 if (set.add(hostEntry)) { 076 log.info("Added host '{}' port {}/{}", hostEntry.getName(), hostEntry.getJmxPort(), hostEntry.getRmiPort()); 077 } else { 078 // TODO WTF?! 079 set.remove(hostEntry); 080 set.add(hostEntry); 081 log.trace("Updated time for '{}' port {}/{} to {}", hostEntry.getName(), hostEntry.getJmxPort(), 082 hostEntry.getRmiPort(), hostEntry.getTime()); 083 } 084 } 085 086 /** 087 * Get all JMX host entries. 088 * 089 * @return All JMX host entries. 090 */ 091 public synchronized Map<String, Set<HostEntry>> getHostEntries() { 092 return new HashMap<String, Set<HostEntry>>((hostEntries)); 093 } 094 095}