Author: bpapez
Date: Wed Jun 6 17:45:57 2007
New Revision: 17473
URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17473&repname=
=3Djahia
Log:
Add JspPrecompileServlet
Added:
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompile/
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompile/J=
spPrecompileServlet.java
Modified:
branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/web.xml
Added: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompil=
e/JspPrecompileServlet.java
URL: https://svndev.jahia.net/websvn/filedetails.php?path=3D/branches/JAHIA=
-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompile/JspPrecompileServle=
t.java&rev=3D17473&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompile/J=
spPrecompileServlet.java (added)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/tools/precompile/J=
spPrecompileServlet.java Wed Jun 6 17:45:57 2007
@@ -0,0 +1,323 @@
+package org.jahia.tools.precompile;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Helps to precompile JSPs of a WebApp.
+ * The Servlet performs 3 actions depending on the passed params:
+ * - if jsp_name param is passed,
+ * the servlet tries to forward to the JSP with the passed name
+ * - if compile_type=3Dall is passed,
+ * the servlet tries to forward to all found JSPs and
+ * generates a report HTML output
+ * - if compile_type=3Dtemplates is passed,
+ * the servlet tries to forward to all found templates JSPs and
+ * generates a report HTML output
+ * - if compile_type=3Dsite is passed,
+ * the servlet tries to forward to all found templates JSPs of a site a=
nd
+ * generates a report HTML output =
+ * - if no special param is passed,
+ * the servlet generates a page with links for the above described purp=
oses
+ */
+public class JspPrecompileServlet extends HttpServlet implements Servlet
+{
+ private static final String JSP_NAME_PARAM =3D "jsp_name";
+ private static final String COMPILE_TYPE_PARAM =3D "compile_type";
+ private static final String SITE_KEY_PARAM =3D "site_key";
+ private static final String TEMPLATES_DIR =3D "jsp/jahia/templates";
+
+ private static final String MAGIC_TOMCAT_PARAM =3D "jsp_precompile=3Dtru=
e";
+ =
+ public void doGet(HttpServletRequest aRequest, HttpServletResponse aResp=
onse)
+ throws ServletException, IOException
+ {
+ doWork(aRequest, aResponse);
+ }
+
+ public void doPost(HttpServletRequest aRequest, HttpServletResponse aRes=
ponse)
+ throws ServletException, IOException
+ {
+ doWork(aRequest, aResponse);
+ }
+
+ /**
+ * Performs depending on the passed request params the actions
+ * mentioned in the class description. =
+ */
+ private void doWork(
+ HttpServletRequest aRequest, HttpServletResponse aResponse)
+ throws ServletException, IOException
+ {
+ aRequest.getSession(true);
+ =
+ String jspName =3D aRequest.getParameter(JSP_NAME_PARAM);
+ String compileType =3D aRequest.getParameter(COMPILE_TYPE_PARAM);
+ if(jspName !=3D null) =
+ {
+ //precompile single JSP
+ RequestDispatcher rd =3D aRequest.getRequestDispatcher(jspName);
+ rd.forward(aRequest, aResponse);
+ }
+ else if("all".equals(compileType))
+ {
+ //precompile all JSPs and generate report
+ precompileJsps(searchForJsps(), aRequest, aResponse);
+ }
+ else if("templates".equals(compileType))
+ {
+ //precompile all JSPs and generate report
+ precompileJsps(searchForJsps(TEMPLATES_DIR), aRequest, aResponse);
+ }
+ else if("site".equals(compileType))
+ {
+ //precompile all JSPs and generate report
+ precompileJsps(searchForJsps(TEMPLATES_DIR + "/" + aRequest.getParam=
eter(SITE_KEY_PARAM)), aRequest, aResponse);
+ } =
+ else =
+ {
+ //generate output with links for compile all and all JSPs =
+ PrintWriter out =3D aResponse.getWriter();
+ List foundJsps =3D searchForJsps();
+ =
+ aResponse.setContentType("text/html;charset=3DISO-8859-1");
+
+ out.print(
+ "<html>\r\n"
+ + "<head>"
+ + "<META http-equiv=3D\"expires\" content=3D\"0\">"
+ + "<title>JSPs in WebApp</title>"
+ + "</head>\r\n"
+
+ + "<body>\r\n"
+ + "<b>");
+ out.print(new Date().toString());
+ out.print(
+ "</b><br/>\r\n"
+
+ + "#JSPs in WebApp: ");
+ out.print(foundJsps.size());
+ out.print(
+ "<br>\r\n"
+
+ + "<a target=3D\"_blank\" href=3D\"");
+ =
+ long now =3D System.currentTimeMillis();
+ =
+ String url =3D aResponse.encodeURL(aRequest.getContextPath()
+ + aRequest.getServletPath() + "?" + COMPILE_TYPE_PARAM
+ + "=3Dall×tamp=3D" + now + "&"
+ + MAGIC_TOMCAT_PARAM);
+
+ out.print(url);
+ out.print("\">precompile all</a><br/>\r\n");
+ out.print("<a target=3D\"_blank\" href=3D\"");
+ =
+ url =3D aResponse.encodeURL(aRequest.getContextPath()
+ + aRequest.getServletPath() + "?" + COMPILE_TYPE_PARAM
+ + "=3Dtemplates×tamp=3D" + now + "&"
+ + MAGIC_TOMCAT_PARAM);
+
+ out.print(url);
+ out.print("\">precompile templates</a><br/>\r\n"); =
+ =
+ listSites(out, aRequest, aResponse, now); =
+ =
+ listFiles(out, aRequest.getContextPath(), aRequest.getServletPath(),
+ foundJsps, aResponse, now);
+
+ out.print(
+ "</body>\r\n"
+ + "</html>");
+ }
+ }
+
+ /**
+ * Searches for Files with extension JSP in the whole web app directory.
+ * @return List of context relative JSP names (Strings)
+ */
+ private List searchForJsps()
+ {
+ return searchForJsps("");
+ }
+
+ /**
+ * Searches for Files with extension JSP in the whole web app directory.
+ * @return List of context relative JSP names (Strings)
+ */
+ private List searchForJsps(String attachPath)
+ {
+ String webModulePath =3D getServletContext().getRealPath("/");
+ File jspsDir =3D new File(webModulePath + attachPath);
+ List foundJsps =3D new ArrayList();
+ searchForJsps(webModulePath, jspsDir, foundJsps);
+ return foundJsps;
+ } =
+ =
+ /**
+ * Fills passed List with context relative URLs of found JSPs.
+ * If passed dir contains subdirs,
+ * the method is called recursive for this subdirs.
+ */
+ private void searchForJsps(String aWebModulePath, File aDir, List aFound=
Jsps)
+ {
+ File[] files =3D aDir.listFiles();
+ for (int i =3D 0; i < files.length; i++)
+ {
+ if (files[i].isDirectory())
+ {
+ //subdir found
+ searchForJsps(aWebModulePath, files[i], aFoundJsps);
+ }
+ else
+ {
+ int extIdx =3D files[i].getName().lastIndexOf('.');
+ if (extIdx !=3D -1
+ && files[i].getName().length() =3D=3D extIdx + 4 // ... + ".jsp"
+ && files[i].getName().regionMatches(true, extIdx + 1, "jsp", 0, =
3))
+ {
+ //JSP found!
+ String jspPath =3D files[i].getPath();
+ jspPath =3D jspPath.substring(aWebModulePath.length());
+ jspPath =3D jspPath.replace('\\', '/');
+ aFoundJsps.add(jspPath);
+ }
+ }
+ }
+ }
+ =
+ /**
+ * Loops through list of all JSP URLs, "includes" each JSP and
+ * generates a report HTML response. =
+ * Progress information is printed to stdout. =
+ */
+ private void precompileJsps(List foundJsps,
+ HttpServletRequest aRequest, HttpServletResponse aResponse)
+ throws ServletException, IOException
+ {
+ System.out.println("Precompile started...");
+ List buggyJsps =3D new ArrayList();
+ int i =3D 1;
+ for(Iterator it =3D foundJsps.iterator(); it.hasNext(); i++)
+ {
+ String jspPath =3D (String)it.next();
+ RequestDispatcher rd =3D aRequest.getRequestDispatcher(jspPath);
+ try
+ {
+ System.out.print("Compiling (" + i + ") " + jspPath + "...");
+ rd.include(aRequest, aResponse);
+ System.out.println(" OK.");
+ }
+ catch(Exception ex)
+ {
+ System.out.println(" ERROR.");
+ buggyJsps.add(jspPath); =
+ }
+ aResponse.resetBuffer();
+ }
+ System.out.println("Precompile ended!");
+ PrintWriter out =3D aResponse.getWriter();
+ aResponse.setContentType("text/html;charset=3DISO-8859-1");
+ out.print(
+ "<html>"
+ + "<head>"
+ + "<META http-equiv=3D\"expires\" content=3D\"0\">"
+ + "<title>JSP precompile result</title>"
+ + "</head>\r\n"
+ + "<body>\r\n"
+ + "<b>");
+ out.print(foundJsps.size());
+ out.print(" JSPs processed.</b><br/>\r\n");
+ if(buggyJsps.size() =3D=3D 0) =
+ out.print("No problems found!\r\n");
+ else
+ {
+ out.print("Precompile failed for following "
+ + buggyJsps.size() + " JSPs:\r\n");
+ listFiles(out, aRequest.getContextPath(), aRequest.getServletPath(),
+ buggyJsps, aResponse, System.currentTimeMillis());
+ }
+ out.println(
+ "</body>"
+ + "</html>"); =
+ }
+ =
+ /**
+ * Adds a hyperlinks for each JSP to the output.
+ * Each link contains the JSP name.
+ * If the JSP is located somewhere below WEB-INF dir,
+ * it can not be reached from outside,
+ * therefore a link to the servlet is created with a jsp_name param.
+ * Tomcat specific jsp_precompile param is also added to each link.
+ * Also current timestamp is added to help the browser marking visited l=
inks. =
+ */
+ private void listFiles(PrintWriter anOut, String aContextPath,
+ String aServletPath, List aFoundJsps, HttpServletResponse aResponse, l=
ong now)
+ {
+ for(Iterator it =3D aFoundJsps.iterator(); it.hasNext(); )
+ {
+ String jspPath =3D (String)it.next();
+ anOut.print("<br/>");
+ anOut.print("<a target=3D\"_blank\" href=3D\"");
+ String url =3D null;
+
+
+ if(jspPath.startsWith("WEB-INF", 1)) =
+ {
+ //create link to JspPrecompileServlet with jsp_name param
+ url =3D aContextPath + aServletPath + "?" + JSP_NAME_PARAM + "=3D"
+ + jspPath + "&" + MAGIC_TOMCAT_PARAM;
+ }
+ else
+ {
+ //create direct link to jsp file
+ url =3D aContextPath + "/" + jspPath + "?" + MAGIC_TOMCAT_PARAM; =
=
+ }
+ url =3D url + "&now=3D" + now;
+ anOut.print(aResponse.encodeURL(url));
+ =
+ anOut.print("\">");
+ anOut.print(jspPath);
+ anOut.println("</a>");
+ }
+ }
+ =
+ /**
+ * Adds hyperlinks for each site to the output.
+ * Each link contains the directory name of the site.
+ * Tomcat specific jsp_precompile param is also added to each link.
+ * Also current timestamp is added to help the browser marking visited l=
inks. =
+ */
+ private void listSites(PrintWriter anOut, HttpServletRequest aRequest, H=
ttpServletResponse aResponse, long now)
+ {
+ String templatesPath =3D getServletContext().getRealPath("/") + TEMPLA=
TES_DIR;
+ File templatesDir =3D new File(templatesPath);
+ File[] files =3D templatesDir.listFiles();
+ for (int i =3D 0; i < files.length; i++)
+ {
+ if (files[i].isDirectory())
+ { =
+ anOut.print("<a target=3D\"_blank\" href=3D\""); =
+ String url =3D aResponse.encodeURL(aRequest.getContextPath()
+ + aRequest.getServletPath() + "?" + COMPILE_TYPE_PARAM
+ + "=3Dsite&site_key=3D" + files[i].getName() + "×ta=
mp=3D" + now + "&"
+ + MAGIC_TOMCAT_PARAM);
+
+ anOut.print(url);
+ anOut.print("\">precompile site: " + files[i].getName() + "</a><=
br/>\r\n"); =
+ }
+ } =
+ } =
+}
\ No newline at end of file
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/web.xml
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/webapp/WEB-INF/web.xml&rev=3D17473&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/web.xml (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/web.xml Wed Jun 6=
17:45:57 2007
@@ -572,6 +572,11 @@
org.jahia.services.esi.EsiTemplateCachingServlet
</servlet-class>
</servlet>
+ =
+ <servlet>
+ <servlet-name>JspPrecompileServlet</servlet-name>
+ <servlet-class>org.jahia.tools.precompile.JspPrecompileServlet</servl=
et-class>
+ </servlet> =
=
<!-- [INSERT FRAGMENT HERE] -->
=
@@ -704,6 +709,11 @@
<url-pattern>/jsp/jahia/javascript/zimbra/ajaxTK/msgs/*</url-patte=
rn>
</servlet-mapping>
=
+ <servlet-mapping>
+ <servlet-name>JspPrecompileServlet</servlet-name>
+ <url-pattern>/precompileServlet</url-pattern>
+ </servlet-mapping>
+ =
<!-- Session timeout value is set in minutes. Comment this section out
in order to set the timeout time back to the server's value -->
<!--
_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list