KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > InternalResources


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 package org.ejbca.core.model;
14
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.ejb.EJBException JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.model.ra.raadmin.GlobalConfiguration;
26
27 /**
28  * Class managing internal localization of texts such as notification messages
29  * and log comments.
30  *
31  * If fetched the resource files from the src/intlocalization directory and
32  * is included in the file ejbca-ejb.jar
33  *
34  * @author Philip Vendil 2006 sep 24
35  *
36  * @version $Id: InternalResources.java,v 1.6.2.1 2007/02/02 18:13:16 anatom Exp $
37  */

38 public class InternalResources implements Serializable JavaDoc {
39     
40     private static final Logger log = Logger.getLogger(InternalResources.class);
41     
42     /**
43      * Determines if a de-serialized file is compatible with this class.
44      *
45      * Maintainers must change this value if and only if the new version
46      * of this class is not compatible with old versions. See Sun docs
47      * for <a HREF=http://java.sun.com/products/jdk/1.1/docs/guide
48      * /serialization/spec/version.doc.html> details. </a>
49      *
50      */

51     private static final long serialVersionUID = -1001L;
52
53     protected static InternalResources instance = null;
54     
55     private static Properties JavaDoc primaryResource = new Properties JavaDoc();
56     private static Properties JavaDoc secondaryResource = new Properties JavaDoc();
57     
58     private static final String JavaDoc RESOURCE_LOCATION = "/intresources/intresources.";
59     
60     /**
61      * Method used to setup the Internal Resource management.
62      *
63      * @param globalConfiguration used to retrieve the internal language
64      * of the application, configured in the System Configuration.
65      */

66     protected InternalResources(boolean test) {
67         String JavaDoc primaryLanguage = GlobalConfiguration.PREFEREDINTERNALRESOURCES.toLowerCase();
68         String JavaDoc secondaryLanguage = GlobalConfiguration.SECONDARYINTERNALRESOURCES.toLowerCase();
69         // The test flag is defined when called from test code (junit)
70
InputStream JavaDoc primaryStream = null;
71         InputStream JavaDoc secondaryStream = null;
72         try {
73             if (test) {
74                 primaryLanguage = "en";
75                 secondaryLanguage = "se";
76                 try {
77                     primaryStream = new FileInputStream JavaDoc("src/intresources/intresources." + primaryLanguage + ".properties");
78                     secondaryStream = new FileInputStream JavaDoc("src/intresources/intresources." + secondaryLanguage + ".properties");
79                 } catch (FileNotFoundException JavaDoc e) {}
80             } else {
81                 primaryStream = InternalResources.class.getResourceAsStream(RESOURCE_LOCATION + primaryLanguage + ".properties");
82                 secondaryStream = InternalResources.class.getResourceAsStream(RESOURCE_LOCATION + secondaryLanguage + ".properties");
83             }
84
85             try {
86                 if (primaryStream != null) {
87                     primaryResource.load(primaryStream);
88                 } else {
89                     log.error("primaryResourse == null");
90                 }
91                 if (secondaryStream != null) {
92                     secondaryResource.load(secondaryStream);
93                 } else {
94                     log.error("secondaryResource == null");
95                 }
96             } catch (IOException JavaDoc e) {
97                 throw new EJBException JavaDoc("Error reading internal resourcefile", e);
98             }
99         } finally {
100             try {
101                 if (primaryStream != null) primaryStream.close();
102                 if (secondaryStream != null) secondaryStream.close();
103             } catch (IOException JavaDoc e) {
104                 log.error("Error closing internal resources language streams: ", e);
105             }
106         }
107     }
108     
109     /**
110      * Metod that returs a instance of the InternalResources
111      * might be null if load() haven't been called before this method.
112      */

113     public static synchronized InternalResources getInstance(){
114         if(instance == null){
115             instance = new InternalResources(false);
116         }
117         return instance;
118     }
119     
120     /**
121      * Method returning the localized message for the given resource key.
122      *
123      * It first looks up in the primary language then in the secondary
124      * If not found in any of the resource file "No text available" is returned.
125      *
126      */

127     public String JavaDoc getLocalizedMessage(String JavaDoc key){
128         Object JavaDoc[] params = {};
129         return getLocalizedMessage(key,params, 0);
130     }
131
132     /**
133      * Method returning the localized message for the given resource key.
134      *
135      * It first looks up in the primary language then in the secondary
136      * If not found in any of the resource file "no text" is returned.
137      *
138      * @param key is the key searched for in the resource files
139      * @param paramX indicaties the parameter that will be replaced by {X} in
140      * the language resource, a maximum of 10 parameters can be given.
141      *
142      * Ex Calling the method with key = TEST and param0 set to "hi"
143      * and the resource file have "TEST = messages is {0}" will
144      * result in the string "message is hi".
145      *
146      */

147     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0){
148         Object JavaDoc[] params = {param0};
149         return getLocalizedMessage(key,params, 1);
150     }
151     
152     /**
153      * Method returning the localized message for the given resource key.
154      *
155      * It first looks up in the primary language then in the secondary
156      * If not found in any of the resource file "no text" is returned.
157      *
158      * @param key is the key searched for in the resource files
159      * @param paramX indicaties the parameter that will be replaced by {X} in
160      * the language resource, a maximum of 10 parameters can be given.
161      *
162      * Ex Calling the method with key = TEST and param0 set to "hi"
163      * and the resource file have "TEST = messages is {0}" will
164      * result in the string "message is hi".
165      *
166      */

167     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1){
168         Object JavaDoc[] params = {param0,param1};
169         return getLocalizedMessage(key,params, 2);
170     }
171     
172     /**
173      * Method returning the localized message for the given resource key.
174      *
175      * It first looks up in the primary language then in the secondary
176      * If not found in any of the resource file "no text" is returned.
177      *
178      * @param key is the key searched for in the resource files
179      * @param paramX indicaties the parameter that will be replaced by {X} in
180      * the language resource, a maximum of 10 parameters can be given.
181      *
182      * Ex Calling the method with key = TEST and param0 set to "hi"
183      * and the resource file have "TEST = messages is {1}" will
184      * result in the string "message is hi".
185      *
186      */

187     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0,Object JavaDoc param1, Object JavaDoc param2){
188         Object JavaDoc[] params = {param0,param1,param2};
189         return getLocalizedMessage(key,params, 3);
190     }
191     
192     /**
193      * Method returning the localized message for the given resource key.
194      *
195      * It first looks up in the primary language then in the secondary
196      * If not found in any of the resource file "no text" is returned.
197      *
198      * @param key is the key searched for in the resource files
199      * @param paramX indicaties the parameter that will be replaced by {X} in
200      * the language resource, a maximum of 10 parameters can be given.
201      *
202      * Ex Calling the method with key = TEST and param0 set to "hi"
203      * and the resource file have "TEST = messages is {1}" will
204      * result in the string "message is hi".
205      *
206      */

207     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3){
208         Object JavaDoc[] params = {param0,param1,param2,param3};
209         return getLocalizedMessage(key,params, 4);
210     }
211     
212     /**
213      * Method returning the localized message for the given resource key.
214      *
215      * It first looks up in the primary language then in the secondary
216      * If not found in any of the resource file "no text" is returned.
217      *
218      * @param key is the key searched for in the resource files
219      * @param paramX indicaties the parameter that will be replaced by {X} in
220      * the language resource, a maximum of 10 parameters can be given.
221      *
222      * Ex Calling the method with key = TEST and param0 set to "hi"
223      * and the resource file have "TEST = messages is {1}" will
224      * result in the string "message is hi".
225      *
226      */

227     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4){
228         Object JavaDoc[] params = {param0,param1,param2,param3,param4};
229         return getLocalizedMessage(key,params, 5);
230     }
231     
232     /**
233      * Method returning the localized message for the given resource key.
234      *
235      * It first looks up in the primary language then in the secondary
236      * If not found in any of the resource file "no text" is returned.
237      *
238      * @param key is the key searched for in the resource files
239      * @param paramX indicaties the parameter that will be replaced by {X} in
240      * the language resource, a maximum of 10 parameters can be given.
241      *
242      * Ex Calling the method with key = TEST and param0 set to "hi"
243      * and the resource file have "TEST = messages is {1}" will
244      * result in the string "message is hi".
245      *
246      */

247     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5){
248         Object JavaDoc[] params = {param0,param1,param2,param3,param4,param5};
249         return getLocalizedMessage(key,params, 6);
250     }
251     
252     /**
253      * Method returning the localized message for the given resource key.
254      *
255      * It first looks up in the primary language then in the secondary
256      * If not found in any of the resource file "no text" is returned.
257      *
258      * @param key is the key searched for in the resource files
259      * @param paramX indicaties the parameter that will be replaced by {X} in
260      * the language resource, a maximum of 10 parameters can be given.
261      *
262      * Ex Calling the method with key = TEST and param0 set to "hi"
263      * and the resource file have "TEST = messages is {1}" will
264      * result in the string "message is hi".
265      *
266      */

267     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5, Object JavaDoc param6){
268         Object JavaDoc[] params = {param0,param1,param2,param3,param4,param5,param6};
269         return getLocalizedMessage(key,params, 7);
270     }
271     
272     /**
273      * Method returning the localized message for the given resource key.
274      *
275      * It first looks up in the primary language then in the secondary
276      * If not found in any of the resource file "no text" is returned.
277      *
278      * @param key is the key searched for in the resource files
279      * @param paramX indicaties the parameter that will be replaced by {X} in
280      * the language resource, a maximum of 10 parameters can be given.
281      *
282      * Ex Calling the method with key = TEST and param0 set to "hi"
283      * and the resource file have "TEST = messages is {1}" will
284      * result in the string "message is hi".
285      *
286      */

287     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5, Object JavaDoc param6, Object JavaDoc param7){
288         Object JavaDoc[] params = {param0,param1,param2,param3,param4,param5,param6,param7};
289         return getLocalizedMessage(key,params, 8);
290     }
291     
292     /**
293      * Method returning the localized message for the given resource key.
294      *
295      * It first looks up in the primary language then in the secondary
296      * If not found in any of the resource file "no text" is returned.
297      *
298      * @param key is the key searched for in the resource files
299      * @param paramX indicaties the parameter that will be replaced by {X} in
300      * the language resource, a maximum of 10 parameters can be given.
301      *
302      * Ex Calling the method with key = TEST and param0 set to "hi"
303      * and the resource file have "TEST = messages is {1}" will
304      * result in the string "message is hi".
305      *
306      */

307     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5, Object JavaDoc param6, Object JavaDoc param7, Object JavaDoc param8){
308         Object JavaDoc[] params = {param0,param1,param2,param3,param4,param5,param6,param7,param8};
309         return getLocalizedMessage(key,params, 9);
310     }
311     
312     /**
313      * Method returning the localized message for the given resource key.
314      *
315      * It first looks up in the primary language then in the secondary
316      * If not found in any of the resource file "no text" is returned.
317      *
318      * @param key is the key searched for in the resource files
319      * @param paramX indicaties the parameter that will be replaced by {X} in
320      * the language resource, a maximum of 10 parameters can be given.
321      *
322      * Ex Calling the method with key = TEST and param0 set to "hi"
323      * and the resource file have "TEST = messages is {0}" will
324      * result in the string "message is hi".
325      *
326      */

327     public String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc param0, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3, Object JavaDoc param4, Object JavaDoc param5, Object JavaDoc param6, Object JavaDoc param7, Object JavaDoc param8, Object JavaDoc param9){
328         Object JavaDoc[] params = {param0,param1,param2,param3,param4,param5,param6,param7,param8,param9};
329         return getLocalizedMessage(key,params, 10);
330     }
331     
332     /**
333      * Method returning the message from the resource file for the given resource key.
334      *
335      * It first looks up in the primary language then in the secondary
336      * If not found in any of the resource file "No text available" is returned.
337      *
338      */

339     private String JavaDoc getMessageString(String JavaDoc key){
340         String JavaDoc retval = primaryResource.getProperty(key);
341         if(retval == null){
342             retval = secondaryResource.getProperty(key);
343         }
344         if(retval == null){
345             retval = "No text available";
346         }
347         return retval.trim();
348     }
349     
350     private String JavaDoc getLocalizedMessage(String JavaDoc key, Object JavaDoc[] params, int numOfParams){
351         String JavaDoc localizedString = getMessageString(key);
352         for(int i=0;i<numOfParams;i++){
353             Object JavaDoc obj = params[i];
354             String JavaDoc param = "";
355             if (obj != null) {
356                 param = obj.toString();
357             }
358             localizedString = localizedString.replaceAll("\\{" + i + "\\}", param);
359         }
360         // Remove all remaining {} if any
361
localizedString = localizedString.replaceAll("\\{\\d\\}", "");
362         
363         return localizedString;
364     }
365 }
366
Popular Tags