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.appender.db.jpa.converter;
18  
19  import javax.persistence.AttributeConverter;
20  
21  /**
22   * A JPA 2.1 attribute converter for {@link StackTraceElement}s in {@link org.apache.logging.log4j.core.LogEvent}s. This
23   * converter is capable of converting both to and from {@link String}s.
24   */
25  public class StackTraceElementAttributeConverter implements AttributeConverter<StackTraceElement, String> {
26      private static final int UNKNOWN_SOURCE = -1;
27  
28      private static final int NATIVE_METHOD = -2;
29  
30      @Override
31      public String convertToDatabaseColumn(final StackTraceElement element) {
32          return element.toString();
33      }
34  
35      @Override
36      public StackTraceElement convertToEntityAttribute(final String s) {
37          return StackTraceElementAttributeConverter.convertString(s);
38      }
39  
40      static StackTraceElement convertString(final String s) {
41          int open = s.indexOf("(");
42  
43          String classMethod = s.substring(0, open);
44          String className = classMethod.substring(0, classMethod.lastIndexOf("."));
45          String methodName = classMethod.substring(classMethod.lastIndexOf(".") + 1);
46  
47          String parenthesisContents = s.substring(open + 1, s.indexOf(")"));
48  
49          String fileName = null;
50          int lineNumber = UNKNOWN_SOURCE;
51          if ("Native Method".equals(parenthesisContents)) {
52              lineNumber = NATIVE_METHOD;
53          } else if (!"Unknown Source".equals(parenthesisContents)) {
54              int colon = parenthesisContents.indexOf(":");
55              if (colon > UNKNOWN_SOURCE) {
56                  fileName = parenthesisContents.substring(0, colon);
57                  try {
58                      lineNumber = Integer.parseInt(parenthesisContents.substring(colon + 1));
59                  } catch (NumberFormatException ignore) {
60                      // we don't care
61                  }
62              } else {
63                  fileName = parenthesisContents.substring(0);
64              }
65          }
66  
67          return new StackTraceElement(className, methodName, fileName, lineNumber);
68      }
69  }