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.log4j.Logger;
022import org.apache.wiki.WikiContext;
023import org.apache.wiki.api.core.Context;
024
025import javax.servlet.jsp.JspException;
026import javax.servlet.jsp.PageContext;
027import javax.servlet.jsp.tagext.BodyTagSupport;
028import javax.servlet.jsp.tagext.TryCatchFinally;
029import java.io.IOException;
030
031
032/**
033 *  This is a class that provides the same services as the WikiTagBase, but this time it
034 *   works for the BodyTagSupport base class.
035 */
036public abstract class WikiBodyTag extends BodyTagSupport implements TryCatchFinally {
037
038    private static final long serialVersionUID = -6732266865112847897L;
039    protected WikiContext m_wikiContext;
040    private static final Logger log = Logger.getLogger( WikiBodyTag.class );
041
042    public int doStartTag() throws JspException {
043        try {
044            m_wikiContext = (WikiContext) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
045            if( m_wikiContext == null ) {
046                throw new JspException("WikiContext may not be NULL - serious internal problem!");
047            }
048
049            return doWikiStartTag();
050        } catch( final Exception e ) {
051            log.error( "Tag failed", e );
052            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
053        }
054    }
055
056    /**
057     * A local stub for doing tags.  This is just called after the local variables
058     * have been set.
059     * @return As doStartTag()
060     * @throws JspException
061     * @throws IOException
062     */
063    public abstract int doWikiStartTag() throws JspException, IOException;
064
065    public void doCatch( final Throwable arg0) throws Throwable {
066    }
067
068    public void doFinally()
069    {
070        m_wikiContext = null;
071    }  
072    
073}