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.JMSTopicManager; 031 032/** 033 * Appender to write to a JMS Topic. 034 */ 035@Plugin(name = "JMSTopic", category = "Core", elementType = "appender", printObject = true) 036public final class JMSTopicAppender extends AbstractAppender { 037 038 private final JMSTopicManager manager; 039 040 private JMSTopicAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, 041 final JMSTopicManager manager, final boolean ignoreExceptions) { 042 super(name, filter, layout, ignoreExceptions); 043 this.manager = manager; 044 } 045 046 /** 047 * Actual writing occurs here. 048 * <p/> 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 JMSTopicAppender. 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 TopicConnectionFactory. 070 * @param topicBindingName The name to use to locate the Topic. 071 * @param userName The userid to use to create the Topic Connection. 072 * @param password The password to use to create the Topic 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 JMSTopicAppender. 078 */ 079 @PluginFactory 080 public static JMSTopicAppender 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("topicBindingName") final String topicBindingName, 089 @PluginAttribute("userName") final String userName, 090 @PluginAttribute("password") final String password, 091 @PluginElement("Layout") Layout<? extends Serializable> layout, 092 @PluginElement("Filters") final Filter filter, 093 @PluginAttribute("ignoreExceptions") final String ignore) { 094 095 if (name == null) { 096 LOGGER.error("No name provided for JMSQueueAppender"); 097 return null; 098 } 099 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 100 final JMSTopicManager manager = JMSTopicManager.getJMSTopicManager(factoryName, providerURL, urlPkgPrefixes, 101 securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password); 102 if (manager == null) { 103 return null; 104 } 105 if (layout == null) { 106 layout = SerializedLayout.createLayout(); 107 } 108 return new JMSTopicAppender(name, filter, layout, manager, ignoreExceptions); 109 } 110}