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 if (obj instanceof Integer) { // LOG4J2-1437 unbox auto-boxed primitives to avoid calling toString()
068            buffer.append(((Integer) obj).intValue());
069        } else if (obj instanceof Long) {
070            buffer.append(((Long) obj).longValue());
071        } else if (obj instanceof Double) {
072            buffer.append(((Double) obj).doubleValue());
073        } else if (obj instanceof Boolean) {
074            buffer.append(((Boolean) obj).booleanValue());
075        } else if (obj instanceof Character) {
076            buffer.append(((Character) obj).charValue());
077        } else if (obj instanceof Short) {
078            buffer.append(((Short) obj).shortValue());
079        } else if (obj instanceof Float) {
080            buffer.append(((Float) obj).floatValue());
081        } else {
082            buffer.append(obj);
083        }
084    }
085
086    /**
087     * Returns the object formatted using its toString method.
088     *
089     * @return the String representation of the object.
090     */
091    @Override
092    public String getFormat() {
093        return getFormattedMessage();
094    }
095
096    /**
097     * Returns the object as if it were a parameter.
098     *
099     * @return The object.
100     */
101    @Override
102    public Object[] getParameters() {
103        return new Object[] {obj};
104    }
105
106    @Override
107    public boolean equals(final Object o) {
108        if (this == o) {
109            return true;
110        }
111        if (o == null || getClass() != o.getClass()) {
112            return false;
113        }
114
115        final ObjectMessage that = (ObjectMessage) o;
116        return obj == null ? that.obj == null : equalObjectsOrStrings(obj, that.obj);
117    }
118
119    private boolean equalObjectsOrStrings(final Object left, final Object right) {
120        return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
121    }
122
123    @Override
124    public int hashCode() {
125        return obj != null ? obj.hashCode() : 0;
126    }
127
128    @Override
129    public String toString() {
130        return getFormattedMessage();
131    }
132
133    private void writeObject(final ObjectOutputStream out) throws IOException {
134        out.defaultWriteObject();
135        if (obj instanceof Serializable) {
136            out.writeObject(obj);
137        } else {
138            out.writeObject(String.valueOf(obj));
139        }
140    }
141
142    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
143        in.defaultReadObject();
144        obj = in.readObject();
145    }
146
147    /**
148     * Gets the message if it is a throwable.
149     *
150     * @return the message if it is a throwable.
151     */
152    @Override
153    public Throwable getThrowable() {
154        return obj instanceof Throwable ? (Throwable) obj : null;
155    }
156}