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 java.util.List;
017
018import ch.qos.logback.core.Context;
019import ch.qos.logback.core.spi.ContextAware;
020import ch.qos.logback.core.spi.ContextAwareBase;
021import ch.qos.logback.core.spi.LifeCycle;
022import ch.qos.logback.core.status.Status;
023
024/**
025 * As the name suggests, a DynamicConverter performs a conversion based on the parameter E
026 * given to the {@link #convert(E)} method. Almost all converters are derived from the
027 * DynamicConverter class.
028 *
029 * @param <E>
030 */
031abstract public class DynamicConverter<E> extends FormattingConverter<E> implements LifeCycle, ContextAware {
032
033    ContextAwareBase cab = new ContextAwareBase(this);
034
035    // Contains a list of option Strings.
036    private List<String> optionList;
037
038    /**
039     * Is this component active?
040     */
041    protected boolean started = false;
042
043    /**
044     * Components that depend on options passed during configuration can override
045     * this method in order to make appropriate use of those options. For simpler
046     * components, the trivial implementation found in this abstract class will be
047     * sufficient.
048     */
049    @Override
050    public void start() {
051        started = true;
052    }
053
054    @Override
055    public void stop() {
056        started = false;
057    }
058
059    @Override
060    public boolean isStarted() {
061        return started;
062    }
063
064    public void setOptionList(List<String> optionList) {
065        this.optionList = optionList;
066    }
067
068    /**
069     * Return the first option passed to this component. The returned value may be
070     * null if there are no options.
071     * 
072     * @return First option, may be null.
073     */
074    public String getFirstOption() {
075        if (optionList == null || optionList.size() == 0) {
076            return null;
077        } else {
078            return optionList.get(0);
079        }
080    }
081
082    protected List<String> getOptionList() {
083        return optionList;
084    }
085
086    @Override
087    public void setContext(Context context) {
088        cab.setContext(context);
089    }
090
091    @Override
092    public Context getContext() {
093        return cab.getContext();
094    }
095
096    @Override
097    public void addStatus(Status status) {
098        cab.addStatus(status);
099    }
100
101    @Override
102    public void addInfo(String msg) {
103        cab.addInfo(msg);
104    }
105
106    @Override
107    public void addInfo(String msg, Throwable ex) {
108        cab.addInfo(msg, ex);
109    }
110
111    @Override
112    public void addWarn(String msg) {
113        cab.addWarn(msg);
114    }
115
116    @Override
117    public void addWarn(String msg, Throwable ex) {
118        cab.addWarn(msg, ex);
119    }
120
121    @Override
122    public void addError(String msg) {
123        cab.addError(msg);
124    }
125
126    @Override
127    public void addError(String msg, Throwable ex) {
128        cab.addError(msg, ex);
129    }
130}