1 5 package org.exoplatform.services.resources.impl; 6 7 import java.io.ByteArrayInputStream ; 8 import java.io.InputStream ; 9 import java.net.URL ; 10 import java.util.*; 11 import net.sf.hibernate.Session; 12 import org.apache.commons.logging.Log; 13 import org.exoplatform.Constants; 14 import org.exoplatform.commons.utils.IOUtil; 15 import org.exoplatform.commons.utils.MapResourceBundle; 16 import org.exoplatform.commons.utils.PageList; 17 import org.exoplatform.container.configuration.ConfigurationManager; 18 import org.exoplatform.container.configuration.ServiceConfiguration; 19 import org.exoplatform.container.configuration.ValuesParam; 20 import org.exoplatform.services.cache.CacheService; 21 import org.exoplatform.services.cache.ExoCache; 22 import org.exoplatform.services.database.DBObjectPageList; 23 import org.exoplatform.services.database.HibernateService; 24 import org.exoplatform.services.database.ObjectQuery; 25 import org.exoplatform.services.log.LogService; 26 import org.exoplatform.services.resources.*; 27 28 public class ResourceBundleServiceImpl implements ResourceBundleService { 29 private static Object NO_SUCH_DATA = new Object (); 30 31 static private String [] MAPPING = { 32 "org/exoplatform/services/resources/impl/ResourceBundleDescriptionImpl.hbm.xml", 33 "org/exoplatform/services/resources/impl/ResourceBundleDataImpl.hbm.xml" }; 34 35 final static String [] DEFAULT_USER = { "admin", "demo", "default", 36 Constants.DEFAUL_PORTAL_OWNER }; 37 38 private HibernateService hService_; 39 40 private Log log_; 41 42 private ExoCache cache_; 43 44 private List persistedPackages_; 45 46 private LocaleConfigService localeService_; 47 48 public ResourceBundleServiceImpl(HibernateService service, 49 LocaleConfigService localeService, LogService lservice, 50 CacheService cService, ConfigurationManager confService) 51 throws Exception { 52 log_ = lservice.getLog("org.exoplatform.services.resources"); 53 cache_ = cService.getCacheInstance(getClass().getName()); 54 hService_ = service; 55 localeService_ = localeService; 56 hService_.addMappingFiles(MAPPING); 57 ServiceConfiguration sconf = confService 58 .getServiceConfiguration(ResourceBundleService.class); 59 if (sconf != null) { 60 ValuesParam param = sconf.getValuesParam("persisted.packages"); 61 persistedPackages_ = param.getValues(); 62 } 63 } 64 65 public ResourceBundle getResourceBundle(String name, Locale locale) { 66 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 67 return getResourceBundle(name, locale, cl); 68 } 69 70 public ResourceBundle getResourceBundle(String name, Locale locale, 71 ClassLoader cl) { 72 if (!isPersistedResource(name)) 73 return ResourceBundle.getBundle(name, locale, cl); 74 String id = name + "_" + locale.getLanguage(); 75 try { 76 Object obj = cache_.get(id); 77 if (obj != null) { 78 if (obj == NO_SUCH_DATA) 79 return null; 80 return (ResourceBundle) obj; 81 } 82 } catch (Exception ex) { 83 } 84 85 try { 86 ResourceBundle res = null; 87 String rootId = name + "_" 88 + localeService_.getDefaultLocaleConfig().getLanguage(); 89 ResourceBundle parent = getResourceBundleFromDb(rootId, null); 90 if (parent != null) { 91 res = getResourceBundleFromDb(id, parent); 92 if (res == null) 93 res = parent; 94 cache_.put(id, res); 95 return res; 96 } 97 98 if (lookForDefaultResources(name)) { 99 loadDefaultResourceBundles(name, cl); 100 } else { 101 cache_.put(id, NO_SUCH_DATA); 102 return null; 103 } 104 105 parent = getResourceBundleFromDb(rootId, null); 106 if (parent != null) { 107 res = getResourceBundleFromDb(id, parent); 108 if (res == null) 109 res = parent; 110 cache_.put(id, res); 111 return res; 112 } 113 cache_.put(id, NO_SUCH_DATA); 114 } catch (Exception ex) { 115 log_.error("Error: " + id, ex); 116 } 117 return null; 118 } 119 120 public ResourceBundle getResourceBundle(String [] name, Locale locale) { 121 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 122 return getResourceBundle(name, locale, cl); 123 } 124 125 public ResourceBundle getResourceBundle(String [] name, Locale locale, ClassLoader cl) { 126 StringBuffer idBuf = new StringBuffer ("merge:"); 127 for (int i = 0; i < name.length; i++) { 128 idBuf.append(name[i]).append("_"); 129 } 130 idBuf.append(locale.getLanguage()); 131 String id = idBuf.toString(); 132 try { 133 ResourceBundle res = null; 134 res = (ResourceBundle) cache_.get(id); 135 if (res != null) 136 return res; 137 138 MapResourceBundle outputBundled = new MapResourceBundle(locale); 139 for (int i = 0; i < name.length; i++) { 140 ResourceBundle temp = getResourceBundle(name[i], locale, cl); 141 if (temp != null) 142 outputBundled.merge(temp); 143 } 144 outputBundled.resolveDependencies(); 145 cache_.put(id, outputBundled); 146 return outputBundled; 147 } catch (Exception ex) { 148 log_.error("Cannot load and merge the bundle: " + id, ex); 149 } 150 return null; 151 } 152 153 public ResourceBundleData getResourceBundleData(String name) 154 throws Exception { 155 return (ResourceBundleDataImpl) hService_.findOne( 156 ResourceBundleDataImpl.class, name); 157 } 158 159 public ResourceBundleData removeResourceBundleData(String id) { 160 ResourceBundleDataImpl data = null; 161 try { 162 data = (ResourceBundleDataImpl) hService_.remove( 163 ResourceBundleDataImpl.class, id); 164 cache_.remove(data.getId()); 165 } catch (Exception ex) { 166 log_.error("Remove Error: " + id, ex); 167 } 168 return data; 169 } 170 171 public PageList findResourceDescriptions(Query q) throws Exception { 172 String name = q.getName(); 173 if (name == null || name.length() == 0) 174 name = "%"; 175 ObjectQuery oq = new ObjectQuery(ResourceBundleDescriptionImpl.class); 176 oq.addLIKE("name", name); 177 oq.addLIKE("language", q.getLanguage()); 178 oq.setDescOrderBy("name"); 179 return new DBObjectPageList(hService_, oq); 180 } 181 182 public void saveResourceBundle(ResourceBundleData data) throws Exception { 183 hService_.save(data); 184 cache_.remove(data.getId()); 185 } 186 187 private ResourceBundle getResourceBundleFromDb(String id, 188 ResourceBundle parent) throws Exception { 189 Session session = hService_.openSession(); 190 ResourceBundleData data = (ResourceBundleDataImpl) session.get( 191 ResourceBundleDataImpl.class, id); 192 if (data != null) { 193 InputStream is = new ByteArrayInputStream (data.getData().getBytes()); 194 ResourceBundle res = new ExoResourceBundle(is, parent); 195 return res; 196 } 197 return null; 198 } 199 200 private void loadDefaultResourceBundles(String baseName, ClassLoader cl) { 201 String name = baseName.replace('.', '/'); 202 String fileName = null; 203 try { 204 Collection localeConfigs = localeService_.getLocalConfigs(); 205 String defaultLang = localeService_.getDefaultLocaleConfig() 206 .getLanguage(); 207 for (Iterator iter = localeConfigs.iterator(); iter.hasNext();) { 208 LocaleConfig localeConfig = (LocaleConfig) iter.next(); 209 String language = localeConfig.getLanguage(); 210 if (defaultLang.equals(language)) { 211 fileName = name + ".properties"; 212 } else { 213 fileName = name + "_" + language + ".properties"; 214 } 215 URL url = cl.getResource(fileName); 216 if (url != null) { 217 InputStream is = url.openStream(); 218 byte buf[] = IOUtil.getStreamContentAsBytes(is); 219 ResourceBundleDataImpl data = new ResourceBundleDataImpl(); 220 data.setName(baseName); 221 data.setLanguage(language); 222 data.setData(new String (buf, "UTF-8")); 223 saveResourceBundle(data); 224 } 225 } 226 } catch (Exception ex) { 227 log_.error("Error while reading the file: " + fileName, ex); 228 } 229 } 230 231 public ResourceBundleData createResourceBundleDataInstance() { 232 return new ResourceBundleDataImpl(); 233 } 234 235 private boolean lookForDefaultResources(String name) { 236 if (name.startsWith("locale.users.")) { 237 for (int i = 0; i < DEFAULT_USER.length; i++) { 238 if (name.endsWith(DEFAULT_USER[i])) 239 return true; 240 } 241 return false; 242 } 243 return true; 244 } 245 246 private boolean isPersistedResource(String name) { 247 if (persistedPackages_ == null) 248 return false; 249 for (int i = 0; i < persistedPackages_.size(); i++) { 250 String pack = (String ) persistedPackages_.get(i); 251 if (name.startsWith(pack)) 252 return true; 253 } 254 return false; 255 } 256 } 257 | Popular Tags |