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