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