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  
44      public StringFormattedMessage(final String messagePattern, final Object... arguments) {
45          this.messagePattern = messagePattern;
46          this.argArray = arguments;
47      }
48  
49      /**
50       * Returns the formatted message.
51       * @return the formatted message.
52       */
53      public String getFormattedMessage() {
54          if (formattedMessage == null) {
55              formattedMessage = formatMessage(messagePattern, argArray);
56          }
57          return formattedMessage;
58      }
59  
60      /**
61       * Returns the message pattern.
62       * @return the message pattern.
63       */
64      public String getFormat() {
65          return messagePattern;
66      }
67  
68      /**
69       * Returns the message parameters.
70       * @return the message parameters.
71       */
72      public Object[] getParameters() {
73          if (argArray != null) {
74              return argArray;
75          }
76          return stringArgs;
77      }
78  
79      protected String formatMessage(final String msgPattern, final Object... args) {
80          try {
81              return String.format(msgPattern, args);
82          } catch (final IllegalFormatException ife) {
83              LOGGER.error("Unable to format msg: " + msgPattern, ife);
84              return msgPattern;
85          }
86      }
87  
88      @Override
89      public boolean equals(final Object o) {
90          if (this == o) {
91              return true;
92          }
93          if (o == null || getClass() != o.getClass()) {
94              return false;
95          }
96  
97          final StringFormattedMessage that = (StringFormattedMessage) o;
98  
99          if (messagePattern != null ? !messagePattern.equals(that.messagePattern) : that.messagePattern != null) {
100             return false;
101         }
102         if (!Arrays.equals(stringArgs, that.stringArgs)) {
103             return false;
104         }
105 
106         return true;
107     }
108 
109     @Override
110     public int hashCode() {
111         int result = messagePattern != null ? messagePattern.hashCode() : 0;
112         result = HASHVAL * result + (stringArgs != null ? Arrays.hashCode(stringArgs) : 0);
113         return result;
114     }
115 
116 
117     @Override
118     public String toString() {
119         return "StringFormatMessage[messagePattern=" + messagePattern + ", args=" +
120             Arrays.toString(argArray) +  "]";
121     }
122 
123     private void writeObject(final ObjectOutputStream out) throws IOException {
124         out.defaultWriteObject();
125         getFormattedMessage();
126         out.writeUTF(formattedMessage);
127         out.writeUTF(messagePattern);
128         out.writeInt(argArray.length);
129         stringArgs = new String[argArray.length];
130         int i = 0;
131         for (final Object obj : argArray) {
132             stringArgs[i] = obj.toString();
133             ++i;
134         }
135     }
136 
137     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
138         in.defaultReadObject();
139         formattedMessage = in.readUTF();
140         messagePattern = in.readUTF();
141         final int length = in.readInt();
142         stringArgs = new String[length];
143         for (int i = 0; i < length; ++i) {
144             stringArgs[i] = in.readUTF();
145         }
146     }
147 
148     /**
149      * Always returns null.
150      *
151      * @return null
152      */
153     public Throwable getThrowable() {
154         return null;
155     }
156 }