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.JMSTopicManager;
31  
32  /**
33   * Appender to write to a JMS Topic.
34   */
35  @Plugin(name = "JMSTopic", category = "Core", elementType = "appender", printObject = true)
36  public final class JMSTopicAppender extends AbstractAppender {
37  
38      private final JMSTopicManager manager;
39  
40      private JMSTopicAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
41                               final JMSTopicManager manager, final boolean ignoreExceptions) {
42          super(name, filter, layout, ignoreExceptions);
43          this.manager = manager;
44      }
45  
46      /**
47       * Actual writing occurs here.
48       * <p/>
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 JMSTopicAppender.
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 TopicConnectionFactory.
70       * @param topicBindingName The name to use to locate the Topic.
71       * @param userName The userid to use to create the Topic Connection.
72       * @param password The password to use to create the Topic 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 JMSTopicAppender.
78       */
79      @PluginFactory
80      public static JMSTopicAppender 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("topicBindingName") final String topicBindingName,
89              @PluginAttribute("userName") final String userName,
90              @PluginAttribute("password") final String password,
91              @PluginElement("Layout") Layout<? extends Serializable> layout,
92              @PluginElement("Filters") final Filter filter,
93              @PluginAttribute("ignoreExceptions") final String ignore) {
94  
95          if (name == null) {
96              LOGGER.error("No name provided for JMSQueueAppender");
97              return null;
98          }
99          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 }