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        StringBuilders.appendValue(buffer, obj);
063    }
064
065    /**
066     * Returns the object formatted using its toString method.
067     *
068     * @return the String representation of the object.
069     */
070    @Override
071    public String getFormat() {
072        return getFormattedMessage();
073    }
074
075    /**
076     * Returns the object parameter.
077     *
078     * @return The object.
079     * @since 2.7
080     */
081    public Object getParameter() {
082        return obj;
083    }
084
085    /**
086     * Returns the object as if it were a parameter.
087     *
088     * @return The object.
089     */
090    @Override
091    public Object[] getParameters() {
092        return new Object[] {obj};
093    }
094
095    @Override
096    public boolean equals(final Object o) {
097        if (this == o) {
098            return true;
099        }
100        if (o == null || getClass() != o.getClass()) {
101            return false;
102        }
103
104        final ObjectMessage that = (ObjectMessage) o;
105        return obj == null ? that.obj == null : equalObjectsOrStrings(obj, that.obj);
106    }
107
108    private boolean equalObjectsOrStrings(final Object left, final Object right) {
109        return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
110    }
111
112    @Override
113    public int hashCode() {
114        return obj != null ? obj.hashCode() : 0;
115    }
116
117    @Override
118    public String toString() {
119        return getFormattedMessage();
120    }
121
122    private void writeObject(final ObjectOutputStream out) throws IOException {
123        out.defaultWriteObject();
124        if (obj instanceof Serializable) {
125            out.writeObject(obj);
126        } else {
127            out.writeObject(String.valueOf(obj));
128        }
129    }
130
131    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
132        in.defaultReadObject();
133        obj = in.readObject();
134    }
135
136    /**
137     * Gets the message if it is a throwable.
138     *
139     * @return the message if it is a throwable.
140     */
141    @Override
142    public Throwable getThrowable() {
143        return obj instanceof Throwable ? (Throwable) obj : null;
144    }
145}