1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27 public class ObjectMessage implements Message {
28
29 private static final long serialVersionUID = -5903272448334166185L;
30
31 private transient Object obj;
32
33
34
35
36
37 public ObjectMessage(Object obj) {
38 if (obj == null) {
39 obj = "null";
40 }
41 this.obj = obj;
42 }
43
44
45
46
47
48 @Override
49 public String getFormattedMessage() {
50 return obj.toString();
51 }
52
53
54
55
56
57 @Override
58 public String getFormat() {
59 return obj.toString();
60 }
61
62
63
64
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
111
112
113
114 @Override
115 public Throwable getThrowable() {
116 return obj instanceof Throwable ? (Throwable) obj : null;
117 }
118 }