KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > configuration > WebLanguages


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  
14 package org.ejbca.ui.web.admin.configuration;
15
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.ejbca.core.model.InternalResources;
23 import org.ejbca.core.model.ra.raadmin.GlobalConfiguration;
24
25
26 /**
27  * An class interpreting the langage properties files. I contains one method getText that returns
28  * the presented text in the users prefered language.
29  *
30  * @author Philip Vendil
31  * @version $Id: WebLanguages.java,v 1.4.2.1 2007/01/24 08:47:41 anatom Exp $
32  */

33 public class WebLanguages implements java.io.Serializable JavaDoc {
34     
35     /** Internal localization of logs and errors */
36     private static final InternalResources intres = InternalResources.getInstance();
37
38     /** Construtor used to load static content. An instance must be declared with this constructor before
39      * any WebLanguage object can be used. */

40     /** Special constructor used by Ejbca web bean */
41     private void init(ServletContext JavaDoc servletContext, GlobalConfiguration globalconfiguration) throws IOException JavaDoc {
42         if(languages == null){
43             // Get available languages.
44
availablelanguages=null;
45             
46             String JavaDoc availablelanguagesstring = globalconfiguration.getAvailableLanguagesAsString();
47             availablelanguages = availablelanguagesstring.split(",");
48             for(int i=0; i < availablelanguages.length;i++){
49                 availablelanguages[i] = availablelanguages[i].trim().toUpperCase();
50             }
51             // Load availabe languages
52
languages = new LanguageProperties[availablelanguages.length];
53             for(int i = 0; i < availablelanguages.length; i++){
54                 languages[i] = new LanguageProperties();
55                 String JavaDoc propsfile = "/" + globalconfiguration.getLanguagePath() + "/"
56                 + globalconfiguration.getLanguageFilename() + "."
57                 + availablelanguages[i].toLowerCase() +".properties";
58                 
59                 InputStream JavaDoc is = null;
60                 if (servletContext != null) {
61                     is = servletContext.getResourceAsStream(propsfile);
62                 } else {
63                     is = this.getClass().getResourceAsStream(propsfile);
64                 }
65                 if(is==null) {
66                     //if not available as stream, try it as a file
67
is = new FileInputStream JavaDoc("/tmp"+propsfile);
68                 }
69                 languages[i].load(is);
70             }
71         }
72     }
73
74     public WebLanguages(ServletContext JavaDoc servletContext, GlobalConfiguration globalconfiguration, int preferedlang, int secondarylang) throws IOException JavaDoc {
75         init(servletContext, globalconfiguration);
76         this.userspreferedlanguage=preferedlang;
77         this.userssecondarylanguage=secondarylang;
78     }
79
80
81     /** The main method that looks up the template text in the users prefered language. */
82     public String JavaDoc getText(String JavaDoc template){
83       String JavaDoc returnvalue = null;
84       try{
85         returnvalue= languages[userspreferedlanguage].getProperty(template);
86         if(returnvalue == null){
87           returnvalue= languages[userssecondarylanguage].getProperty(template);
88         }
89         if(returnvalue == null){
90             returnvalue= intres.getLocalizedMessage(template);
91         }
92       }catch(java.lang.NullPointerException JavaDoc e){}
93       if(returnvalue == null)
94         returnvalue= "No text available";
95       return returnvalue;
96     }
97
98     /* Returns a textstring containing the available languages */
99     public String JavaDoc[] getAvailableLanguages(){
100       return availablelanguages;
101     }
102
103
104     // Protected fields
105
private int userspreferedlanguage;
106     private int userssecondarylanguage;
107
108     private String JavaDoc[] availablelanguages;
109     private LanguageProperties[] languages = null;
110
111 }
112
Popular Tags