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