View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.action;
16  
17  import org.figure8.join.control.JoinAction;
18  import org.figure8.join.control.form.DefaultForm;
19  import org.figure8.join.core.setup.ApplicationConfig;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import java.util.List;
31  import java.util.ArrayList;
32  import java.util.Properties;
33  import java.util.StringTokenizer;
34  import java.io.InputStream;
35  /**
36   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37   * @version $Revision: 1.1 $
38   *
39   * @struts.action path="/configuration" name="defaultForm"
40   *                scope="request" parameter="op" validate="true"
41   *                input="/pages/mainpage.jsp?body=/jsp/configuration.jsp"
42   * @struts.action-forward name="Configuration" path="/jsp/configuration.jsp"
43   */
44  public class ConfigurationActions extends JoinAction{
45  
46     // Static -------------------------------------------------------------------
47  
48     /** Get a commons logger */
49     private static final Log log = LogUtil.getLog(ConfigurationActions.class);
50  
51     /** Operation code for saving all edited configuration properties */
52     public static final String SAVE_OP = "save";
53  
54     /** Constant representing the property containing all configurable properties */
55     public static final String CONFIG_PROPERTIES_LIST = "join.properties";
56     /** Constant for path to properties file containing all configurable properties */
57     public static final String CONFIG_PROPERTIES_FILE = "/join-config.properties";
58  
59     /**
60      * Name of the request attribute key used for storing the list of
61      * configuratble properties names.
62      */
63     public static final String PROPERTIES_KEY = "properties";
64  
65  
66     // Attributes ---------------------------------------------------------------
67  
68     /** A flag telling if properties have already been read */
69     private boolean initialized = false;
70     /** The list of properties configurable through this action */
71     private List propertiesNames = new ArrayList();
72     /** The whole configuration properties and their description loaded from file */
73     private Properties configurationProperties = new Properties();
74  
75     /** The Join application configuration handler */
76     private ApplicationConfig applicationConfig = null;
77  
78  
79     // Constructors -------------------------------------------------------------
80  
81     /** Creates a new instance of ConfigurationActions */
82     public ConfigurationActions(){
83     }
84  
85  
86     // Public -------------------------------------------------------------------
87  
88     /** @param applicationConfig The application configuration handler object */
89     public void setApplicationConfig(ApplicationConfig applicationConfig){
90        this.applicationConfig = applicationConfig;
91     }
92  
93  
94     // Implementation of JoinAction ---------------------------------------------
95  
96     /**
97      *
98      * @param operation String representing the operation to invoke on Action
99      * @param mapping Mapping between forwards name and path for this action
100     * @param form The form object containing request parameters
101     * @param request The servlet container request wrapper
102     * @param response The servlet container response wrapper
103     * @return A forward to the next view to render and display
104     * @throws Exception such as InfraStructureExceptions ...
105     */
106    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
107            throws Exception{
108       // Trace operation to execute.
109       log.debug("doExecute() called for '" + operation + "' operation");
110 
111       if (form instanceof DefaultForm){
112          // Call initialization method.
113          initializeConfigurationProperties();
114 
115          if (SAVE_OP.equals(operation)){
116             // Update all configurable properties from values in request.
117             for (int i=0; i<propertiesNames.size(); i++){
118                String key = (String)propertiesNames.get(i);
119                String value = request.getParameter(key);
120                applicationConfig.setProperty(key, value);
121                if (log.isDebugEnabled())
122                   log.debug("Setting property '" + key + "' to value: " + value);
123             }
124             // Save the application config in Xml file.
125             log.info("Saving application configuration with new values...");
126             applicationConfig.save();
127          }
128 
129          // In all the case, provide properties list.
130          request.setAttribute(PROPERTIES_KEY, propertiesNames);
131          // Provide also their value, default value and description.
132          for (int i=0; i<propertiesNames.size(); i++){
133             // Take care of current value.
134             String key = (String)propertiesNames.get(i);
135             String value = (String)applicationConfig.getProperty(key);
136             // Take care of default value.
137             String defValue = configurationProperties.getProperty(key + ".default");
138             request.setAttribute(key + ".default", defValue);
139             // Take care of description.
140             String description = configurationProperties.getProperty(key + ".description");
141             request.setAttribute(key + ".description", description);
142 
143             // Assign current or default value.
144             if (value != null && value.length() > 0)
145                request.setAttribute(key, value);
146             else request.setAttribute(key, defValue);
147          }
148 
149          return mapping.findForward("Configuration");
150       }
151       // This should not happen...
152       return null;
153    }
154 
155 
156    // Private ------------------------------------------------------------------
157 
158    /**
159     * Initialize configuration properties by loading and parsing the
160     * CONFIG_PROPERTIES_FILE. Property providing list of configurable properties
161     * is then tokenized into <b>propertiesName</b>.
162     */
163    private void initializeConfigurationProperties(){
164       if (!initialized){
165          InputStream is = this.getClass().getResourceAsStream(CONFIG_PROPERTIES_FILE);
166 
167          try {configurationProperties.load(is);}
168          catch (Exception e){
169             // Log and wrap into runtime exception.
170             log.error(CONFIG_PROPERTIES_FILE + " is not loadable into properties");
171             log.error("Here's the detailed message: " + e.getMessage());
172             throw new IllegalStateException(CONFIG_PROPERTIES_FILE + " cannot be found or reached");
173          }
174          // Get properties list.
175          String propertiesList = configurationProperties.getProperty(CONFIG_PROPERTIES_LIST);
176 
177          // Browse and tokenizer properties list.
178          StringTokenizer tokenizer = new StringTokenizer(propertiesList, ",");
179          while (tokenizer != null && tokenizer.hasMoreTokens()){
180             String configurationProperty = tokenizer.nextToken();
181             propertiesNames.add(configurationProperty);
182          }
183          // Update init flag.
184          initialized = true;
185       }
186    }
187 }