<%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<%@ page import="java.util.Iterator"        %>
<%@ page import="java.io.File"              %>

<html>
<head>

    <title>Precompiling *.JSPs</title>

    <META HTTP-EQUIV="Content-Type"        CONTENT="text/html; charset=iso-8859-1">
    <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
    <META HTTP-EQUIV="Content-Style-Type"  CONTENT="text/css">

    <style type='text/css'>
    <!--
        body, td, table {
            font-family: sans-serif;
            font-size: 13px;
            font-weight: normal;
        }

        .large {
            font-family: sans-serif;
            font-size:14px;
            font-weight:bold;
        }
    -->
    </style>
    
</head> 
<body bgcolor="#ffffff"> 
<span class="large">Precompiling JSP pages for context path '<%=request.getContextPath()%>':</span><br><br>
<% 
    synchronized ( this ) {
        HttpServletRequest req = new HttpServletRequestWrapper(request) { 
            public String getQueryString() {
                return "jsp_precompile=true"; 
            }
        };
        numPages = 0;
        numErrors = 0;
        totalBytes = 0;
        compileAllJsps( pageContext, out, req, response, "/");
    }
%>
<br><br><span class="large">Done [ <%=numPages%> pages, <%=numErrors%> <%= numErrors != 1 ? "errors" : "error" %>, compiled bytes <%=totalBytes%> ]</span> 
</body> 
</html>

<%! 
    int  numPages  = 0;
    int  numErrors = 0;
    long totalBytes = 0;
    
    private void compileAllJsps( PageContext         pageContext, 
						         JspWriter           out, 
						         HttpServletRequest  request, 
						         HttpServletResponse response, 
						         String              uripath ) 
    throws java.io.IOException, 
    	   javax.servlet.ServletException { 

    for ( Iterator iter = pageContext.getServletContext().getResourcePaths( uripath ).iterator(); iter.hasNext(); ) { 

        String uri = (String) iter.next(); 

        if ( uri.endsWith( ".jsp" ) ) { 
            
            numPages++;

            try {

                out.write( "<nobr>" + uri + "</b>&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;</nobr>" ); 
                out.flush();

                File jspFile = new File( vkc.view.WebShare.getWebApplicationDir(), uri );

                RequestDispatcher rd = getServletContext().getRequestDispatcher( uri ); 
                if (rd == null) {
                    throw new Exception( "RequestDispatcher is null for uri '" + uri + "'" ); 
                }

                rd.include( request, response ); 
                
                totalBytes += jspFile.length();
                
                out.write( "&nbsp;<img src='" + request.getContextPath() + "/common/images/checkmark-small.gif' border='0'><br>" );
                out.flush();

            } catch( Throwable ex ) {

                numErrors++;

                out.write( "&nbsp;<img src='" + request.getContextPath() + "/common/images/error.gif' border='0' valign='baseline'><br><br>" );
                String s = vkc.util.VKCUtils.getStackTrace( ex );
                out.write( vkc.util.StringUtils.getHtmlFormatted( s ) );
                out.flush();
            }

        } else if ( uri.endsWith("/") ) { 
            compileAllJsps(pageContext, out, request, response, uri); // process subfolders 
        }
    }
} 
%> 