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