1 19 20 package com.sslexplorer.language; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.zip.ZipEntry ; 35 import java.util.zip.ZipInputStream ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import com.sslexplorer.boot.ContextHolder; 41 import com.sslexplorer.boot.Util; 42 43 56 public class LanguagePackManager { 57 58 final static Log log = LogFactory.getLog(LanguagePackManager.class); 59 60 private Map <String , LanguagePackDefinition> languagePackDefinitions; 62 private List <LanguageCategory> detectedCategories; 63 private Hashtable <String , LanguageCategory> detectedHaCategories; 64 private List <LanguageCategory> categories; 65 private Hashtable <String , LanguageCategory> haCategories; 66 private URL [] classpath; 67 68 private static LanguagePackManager instance; 70 71 75 public LanguagePackManager() { 76 super(); 77 languagePackDefinitions = new HashMap <String , LanguagePackDefinition>(); 78 categories = new ArrayList <LanguageCategory>(); 79 haCategories = new Hashtable <String , LanguageCategory>(); 80 } 81 82 91 public void registerCategory(LanguageCategory category) { 92 categories.add(category); 93 haCategories.put(category.getId(), category); 94 } 95 96 101 public void addLanguagePackDefinition(LanguagePackDefinition def) { 102 log.info("Adding new language pack '" + def.getName() + "'"); 103 if (def.getExtensionDescriptor() != null) { 104 log.info("Pack requires host version " + def.getExtensionDescriptor().getApplicationBundle().getRequiredHostVersion()); 105 } 106 languagePackDefinitions.put(def.getName(), def); 107 } 108 109 114 public void removeLanguagePack(LanguagePackDefinition def) { 115 log.info("Removing language pack '" + def.getName() + "'"); 116 languagePackDefinitions.remove(def.getName()); 117 } 118 119 124 public static LanguagePackManager getInstance() { 125 if (instance == null) { 126 instance = new LanguagePackManager(); 127 } 128 return instance; 129 } 130 131 137 public Iterator packDefinitions() { 138 return languagePackDefinitions.values().iterator(); 139 } 140 141 146 public Iterator languages() { 147 List <Language> l = new ArrayList <Language>(); 148 for (Iterator i = languagePackDefinitions.values().iterator(); i.hasNext();) { 149 LanguagePackDefinition def = (LanguagePackDefinition) i.next(); 150 for (Iterator j = def.languages(); j.hasNext();) { 151 Language lang = (Language) j.next(); 152 if (!l.contains(lang)) { 153 l.add(lang); 154 } 155 } 156 } 157 Collections.sort(l); 158 return l.iterator(); 159 } 160 161 175 public List <LanguageCategory> getCategories() throws IOException { 176 checkCategories(); 177 List <LanguageCategory> l = new ArrayList <LanguageCategory>(); 178 l.addAll(detectedCategories); 179 l.addAll(categories); 180 return l; 181 } 182 183 synchronized void checkCategories() throws IOException { 184 boolean classpathChanged = false; 185 if (classpath != null) { 186 URL [] newClasspath = ContextHolder.getContext().getContextLoaderClassPath(); 187 if (classpathDiffer(classpath, newClasspath)) { 188 classpath = newClasspath; 189 classpathChanged = true; 190 } 191 } else { 192 classpath = ContextHolder.getContext().getContextLoaderClassPath(); 193 } 194 if (detectedCategories == null || classpathChanged) { 195 log.info("Scanning classpath for default message resources"); 196 detectedCategories = new ArrayList <LanguageCategory>(); 197 detectedHaCategories = new Hashtable <String , LanguageCategory>(); 198 for (int i = 0; i < classpath.length; i++) { 199 log.debug("Scanning " + classpath[i]); 200 if (classpath[i].getProtocol().equals("file")) { 201 if (classpath[i].getPath().endsWith(".jar")) { 202 addFileJarCategory(classpath[i]); 203 } else { 204 addFileDirectoryCategory(classpath[i]); 205 } 206 } 207 } 208 } 209 } 210 211 boolean classpathDiffer(URL [] classpath, URL [] newClasspath) { 212 if (classpath.length != newClasspath.length) { 213 return true; 214 } 215 for (int i = 0; i < classpath.length; i++) { 216 if (!classpath[i].equals(newClasspath[i])) { 217 return true; 218 } 219 } 220 return false; 221 } 222 223 238 public Hashtable getHaCategories() throws IOException { 239 checkCategories(); 240 Hashtable <String , LanguageCategory> all = new Hashtable <String , LanguageCategory>(); 241 all.putAll(detectedHaCategories); 242 all.putAll(haCategories); 243 return all; 244 } 245 246 void addFileDirectoryCategory(URL url) throws IOException { 247 File p = new File (url.getPath()); 248 List <File > files = new ArrayList <File >(); 249 findFile(p, "ApplicationResources.properties", files); 250 for (Iterator it = files.iterator(); it.hasNext();) { 251 File f = (File ) it.next(); 252 String path = f.getAbsolutePath().substring(p.getAbsolutePath().length() + 1).replace("\\", "/"); 253 InputStream in = null; 254 try { 255 String name = path; 256 String resourceBundleId = path; 257 int idx = name.lastIndexOf('/'); 258 if (idx != -1) { 259 resourceBundleId = name.substring(0, idx).replace('/', '.'); 260 name = name.substring(idx + 1); 261 } 262 in = new FileInputStream (f); 263 LanguageCategory category = new LanguageCategory(in, url, path, resourceBundleId); 264 if (!detectedHaCategories.containsKey(category.getId())) { 265 detectedCategories.add(category); 266 detectedHaCategories.put(category.getId(), category); 267 } 268 } 269 finally { 270 Util.closeStream(in); 271 } 272 } 273 274 } 275 276 void addFileJarCategory(URL url) throws IOException { 277 278 InputStream in = null; 279 try { 280 File f = new File (url.getPath()); 281 if (!f.exists()) { 282 return; 283 } 284 in = new FileInputStream (url.getPath()); 285 ZipInputStream zin = new ZipInputStream (in); 286 while (true) { 287 ZipEntry entry = zin.getNextEntry(); 288 if (entry == null) { 289 break; 290 } 291 String path = entry.getName(); 292 String name = path; 293 String resourceBundleId = path; 294 int idx = name.lastIndexOf('/'); 295 if (idx != -1) { 296 resourceBundleId = name.substring(0, idx).replace('/', '.'); 297 ; 298 name = name.substring(idx + 1); 299 } 300 if (name.equals("ApplicationResources.properties")) { 301 LanguageCategory category = new LanguageCategory(zin, url, path, resourceBundleId); 302 if (!detectedHaCategories.containsKey(category.getId())) { 303 detectedCategories.add(category); 304 detectedHaCategories.put(category.getId(), category); 305 } 306 } 307 } 308 } finally { 309 Util.closeStream(in); 310 } 311 312 } 313 314 321 public LanguagePackDefinition getLanguagePack(String name) { 322 return (LanguagePackDefinition)languagePackDefinitions.get(name); 323 } 324 325 void findFile(File dir, String name, List <File > files) { 326 File [] l = dir.listFiles(); 327 for (int i = 0; l != null && i < l.length; i++) { 328 if (l[i].isDirectory()) { 329 findFile(l[i], name, files); 330 } else if (l[i].isFile() && l[i].getName().equals(name)) { 331 files.add(l[i]); 332 } 333 } 334 } 335 } 336 | Popular Tags |