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.appender;
018
019import java.io.Serializable;
020
021import org.apache.logging.log4j.core.Filter;
022import org.apache.logging.log4j.core.Layout;
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.config.plugins.Plugin;
025import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
026import org.apache.logging.log4j.core.config.plugins.PluginElement;
027import org.apache.logging.log4j.core.config.plugins.PluginFactory;
028import org.apache.logging.log4j.core.helpers.Booleans;
029import org.apache.logging.log4j.core.layout.SerializedLayout;
030import org.apache.logging.log4j.core.net.JMSQueueManager;
031
032/**
033 * Appender to write to a JMS Queue.
034 */
035@Plugin(name = "JMSQueue", category = "Core", elementType = "appender", printObject = true)
036public final class JMSQueueAppender extends AbstractAppender {
037
038    private final JMSQueueManager manager;
039
040    private JMSQueueAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
041                             final JMSQueueManager manager, final boolean ignoreExceptions) {
042        super(name, filter, layout, ignoreExceptions);
043        this.manager = manager;
044    }
045
046    /**
047     * Actual writing occurs here.
048     * 
049     * @param event The LogEvent.
050     */
051    @Override
052    public void append(final LogEvent event) {
053        try {
054            manager.send(getLayout().toSerializable(event));
055        } catch (final Exception ex) {
056            throw new AppenderLoggingException(ex);
057        }
058    }
059
060    /**
061     * Create a JMSQueueAppender.
062     * @param name The name of the Appender.
063     * @param factoryName The fully qualified class name of the InitialContextFactory.
064     * @param providerURL The URL of the provider to use.
065     * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
066     * will create a URL context factory
067     * @param securityPrincipalName The name of the identity of the Principal.
068     * @param securityCredentials The security credentials of the Principal.
069     * @param factoryBindingName The name to locate in the Context that provides the QueueConnectionFactory.
070     * @param queueBindingName The name to use to locate the Queue.
071     * @param userName The user ID to use to create the Queue Connection.
072     * @param password The password to use to create the Queue Connection.
073     * @param layout The layout to use (defaults to SerializedLayout).
074     * @param filter The Filter or null.
075     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
076     *               they are propagated to the caller.
077     * @return The JMSQueueAppender.
078     */
079    @PluginFactory
080    public static JMSQueueAppender createAppender(
081            @PluginAttribute("name") final String name,
082            @PluginAttribute("factoryName") final String factoryName,
083            @PluginAttribute("providerURL") final String providerURL,
084            @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes,
085            @PluginAttribute("securityPrincipalName") final String securityPrincipalName,
086            @PluginAttribute("securityCredentials") final String securityCredentials,
087            @PluginAttribute("factoryBindingName") final String factoryBindingName,
088            @PluginAttribute("queueBindingName") final String queueBindingName,
089            @PluginAttribute("userName") final String userName,
090            @PluginAttribute("password") final String password,
091            @PluginElement("Layout") Layout<? extends Serializable> layout,
092            @PluginElement("Filter") final Filter filter,
093            @PluginAttribute("ignoreExceptions") final String ignore) {
094        if (name == null) {
095            LOGGER.error("No name provided for JMSQueueAppender");
096            return null;
097        }
098        final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
099        final JMSQueueManager manager = JMSQueueManager.getJMSQueueManager(factoryName, providerURL, urlPkgPrefixes,
100            securityPrincipalName, securityCredentials, factoryBindingName, queueBindingName, userName, password);
101        if (manager == null) {
102            return null;
103        }
104        if (layout == null) {
105            layout = SerializedLayout.createLayout();
106        }
107        return new JMSQueueAppender(name, filter, layout, manager, ignoreExceptions);
108    }
109}