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;
15  
16  import java.util.List;
17  
18  import ch.qos.logback.core.filter.Filter;
19  import ch.qos.logback.core.spi.ContextAwareBase;
20  import ch.qos.logback.core.spi.FilterAttachableImpl;
21  import ch.qos.logback.core.spi.FilterReply;
22  import ch.qos.logback.core.status.WarnStatus;
23  import ch.qos.logback.core.util.ReentryGuard;
24  import ch.qos.logback.core.util.ReentryGuardFactory;
25  import ch.qos.logback.core.util.SimpleTimeBasedGuard;
26  
27  /**
28   * Similar to {@link AppenderBase} except that derived appenders need to handle thread
29   * synchronization on their own.
30   * 
31   * @author Ceki Gülcü
32   * @author Ralph Goers
33   */
34  abstract public class UnsynchronizedAppenderBase<E> extends ContextAwareBase implements Appender<E> {
35  
36      protected volatile boolean started = false;
37      /**
38       * The guard prevents an appender from repeatedly calling its own doAppend
39       * method.
40       *
41       * @since 1.5.21
42       */
43      private ReentryGuard  reentryGuard;
44  
45      /**
46       * Appenders are named.
47       */
48      protected String name;
49  
50      private FilterAttachableImpl<E> fai = new FilterAttachableImpl<E>();
51  
52      public String getName() {
53          return name;
54      }
55  
56      private SimpleTimeBasedGuard notStartedGuard = new SimpleTimeBasedGuard();
57      private SimpleTimeBasedGuard exceptionGuard = new SimpleTimeBasedGuard();
58  
59  
60      public void doAppend(E eventObject) {
61          if (!this.started) {
62  
63              if (notStartedGuard.allow()) {
64                  addStatus(new WarnStatus("Attempted to append to non started appender [" + name + "].", this));
65              }
66              return;
67          }
68  
69          // prevent re-entry.
70          if (reentryGuard.isLocked()) {
71              return;
72          }
73  
74          try {
75              reentryGuard.lock();
76  
77              if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
78                  return;
79              }
80  
81              // ok, we now invoke derived class' implementation of append
82              this.append(eventObject);
83  
84          } catch (Exception e) {
85              if (exceptionGuard.allow()) {
86                  addError("Appender [" + name + "] failed to append.", e);
87              }
88          } finally {
89              reentryGuard.unlock();
90          }
91      }
92  
93      abstract protected void append(E eventObject);
94  
95      /**
96       * Set the name of this appender.
97       */
98      public void setName(String name) {
99          this.name = name;
100     }
101 
102     public void start() {
103         this.reentryGuard = buildReentryGuard();
104         started = true;
105     }
106 
107     /**
108      * Create a {@link ReentryGuard} instance used by this appender to prevent
109      * recursive/re-entrant calls to {@link #doAppend(Object)}.
110      *
111      * <p>The default implementation returns a no-op guard produced by
112      * {@link ReentryGuardFactory#makeGuard(ch.qos.logback.core.util.ReentryGuardFactory.GuardType)}
113      * using {@code GuardType.NOP}. Subclasses that require actual re-entry
114      * protection (for example using a thread-local or lock-based guard) should
115      * override this method to return an appropriate {@link ReentryGuard}
116      * implementation.</p>
117      *
118      * <p>Contract/expectations:
119      * <ul>
120      *   <li>Called from {@link #start()} to initialize the appender's guard.</li>
121      *   <li>Implementations should be lightweight and thread-safe.</li>
122      *   <li>Return value must not be {@code null}.</li>
123      * </ul>
124      * </p>
125      *
126      * @return a non-null {@link ReentryGuard} used to detect and prevent
127      *         re-entrant appends. By default, this is a no-op guard.
128      * @since 1.5.21
129      */
130     protected ReentryGuard buildReentryGuard() {
131         return ReentryGuardFactory.makeGuard(ReentryGuardFactory.GuardType.NOP);
132     }
133 
134     public void stop() {
135         started = false;
136     }
137 
138     public boolean isStarted() {
139         return started;
140     }
141 
142     public String toString() {
143         return this.getClass().getName() + "[" + name + "]";
144     }
145 
146     public void addFilter(Filter<E> newFilter) {
147         fai.addFilter(newFilter);
148     }
149 
150     public void clearAllFilters() {
151         fai.clearAllFilters();
152     }
153 
154     public List<Filter<E>> getCopyOfAttachedFiltersList() {
155         return fai.getCopyOfAttachedFiltersList();
156     }
157 
158     public FilterReply getFilterChainDecision(E event) {
159         return fai.getFilterChainDecision(event);
160     }
161 }