KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > status > ManageStatus


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  *
15  * ----- BEGIN LICENSE BLOCK -----
16  * Version: JCSL 1.0
17  *
18  * The contents of this file are subject to the Jahia Community Source License
19  * 1.0 or later (the "License"); you may not use this file except in
20  * compliance with the License. You may obtain a copy of the License at
21  * http://www.jahia.org/license
22  *
23  * Software distributed under the License is distributed on an "AS IS" basis,
24  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
25  * for the rights, obligations and limitations governing use of the contents
26  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
27  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
28  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
29  *
30  * The Shared Modifications are Jahia View Helper.
31  *
32  * The Developer of the Shared Modifications is Jahia Solution S�rl.
33  * Portions created by the Initial Developer are Copyright (C) 2002 by the
34  * Initial Developer. All Rights Reserved.
35  *
36  * Contributor(s):
37  * Jun-Aug 2003, Jahia Solutions Sarl: Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.admin.status;
44
45 import java.io.IOException JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.servlet.http.HttpSession JavaDoc;
52
53 import org.jahia.bin.JahiaAdministration;
54 import org.jahia.registries.ServicesRegistry;
55 import org.jahia.services.cache.CacheFactory;
56 import org.jahia.services.cache.Cache;
57
58
59 /**
60  * <p>Title: Manage Jahia status</p>
61  * <p>Description: The purpose of this class is to present an administration
62  * tool for viewing and manipulating Jahia's internal status, including
63  * cache(s) status, memory consumption, etc... </p>
64  * <p>Copyright: Copyright (c) 2002</p>
65  * <p>Company: Jahia Inc.</p>
66  * @author Serge Huber, Fulco Houkes
67  * @version 3.1
68  */

69
70 public class ManageStatus {
71
72     /** logging */
73     private static org.apache.log4j.Logger logger =
74             org.apache.log4j.Logger.getLogger (ManageStatus.class);
75
76     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
77     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
78
79
80     /**
81      * Default constructor.
82      *
83      * @param request Servlet request.
84      * @param response Servlet response.
85      * @param session Servlet session for the current user.
86      */

87     public ManageStatus (HttpServletRequest JavaDoc request,
88                          HttpServletResponse JavaDoc response,
89                          HttpSession JavaDoc session)
90             throws Throwable JavaDoc {
91         userRequestDispatcher (request, response, session);
92     }
93
94
95     /**
96      * This method is used like a dispatcher for user requests.
97      *
98      * @param request Servlet request.
99      * @param response Servlet response.
100      * @param session Servlet session for the current user.
101      */

102     private void userRequestDispatcher (HttpServletRequest JavaDoc request,
103                                         HttpServletResponse JavaDoc response,
104                                         HttpSession JavaDoc session)
105             throws Throwable JavaDoc {
106         String JavaDoc operation = request.getParameter ("sub");
107
108         if (operation.equals ("display")) {
109             displaySettings (request, response, session);
110         } else if (operation.equals ("process")) {
111             processSettings (request, response, session);
112         }
113     }
114
115
116     /**
117      * Display the server settings page, using doRedirect().
118      *
119      * @param request Servlet request.
120      * @param response Servlet response.
121      * @param session Servlet session for the current user.
122      */

123     private void displaySettings (HttpServletRequest JavaDoc request,
124                                   HttpServletResponse JavaDoc response,
125                                   HttpSession JavaDoc session)
126             throws IOException JavaDoc, ServletException JavaDoc {
127         // retrieve previous form values...
128
Long JavaDoc freeMemoryInBytes = (Long JavaDoc)session.getAttribute (CLASS_NAME + "freeMemoryInBytes");
129         Long JavaDoc totalMemoryInBytes = (Long JavaDoc)session.getAttribute (CLASS_NAME + "totalMemoryInBytes");
130         Integer JavaDoc outputCacheSize = (Integer JavaDoc)session.getAttribute (CLASS_NAME + "outputCacheSize");
131         Integer JavaDoc outputCacheMaxSize = (Integer JavaDoc)session.getAttribute (CLASS_NAME + "outputCacheMaxSize");
132
133         // set default values (if necessary)...
134
if (freeMemoryInBytes == null) {
135             freeMemoryInBytes = new Long JavaDoc (Runtime.getRuntime ().freeMemory ());
136         }
137         if (totalMemoryInBytes == null) {
138             totalMemoryInBytes = new Long JavaDoc (Runtime.getRuntime ().totalMemory ());
139         }
140         try {
141             if (outputCacheSize == null) {
142                 outputCacheSize = new Integer JavaDoc(CacheFactory.getHtmlCache().size());
143             }
144             if (outputCacheMaxSize == null) {
145                 outputCacheMaxSize = new Integer JavaDoc(CacheFactory.getHtmlCache().
146                                                  getCacheLimit());
147             }
148         } catch (Throwable JavaDoc t) {
149             logger.error("Error while trying to retrieve HTML status", t);
150         }
151
152         // set request attributes...
153
request.setAttribute ("freeMemoryInBytes", freeMemoryInBytes);
154         request.setAttribute ("totalMemoryInBytes", totalMemoryInBytes);
155         request.setAttribute ("outputCacheSize", outputCacheSize);
156         request.setAttribute ("outputCacheMaxSize", outputCacheMaxSize);
157
158         JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "status.jsp");
159     }
160
161
162     /**
163      * Process and check the validity of the server settings page. If they are
164      * not valid, display the server settings page to the user.
165      *
166      * @param request Servlet request.
167      * @param response Servlet response.
168      * @param session Servlet session for the current user.
169      */

170     private void processSettings (HttpServletRequest JavaDoc request,
171                                   HttpServletResponse JavaDoc response,
172                                   HttpSession JavaDoc session)
173             throws IOException JavaDoc, ServletException JavaDoc {
174         if (request.getParameter ("flushHTML") != null) {
175             logger.debug ("Flushing Output Cache");
176             try {
177                 CacheFactory.getHtmlCache().flush();
178             } catch (Throwable JavaDoc t) {
179                 logger.error("Error while trying to flush HTML cache", t);
180             }
181         }
182
183         if (request.getParameter ("flushLocks") != null) {
184             logger.debug ("Flushing Locks");
185             ServicesRegistry.getInstance ().getLockService().purgeLocks();
186         }
187
188         if (request.getParameter ("flushAllCaches") != null) {
189             logger.debug ("Flushing all caches");
190             CacheFactory.getInstance().flushAllCaches();
191         }
192
193         // get the cache factory instance
194
CacheFactory cacheFactory = CacheFactory.getInstance ();
195
196         // get the registered cache names
197
Iterator JavaDoc cacheNameIte = cacheFactory.getNames ().iterator();
198
199         // for each cache..
200
while (cacheNameIte.hasNext()) {
201
202             // get the cache name
203
String JavaDoc curCacheName = (String JavaDoc)cacheNameIte.next ();
204
205             if (request.getParameter ("flush_" + curCacheName) != null) {
206                 Cache cache = CacheFactory.getInstance().getCache (curCacheName);
207                 if (cache != null) {
208                     logger.debug ("Flushing cache : " + curCacheName);
209                     cache.flush();
210                 }
211             }
212         }
213
214         displaySettings (request, response, session);
215     }
216
217 }
218
Popular Tags