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.core.jackson;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.core.JsonParser;
22  import com.fasterxml.jackson.core.JsonProcessingException;
23  import com.fasterxml.jackson.core.JsonToken;
24  import com.fasterxml.jackson.databind.DeserializationContext;
25  import com.fasterxml.jackson.databind.JsonMappingException;
26  import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
27  
28  /**
29   * Copy and edit the Jackson (Apache License 2.0) class to use Log4j attribute names. Does not work as of Jackson 2.3.2.
30   * <p>
31   * <em>Consider this class private.</em>
32   * </p>
33   */
34  public final class Log4jStackTraceElementDeserializer extends StdScalarDeserializer<StackTraceElement> {
35      private static final long serialVersionUID = 1L;
36  
37      /**
38       * Constructs a new initialized instance.
39       */
40      public Log4jStackTraceElementDeserializer() {
41          super(StackTraceElement.class);
42      }
43  
44      @Override
45      public StackTraceElement deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException,
46              JsonProcessingException {
47          JsonToken t = jp.getCurrentToken();
48          // Must get an Object
49          if (t == JsonToken.START_OBJECT) {
50              String className = null, methodName = null, fileName = null;
51              int lineNumber = -1;
52  
53              while ((t = jp.nextValue()) != JsonToken.END_OBJECT) {
54                  final String propName = jp.getCurrentName();
55                  if ("class".equals(propName)) {
56                      className = jp.getText();
57                  } else if ("file".equals(propName)) {
58                      fileName = jp.getText();
59                  } else if ("line".equals(propName)) {
60                      if (t.isNumeric()) {
61                          lineNumber = jp.getIntValue();
62                      } else {
63                          // An XML number always comes in a string since there is no syntax help as with JSON.
64                          try {
65                              lineNumber = Integer.parseInt(jp.getText().trim());
66                          } catch (final NumberFormatException e) {
67                              throw JsonMappingException.from(jp, "Non-numeric token (" + t + ") for property 'line'", e);
68                          }
69                      }
70                  } else if ("method".equals(propName)) {
71                      methodName = jp.getText();
72                  } else if ("nativeMethod".equals(propName)) {
73                      // no setter, not passed via constructor: ignore
74                  } else {
75                      this.handleUnknownProperty(jp, ctxt, this._valueClass, propName);
76                  }
77              }
78              return new StackTraceElement(className, methodName, fileName, lineNumber);
79          }
80          throw ctxt.mappingException(this._valueClass, t);
81      }
82  }