001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.tags;
020
021import org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.WikiContext;
024import org.apache.wiki.api.core.Context;
025
026import javax.servlet.jsp.JspException;
027import javax.servlet.jsp.PageContext;
028import javax.servlet.jsp.tagext.BodyTagSupport;
029import javax.servlet.jsp.tagext.TryCatchFinally;
030import java.io.IOException;
031
032
033/**
034 *  This is a class that provides the same services as the WikiTagBase, but this time it
035 *   works for the BodyTagSupport base class.
036 */
037public abstract class WikiBodyTag extends BodyTagSupport implements TryCatchFinally {
038
039    private static final long serialVersionUID = -6732266865112847897L;
040    protected WikiContext m_wikiContext;
041    private static final Logger LOG = LogManager.getLogger( WikiBodyTag.class );
042
043    @Override
044    public int doStartTag() throws JspException {
045        try {
046            m_wikiContext = (WikiContext) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
047            if( m_wikiContext == null ) {
048                throw new JspException("WikiContext may not be NULL - serious internal problem!");
049            }
050
051            return doWikiStartTag();
052        } catch( final Exception e ) {
053            LOG.error( "Tag failed", e );
054            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
055        }
056    }
057
058    /**
059     * A local stub for doing tags.  This is just called after the local variables
060     * have been set.
061     * @return As doStartTag()
062     * @throws JspException
063     * @throws IOException
064     */
065    public abstract int doWikiStartTag() throws JspException, IOException;
066
067    @Override
068    public void doCatch(final Throwable arg0) throws Throwable {
069    }
070
071    @Override
072    public void doFinally()
073    {
074        m_wikiContext = null;
075    }  
076    
077}