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     */
017    package org.apache.logging.log4j.core.net.jms;
018    
019    import java.io.Serializable;
020    
021    import javax.jms.JMSException;
022    import javax.jms.ObjectMessage;
023    import javax.naming.Context;
024    import javax.naming.NameNotFoundException;
025    import javax.naming.NamingException;
026    
027    import org.apache.logging.log4j.LogManager;
028    import org.apache.logging.log4j.Logger;
029    import org.apache.logging.log4j.core.LogEvent;
030    import org.apache.logging.log4j.core.LogEventListener;
031    
032    /**
033     * Abstract base class for receiving LogEvents over JMS. This class expects all messages to be serialized log events.
034     */
035    public abstract class AbstractJmsReceiver extends LogEventListener implements javax.jms.MessageListener {
036    
037        /**
038         * Logger to capture diagnostics.
039         */
040        protected Logger logger = LogManager.getLogger(this.getClass().getName());
041    
042        /**
043         * Listener that receives the event.
044         * @param message The received message.
045         */
046        @Override
047        public void onMessage(final javax.jms.Message message) {
048            try {
049                if (message instanceof ObjectMessage) {
050                    final ObjectMessage objectMessage = (ObjectMessage) message;
051                    final Serializable object = objectMessage.getObject();
052                    if (object instanceof LogEvent) {
053                        log((LogEvent) object);
054                    } else {
055                        logger.warn("Received message is of type " + object.getClass().getName() + ", was expecting LogEvent.");
056                    }
057                } else {
058                    logger.warn("Received message is of type " + message.getJMSType()
059                        + ", was expecting ObjectMessage.");
060                }
061            } catch (final JMSException jmse) {
062                logger.error("Exception thrown while processing incoming message.",
063                    jmse);
064            }
065        }
066    
067        /**
068         * Looks up an object from the Context.
069         * @param ctx The Context.
070         * @param name The name of the object to locate.
071         * @return The object.
072         * @throws NamingException if an error occurs.
073         */
074        protected Object lookup(final Context ctx, final String name) throws NamingException {
075            try {
076                return ctx.lookup(name);
077            } catch (final NameNotFoundException e) {
078                logger.error("Could not find name [" + name + "].");
079                throw e;
080            }
081        }
082    
083    }