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.net.jms;
18  
19  import java.io.Serializable;
20  import java.util.Properties;
21  
22  import javax.jms.JMSException;
23  import javax.jms.Message;
24  import javax.jms.MessageProducer;
25  import javax.jms.ObjectMessage;
26  import javax.jms.Session;
27  import javax.jms.TextMessage;
28  import javax.naming.Context;
29  import javax.naming.InitialContext;
30  import javax.naming.NameNotFoundException;
31  import javax.naming.NamingException;
32  
33  import org.apache.logging.log4j.core.appender.AbstractManager;
34  
35  /**
36   * Base Class for Managers of JMS connections.
37   */
38  public abstract class AbstractJmsManager extends AbstractManager {
39  
40      /**
41       * The Constructor.
42       * @param name The name of the Appender.
43       */
44      public AbstractJmsManager(final String name) {
45          super(name);
46      }
47  
48      /**
49       * Create the InitialContext.
50       * @param factoryName The fully qualified class name of the InitialContextFactory.
51       * @param providerURL The URL of the provider to use.
52       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
53       * will create a URL context factory
54       * @param securityPrincipalName The name of the identity of the Principal.
55       * @param securityCredentials The security credentials of the Principal.
56       * @return the InitialContext.
57       * @throws NamingException if a naming error occurs.
58       */
59      protected static Context createContext(final String factoryName, final String providerURL,
60                                             final String urlPkgPrefixes, final String securityPrincipalName,
61                                             final String securityCredentials)
62          throws NamingException {
63  
64          final Properties props = getEnvironment(factoryName, providerURL, urlPkgPrefixes, securityPrincipalName,
65                                            securityCredentials);
66          return new InitialContext(props);
67      }
68  
69      /**
70       * Looks up the name in the context.
71       * @param ctx The Context.
72       * @param name The name to locate.
73       * @return The object to be located.
74       * @throws NamingException If an error occurs locating the name.
75       */
76      protected static Object lookup(final Context ctx, final String name) throws NamingException {
77          try {
78              return ctx.lookup(name);
79          } catch (final NameNotFoundException e) {
80              LOGGER.warn("Could not find name [{}].", name);
81              throw e;
82          }
83      }
84  
85      /**
86       * Sets up the properties to pass to the InitialContext.
87       * @param factoryName The fully qualified class name of the InitialContextFactory.
88       * @param providerURL The URL of the provider to use.
89       * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that
90       * will create a URL context factory
91       * @param securityPrincipalName The name of the identity of the Principal.
92       * @param securityCredentials The security credentials of the Principal.
93       * @return The Properties.
94       */
95      protected static Properties getEnvironment(final String factoryName, final String providerURL,
96                                                 final String urlPkgPrefixes, final String securityPrincipalName,
97                                                 final String securityCredentials) {
98          final Properties props = new Properties();
99          if (factoryName != null) {
100             props.put(Context.INITIAL_CONTEXT_FACTORY, factoryName);
101             if (providerURL != null) {
102                 props.put(Context.PROVIDER_URL, providerURL);
103             } else {
104                 LOGGER.warn("The InitialContext factory name has been provided without a ProviderURL. " +
105                     "This is likely to cause problems");
106             }
107             if (urlPkgPrefixes != null) {
108                 props.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
109             }
110             if (securityPrincipalName != null) {
111                 props.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
112                 if (securityCredentials != null) {
113                     props.put(Context.SECURITY_CREDENTIALS, securityCredentials);
114                 } else {
115                     LOGGER.warn("SecurityPrincipalName has been set without SecurityCredentials. " +
116                         "This is likely to cause problems.");
117                 }
118             }
119             return props;
120         }
121         return null;
122     }
123 
124     /**
125      * Send the message.
126      * @param object The Object to sent.
127      * @throws Exception if an error occurs.
128      */
129     public abstract void send(Serializable object) throws Exception;
130 
131     /**
132      * Send the Object.
133      * @param object The Object to send.
134      * @param session The Session.
135      * @param producer The MessageProducer.
136      * @throws Exception if an error occurs.
137      */
138     public synchronized void send(final Serializable object, final Session session, final MessageProducer producer)
139         throws Exception {
140         try {
141             Message msg;
142             if (object instanceof String) {
143                 msg = session.createTextMessage();
144                 ((TextMessage) msg).setText((String) object);
145             } else {
146                 msg = session.createObjectMessage();
147                 ((ObjectMessage) msg).setObject(object);
148             }
149             producer.send(msg);
150         } catch (final JMSException ex) {
151             LOGGER.error("Could not publish message via JMS {}", getName());
152             throw ex;
153         }
154     }
155 }