View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.web.system;
35  
36  import fr.paris.lutece.portal.service.cache.CacheService;
37  import fr.paris.lutece.portal.service.cache.CacheableService;
38  import fr.paris.lutece.portal.service.i18n.I18nService;
39  import fr.paris.lutece.portal.service.template.AppTemplateService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.HashMap;
47  import java.util.List;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class provides the user interface to manage system features ( manage logs, view system files, ... ).
54   */
55  public class CacheJspBean extends AdminFeaturesPageJspBean
56  {
57      // Right
58      public static final String RIGHT_CACHE_MANAGEMENT = "CORE_CACHE_MANAGEMENT";
59  
60      // Jsp definition
61      public static final String JSP_MANAGE_CACHES = "ManageCaches.jsp";
62      private static final long serialVersionUID = 7010476999488231065L;
63  
64      // Markers
65      private static final String MARK_SERVICES_LIST = "services_list";
66  
67      // Template Files path
68      private static final String TEMPLATE_MANAGE_CACHES = "admin/system/manage_caches.html";
69      private static final String TEMPLATE_CACHE_INFOS = "admin/system/cache_infos.html";
70      private static final String PARAMETER_ID_CACHE = "id_cache";
71  
72      /**
73       * Returns the page to manage caches
74       * @param request The HttpServletRequest
75       * @return The HTML code.
76       */
77      public String getManageCaches( HttpServletRequest request )
78      {
79          HashMap<String, Collection<CacheableService>> model = new HashMap<String, Collection<CacheableService>>(  );
80          model.put( MARK_SERVICES_LIST, CacheService.getCacheableServicesList(  ) );
81  
82          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CACHES, getLocale(  ), model );
83  
84          return getAdminPage( template.getHtml(  ) );
85      }
86  
87      /**
88       * Process cache resetting
89       *
90       * @param request The HTTP request
91       * @return The URL to display when the process is done.
92       */
93      public static String doResetCaches( HttpServletRequest request )
94      {
95          String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
96  
97          if ( strCacheIndex != null )
98          {
99              int nCacheIndex = Integer.parseInt( strCacheIndex );
100             CacheableService cs = CacheService.getCacheableServicesList(  ).get( nCacheIndex );
101             cs.resetCache(  );
102         }
103         else
104         {
105             CacheService.resetCaches(  );
106             AppTemplateService.resetCache(  );
107             I18nService.resetCache(  );
108         }
109 
110         return JSP_MANAGE_CACHES;
111     }
112 
113     /**
114      * Reload all properties files of the application
115      *
116      * @return The URL to display when the process is done.
117      */
118     public String doReloadProperties(  )
119     {
120         AppPropertiesService.reloadAll(  );
121 
122         return JSP_MANAGE_CACHES;
123     }
124 
125     /**
126      * Gets cache infos for all caches
127      * @param request The HTTP request
128      * @return HTML formated cache infos
129      */
130     public String getCacheInfos( HttpServletRequest request )
131     {
132         List<CacheableService> list;
133         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
134 
135         if ( strCacheIndex != null )
136         {
137             int nCacheIndex = Integer.parseInt( strCacheIndex );
138             CacheableService cs = CacheService.getCacheableServicesList(  ).get( nCacheIndex );
139             list = new ArrayList<CacheableService>(  );
140             list.add( cs );
141         }
142         else
143         {
144             list = CacheService.getCacheableServicesList(  );
145         }
146 
147         HashMap<String, Collection<CacheableService>> model = new HashMap<String, Collection<CacheableService>>(  );
148         model.put( MARK_SERVICES_LIST, list );
149 
150         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CACHE_INFOS, getLocale(  ), model );
151 
152         return getAdminPage( template.getHtml(  ) );
153     }
154 
155     /**
156      * Process cache toggle on/off
157      *
158      * @param request The HTTP request
159      * @return The URL to display when the process is done.
160      */
161     public static String doToggleCache( HttpServletRequest request )
162     {
163         String strCacheIndex = request.getParameter( PARAMETER_ID_CACHE );
164 
165         if ( strCacheIndex != null )
166         {
167             int nCacheIndex = Integer.parseInt( strCacheIndex );
168             CacheableService cs = CacheService.getCacheableServicesList(  ).get( nCacheIndex );
169             cs.enableCache( !cs.isCacheEnable(  ) );
170         }
171 
172         return JSP_MANAGE_CACHES;
173     }
174 }