View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.message;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.util.Arrays;
23  
24  /**
25   * Handles messages that contain an Object[].
26   * <p>
27   * Created for use with the CSV layout. For example:
28   * </p>
29   * <p>
30   * {@code logger.debug(new ObjectArrayMessage(1, 2, "Bob"));}
31   * </p>
32   * 
33   * @since 2.4
34   */
35  public final class ObjectArrayMessage implements Message {
36  
37      private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
38  
39      private static final long serialVersionUID = -5903272448334166185L;
40  
41      private transient Object[] array;
42      private transient String arrayString;
43  
44      /**
45       * Creates the ObjectMessage.
46       * 
47       * @param obj
48       *            The Object to format.
49       */
50      public ObjectArrayMessage(final Object... obj) {
51          this.array = obj == null ? EMPTY_OBJECT_ARRAY : obj;
52      }
53  
54      private boolean equalObjectsOrStrings(final Object[] left, final Object[] right) {
55          return left.equals(right) || String.valueOf(left).equals(String.valueOf(right));
56      }
57  
58      @Override
59      public boolean equals(final Object o) {
60          if (this == o) {
61              return true;
62          }
63          if (o == null || getClass() != o.getClass()) {
64              return false;
65          }
66  
67          final ObjectArrayMessage that = (ObjectArrayMessage) o;
68          return array == null ? that.array == null : equalObjectsOrStrings(array, that.array);
69      }
70  
71      /**
72       * Returns the object formatted using its toString method.
73       * 
74       * @return the String representation of the object.
75       */
76      @Override
77      public String getFormat() {
78          return getFormattedMessage();
79      }
80  
81      /**
82       * Returns the formatted object message.
83       * 
84       * @return the formatted object message.
85       */
86      @Override
87      public String getFormattedMessage() {
88          // LOG4J2-763: cache formatted string in case obj changes later
89          if (arrayString == null) {
90              arrayString = Arrays.toString(array);
91          }
92          return arrayString;
93      }
94  
95      /**
96       * Returns the object as if it were a parameter.
97       * 
98       * @return The object.
99       */
100     @Override
101     public Object[] getParameters() {
102         return array;
103     }
104 
105     /**
106      * Returns null.
107      *
108      * @return null.
109      */
110     @Override
111     public Throwable getThrowable() {
112         return null;
113     }
114 
115     @Override
116     public int hashCode() {
117         return array.hashCode();
118     }
119 
120     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
121         in.defaultReadObject();
122         array = (Object[]) in.readObject();
123     }
124 
125     @Override
126     public String toString() {
127         return "ObjectArrayMessage[obj=" + getFormattedMessage() + ']';
128     }
129 
130     private void writeObject(final ObjectOutputStream out) throws IOException {
131         out.defaultWriteObject();
132         out.writeObject(array);
133     }
134 }