KickJava   Java API By Example, From Geeks To Geeks.

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


1 package info.magnolia.cms.cache;
2
3 import info.magnolia.cms.beans.config.ContentRepository;
4 import info.magnolia.cms.beans.config.Server;
5 import info.magnolia.cms.core.Content;
6 import info.magnolia.cms.core.HierarchyManager;
7 import info.magnolia.cms.i18n.MessagesManager;
8 import info.magnolia.context.MgnlContext;
9
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServlet JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21
22 /**
23  * Load all the nodes of a website to that they can be cached for immediate production use.
24  * @author niko
25  * @author Fabrizio Giustina
26  */

27 public class CacheGeneratorServlet extends HttpServlet JavaDoc {
28
29     /**
30      * Stable serialVersionUID.
31      */

32     private static final long serialVersionUID = 222L;
33
34     private static final String JavaDoc CACHE_GENERATE_ACTION = "create.cache";
35
36     private CacheManager cacheManager = CacheManagerFactory.getCacheManager();
37
38     /**
39      * Logger.
40      */

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

46     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
47
48         if (request.getParameter(CACHE_GENERATE_ACTION) == null) {
49             displayCacheForm(request, response.getWriter());
50         }
51         else {
52             try {
53                 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
54                 cachePage(hm.getRoot(), request);
55                 cachePage(hm.getRoot(), request);
56                 response.getWriter().write(MessagesManager.get("cacheservlet.success"));
57             }
58             catch (Exception JavaDoc e) {
59                 throw new ServletException JavaDoc(e);
60             }
61         }
62     }
63
64     /**
65      * Display a simple form for importing/exporting data.
66      * @param request
67      * @param out
68      */

69     private void displayCacheForm(HttpServletRequest JavaDoc request, PrintWriter JavaDoc out) {
70
71         out.println("<html><head><title>Magnolia</title>"); //$NON-NLS-1$
72
// @todo FIXME! out.println(new Sources(request.getContextPath()).getHtmlCss());
73
out.println("</head><body class=\"mgnlBgLight mgnlImportExport\">"); //$NON-NLS-1$
74

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

89         out.println("</form></body></html>"); //$NON-NLS-1$
90
}
91
92     private void cachePage(Content page, HttpServletRequest JavaDoc request) {
93         // Collection children = page.getChildren();
94
// Iterator iter = children.iterator();
95
// while (iter.hasNext()) {
96
// Content item = (Content) iter.next();
97
// MockCacheRequest mock = new MockCacheRequest(request, item);
98
// // @todo fix the canCompress parameter
99
// this.cacheManager.cacheRequest(mock, true);
100
// if (log.isDebugEnabled())
101
// log.debug("Trying to cache request:" + mock.getRequestURL());
102
// cachePage(item, request);
103
// }
104

105     }
106
107     /**
108      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
109      * javax.servlet.http.HttpServletResponse)
110      */

111     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
112         IOException JavaDoc {
113         doGet(request, response);
114     }
115 }
116
Popular Tags