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    
018    package org.apache.logging.log4j.core.net.server;
019    
020    import java.util.concurrent.atomic.AtomicReference;
021    import javax.jms.JMSException;
022    import javax.jms.Message;
023    import javax.jms.MessageConsumer;
024    import javax.jms.MessageListener;
025    import javax.jms.ObjectMessage;
026    
027    import org.apache.logging.log4j.Logger;
028    import org.apache.logging.log4j.LoggingException;
029    import org.apache.logging.log4j.core.LifeCycle;
030    import org.apache.logging.log4j.core.LogEvent;
031    import org.apache.logging.log4j.core.LogEventListener;
032    import org.apache.logging.log4j.core.appender.mom.JmsManager;
033    import org.apache.logging.log4j.core.net.JndiManager;
034    import org.apache.logging.log4j.status.StatusLogger;
035    
036    /**
037     * LogEventListener server that receives LogEvents over a JMS {@link javax.jms.Destination}.
038     *
039     * @since 2.1
040     */
041    public class JmsServer extends LogEventListener implements MessageListener, LifeCycle {
042    
043        private static final Logger LOGGER = StatusLogger.getLogger();
044        private final AtomicReference<State> state = new AtomicReference<State>(State.INITIALIZED);
045        private final JmsManager jmsManager;
046        private MessageConsumer messageConsumer;
047    
048        public JmsServer(final String connectionFactoryBindingName,
049                         final String destinationBindingName,
050                         final String username,
051                         final String password) {
052            final String managerName = JmsServer.class.getName() + '@' + JmsServer.class.hashCode();
053            final JndiManager jndiManager = JndiManager.getDefaultManager(managerName);
054            jmsManager = JmsManager.getJmsManager(managerName, jndiManager, connectionFactoryBindingName,
055                destinationBindingName, username, password);
056        }
057    
058        @Override
059        public void onMessage(final Message message) {
060            try {
061                if (message instanceof ObjectMessage) {
062                    final Object body = ((ObjectMessage) message).getObject();
063                    if (body instanceof LogEvent) {
064                        log((LogEvent) body);
065                    } else {
066                        LOGGER.warn("Expected ObjectMessage to contain LogEvent. Got type {} instead.", body.getClass());
067                    }
068                } else {
069                    LOGGER.warn("Received message of type {} and JMSType {} which cannot be handled.", message.getClass(),
070                        message.getJMSType());
071                }
072            } catch (final JMSException e) {
073                LOGGER.catching(e);
074            }
075        }
076    
077        @Override
078        public void start() {
079            if (state.compareAndSet(State.INITIALIZED, State.STARTING)) {
080                try {
081                    messageConsumer = jmsManager.createMessageConsumer();
082                    messageConsumer.setMessageListener(this);
083                } catch (final JMSException e) {
084                    throw new LoggingException(e);
085                }
086            }
087        }
088    
089        @Override
090        public void stop() {
091            try {
092                messageConsumer.close();
093            } catch (final JMSException ignored) {
094            }
095            jmsManager.release();
096        }
097    
098        @Override
099        public boolean isStarted() {
100            return state.get() == State.STARTED;
101        }
102    
103        @Override
104        public boolean isStopped() {
105            return state.get() == State.STOPPED;
106        }
107    }