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