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.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Properties;
26  
27  import org.apache.struts.util.MessageResources;
28  
29  /***
30   * <p>
31   * This class is a Custom Message Resource implementation for Struts. This class
32   * searches through a series of resource files passed to it via the constructor
33   * to obtain the localised value for a key passed. If the key cannot be
34   * resolved, it prints a warning message to the logger output.
35   * </p>
36   * 
37   * <pre>
38   * &lt;message-resources null=&quot;false&quot;
39   *     parameter=&quot;net.sf.oness.common.webapp.messages.Messages,net.sf.oness.common.webapp.messages.OtherMessages&quot;/&gt;
40   * </pre>
41   * 
42   * @todo improve this class
43   * 
44   * @author Carlos Sanchez
45   * @version $Revision: 1.1 $
46   */
47  public class CustomMessageResources extends MessageResources {
48  
49      /***
50       * hashmap holding the resource bundle for which we have already loaded the
51       * values
52       */
53      protected HashMap bundlesLoaded = new HashMap();
54  
55      /*** hashmap for holding ther resource bundle key and value */
56      protected HashMap resourceMap = new HashMap();
57  
58      /*** the array containing the names of the resource bundles to look for */
59      List bundles = new ArrayList();
60  
61      /***
62       * Constructor accepting a list of resource bundle files.
63       * 
64       * @param configFiles
65       *            A List of resouce bundle files.
66       */
67      public CustomMessageResources(List configFiles) {
68          super(null, null);
69          this.bundles = configFiles;
70      }
71  
72      /***
73       * @see org.apache.struts.util.MessageResources#getMessage(java.util.Locale,
74       *      java.lang.String)
75       */
76      public String getMessage(Locale locale, String key) {
77  
78          final String methodSig = "getMessage";
79          String message = null;
80          /* initialise the variables we need */
81          String localeKey = "";
82          if (!super.defaultLocale.equals(locale)) {
83              this.localeKey(locale);
84          }
85  
86          /* go through all the resource bundle files and load them */
87          for (int i = 0; i < bundles.size(); i++) {
88              String config = (String) bundles.get(i);
89              this.loadBundle(localeKey, config);
90              /* now look if the message can be found */
91              message = (String) resourceMap.get(key);
92              /* message found, so get out of the loop */
93              if (message != null) {
94                  break;
95              } else {
96                  System.out
97                          .println("COULD NOT FIND RESOURCE FILE VALUE FOR KEY: "
98                                  + key);
99              }
100         }
101         return message;
102     }
103 
104     /***
105      * Loads the resource bundle based on the locale.
106      * 
107      * @param localeKey
108      *            The locale
109      * @param config
110      *            The resource file.
111      */
112     protected void loadBundle(String localeKey, String config) {
113         final String methodSig = "loadBundle";
114         String fileName = null;
115         InputStream inStream = null;
116         Properties props = new Properties();
117         /* first check if this bundle is alreay loaded previously */
118         if (bundlesLoaded.get(config) != null) {
119             return;
120         }
121 
122         /* otherwise mark this bundle being loaded */
123         this.bundlesLoaded.put(config, localeKey);
124 
125         /* Set up to load the property resource for this locale key, if we can */
126         fileName = config.replace('.', '/');
127         if (localeKey.length() > 0) {
128             fileName += "_" + localeKey;
129         }
130         fileName += ".properties";
131 
132         /* obtain a class loader */
133         ClassLoader classLoader = Thread.currentThread()
134                 .getContextClassLoader();
135         /*
136          * if this classloader is null, use the class loader used to load this
137          * class
138          */
139         if (classLoader == null) {
140             classLoader = this.getClass().getClassLoader();
141         }
142 
143         /* load the file as Stream */
144         inStream = classLoader.getResourceAsStream(fileName);
145 
146         /* check if the Stream in not null */
147         if (inStream != null) {
148             /* populate the properties from this input Stream */
149             try {
150                 props.load(inStream);
151             } catch (IOException e) {
152                 // TODO Auto-generated catch block
153                 e.printStackTrace();
154             } finally {
155                 try {
156                     inStream.close();
157                 } catch (IOException e1) {
158                     // TODO Auto-generated catch block
159                     e1.printStackTrace();
160                 }
161             }
162 
163             /*
164              * now retrieve the key value pairs from the resource file and put
165              * it in the map
166              */
167             synchronized (this.resourceMap) {
168                 Iterator iterator = props.keySet().iterator();
169 
170                 while (iterator.hasNext()) {
171                     String key = (String) iterator.next();
172                     /* String messageKey = this.messageKey(localeKey,key); */
173                     String value = props.getProperty(key);
174 
175                     this.resourceMap.put(key, value);
176                 }
177             }
178 
179         } else {
180             System.out.println("COULD NOT LOAD RESOURCE FILE: " + fileName);
181         }
182 
183     }
184 
185 }