View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.pattern;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.spi.ContextAware;
18  
19  public class ConverterUtil {
20  
21      /**
22       * Start converters in the chain of converters.
23       *
24       * @param head
25       */
26      public static <E> void startConverters(Converter<E> head) {
27          Converter<E> c = head;
28          while (c != null) {
29              // CompositeConverter is a subclass of DynamicConverter
30              if (c instanceof CompositeConverter) {
31                  CompositeConverter<E> cc = (CompositeConverter<E>) c;
32                  Converter<E> childConverter = cc.childConverter;
33                  startConverters(childConverter);
34                  cc.start();
35              } else if (c instanceof DynamicConverter) {
36                  DynamicConverter<E> dc = (DynamicConverter<E>) c;
37                  dc.start();
38              }
39              c = c.getNext();
40          }
41      }
42  
43      public static <E> Converter<E> findTail(Converter<E> head) {
44          Converter<E> p = head;
45          while (p != null) {
46              Converter<E> next = p.getNext();
47              if (next == null) {
48                  break;
49              } else {
50                  p = next;
51              }
52          }
53          return p;
54      }
55  
56      public static <E> void setContextForConverters(Context context, Converter<E> head) {
57          Converter<E> c = head;
58          while (c != null) {
59              if (c instanceof ContextAware) {
60                  ((ContextAware) c).setContext(context);
61              }
62              if (c instanceof CompositeConverter) {
63                  CompositeConverter<E> cc = (CompositeConverter<E>) c;
64                  Converter<E> childConverter = cc.childConverter;
65                  setContextForConverters(context, childConverter);
66              }
67              c = c.getNext();
68          }
69      }
70  }