View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.io.Serializable;
20  
21  import org.apache.logging.log4j.core.Filter;
22  import org.apache.logging.log4j.core.Layout;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
26  import org.apache.logging.log4j.core.config.plugins.PluginElement;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  import org.apache.logging.log4j.core.helpers.Booleans;
29  import org.apache.logging.log4j.core.layout.SerializedLayout;
30  import org.apache.logging.log4j.core.net.JMSQueueManager;
31  
32  /**
33   * Appender to write to a JMS Queue.
34   */
35  @Plugin(name = "JMSQueue", category = "Core", elementType = "appender", printObject = true)
36  public final class JMSQueueAppender extends AbstractAppender {
37  
38      private final JMSQueueManager manager;
39  
40      private JMSQueueAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
41                               final JMSQueueManager manager, final boolean ignoreExceptions) {
42          super(name, filter, layout, ignoreExceptions);
43          this.manager = manager;
44      }
45  
46      /**
47       * Actual writing occurs here.
48       * 
49       * @param event The LogEvent.
50       */
51      @Override
52      public void append(final LogEvent event) {
53          try {
54              manager.send(getLayout().toSerializable(event));
55          } catch (final Exception ex) {
56              throw new AppenderLoggingException(ex);
57          }
58      }
59  
60      /**
61       * Create a JMSQueueAppender.
62       * @param name The name of the Appender.
63       * @param factoryName The fully qualified class name of the InitialContextFactory.
64       * @param providerURL The URL of the provider to use.
65       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
66       * will create a URL context factory
67       * @param securityPrincipalName The name of the identity of the Principal.
68       * @param securityCredentials The security credentials of the Principal.
69       * @param factoryBindingName The name to locate in the Context that provides the QueueConnectionFactory.
70       * @param queueBindingName The name to use to locate the Queue.
71       * @param userName The user ID to use to create the Queue Connection.
72       * @param password The password to use to create the Queue Connection.
73       * @param layout The layout to use (defaults to SerializedLayout).
74       * @param filter The Filter or null.
75       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
76       *               they are propagated to the caller.
77       * @return The JMSQueueAppender.
78       */
79      @PluginFactory
80      public static JMSQueueAppender createAppender(
81              @PluginAttribute("name") final String name,
82              @PluginAttribute("factoryName") final String factoryName,
83              @PluginAttribute("providerURL") final String providerURL,
84              @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes,
85              @PluginAttribute("securityPrincipalName") final String securityPrincipalName,
86              @PluginAttribute("securityCredentials") final String securityCredentials,
87              @PluginAttribute("factoryBindingName") final String factoryBindingName,
88              @PluginAttribute("queueBindingName") final String queueBindingName,
89              @PluginAttribute("userName") final String userName,
90              @PluginAttribute("password") final String password,
91              @PluginElement("Layout") Layout<? extends Serializable> layout,
92              @PluginElement("Filter") final Filter filter,
93              @PluginAttribute("ignoreExceptions") final String ignore) {
94          if (name == null) {
95              LOGGER.error("No name provided for JMSQueueAppender");
96              return null;
97          }
98          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
99          final JMSQueueManager manager = JMSQueueManager.getJMSQueueManager(factoryName, providerURL, urlPkgPrefixes,
100             securityPrincipalName, securityCredentials, factoryBindingName, queueBindingName, userName, password);
101         if (manager == null) {
102             return null;
103         }
104         if (layout == null) {
105             layout = SerializedLayout.createLayout();
106         }
107         return new JMSQueueAppender(name, filter, layout, manager, ignoreExceptions);
108     }
109 }