001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2012 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020package org.shredzone.commons.taglib.proxy;
021
022import javax.annotation.Nonnull;
023import javax.servlet.jsp.JspException;
024import javax.servlet.jsp.PageContext;
025import javax.servlet.jsp.tagext.Tag;
026import javax.servlet.jsp.tagext.TryCatchFinally;
027
028/**
029 * A proxy that implements {@link Tag} and allows the target implementation to use
030 * dependency injection.
031 *
032 * @param <T>
033 *            Type of the {@link Tag} this proxy delegates to
034 * @author Richard "Shred" Körber
035 */
036public abstract class TagProxy<T extends Tag> extends AbstractTagProxy<T> implements Tag {
037
038    @Override
039    public void setPageContext(PageContext pageContext) {
040        initTargetBean(pageContext);
041        getTargetBean().setPageContext(pageContext);
042    }
043
044    @Override
045    public int doEndTag() throws JspException {
046        return getTargetBean().doEndTag();
047    }
048
049    @Override
050    public int doStartTag() throws JspException {
051        return getTargetBean().doStartTag();
052    }
053
054    @Override
055    public Tag getParent() {
056        return getTargetBean().getParent();
057    }
058
059    @Override
060    public void release() {
061        getTargetBean().release();
062    }
063
064    @Override
065    public void setParent(Tag t) {
066        getTargetBean().setParent(t);
067    }
068
069    /**
070     * Handles {@link TryCatchFinally} implementations.
071     *
072     * @param t
073     *            Caught exception
074     */
075    public void doCatch(@Nonnull Throwable t)
076    throws Throwable {
077        T target = getTargetBean();
078        if (target instanceof TryCatchFinally) {
079            TryCatchFinally tcf = (TryCatchFinally) target;
080            tcf.doCatch(t);
081        } else {
082            throw t;
083        }
084    }
085
086    /**
087     * Handles {@link TryCatchFinally} implementations.
088     */
089    public void doFinally() {
090        T target = getTargetBean();
091        if (target instanceof TryCatchFinally) {
092            TryCatchFinally tcf = (TryCatchFinally) target;
093            tcf.doFinally();
094        }
095    }
096
097}