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.log4j.helpers;
18  
19  import org.apache.log4j.spi.LoggingEvent;
20  import org.apache.log4j.pattern.LogEvent;
21  
22  import java.lang.reflect.Method;
23  import java.lang.reflect.InvocationTargetException;
24  import java.util.Set;
25  import java.io.ByteArrayOutputStream;
26  import java.io.ObjectOutputStream;
27  import java.io.ByteArrayInputStream;
28  import java.io.ObjectInputStream;
29  
30  
31  public final class MDCKeySetExtractor {
32      private final Method getKeySetMethod;
33      public static final MDCKeySetExtractor INSTANCE =
34              new MDCKeySetExtractor();
35  
36  
37      private MDCKeySetExtractor() {
38          //
39          //  log4j 1.2.15 and later will have method to get names
40          //     of all keys in MDC
41          //
42        Method getMethod = null;
43  
44          try {
45             getMethod = LoggingEvent.class.getMethod(
46                        "getPropertyKeySet", null);
47          } catch(Exception ex) {
48              getMethod = null;
49          }
50        getKeySetMethod = getMethod;
51  
52      }
53  
54      public Set getPropertyKeySet(final LoggingEvent event) throws Exception {
55          //
56          //  MDC keys are not visible prior to log4j 1.2.15
57          //
58          Set keySet = null;
59          if (getKeySetMethod != null) {
60                keySet = (Set) getKeySetMethod.invoke(event, null);
61          } else {
62              //
63              //  for 1.2.14 and earlier could serialize and
64              //    extract MDC content
65                ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
66                ObjectOutputStream os = new ObjectOutputStream(outBytes);
67                os.writeObject(event);
68                os.close();
69  
70                byte[] raw = outBytes.toByteArray();
71                //
72                //   bytes 6 and 7 should be the length of the original classname
73                //     should be the same as our substitute class name
74                final String subClassName = LogEvent.class.getName();
75                if (raw[6] == 0 || raw[7] == subClassName.length()) {
76                    //
77                    //  manipulate stream to use our class name
78                    //
79                    for (int i = 0; i < subClassName.length(); i++) {
80                        raw[8 + i] = (byte) subClassName.charAt(i);
81                    }
82                    ByteArrayInputStream inBytes = new ByteArrayInputStream(raw);
83                    ObjectInputStream is = new ObjectInputStream(inBytes);
84                    Object cracked = is.readObject();
85                    if (cracked instanceof LogEvent) {
86                        keySet = ((LogEvent) cracked).getPropertyKeySet();
87                    }
88                    is.close();
89                }
90          }
91          return keySet;        
92      }
93  }