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 AppenderBase {
34  
35      private final JMSTopicManager manager;
36  
37      private JMSTopicAppender(String name, Filter filter, Layout layout, JMSTopicManager manager,
38                              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(LogEvent event) {
49          try {
50              manager.send(getLayout().formatAs(event));
51          } catch (Exception ex) {
52              throw new AppenderRuntimeException(ex);
53          }
54      }
55  
56      /**
57       * Create a JMSTopicAppender.
58       * @param factoryName The fully qualified class name of the InitialContextFactory.
59       * @param providerURL The URL of the provider to use.
60       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
61       * will create a URL context factory
62       * @param securityPrincipalName The name of the identity of the Principal.
63       * @param securityCredentials The security credentials of the Principal.
64       * @param factoryBindingName The name to locate in the Context that provides the TopicConnectionFactory.
65       * @param topicBindingName The name to use to locate the Topic.
66       * @param userName The userid to use to create the Topic Connection.
67       * @param password The password to use to create the Topic Connection.
68       * @param layout The layout to use (defaults to SerlializedLayout).
69       * @param filter The Filter or null.
70       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
71       * The default is "true".
72       * @return The JMSTopicAppender.
73       */
74      @PluginFactory
75      public static JMSTopicAppender createAppender(@PluginAttr("factoryName") String factoryName,
76                                                    @PluginAttr("providerURL") String providerURL,
77                                                    @PluginAttr("urlPkgPrefixes") String urlPkgPrefixes,
78                                                    @PluginAttr("securityPrincipalName") String securityPrincipalName,
79                                                    @PluginAttr("securityCredentials") String securityCredentials,
80                                                    @PluginAttr("factoryBindingName") String factoryBindingName,
81                                                    @PluginAttr("topicBindingName") String topicBindingName,
82                                                    @PluginAttr("userName") String userName,
83                                                    @PluginAttr("password") String password,
84                                                    @PluginElement("layout") Layout layout,
85                                                    @PluginElement("filters") Filter filter,
86                                                    @PluginAttr("suppressExceptions") String suppress) {
87  
88          String name = "JMSTopic" + factoryBindingName + "." + topicBindingName;
89          boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
90          JMSTopicManager manager = JMSTopicManager.getJMSTopicManager(factoryName, providerURL, urlPkgPrefixes,
91              securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password);
92          if (manager == null) {
93              return null;
94          }
95          if (layout == null) {
96              layout = SerializedLayout.createLayout();
97          }
98          return new JMSTopicAppender(name, filter, layout, manager, handleExceptions);
99      }
100 }