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.jms;
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.appender.AbstractAppender;
25  import org.apache.logging.log4j.core.appender.AppenderLoggingException;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28  import org.apache.logging.log4j.core.config.plugins.PluginElement;
29  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
30  import org.apache.logging.log4j.core.layout.SerializedLayout;
31  import org.apache.logging.log4j.core.net.jms.JmsTopicManager;
32  import org.apache.logging.log4j.core.util.Booleans;
33  
34  /**
35   * Appender to write to a JMS Topic.
36   */
37  @Plugin(name = "JMSTopic", category = "Core", elementType = "appender", printObject = true)
38  public final class JmsTopicAppender extends AbstractAppender {
39  
40      private final JmsTopicManager manager;
41  
42      private JmsTopicAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
43                               final JmsTopicManager manager, final boolean ignoreExceptions) {
44          super(name, filter, layout, ignoreExceptions);
45          this.manager = manager;
46      }
47  
48      /**
49       * Actual writing occurs here.
50       * <p/>
51       * @param event The LogEvent.
52       */
53      @Override
54      public void append(final LogEvent event) {
55          try {
56              manager.send(getLayout().toSerializable(event));
57          } catch (final Exception ex) {
58              throw new AppenderLoggingException(ex);
59          }
60      }
61  
62      /**
63       * Create a JmsTopicAppender.
64       * @param name The name of the Appender.
65       * @param factoryName The fully qualified class name of the InitialContextFactory.
66       * @param providerURL The URL of the provider to use.
67       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
68       * will create a URL context factory
69       * @param securityPrincipalName The name of the identity of the Principal.
70       * @param securityCredentials The security credentials of the Principal.
71       * @param factoryBindingName The name to locate in the Context that provides the TopicConnectionFactory.
72       * @param topicBindingName The name to use to locate the Topic.
73       * @param userName The userid to use to create the Topic Connection.
74       * @param password The password to use to create the Topic Connection.
75       * @param layout The layout to use (defaults to SerializedLayout).
76       * @param filter The Filter or null.
77       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
78       *               they are propagated to the caller.
79       * @return The JmsTopicAppender.
80       */
81      @PluginFactory
82      public static JmsTopicAppender createAppender(
83              @PluginAttribute("name") final String name,
84              @PluginAttribute("factoryName") final String factoryName,
85              @PluginAttribute("providerURL") final String providerURL,
86              @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes,
87              @PluginAttribute("securityPrincipalName") final String securityPrincipalName,
88              @PluginAttribute("securityCredentials") final String securityCredentials,
89              @PluginAttribute("factoryBindingName") final String factoryBindingName,
90              @PluginAttribute("topicBindingName") final String topicBindingName,
91              @PluginAttribute("userName") final String userName,
92              @PluginAttribute("password") final String password,
93              @PluginElement("Layout") Layout<? extends Serializable> layout,
94              @PluginElement("Filter") final Filter filter,
95              @PluginAttribute("ignoreExceptions") final String ignore) {
96  
97          if (name == null) {
98              LOGGER.error("No name provided for JmsQueueAppender");
99              return null;
100         }
101         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
102         final JmsTopicManager manager = JmsTopicManager.getJmsTopicManager(factoryName, providerURL, urlPkgPrefixes,
103             securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password);
104         if (manager == null) {
105             return null;
106         }
107         if (layout == null) {
108             layout = SerializedLayout.createLayout();
109         }
110         return new JmsTopicAppender(name, filter, layout, manager, ignoreExceptions);
111     }
112 }