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; 018 019 import org.apache.logging.log4j.LogManager; 020 import org.apache.logging.log4j.Logger; 021 import org.apache.logging.log4j.core.AbstractServer; 022 import org.apache.logging.log4j.core.LogEvent; 023 024 import javax.jms.JMSException; 025 import javax.jms.ObjectMessage; 026 import javax.naming.Context; 027 import javax.naming.NameNotFoundException; 028 import javax.naming.NamingException; 029 030 /** 031 * Abstract base class for receiving LogEvents over JMS. This class expects all messages to be serialized log events. 032 */ 033 public 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 public void onMessage(final javax.jms.Message message) { 045 046 try { 047 if (message instanceof ObjectMessage) { 048 final ObjectMessage objectMessage = (ObjectMessage) message; 049 log((LogEvent) objectMessage.getObject()); 050 } else { 051 logger.warn("Received message is of type " + message.getJMSType() 052 + ", was expecting ObjectMessage."); 053 } 054 } catch (final JMSException jmse) { 055 logger.error("Exception thrown while processing incoming message.", 056 jmse); 057 } 058 } 059 060 /** 061 * Looks up an object from the Context. 062 * @param ctx The Context. 063 * @param name The name of the object to locate. 064 * @return The object. 065 * @throws NamingException if an error occurs. 066 */ 067 protected Object lookup(final Context ctx, final String name) throws NamingException { 068 try { 069 return ctx.lookup(name); 070 } catch (final NameNotFoundException e) { 071 logger.error("Could not find name [" + name + "]."); 072 throw e; 073 } 074 } 075 076 }