KickJava   Java API By Example, From Geeks To Geeks.

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


1 package info.magnolia.cms.servlets;
2
3 import info.magnolia.cms.gui.misc.Sources;
4 import info.magnolia.cms.i18n.MessagesManager;
5
6 import java.io.IOException JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.http.HttpServlet JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16
17 /**
18  * Simple servlet to reload resource bundles and thus update the content of the internationalization
19  * @author niko
20  */

21 public class ReloadI18nServlet extends HttpServlet JavaDoc {
22
23     /**
24      * Stable serialVersionUID.
25      */

26     private static final long serialVersionUID = 222L;
27
28     /**
29      * request parameter: reload requested.
30      */

31     private static final String JavaDoc PARAM_RELOAD_ACTION = "reload"; //$NON-NLS-1$
32

33     /**
34      * access the MessagesManager and ask him to reload the bundles
35      */

36     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
37         PrintWriter JavaDoc out = response.getWriter();
38         displayReloadForm(request, out);
39         out.flush();
40         out.close();
41     }
42
43     /**
44      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
45      * javax.servlet.http.HttpServletResponse)
46      */

47     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
48         IOException JavaDoc {
49         PrintWriter JavaDoc out = response.getWriter();
50         displayReloadForm(request, out);
51         out.println("----------------------<br/>"); //$NON-NLS-1$
52
executeReload(request, out);
53         out.flush();
54         out.close();
55     }
56
57     private void executeReload(HttpServletRequest JavaDoc request, PrintWriter JavaDoc out) {
58         try {
59             MessagesManager.getMessages(request, null, MessagesManager.getDefaultLocale()).reloadBundles();
60             out
61                 .println(MessagesManager.get(request, "reloadi18n.reloaded") + ":" + new SimpleDateFormat JavaDoc().format(new Date JavaDoc()) + "<br><br>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62
}
63         catch (Exception JavaDoc e) {
64             out.println(MessagesManager.get(request, "reloadi18n.error") + "<br/><br/>"); //$NON-NLS-1$ //$NON-NLS-2$
65
}
66
67     }
68
69     /**
70      * Display a simple form for reloading data.
71      * @param response HttpServletResponse
72      * @param repository selected repository
73      * @param basepath base path in repository (extracted from request parameter or default)
74      */

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

81         out.println("<h2>"); //$NON-NLS-1$
82
out.println(MessagesManager.get(request, "reloadi18n.title")); //$NON-NLS-1$
83
out.println("</h2>"); //$NON-NLS-1$
84
out.println("<form method=\"post\" action=\"\">"); //$NON-NLS-1$
85

86         out.println("<input type=\"submit\" name=\"" //$NON-NLS-1$
87
+ PARAM_RELOAD_ACTION + "\" value=\"" //$NON-NLS-1$
88
+ MessagesManager.get(request, "reloadi18n.action") //$NON-NLS-1$
89
+ "\" />"); //$NON-NLS-1$
90

91         out.println("</form></body></html>"); //$NON-NLS-1$
92
}
93 }
94
Popular Tags