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.distribute;
024
025import javax.jms.Message;
026import javax.jms.MessageListener;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import dk.netarkivet.common.distribute.JMSConnection;
032import dk.netarkivet.common.exceptions.ArgumentNotValid;
033import dk.netarkivet.common.exceptions.PermissionDenied;
034import dk.netarkivet.monitor.registry.distribute.RegisterHostMessage;
035
036/**
037 * This default message handler shields of all unimplemented methods from the MonitorMessageVisitor interface.
038 * <p>
039 * Classes should not implement MonitorMessageVisitor but extend this class.
040 *
041 * @see MonitorMessageVisitor
042 */
043public abstract class MonitorMessageHandler implements MonitorMessageVisitor, MessageListener {
044
045    private static final Logger log = LoggerFactory.getLogger(MonitorMessageHandler.class);
046
047    /**
048     * Creates a MonitorMessageHandler object.
049     */
050    public MonitorMessageHandler() {
051    }
052
053    /**
054     * Unpacks and calls accept() on the message object.
055     * <p>
056     * This method catches <b>all</b> exceptions and logs them.
057     *
058     * @param msg a ObjectMessage
059     */
060    public void onMessage(Message msg) {
061        ArgumentNotValid.checkNotNull(msg, "Message msg");
062        log.trace("Message received:\n{}", msg.toString());
063        try {
064            ((MonitorMessage) JMSConnection.unpack(msg)).accept(this);
065        } catch (ClassCastException e) {
066            log.warn("Invalid message type", e);
067        } catch (Throwable e) {
068            log.warn("Error processing message '{}'", msg, e);
069        }
070    }
071
072    /**
073     * Handles when a handler receives a message it is not prepare to handle.
074     *
075     * @param msg The received message.
076     * @throws PermissionDenied Always
077     */
078    private void deny(MonitorMessage msg) {
079        throw new PermissionDenied("'" + this + "' provides no handling for " + msg + " of type "
080                + msg.getClass().getName() + " and should not be invoked!");
081    }
082
083    /**
084     * This method should be overridden and implemented by a sub class if message handling is wanted.
085     *
086     * @param msg a RegisterHostMessage
087     * @throws PermissionDenied when invoked
088     */
089    public void visit(RegisterHostMessage msg) throws PermissionDenied {
090        ArgumentNotValid.checkNotNull(msg, "RegsiterHostMessage msg");
091        deny(msg);
092    }
093
094}