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