View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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.classic.model.processor;
15  
16  import ch.qos.logback.classic.model.ReceiverModel;
17  import ch.qos.logback.classic.net.ReceiverBase;
18  import ch.qos.logback.classic.net.SocketReceiver;
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.model.Model;
21  import ch.qos.logback.core.model.processor.ModelHandlerBase;
22  import ch.qos.logback.core.model.processor.ModelHandlerException;
23  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
24  import ch.qos.logback.core.util.OptionHelper;
25  
26  /**
27   * A Joran {@link ModelHandler} for a {@link SocketReceiver} configuration.
28   *
29   * @author Carl Harris
30   */
31  public class ReceiverModelHandler extends ModelHandlerBase {
32  
33      private ReceiverBase receiver;
34      private boolean inError;
35  
36      public ReceiverModelHandler(Context context) {
37          super(context);
38      }
39  
40      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
41          return new ReceiverModelHandler(context);
42      }
43  
44      @Override
45      protected Class<ReceiverModel> getSupportedModelClass() {
46          return ReceiverModel.class;
47      }
48  
49      @Override
50      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
51          ReceiverModel receiverModel = (ReceiverModel) model;
52          String className = receiverModel.getClassName();
53  
54          if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
55              addError("Missing class name for receiver. ");
56              inError = true;
57              return;
58          } else {
59              className = mic.getImport(className);
60          }
61  
62          try {
63              addInfo("About to instantiate receiver of type [" + className + "]");
64  
65              receiver = (ReceiverBase) OptionHelper.instantiateByClassName(className, ReceiverBase.class, context);
66              receiver.setContext(context);
67  
68              mic.pushObject(receiver);
69          } catch (Exception ex) {
70              inError = true;
71              addError("Could not create a receiver of type [" + className + "].", ex);
72              throw new ModelHandlerException(ex);
73          }
74  
75      }
76  
77      @Override
78      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
79          if (inError) {
80              return;
81          }
82  
83          Object o = mic.peekObject();
84          if (o != receiver) {
85              addWarn("The object at the of the stack is not the receiver pushed earlier.");
86          } else {
87              mic.popObject();
88              addInfo("Registering receiver with context.");
89              mic.getContext().register(receiver);
90              receiver.start();
91  
92          }
93      }
94  }