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;
025
026/**
027 * Handles messages that contain an Object.
028 */
029public class ObjectMessage implements Message, StringBuilderFormattable {
030
031    private static final long serialVersionUID = -5903272448334166185L;
032
033    private transient Object obj;
034    private transient String objectString;
035
036    /**
037     * Creates the ObjectMessage.
038     *
039     * @param obj The Object to format.
040     */
041    public ObjectMessage(final Object obj) {
042        this.obj = obj == null ? "null" : obj;
043    }
044
045    /**
046     * Returns the formatted object message.
047     *
048     * @return the formatted object message.
049     */
050    @Override
051    public String getFormattedMessage() {
052        // LOG4J2-763: cache formatted string in case obj changes later
053        if (objectString == null) {
054            objectString = String.valueOf(obj);
055        }
056        return objectString;
057    }
058
059    @Override
060    public void formatTo(final StringBuilder buffer) {
061        if (obj == null || obj instanceof String) {
062            buffer.append((String) obj);
063        } else if (obj instanceof StringBuilderFormattable) {
064            ((StringBuilderFormattable) obj).formatTo(buffer);
065        } else if (obj instanceof CharSequence) {
066            buffer.append((CharSequence) obj);
067        } else {
068            buffer.append(obj);
069        }
070    }
071
072    /**
073     * Returns the object formatted using its toString method.
074     *
075     * @return the String representation of the object.
076     */
077    @Override
078    public String getFormat() {
079        return getFormattedMessage();
080    }
081
082    /**
083     * Returns the object as if it were a parameter.
084     *
085     * @return The object.
086     */
087    @Override
088    public Object[] getParameters() {
089        return new Object[] {obj};
090    }
091
092    @Override
093    public boolean equals(final Object o) {
094        if (this == o) {
095            return true;
096        }
097        if (o == null || getClass() != o.getClass()) {
098            return false;
099        }
100
101        final ObjectMessage that = (ObjectMessage) o;
102        return obj == null ? that.obj == null : equalObjectsOrStrings(obj, that.obj);
103    }
104
105    private boolean equalObjectsOrStrings(final Object left, final Object right) {
106        return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
107    }
108
109    @Override
110    public int hashCode() {
111        return obj != null ? obj.hashCode() : 0;
112    }
113
114    @Override
115    public String toString() {
116        return getFormattedMessage();
117    }
118
119    private void writeObject(final ObjectOutputStream out) throws IOException {
120        out.defaultWriteObject();
121        if (obj instanceof Serializable) {
122            out.writeObject(obj);
123        } else {
124            out.writeObject(String.valueOf(obj));
125        }
126    }
127
128    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
129        in.defaultReadObject();
130        obj = in.readObject();
131    }
132
133    /**
134     * Gets the message if it is a throwable.
135     *
136     * @return the message if it is a throwable.
137     */
138    @Override
139    public Throwable getThrowable() {
140        return obj instanceof Throwable ? (Throwable) obj : null;
141    }
142}