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