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 org.apache.logging.log4j.core.Filter;
20  import org.apache.logging.log4j.core.Layout;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
24  import org.apache.logging.log4j.core.config.plugins.PluginElement;
25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
26  import org.apache.logging.log4j.core.layout.SerializedLayout;
27  import org.apache.logging.log4j.core.net.JMSTopicManager;
28  
29  /**
30   * Appender to write to a JMS Topic.
31   */
32  @Plugin(name = "JMSTopic", type = "Core", elementType = "appender", printObject = true)
33  public final class JMSTopicAppender extends AbstractAppender {
34  
35      private final JMSTopicManager manager;
36  
37      private JMSTopicAppender(final String name, final Filter filter, final Layout layout, final JMSTopicManager manager,
38                              final boolean handleExceptions) {
39          super(name, filter, layout, handleExceptions);
40          this.manager = manager;
41      }
42  
43      /**
44       * Actual writing occurs here.
45       * <p/>
46       * @param event The LogEvent.
47       */
48      public void append(final LogEvent event) {
49          try {
50              manager.send(getLayout().toSerializable(event));
51          } catch (final Exception ex) {
52              throw new AppenderRuntimeException(ex);
53          }
54      }
55  
56      /**
57       * Create a JMSTopicAppender.
58       * @param name The name of the Appender.
59       * @param factoryName The fully qualified class name of the InitialContextFactory.
60       * @param providerURL The URL of the provider to use.
61       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
62       * will create a URL context factory
63       * @param securityPrincipalName The name of the identity of the Principal.
64       * @param securityCredentials The security credentials of the Principal.
65       * @param factoryBindingName The name to locate in the Context that provides the TopicConnectionFactory.
66       * @param topicBindingName The name to use to locate the Topic.
67       * @param userName The userid to use to create the Topic Connection.
68       * @param password The password to use to create the Topic Connection.
69       * @param layout The layout to use (defaults to SerializedLayout).
70       * @param filter The Filter or null.
71       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
72       * The default is "true".
73       * @return The JMSTopicAppender.
74       */
75      @PluginFactory
76      public static JMSTopicAppender createAppender(
77                                                  @PluginAttr("name") final String name,
78                                                  @PluginAttr("factoryName") final String factoryName,
79                                                  @PluginAttr("providerURL") final String providerURL,
80                                                  @PluginAttr("urlPkgPrefixes") final String urlPkgPrefixes,
81                                                  @PluginAttr("securityPrincipalName") final String securityPrincipalName,
82                                                  @PluginAttr("securityCredentials") final String securityCredentials,
83                                                  @PluginAttr("factoryBindingName") final String factoryBindingName,
84                                                  @PluginAttr("topicBindingName") final String topicBindingName,
85                                                  @PluginAttr("userName") final String userName,
86                                                  @PluginAttr("password") final String password,
87                                                  @PluginElement("layout") Layout layout,
88                                                  @PluginElement("filters") final Filter filter,
89                                                  @PluginAttr("suppressExceptions") final String suppress) {
90  
91          if (name == null) {
92              LOGGER.error("No name provided for JMSQueueAppender");
93              return null;
94          }
95          final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
96          final JMSTopicManager manager = JMSTopicManager.getJMSTopicManager(factoryName, providerURL, urlPkgPrefixes,
97              securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password);
98          if (manager == null) {
99              return null;
100         }
101         if (layout == null) {
102             layout = SerializedLayout.createLayout();
103         }
104         return new JMSTopicAppender(name, filter, layout, manager, handleExceptions);
105     }
106 }