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 java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  
24  /**
25   * Handles messages that contain an Object.
26   */
27  public class ObjectMessage implements Message {
28  
29      private static final long serialVersionUID = -5903272448334166185L;
30  
31      private transient Object obj;
32      private transient String objectString;
33  
34      /**
35       * Create the ObjectMessage.
36       * @param obj The Object to format.
37       */
38      public ObjectMessage(final Object obj) {
39          this.obj = obj == null ? "null" : obj;
40      }
41  
42      /**
43       * Returns the formatted object message.
44       * @return the formatted object message.
45       */
46      @Override
47      public String getFormattedMessage() {
48          // LOG4J2-763: cache formatted string in case obj changes later
49          if (objectString == null) {
50              objectString = String.valueOf(obj);
51          }
52          return objectString;
53      }
54  
55      /**
56       * Returns the object formatted using its toString method.
57       * @return the String representation of the object.
58       */
59      @Override
60      public String getFormat() {
61          return getFormattedMessage();
62      }
63  
64      /**
65       * Returns the object as if it were a parameter.
66       * @return The object.
67       */
68      @Override
69      public Object[] getParameters() {
70          return new Object[] { obj };
71      }
72  
73      @Override
74      public boolean equals(final Object o) {
75          if (this == o) {
76              return true;
77          }
78          if (o == null || getClass() != o.getClass()) {
79              return false;
80          }
81  
82          final ObjectMessage that = (ObjectMessage) o;
83          return obj == null ? that.obj == null : equalObjectsOrStrings(obj, that.obj);
84      }
85      
86      private boolean equalObjectsOrStrings(final Object left, final Object right) {
87          return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
88      }
89  
90      @Override
91      public int hashCode() {
92          return obj != null ? obj.hashCode() : 0;
93      }
94  
95      @Override
96      public String toString() {
97          return "ObjectMessage[obj=" + getFormattedMessage() + ']';
98      }
99  
100     private void writeObject(final ObjectOutputStream out) throws IOException {
101         out.defaultWriteObject();
102         if (obj instanceof Serializable) {
103             out.writeObject(obj);
104         } else {
105             out.writeObject(String.valueOf(obj));
106         }
107     }
108 
109     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
110         in.defaultReadObject();
111         obj = in.readObject();
112     }
113 
114     /**
115      * Gets the message if it is a throwable.
116      *
117      * @return the message if it is a throwable.
118      */
119     @Override
120     public Throwable getThrowable() {
121         return obj instanceof Throwable ? (Throwable) obj : null;
122     }
123 }