1 13 package org.ejbca.core.model; 14 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.Serializable ; 20 import java.util.Properties ; 21 22 import javax.ejb.EJBException ; 23 24 import org.apache.log4j.Logger; 25 import org.ejbca.core.model.ra.raadmin.GlobalConfiguration; 26 27 38 public class InternalResources implements Serializable { 39 40 private static final Logger log = Logger.getLogger(InternalResources.class); 41 42 51 private static final long serialVersionUID = -1001L; 52 53 protected static InternalResources instance = null; 54 55 private static Properties primaryResource = new Properties (); 56 private static Properties secondaryResource = new Properties (); 57 58 private static final String RESOURCE_LOCATION = "/intresources/intresources."; 59 60 66 protected InternalResources(boolean test) { 67 String primaryLanguage = GlobalConfiguration.PREFEREDINTERNALRESOURCES.toLowerCase(); 68 String secondaryLanguage = GlobalConfiguration.SECONDARYINTERNALRESOURCES.toLowerCase(); 69 InputStream primaryStream = null; 71 InputStream secondaryStream = null; 72 try { 73 if (test) { 74 primaryLanguage = "en"; 75 secondaryLanguage = "se"; 76 try { 77 primaryStream = new FileInputStream ("src/intresources/intresources." + primaryLanguage + ".properties"); 78 secondaryStream = new FileInputStream ("src/intresources/intresources." + secondaryLanguage + ".properties"); 79 } catch (FileNotFoundException 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 e) { 97 throw new EJBException ("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 e) { 104 log.error("Error closing internal resources language streams: ", e); 105 } 106 } 107 } 108 109 113 public static synchronized InternalResources getInstance(){ 114 if(instance == null){ 115 instance = new InternalResources(false); 116 } 117 return instance; 118 } 119 120 127 public String getLocalizedMessage(String key){ 128 Object [] params = {}; 129 return getLocalizedMessage(key,params, 0); 130 } 131 132 147 public String getLocalizedMessage(String key, Object param0){ 148 Object [] params = {param0}; 149 return getLocalizedMessage(key,params, 1); 150 } 151 152 167 public String getLocalizedMessage(String key, Object param0, Object param1){ 168 Object [] params = {param0,param1}; 169 return getLocalizedMessage(key,params, 2); 170 } 171 172 187 public String getLocalizedMessage(String key, Object param0,Object param1, Object param2){ 188 Object [] params = {param0,param1,param2}; 189 return getLocalizedMessage(key,params, 3); 190 } 191 192 207 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3){ 208 Object [] params = {param0,param1,param2,param3}; 209 return getLocalizedMessage(key,params, 4); 210 } 211 212 227 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4){ 228 Object [] params = {param0,param1,param2,param3,param4}; 229 return getLocalizedMessage(key,params, 5); 230 } 231 232 247 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4, Object param5){ 248 Object [] params = {param0,param1,param2,param3,param4,param5}; 249 return getLocalizedMessage(key,params, 6); 250 } 251 252 267 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4, Object param5, Object param6){ 268 Object [] params = {param0,param1,param2,param3,param4,param5,param6}; 269 return getLocalizedMessage(key,params, 7); 270 } 271 272 287 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4, Object param5, Object param6, Object param7){ 288 Object [] params = {param0,param1,param2,param3,param4,param5,param6,param7}; 289 return getLocalizedMessage(key,params, 8); 290 } 291 292 307 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4, Object param5, Object param6, Object param7, Object param8){ 308 Object [] params = {param0,param1,param2,param3,param4,param5,param6,param7,param8}; 309 return getLocalizedMessage(key,params, 9); 310 } 311 312 327 public String getLocalizedMessage(String key, Object param0, Object param1, Object param2, Object param3, Object param4, Object param5, Object param6, Object param7, Object param8, Object param9){ 328 Object [] params = {param0,param1,param2,param3,param4,param5,param6,param7,param8,param9}; 329 return getLocalizedMessage(key,params, 10); 330 } 331 332 339 private String getMessageString(String key){ 340 String 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 getLocalizedMessage(String key, Object [] params, int numOfParams){ 351 String localizedString = getMessageString(key); 352 for(int i=0;i<numOfParams;i++){ 353 Object obj = params[i]; 354 String param = ""; 355 if (obj != null) { 356 param = obj.toString(); 357 } 358 localizedString = localizedString.replaceAll("\\{" + i + "\\}", param); 359 } 360 localizedString = localizedString.replaceAll("\\{\\d\\}", ""); 362 363 return localizedString; 364 } 365 } 366 | Popular Tags |