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