001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.message;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023
024import org.apache.logging.log4j.util.StringBuilderFormattable;
025import org.apache.logging.log4j.util.StringBuilders;
026
027/**
028 * Handles messages that contain an Object.
029 */
030public class ObjectMessage implements Message, StringBuilderFormattable {
031
032    private static final long serialVersionUID = -5903272448334166185L;
033
034    private transient Object obj;
035    private transient String objectString;
036
037    /**
038     * Creates the ObjectMessage.
039     *
040     * @param obj The Object to format.
041     */
042    public ObjectMessage(final Object obj) {
043        this.obj = obj == null ? "null" : obj;
044    }
045
046    /**
047     * Returns the formatted object message.
048     *
049     * @return the formatted object message.
050     */
051    @Override
052    public String getFormattedMessage() {
053        // LOG4J2-763: cache formatted string in case obj changes later
054        if (objectString == null) {
055            objectString = String.valueOf(obj);
056        }
057        return objectString;
058    }
059
060    @Override
061    public void formatTo(final StringBuilder buffer) {
062        if (objectString != null) { //
063            buffer.append(objectString);
064        } else {
065            StringBuilders.appendValue(buffer, obj);
066        }
067    }
068
069    /**
070     * Returns the object formatted using its toString method.
071     *
072     * @return the String representation of the object.
073     */
074    @Override
075    public String getFormat() {
076        return getFormattedMessage();
077    }
078
079    /**
080     * Returns the object parameter.
081     *
082     * @return The object.
083     * @since 2.7
084     */
085    public Object getParameter() {
086        return obj;
087    }
088
089    /**
090     * Returns the object as if it were a parameter.
091     *
092     * @return The object.
093     */
094    @Override
095    public Object[] getParameters() {
096        return new Object[] {obj};
097    }
098
099    @Override
100    public boolean equals(final Object o) {
101        if (this == o) {
102            return true;
103        }
104        if (o == null || getClass() != o.getClass()) {
105            return false;
106        }
107
108        final ObjectMessage that = (ObjectMessage) o;
109        return obj == null ? that.obj == null : equalObjectsOrStrings(obj, that.obj);
110    }
111
112    private boolean equalObjectsOrStrings(final Object left, final Object right) {
113        return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
114    }
115
116    @Override
117    public int hashCode() {
118        return obj != null ? obj.hashCode() : 0;
119    }
120
121    @Override
122    public String toString() {
123        return getFormattedMessage();
124    }
125
126    private void writeObject(final ObjectOutputStream out) throws IOException {
127        out.defaultWriteObject();
128        if (obj instanceof Serializable) {
129            out.writeObject(obj);
130        } else {
131            out.writeObject(String.valueOf(obj));
132        }
133    }
134
135    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
136        in.defaultReadObject();
137        obj = in.readObject();
138    }
139
140    /**
141     * Gets the message if it is a throwable.
142     *
143     * @return the message if it is a throwable.
144     */
145    @Override
146    public Throwable getThrowable() {
147        return obj instanceof Throwable ? (Throwable) obj : null;
148    }
149}