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