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      @Override
43      public void start() {
44          started = true;
45      }
46  
47      @Override
48      public void stop() {
49          started = false;
50      }
51  
52      @Override
53      public boolean isStarted() {
54          return started;
55      }
56  
57      public void setOptionList(List<String> optionList) {
58          this.optionList = optionList;
59      }
60  
61      /**
62       * Return the first option passed to this component. The returned value may be
63       * null if there are no options.
64       * 
65       * @return First option, may be null.
66       */
67      public String getFirstOption() {
68          if (optionList == null || optionList.size() == 0) {
69              return null;
70          } else {
71              return optionList.get(0);
72          }
73      }
74  
75      protected List<String> getOptionList() {
76          return optionList;
77      }
78  
79      @Override
80      public void setContext(Context context) {
81          cab.setContext(context);
82      }
83  
84      @Override
85      public Context getContext() {
86          return cab.getContext();
87      }
88  
89      @Override
90      public void addStatus(Status status) {
91          cab.addStatus(status);
92      }
93  
94      @Override
95      public void addInfo(String msg) {
96          cab.addInfo(msg);
97      }
98  
99      @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 }