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