KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > resources > impl > ResourceBundleServiceImpl


1 /*******************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. * Please look
3  * at license.txt in info directory for more license detail. *
4  ******************************************************************************/

5 package org.exoplatform.services.resources.impl;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.net.URL JavaDoc;
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 JavaDoc NO_SUCH_DATA = new Object JavaDoc();
30
31     static private String JavaDoc[] MAPPING = {
32             "org/exoplatform/services/resources/impl/ResourceBundleDescriptionImpl.hbm.xml",
33             "org/exoplatform/services/resources/impl/ResourceBundleDataImpl.hbm.xml" };
34
35     final static String JavaDoc[] 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 JavaDoc {
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 JavaDoc name, Locale locale) {
66         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
67         return getResourceBundle(name, locale, cl);
68     }
69
70     public ResourceBundle getResourceBundle(String JavaDoc name, Locale locale,
71             ClassLoader JavaDoc cl) {
72         if (!isPersistedResource(name))
73             return ResourceBundle.getBundle(name, locale, cl);
74         String JavaDoc id = name + "_" + locale.getLanguage();
75         try {
76             Object JavaDoc 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 JavaDoc ex) {
83         }
84
85         try {
86             ResourceBundle res = null;
87             String JavaDoc 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 JavaDoc ex) {
115             log_.error("Error: " + id, ex);
116         }
117         return null;
118     }
119
120     public ResourceBundle getResourceBundle(String JavaDoc[] name, Locale locale) {
121         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
122         return getResourceBundle(name, locale, cl);
123     }
124
125     public ResourceBundle getResourceBundle(String JavaDoc[] name, Locale locale, ClassLoader JavaDoc cl) {
126         StringBuffer JavaDoc idBuf = new StringBuffer JavaDoc("merge:");
127         for (int i = 0; i < name.length; i++) {
128             idBuf.append(name[i]).append("_");
129         }
130         idBuf.append(locale.getLanguage());
131         String JavaDoc 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 JavaDoc ex) {
148             log_.error("Cannot load and merge the bundle: " + id, ex);
149         }
150         return null;
151     }
152
153     public ResourceBundleData getResourceBundleData(String JavaDoc name)
154             throws Exception JavaDoc {
155         return (ResourceBundleDataImpl) hService_.findOne(
156                 ResourceBundleDataImpl.class, name);
157     }
158
159     public ResourceBundleData removeResourceBundleData(String JavaDoc id) {
160         ResourceBundleDataImpl data = null;
161         try {
162             data = (ResourceBundleDataImpl) hService_.remove(
163                     ResourceBundleDataImpl.class, id);
164             cache_.remove(data.getId());
165         } catch (Exception JavaDoc ex) {
166             log_.error("Remove Error: " + id, ex);
167         }
168         return data;
169     }
170
171     public PageList findResourceDescriptions(Query q) throws Exception JavaDoc {
172         String JavaDoc 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 JavaDoc {
183         hService_.save(data);
184         cache_.remove(data.getId());
185     }
186
187     private ResourceBundle getResourceBundleFromDb(String JavaDoc id,
188             ResourceBundle parent) throws Exception JavaDoc {
189         Session session = hService_.openSession();
190         ResourceBundleData data = (ResourceBundleDataImpl) session.get(
191                 ResourceBundleDataImpl.class, id);
192         if (data != null) {
193             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(data.getData().getBytes());
194             ResourceBundle res = new ExoResourceBundle(is, parent);
195             return res;
196         }
197         return null;
198     }
199
200     private void loadDefaultResourceBundles(String JavaDoc baseName, ClassLoader JavaDoc cl) {
201         String JavaDoc name = baseName.replace('.', '/');
202         String JavaDoc fileName = null;
203         try {
204             Collection localeConfigs = localeService_.getLocalConfigs();
205             String JavaDoc defaultLang = localeService_.getDefaultLocaleConfig()
206                     .getLanguage();
207             for (Iterator iter = localeConfigs.iterator(); iter.hasNext();) {
208                 LocaleConfig localeConfig = (LocaleConfig) iter.next();
209                 String JavaDoc language = localeConfig.getLanguage();
210                 if (defaultLang.equals(language)) {
211                     fileName = name + ".properties";
212                 } else {
213                     fileName = name + "_" + language + ".properties";
214                 }
215                 URL JavaDoc url = cl.getResource(fileName);
216                 if (url != null) {
217                     InputStream JavaDoc 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 JavaDoc(buf, "UTF-8"));
223                     saveResourceBundle(data);
224                 }
225             }
226         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc name) {
247         if (persistedPackages_ == null)
248             return false;
249         for (int i = 0; i < persistedPackages_.size(); i++) {
250             String JavaDoc pack = (String JavaDoc) persistedPackages_.get(i);
251             if (name.startsWith(pack))
252                 return true;
253         }
254         return false;
255     }
256 }
257
Popular Tags