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 java.io.IOException;
022
023import javax.servlet.jsp.JspException;
024import javax.servlet.jsp.PageContext;
025import javax.servlet.jsp.tagext.BodyTagSupport;
026import javax.servlet.jsp.tagext.TryCatchFinally;
027
028import org.apache.log4j.Logger;
029
030import org.apache.wiki.WikiContext;
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 = Logger.getLogger( WikiBodyTag.class );
042
043    public int doStartTag() throws JspException
044    {
045        try
046        {
047            m_wikiContext = (WikiContext) pageContext.getAttribute( WikiTagBase.ATTR_CONTEXT,
048                                                                    PageContext.REQUEST_SCOPE );
049
050            if( m_wikiContext == null )
051            {
052                throw new JspException("WikiContext may not be NULL - serious internal problem!");
053            }
054
055            return doWikiStartTag();
056        }
057        catch( Exception e )
058        {
059            log.error( "Tag failed", e );
060            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
061        }
062    }
063
064    /**
065     * A local stub for doing tags.  This is just called after the local variables
066     * have been set.
067     * @return As doStartTag()
068     * @throws JspException
069     * @throws IOException
070     */
071    public abstract int doWikiStartTag() throws JspException, IOException;
072
073    public void doCatch(Throwable arg0) throws Throwable
074    {
075    }
076
077    public void doFinally()
078    {
079        m_wikiContext = null;
080    }  
081    
082    
083}