KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > CacheGeneratorServlet


1 package info.magnolia.cms.servlets;
2
3 import info.magnolia.cms.beans.config.Server;
4 import info.magnolia.cms.core.CacheHandler;
5 import info.magnolia.cms.core.Content;
6 import info.magnolia.cms.core.HierarchyManager;
7 import info.magnolia.cms.gui.misc.Sources;
8 import info.magnolia.cms.i18n.MessagesManager;
9 import info.magnolia.cms.security.SessionAccessControl;
10 import info.magnolia.cms.util.MockCacheRequest;
11
12 import java.io.IOException JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import javax.jcr.Workspace;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServlet JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.log4j.Logger;
24
25
26 /**
27  * Load all the nodes of a website to that they can be cached for immediate production use.
28  * @author niko
29  */

30 public class CacheGeneratorServlet extends HttpServlet JavaDoc {
31
32     /**
33      * Stable serialVersionUID.
34      */

35     private static final long serialVersionUID = 222L;
36
37     private static final String JavaDoc CACHE_GENERATE_ACTION = "create.cache";
38
39     /**
40      * Logger.
41      */

42     private static Logger log = Logger.getLogger(CacheGeneratorServlet.class);
43
44     /**
45      * access the MessagesManager and ask him to reload the bundles
46      */

47     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
48
49         if (request.getParameter(CACHE_GENERATE_ACTION) == null) {
50             displayCacheForm(request, response.getWriter());
51         }
52         else {
53             try {
54                 HierarchyManager hm = SessionAccessControl.getHierarchyManager(request);
55                 Workspace ws = hm.getWorkspace();
56                 cachePage(hm.getRoot(), request);
57                 cachePage(hm.getRoot(), request);
58                 response.getWriter().write(MessagesManager.get(request, "cacheservlet.success"));
59
60             }
61             catch (Exception JavaDoc e) {
62                 throw new ServletException JavaDoc(e);
63             }
64         }
65     }
66
67     /**
68      * Display a simple form for importing/exporting data.
69      * @param response HttpServletResponse
70      * @param repository selected repository
71      * @param basepath base path in repository (extracted from request parameter or default)
72      */

73     private void displayCacheForm(HttpServletRequest JavaDoc request, PrintWriter JavaDoc out) {
74
75         out.println("<html><head><title>Magnolia</title>"); //$NON-NLS-1$
76
out.println(new Sources(request.getContextPath()).getHtmlCss());
77         out.println("</head><body class=\"mgnlBgLight mgnlImportExport\">"); //$NON-NLS-1$
78

79         out.println("<h2>"); //$NON-NLS-1$
80
out.println(MessagesManager.get(request, "cacheservlet.title")); //$NON-NLS-1$
81
out.println("</h2>"); //$NON-NLS-1$
82
if (Server.isAdmin())
83             out.println("<h3 style=\"color:red;\">"
84                 + MessagesManager.get(request, "cacheservlet.warning")
85                 + "</h3>");
86
87         out.println("<form method=\"get\" action=\"\">"); //$NON-NLS-1$
88
out.println("<input type=\"submit\" name=\"" //$NON-NLS-1$
89
+ CACHE_GENERATE_ACTION + "\" value=\"" //$NON-NLS-1$
90
+ MessagesManager.get(request, "cacheservlet.generate") //$NON-NLS-1$
91
+ "\" />"); //$NON-NLS-1$
92

93         out.println("</form></body></html>"); //$NON-NLS-1$
94
}
95
96     private void cachePage(Content page, HttpServletRequest JavaDoc request) {
97         Collection JavaDoc children = page.getChildren();
98         String JavaDoc rootURL = "http://" + request.getRemoteAddr() + request.getContextPath() + "/";
99         Iterator JavaDoc iter = children.iterator();
100         while (iter.hasNext()) {
101             Content item = (Content) iter.next();
102             String JavaDoc node = item.getName();
103             MockCacheRequest mock = new MockCacheRequest(request, item);
104             CacheHandler.cacheURI(mock);
105             if (log.isDebugEnabled())
106                 log.debug("Trying to cache request:" + mock.getRequestURL());
107             cachePage(item, request);
108         }
109
110     }
111
112     /**
113      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
114      * javax.servlet.http.HttpServletResponse)
115      */

116     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
117         IOException JavaDoc {
118         doGet(request, response);
119     }
120 }
121
Popular Tags