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.JMSTopicManager; 028 029 import java.io.Serializable; 030 031 /** 032 * Appender to write to a JMS Topic. 033 */ 034 @Plugin(name = "JMSTopic", category = "Core", elementType = "appender", printObject = true) 035 public final class JMSTopicAppender<T extends Serializable> extends AbstractAppender<T> { 036 037 private final JMSTopicManager manager; 038 039 private JMSTopicAppender(final String name, final Filter filter, final Layout<T> layout, 040 final JMSTopicManager 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 JMSTopicAppender. 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 TopicConnectionFactory. 069 * @param topicBindingName The name to use to locate the Topic. 070 * @param userName The userid to use to create the Topic Connection. 071 * @param password The password to use to create the Topic 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 JMSTopicAppender. 077 */ 078 @PluginFactory 079 public static <S extends Serializable> JMSTopicAppender<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("topicBindingName") final String topicBindingName, 088 @PluginAttr("userName") final String userName, 089 @PluginAttr("password") final String password, 090 @PluginElement("layout") Layout<S> layout, 091 @PluginElement("filters") final Filter filter, 092 @PluginAttr("suppressExceptions") final String suppress) { 093 094 if (name == null) { 095 LOGGER.error("No name provided for JMSQueueAppender"); 096 return null; 097 } 098 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 099 final JMSTopicManager manager = JMSTopicManager.getJMSTopicManager(factoryName, providerURL, urlPkgPrefixes, 100 securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password); 101 if (manager == null) { 102 return null; 103 } 104 if (layout == null) { 105 @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) 106 Layout<S> l = (Layout<S>)SerializedLayout.createLayout(); 107 layout = l; 108 } 109 return new JMSTopicAppender<S>(name, filter, layout, manager, handleExceptions); 110 } 111 }