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    public int doStartTag() throws JspException {
044        try {
045            m_wikiContext = (WikiContext) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
046            if( m_wikiContext == null ) {
047                throw new JspException("WikiContext may not be NULL - serious internal problem!");
048            }
049
050            return doWikiStartTag();
051        } catch( final Exception e ) {
052            log.error( "Tag failed", e );
053            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
054        }
055    }
056
057    /**
058     * A local stub for doing tags.  This is just called after the local variables
059     * have been set.
060     * @return As doStartTag()
061     * @throws JspException
062     * @throws IOException
063     */
064    public abstract int doWikiStartTag() throws JspException, IOException;
065
066    public void doCatch( final Throwable arg0) throws Throwable {
067    }
068
069    public void doFinally()
070    {
071        m_wikiContext = null;
072    }  
073    
074}