View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2002, 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.sift;
15  
16  import java.util.stream.Stream;
17  
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.CoreConstants;
20  import ch.qos.logback.core.model.AppenderModel;
21  import ch.qos.logback.core.model.Model;
22  import ch.qos.logback.core.model.SiftModel;
23  import ch.qos.logback.core.model.processor.ModelHandlerBase;
24  import ch.qos.logback.core.model.processor.ModelHandlerException;
25  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
26  
27  public class SiftModelHandler extends ModelHandlerBase {
28      final static String ONE_AND_ONLY_ONE_URL = CoreConstants.CODES_URL + "#1andOnly1";
29      
30      public SiftModelHandler(Context context) {
31          super(context);
32      }
33  
34      static public SiftModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
35          return new SiftModelHandler(context);
36      }
37  
38      @Override
39      protected Class<SiftModel> getSupportedModelClass() {
40          return SiftModel.class;
41      }
42      
43      @SuppressWarnings("unchecked")
44      @Override
45      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
46          
47          SiftModel siftModel = (SiftModel) model;
48          // don't let the processor handle sub-models
49          siftModel.markAsSkipped();
50          
51          long appenderModelCount = computeAppenderModelCount(siftModel);
52          
53          if(appenderModelCount == 0) {
54              String errMsg = "No nested appenders found within the <sift> element in SiftingAppender.";    
55              addError(errMsg);
56              return;
57          }
58          if(appenderModelCount > 1) {
59              String errMsg = "Only and only one appender can be nested the <sift> element in SiftingAppender. See also " + ONE_AND_ONLY_ONE_URL;
60              addError(errMsg);
61              return;
62          }
63          
64          
65          Object o = mic.peekObject();
66          if (o instanceof SiftingAppenderBase) {
67              @SuppressWarnings("rawtypes")
68              SiftingAppenderBase sa = (SiftingAppenderBase) o;
69  
70              String key = sa.getDiscriminatorKey();
71              @SuppressWarnings("rawtypes")
72              AppenderFactoryUsingSiftModel afusm = new AppenderFactoryUsingSiftModel(mic, siftModel, key);
73           
74              sa.setAppenderFactory(afusm);
75              
76          } else {
77              addError("Unexpected object "+ o);
78          }
79      }
80  
81      private long computeAppenderModelCount(SiftModel siftModel) {
82          Stream<Model> stream = siftModel.getSubModels().stream();
83          long count = stream.filter((Model m) -> m instanceof AppenderModel).count();
84          return count;
85      }
86  
87  }