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.log4j.or.jms;
18  
19  import org.apache.log4j.or.ObjectRenderer;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.status.StatusLogger;
22  
23  import javax.jms.Message;
24  import javax.jms.JMSException;
25  import javax.jms.DeliveryMode;
26  
27  /**
28   * Log4j 1.x JMS Message Renderer
29   */
30  public class MessageRenderer implements ObjectRenderer {
31      private static final Logger LOGGER = StatusLogger.getLogger();
32  
33      /**
34       Render a {@link javax.jms.Message}.
35       */
36      @Override
37      public
38      String  doRender(Object obj) {
39          if (obj instanceof Message) {
40              StringBuilder sb = new StringBuilder();
41              Message message = (Message) obj;
42              try {
43                  sb.append("DeliveryMode=");
44                  switch(message.getJMSDeliveryMode()) {
45                      case DeliveryMode.NON_PERSISTENT :
46                          sb.append("NON_PERSISTENT");
47                          break;
48                      case DeliveryMode.PERSISTENT :
49                          sb.append("PERSISTENT");
50                          break;
51                      default: sb.append("UNKNOWN");
52                  }
53                  sb.append(", CorrelationID=");
54                  sb.append(message.getJMSCorrelationID());
55  
56                  sb.append(", Destination=");
57                  sb.append(message.getJMSDestination());
58  
59                  sb.append(", Expiration=");
60                  sb.append(message.getJMSExpiration());
61  
62                  sb.append(", MessageID=");
63                  sb.append(message.getJMSMessageID());
64  
65                  sb.append(", Priority=");
66                  sb.append(message.getJMSPriority());
67  
68                  sb.append(", Redelivered=");
69                  sb.append(message.getJMSRedelivered());
70  
71                  sb.append(", ReplyTo=");
72                  sb.append(message.getJMSReplyTo());
73  
74                  sb.append(", Timestamp=");
75                  sb.append(message.getJMSTimestamp());
76  
77                  sb.append(", Type=");
78                  sb.append(message.getJMSType());
79  
80              } catch(JMSException e) {
81                  LOGGER.error("Could not parse Message.", e);
82              }
83              return sb.toString();
84          }
85          return obj.toString();
86      }
87  }