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.replay;
015
016import org.xml.sax.Attributes;
017
018import ch.qos.logback.core.joran.action.Action;
019import ch.qos.logback.core.joran.spi.ActionException;
020import ch.qos.logback.core.joran.spi.InterpretationContext;
021import ch.qos.logback.core.util.OptionHelper;
022
023/** 
024 * The Fruit* code is intended to test Joran's replay capability
025 * */
026public class FruitShellAction extends Action {
027
028    FruitShell fruitShell;
029    private boolean inError = false;
030
031    @Override
032    public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
033
034        // We are just beginning, reset variables
035        fruitShell = new FruitShell();
036        inError = false;
037
038        try {
039
040            fruitShell.setContext(context);
041
042            String shellName = attributes.getValue(NAME_ATTRIBUTE);
043
044            if (OptionHelper.isEmpty(shellName)) {
045                addWarn("No appender name given for fruitShell].");
046            } else {
047                fruitShell.setName(shellName);
048                addInfo("FruitShell named as [" + shellName + "]");
049            }
050
051            ec.pushObject(fruitShell);
052        } catch (Exception oops) {
053            inError = true;
054            addError("Could not create an FruitShell", oops);
055            throw new ActionException(oops);
056        }
057    }
058
059    @Override
060    public void end(InterpretationContext ec, String name) throws ActionException {
061        if (inError) {
062            return;
063        }
064
065        Object o = ec.peekObject();
066
067        if (o != fruitShell) {
068            addWarn("The object at the of the stack is not the fruitShell named [" + fruitShell.getName() + "] pushed earlier.");
069        } else {
070            addInfo("Popping fruitSHell named [" + fruitShell.getName() + "] from the object stack");
071            ec.popObject();
072            FruitContext fruitContext = (FruitContext) ec.getContext();
073            fruitContext.addFruitShell(fruitShell);
074        }
075    }
076
077}