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.distribute;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import dk.netarkivet.common.distribute.Channels;
029import dk.netarkivet.common.distribute.JMSConnectionFactory;
030import dk.netarkivet.common.exceptions.ArgumentNotValid;
031import dk.netarkivet.common.utils.CleanupIF;
032import dk.netarkivet.monitor.distribute.MonitorMessageHandler;
033import dk.netarkivet.monitor.registry.MonitorRegistry;
034
035/**
036 * The monitor registry server listens on JMS for hosts that wish to register themselves to the service. The registry
037 * lists hosts that can be monitored with JMX.
038 */
039public class MonitorRegistryServer extends MonitorMessageHandler implements CleanupIF {
040
041    private static MonitorRegistryServer instance;
042    private static final Logger log = LoggerFactory.getLogger(MonitorRegistryServer.class);
043
044    /**
045     * Start listening for registry messages.
046     */
047    private MonitorRegistryServer() {
048        JMSConnectionFactory.getInstance().setListener(Channels.getTheMonitorServer(), this);
049        log.info("MonitorRegistryServer listening for messages on channel '{}'", Channels.getTheMonitorServer());
050    }
051
052    /**
053     * Get the registry server singleton.
054     *
055     * @return The registry server.
056     */
057    public static MonitorRegistryServer getInstance() {
058        if (instance == null) {
059            instance = new MonitorRegistryServer();
060        }
061        return instance;
062    }
063
064    /**
065     * This method registers the sender as a host to be monitored with JMX.
066     *
067     * @throws ArgumentNotValid on null parameter.
068     */
069    public void visit(RegisterHostMessage msg) {
070        ArgumentNotValid.checkNotNull(msg, "RegisterHostMessage msg");
071        MonitorRegistry.getInstance().register(msg.getHostEntry());
072    }
073
074    /** Remove listener on shutdown. */
075    public void cleanup() {
076        // FIXME These commands fail when shutting down properly. (kill $PID)
077        // instead of kill -9 $PID. See NAS-1976
078        // JMSConnectionFactory.getInstance().removeListener(
079        // Channels.getTheMonitorServer(), this);
080    }
081
082}