001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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.joran.action;
015
016import ch.qos.logback.core.util.CachingDateFormatter;
017import org.xml.sax.Attributes;
018
019import ch.qos.logback.core.joran.action.ActionUtil.Scope;
020import ch.qos.logback.core.joran.spi.ActionException;
021import ch.qos.logback.core.joran.spi.InterpretationContext;
022import ch.qos.logback.core.util.OptionHelper;
023
024/**
025 * Given a key and a date-and-time pattern, puts a property to the context, with
026 * the specified key and value equal to the current time in the format
027 * corresponding to the specified date-and-time pattern.
028 * 
029 * @author Ceki Gülcü
030 * 
031 */
032public class TimestampAction extends Action {
033    static String DATE_PATTERN_ATTRIBUTE = "datePattern";
034    static String TIME_REFERENCE_ATTRIBUTE = "timeReference";
035    static String CONTEXT_BIRTH = "contextBirth";
036
037    boolean inError = false;
038
039    @Override
040    public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
041        String keyStr = attributes.getValue(KEY_ATTRIBUTE);
042        if (OptionHelper.isEmpty(keyStr)) {
043            addError("Attribute named [" + KEY_ATTRIBUTE + "] cannot be empty");
044            inError = true;
045        }
046        String datePatternStr = attributes.getValue(DATE_PATTERN_ATTRIBUTE);
047        if (OptionHelper.isEmpty(datePatternStr)) {
048            addError("Attribute named [" + DATE_PATTERN_ATTRIBUTE + "] cannot be empty");
049            inError = true;
050        }
051
052        String timeReferenceStr = attributes.getValue(TIME_REFERENCE_ATTRIBUTE);
053        long timeReference;
054        if (CONTEXT_BIRTH.equalsIgnoreCase(timeReferenceStr)) {
055            addInfo("Using context birth as time reference.");
056            timeReference = context.getBirthTime();
057        } else {
058            timeReference = System.currentTimeMillis();
059            addInfo("Using current interpretation time, i.e. now, as time reference.");
060        }
061
062        if (inError)
063            return;
064
065        String scopeStr = attributes.getValue(SCOPE_ATTRIBUTE);
066        Scope scope = ActionUtil.stringToScope(scopeStr);
067
068        CachingDateFormatter sdf = new CachingDateFormatter(datePatternStr);
069        String val = sdf.format(timeReference);
070
071        addInfo("Adding property to the context with key=\"" + keyStr + "\" and value=\"" + val + "\" to the " + scope + " scope");
072        ActionUtil.setProperty(ec, keyStr, val, scope);
073    }
074
075    @Override
076    public void end(InterpretationContext ec, String name) throws ActionException {
077    }
078
079}