1
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
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 }
77