001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.pattern;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.spi.ContextAware;
018
019public class ConverterUtil {
020
021    /**
022     * Start converters in the chain of converters.
023     *
024     * @param head
025     */
026    public static <E> void startConverters(Converter<E> head) {
027        Converter<E> c = head;
028        while (c != null) {
029            // CompositeConverter is a subclass of DynamicConverter
030            if (c instanceof CompositeConverter) {
031                CompositeConverter<E> cc = (CompositeConverter<E>) c;
032                Converter<E> childConverter = cc.childConverter;
033                startConverters(childConverter);
034                cc.start();
035            } else if (c instanceof DynamicConverter) {
036                DynamicConverter<E> dc = (DynamicConverter<E>) c;
037                dc.start();
038            }
039            c = c.getNext();
040        }
041    }
042
043    public static <E> Converter<E> findTail(Converter<E> head) {
044        Converter<E> p = head;
045        while (p != null) {
046            Converter<E> next = p.getNext();
047            if (next == null) {
048                break;
049            } else {
050                p = next;
051            }
052        }
053        return p;
054    }
055
056    public static <E> void setContextForConverters(Context context, Converter<E> head) {
057        Converter<E> c = head;
058        while (c != null) {
059            if (c instanceof ContextAware) {
060                ((ContextAware) c).setContext(context);
061            }
062            c = c.getNext();
063        }
064    }
065}