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