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.message;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.status.StatusLogger;
21  
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.util.Arrays;
26  import java.util.IllegalFormatException;
27  
28  /**
29   * Handles messages that consist of a format string conforming to java.util.Formatter.
30   */
31  public class StringFormattedMessage implements Message {
32  
33      private static final Logger LOGGER = StatusLogger.getLogger();
34  
35      private static final long serialVersionUID = -665975803997290697L;
36  
37      private static final int HASHVAL = 31;
38  
39      private String messagePattern;
40      private transient Object[] argArray;
41      private String[] stringArgs;
42      private transient String formattedMessage;
43      private transient Throwable throwable;
44  
45      public StringFormattedMessage(final String messagePattern, final Object... arguments) {
46          this.messagePattern = messagePattern;
47          this.argArray = arguments;
48          if (arguments != null && arguments.length > 0 && arguments[arguments.length - 1] instanceof Throwable) {
49              this.throwable = (Throwable) arguments[arguments.length - 1];
50          }
51      }
52  
53      /**
54       * Returns the formatted message.
55       * @return the formatted message.
56       */
57      @Override
58      public String getFormattedMessage() {
59          if (formattedMessage == null) {
60              formattedMessage = formatMessage(messagePattern, argArray);
61          }
62          return formattedMessage;
63      }
64  
65      /**
66       * Returns the message pattern.
67       * @return the message pattern.
68       */
69      @Override
70      public String getFormat() {
71          return messagePattern;
72      }
73  
74      /**
75       * Returns the message parameters.
76       * @return the message parameters.
77       */
78      @Override
79      public Object[] getParameters() {
80          if (argArray != null) {
81              return argArray;
82          }
83          return stringArgs;
84      }
85  
86      protected String formatMessage(final String msgPattern, final Object... args) {
87          try {
88              return String.format(msgPattern, args);
89          } catch (final IllegalFormatException ife) {
90              LOGGER.error("Unable to format msg: " + msgPattern, ife);
91              return msgPattern;
92          }
93      }
94  
95      @Override
96      public boolean equals(final Object o) {
97          if (this == o) {
98              return true;
99          }
100         if (o == null || getClass() != o.getClass()) {
101             return false;
102         }
103 
104         final StringFormattedMessage that = (StringFormattedMessage) o;
105 
106         if (messagePattern != null ? !messagePattern.equals(that.messagePattern) : that.messagePattern != null) {
107             return false;
108         }
109         if (!Arrays.equals(stringArgs, that.stringArgs)) {
110             return false;
111         }
112 
113         return true;
114     }
115 
116     @Override
117     public int hashCode() {
118         int result = messagePattern != null ? messagePattern.hashCode() : 0;
119         result = HASHVAL * result + (stringArgs != null ? Arrays.hashCode(stringArgs) : 0);
120         return result;
121     }
122 
123 
124     @Override
125     public String toString() {
126         return "StringFormatMessage[messagePattern=" + messagePattern + ", args=" +
127             Arrays.toString(argArray) +  "]";
128     }
129 
130     private void writeObject(final ObjectOutputStream out) throws IOException {
131         out.defaultWriteObject();
132         getFormattedMessage();
133         out.writeUTF(formattedMessage);
134         out.writeUTF(messagePattern);
135         out.writeInt(argArray.length);
136         stringArgs = new String[argArray.length];
137         int i = 0;
138         for (final Object obj : argArray) {
139             stringArgs[i] = obj.toString();
140             ++i;
141         }
142     }
143 
144     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
145         in.defaultReadObject();
146         formattedMessage = in.readUTF();
147         messagePattern = in.readUTF();
148         final int length = in.readInt();
149         stringArgs = new String[length];
150         for (int i = 0; i < length; ++i) {
151             stringArgs[i] = in.readUTF();
152         }
153     }
154 
155     /**
156      * Return the throwable passed to the Message.
157      *
158      * @return the Throwable.
159      */
160     @Override
161     public Throwable getThrowable() {
162         return throwable;
163     }
164 }