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.daemon.AppDaemonService;
37  import fr.paris.lutece.portal.service.i18n.I18nService;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.web.admin.AdminPageJspBean;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import java.util.HashMap;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  
51  /**
52   *
53   */
54  public class DaemonsJspBean extends AdminPageJspBean
55  {
56      public static final String RIGHT_DAEMONS_MANAGEMENT = "CORE_DAEMONS_MANAGEMENT";
57      private static final long serialVersionUID = 3636973660388119199L;
58      private static final String TEMPLATE_MANAGE_DAEMONS = "admin/system/manage_daemons.html";
59      private static final String MARK_DAEMONS_LIST = "daemons_list";
60      private static final String PARAMETER_DAEMON = "daemon";
61      private static final String PARAMETER_ACTION = "action";
62      private static final String PARAMETER_INTERVAL = "interval";
63      private static final String ACTION_START = "START";
64      private static final String ACTION_STOP = "STOP";
65      private static final String ACTION_UPDATE_INTERVAL = "UPDATE_INTERVAL";
66      private static final String PROPERTY_FIELD_INTERVAL = "portal.system.manage_daemons.columnTitleInterval";
67      private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
68      private static final String MESSAGE_NUMERIC_FIELD = "portal.util.message.errorNumericField";
69  
70      /**
71       * Build the manage daemon page
72       * @param request The HTTP request
73       * @return The Manage daemon page
74       */
75      public String getManageDaemons( HttpServletRequest request )
76      {
77          HashMap<String, Object> model = new HashMap<String, Object>(  );
78          model.put( MARK_DAEMONS_LIST, AppDaemonService.getDaemonEntries(  ) );
79  
80          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DAEMONS, getLocale(  ), model );
81  
82          return getAdminPage( template.getHtml(  ) );
83      }
84  
85      /**
86       * Process the daemon action
87       * @param request The HTTP request
88       * @return The forward URL
89       */
90      public String doDaemonAction( HttpServletRequest request )
91      {
92          String strAction = request.getParameter( PARAMETER_ACTION );
93          String strDaemonKey = request.getParameter( PARAMETER_DAEMON );
94  
95          if ( strAction.equalsIgnoreCase( ACTION_START ) )
96          {
97              AppDaemonService.startDaemon( strDaemonKey );
98          }
99          else if ( strAction.equalsIgnoreCase( ACTION_STOP ) )
100         {
101             AppDaemonService.stopDaemon( strDaemonKey );
102         }
103 
104         else if ( strAction.equalsIgnoreCase( ACTION_UPDATE_INTERVAL ) )
105         {
106             String strErrorMessage = null;
107             String strDaemonInterval = request.getParameter( PARAMETER_INTERVAL );
108 
109             Object[] tabFieldInterval = { I18nService.getLocalizedString( PROPERTY_FIELD_INTERVAL, getLocale(  ) ) };
110 
111             if ( StringUtils.isEmpty( strDaemonInterval ) )
112             {
113                 strErrorMessage = MESSAGE_MANDATORY_FIELD;
114             }
115             else if ( !StringUtils.isNumeric( strDaemonInterval ) )
116             {
117                 strErrorMessage = MESSAGE_NUMERIC_FIELD;
118             }
119 
120             if ( strErrorMessage != null )
121             {
122                 return AdminMessageService.getMessageUrl( request, strErrorMessage, tabFieldInterval,
123                     AdminMessage.TYPE_STOP );
124             }
125 
126             AppDaemonService.modifyDaemonInterval( strDaemonKey, strDaemonInterval );
127         }
128 
129         return getHomeUrl( request );
130     }
131 }