View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 java.util.List;
17  
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.spi.ContextAware;
20  import ch.qos.logback.core.spi.ContextAwareBase;
21  import ch.qos.logback.core.spi.LifeCycle;
22  import ch.qos.logback.core.status.Status;
23  
24  abstract public class DynamicConverter<E> extends FormattingConverter<E>
25      implements LifeCycle, ContextAware {
26  
27    ContextAwareBase cab = new ContextAwareBase(this);
28  
29    // Contains a list of option Strings.
30    private List<String> optionList;
31  
32    /**
33     * Is this component active?
34     */
35    boolean started = false;
36  
37    /**
38     * Components that depend on options passed during configuration can override
39     * this method in order to make appropriate use of those options. For simpler
40     * components, the trivial implementation found in this abstract class will be
41     * sufficient.
42     */
43    public void start() {
44      started = true;
45    }
46  
47    public void stop() {
48      started = false;
49    }
50  
51    public boolean isStarted() {
52      return started;
53    }
54  
55    public void setOptionList(List<String> optionList) {
56      this.optionList = optionList;
57    }
58  
59    /**
60     * Return the first option passed to this component. The returned value may be
61     * null if there are no options.
62     * 
63     * @return First option, may be null.
64     */
65    public String getFirstOption() {
66      if (optionList == null || optionList.size() == 0) {
67        return null;
68      } else {
69        return optionList.get(0);
70      }
71    }
72  
73    protected List<String> getOptionList() {
74      return optionList;
75    }
76  
77    public void setContext(Context context) {
78      cab.setContext(context);
79    }
80  
81    public Context getContext() {
82      return cab.getContext();
83    }
84  
85    public void addStatus(Status status) {
86      cab.addStatus(status);
87    }
88  
89    public void addInfo(String msg) {
90      cab.addInfo(msg);
91    }
92  
93    public void addInfo(String msg, Throwable ex) {
94      cab.addInfo(msg, ex);
95    }
96  
97    public void addWarn(String msg) {
98      cab.addWarn(msg);
99    }
100 
101   public void addWarn(String msg, Throwable ex) {
102     cab.addWarn(msg, ex);
103   }
104 
105   public void addError(String msg) {
106     cab.addError(msg);
107   }
108 
109   public void addError(String msg, Throwable ex) {
110     cab.addError(msg, ex);
111   }
112 }