001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.model.processor;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.joran.action.Action;
018import ch.qos.logback.core.joran.action.ActionUtil;
019import ch.qos.logback.core.joran.action.ActionUtil.Scope;
020import ch.qos.logback.core.joran.action.TimestampAction;
021import ch.qos.logback.core.model.Model;
022import ch.qos.logback.core.model.TimestampModel;
023import ch.qos.logback.core.util.CachingDateFormatter;
024import ch.qos.logback.core.util.OptionHelper;
025
026public class TimestampModelHandler extends ModelHandlerBase {
027
028    boolean inError = false;
029
030    public TimestampModelHandler(Context context) {
031        super(context);
032    }
033
034    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
035        return new TimestampModelHandler(context);
036    }
037
038    @Override
039    protected Class<TimestampModel> getSupportedModelClass() {
040        return TimestampModel.class;
041    }
042
043    @Override
044    public void handle(ModelInterpretationContext interpretationContext, Model model) {
045        TimestampModel timestampModel = (TimestampModel) model;
046        String keyStr = timestampModel.getKey();
047        if (OptionHelper.isNullOrEmptyOrAllSpaces(keyStr)) {
048            addError("Attribute named [" + Action.KEY_ATTRIBUTE + "] cannot be empty");
049            inError = true;
050        }
051        String datePatternStr = timestampModel.getDatePattern();
052        if (OptionHelper.isNullOrEmptyOrAllSpaces(datePatternStr)) {
053            addError("Attribute named [" + TimestampAction.DATE_PATTERN_ATTRIBUTE + "] cannot be empty");
054            inError = true;
055        }
056
057        String timeReferenceStr = timestampModel.getTimeReference();
058        long timeReference;
059        if (TimestampModel.CONTEXT_BIRTH.equalsIgnoreCase(timeReferenceStr)) {
060            addInfo("Using context birth as time reference.");
061            timeReference = context.getBirthTime();
062        } else {
063            timeReference = System.currentTimeMillis();
064            addInfo("Using current interpretation time, i.e. now, as time reference.");
065        }
066
067        if (inError)
068            return;
069
070        String scopeStr = timestampModel.getScopeStr();
071        Scope scope = ActionUtil.stringToScope(scopeStr);
072
073        CachingDateFormatter sdf = new CachingDateFormatter(datePatternStr);
074        String val = sdf.format(timeReference);
075
076        addInfo("Adding property to the context with key=\"" + keyStr + "\" and value=\"" + val + "\" to the " + scope
077                + " scope");
078        ActionUtil.setProperty(interpretationContext, keyStr, val, scope);
079
080    }
081
082}