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