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.classic.joran.action;
15  
16  import org.xml.sax.Attributes;
17  
18  import ch.qos.logback.classic.net.ReceiverBase;
19  import ch.qos.logback.classic.net.SocketReceiver;
20  import ch.qos.logback.core.joran.action.Action;
21  import ch.qos.logback.core.joran.spi.ActionException;
22  import ch.qos.logback.core.joran.spi.InterpretationContext;
23  import ch.qos.logback.core.util.OptionHelper;
24  
25  /**
26   * A Joran {@link Action} for a {@link SocketReceiver} configuration.
27   *
28   * @author Carl Harris
29   */
30  public class ReceiverAction extends Action {
31  
32      private ReceiverBase receiver;
33      private boolean inError;
34  
35      @Override
36      public void begin(InterpretationContext ic, String name, Attributes attributes) throws ActionException {
37  
38          String className = attributes.getValue(CLASS_ATTRIBUTE);
39          if (OptionHelper.isEmpty(className)) {
40              addError("Missing class name for receiver. Near [" + name + "] line " + getLineNumber(ic));
41              inError = true;
42              return;
43          }
44  
45          try {
46              addInfo("About to instantiate receiver of type [" + className + "]");
47  
48              receiver = (ReceiverBase) OptionHelper.instantiateByClassName(className, ReceiverBase.class, context);
49              receiver.setContext(context);
50  
51              ic.pushObject(receiver);
52          } catch (Exception ex) {
53              inError = true;
54              addError("Could not create a receiver of type [" + className + "].", ex);
55              throw new ActionException(ex);
56          }
57      }
58  
59      @Override
60      public void end(InterpretationContext ic, String name) throws ActionException {
61  
62          if (inError)
63              return;
64  
65          ic.getContext().register(receiver);
66          receiver.start();
67  
68          Object o = ic.peekObject();
69          if (o != receiver) {
70              addWarn("The object at the of the stack is not the remote " + "pushed earlier.");
71          } else {
72              ic.popObject();
73          }
74      }
75  
76  }