View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.common.webapp.controller.struts;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.StringTokenizer;
21  
22  import javax.servlet.ServletException;
23  
24  import org.apache.struts.config.MessageResourcesConfig;
25  import org.apache.struts.config.ModuleConfig;
26  
27  /***
28   * Customized ActionServlet to use CustomMessageResources
29   * 
30   * @todo improve this class
31   * 
32   * @author Carlos Sanchez
33   * @version $Revision: 1.1 $
34   */
35  public class ActionServlet extends org.apache.struts.action.ActionServlet {
36  
37      /***
38       * This is the overridden method to load multiple resource bundle files. The
39       * resource bundle files are expected to be defined in the struts-config.xml
40       * file in a comma separated fashion. For example, this is a typical struts
41       * config file declaring multiple resource files <message-resources
42       * null="false" parameter="centaur-event-resources, centaur-page-resources,
43       * centaur-nominal-resources, centaur-error-resources"/>
44       * 
45       * This method makes use of CentaurMessageResource class and passed the list
46       * of resource files to handle.Finally, it puts the CentaurMessageResources
47       * object in the ServletContext against the key specified in the
48       * configuration. As is the case, if no "key" is specified in the
49       * configuration (in our example config), it constructs a default key.
50       * 
51       * @param config
52       *            The ModuleConfig object.
53       * @throws ServletException
54       *             is thrown in case of initialisation problem.
55       */
56      protected void initModuleMessageResources(ModuleConfig config)
57              throws ServletException {
58  
59          List fileNames = new ArrayList();
60  
61          /*
62           * assuming that multiple resource files will be specified as comma
63           * separated list within one <message-resource> tag in the Struts config
64           * file
65           */
66          MessageResourcesConfig resourceConfig = config
67                  .findMessageResourcesConfigs()[0];
68          /*
69           * check the integrity of the configuration 1. check if the underlying
70           * object representing this config has a factory object(it is (by
71           * default in MessageResource class, which is our underlying class) 2.
72           * check if the param value is specified or not
73           */
74          if (resourceConfig.getFactory() == null
75                  || resourceConfig.getParameter() == null) {
76              super.log
77                      .error("Error in message resource configuration. No parameter defined.");
78  
79              return;
80  
81          }
82  
83          /* now identify if there are comma separated values in the parameter */
84          String configParam = resourceConfig.getParameter();
85          if (configParam.indexOf(",") != -1) {
86  
87              StringTokenizer tokenizer = new StringTokenizer(configParam, ",");
88              /* loop thoruhg all the tokens and put them in file array */
89              while (tokenizer.hasMoreTokens()) {
90                  String token = tokenizer.nextToken();
91  
92                  fileNames.add(token.trim());
93  
94              }
95          } else {
96              /* there is only one file to add */
97              fileNames.add(configParam);
98          }
99  
100         /*
101          * create new CustomMessageResource object with the resource bundle
102          * names specified in the config file
103          */
104         CustomMessageResources resources = new CustomMessageResources(fileNames);
105 
106         /*
107          * putting the resources as part of the servlet context with the default
108          * key and prefix assuming that two resource files will NOT be
109          * configured with different keys. if u want to use two different
110          * resources with two different keys, then no need to use custom
111          * ActionServlet, use default Struts ActionServlet class.
112          */
113         getServletContext().setAttribute(
114                 resourceConfig.getKey() + config.getPrefix(), resources);
115 
116     }
117 
118 }