1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.model.processor;
15
16 import ch.qos.logback.core.Context;
17 import ch.qos.logback.core.joran.action.Action;
18 import ch.qos.logback.core.joran.action.ActionUtil;
19 import ch.qos.logback.core.joran.action.ActionUtil.Scope;
20 import ch.qos.logback.core.joran.action.TimestampAction;
21 import ch.qos.logback.core.model.Model;
22 import ch.qos.logback.core.model.TimestampModel;
23 import ch.qos.logback.core.util.CachingDateFormatter;
24 import ch.qos.logback.core.util.OptionHelper;
25
26 public class TimestampModelHandler extends ModelHandlerBase {
27
28 boolean inError = false;
29
30 public TimestampModelHandler(Context context) {
31 super(context);
32 }
33
34 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
35 return new TimestampModelHandler(context);
36 }
37
38 @Override
39 protected Class<TimestampModel> getSupportedModelClass() {
40 return TimestampModel.class;
41 }
42
43 @Override
44 public void handle(ModelInterpretationContext interpretationContext, Model model) {
45 TimestampModel timestampModel = (TimestampModel) model;
46 String keyStr = timestampModel.getKey();
47 if (OptionHelper.isNullOrEmptyOrAllSpaces(keyStr)) {
48 addError("Attribute named [" + Action.KEY_ATTRIBUTE + "] cannot be empty");
49 inError = true;
50 }
51 String datePatternStr = timestampModel.getDatePattern();
52 if (OptionHelper.isNullOrEmptyOrAllSpaces(datePatternStr)) {
53 addError("Attribute named [" + TimestampAction.DATE_PATTERN_ATTRIBUTE + "] cannot be empty");
54 inError = true;
55 }
56
57 String timeReferenceStr = timestampModel.getTimeReference();
58 long timeReference;
59 if (TimestampModel.CONTEXT_BIRTH.equalsIgnoreCase(timeReferenceStr)) {
60 addInfo("Using context birth as time reference.");
61 timeReference = context.getBirthTime();
62 } else {
63 timeReference = System.currentTimeMillis();
64 addInfo("Using current interpretation time, i.e. now, as time reference.");
65 }
66
67 if (inError)
68 return;
69
70 String scopeStr = timestampModel.getScopeStr();
71 Scope scope = ActionUtil.stringToScope(scopeStr);
72
73 CachingDateFormatter sdf = new CachingDateFormatter(datePatternStr);
74 String val = sdf.format(timeReference);
75
76 addInfo("Adding property to the context with key=\"" + keyStr + "\" and value=\"" + val + "\" to the " + scope
77 + " scope");
78 ActionUtil.setProperty(interpretationContext, keyStr, val, scope);
79
80 }
81
82 }