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