001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net;
018
019import javax.jms.JMSException;
020import javax.jms.ObjectMessage;
021import javax.naming.Context;
022import javax.naming.NameNotFoundException;
023import javax.naming.NamingException;
024
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
027import org.apache.logging.log4j.core.AbstractServer;
028import org.apache.logging.log4j.core.LogEvent;
029
030/**
031 * Abstract base class for receiving LogEvents over JMS. This class expects all messages to be serialized log events.
032 */
033public abstract class AbstractJMSReceiver extends AbstractServer implements javax.jms.MessageListener {
034
035    /**
036     * Logger to capture diagnostics.
037     */
038    protected Logger logger = LogManager.getLogger(this.getClass().getName());
039
040    /**
041     * Listener that receives the event.
042     * @param message The received message.
043     */
044    @Override
045    public void onMessage(final javax.jms.Message message) {
046
047        try {
048            if (message instanceof ObjectMessage) {
049                final ObjectMessage objectMessage = (ObjectMessage) message;
050                log((LogEvent) objectMessage.getObject());
051            } else {
052                logger.warn("Received message is of type " + message.getJMSType()
053                    + ", was expecting ObjectMessage.");
054            }
055        } catch (final JMSException jmse) {
056            logger.error("Exception thrown while processing incoming message.",
057                jmse);
058        }
059    }
060
061    /**
062     * Looks up an object from the Context.
063     * @param ctx The Context.
064     * @param name The name of the object to locate.
065     * @return The object.
066     * @throws NamingException if an error occurs.
067     */
068    protected Object lookup(final Context ctx, final String name) throws NamingException {
069        try {
070            return ctx.lookup(name);
071        } catch (final NameNotFoundException e) {
072            logger.error("Could not find name [" + name + "].");
073            throw e;
074        }
075    }
076
077}