KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > ApplicationResources


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.ApplicationResourceStorage;
20 import com.blandware.atleap.model.core.ContentLocale;
21 import com.blandware.atleap.service.core.ApplicationResourceManager;
22 import com.blandware.atleap.service.exception.BeanNotFoundException;
23 import org.apache.struts.util.RequestUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.web.context.support.WebApplicationContextUtils;
28
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.Set JavaDoc;
35
36 /**
37  * <p>Facade class to get messages from application resources storage.<br />
38  * Caches all messages and serves as bridge between client and application resources manager.
39  * </p>
40  * <p><a HREF="ApplicationResourceFacade.java.htm"><i>View Source</i></a></p>
41  *
42  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
43  * @version $Revision: 1.8 $ $Date: 2005/11/26 14:51:26 $
44  * @see com.blandware.atleap.common.util.ApplicationResourceStorage
45  * @see com.blandware.atleap.service.core.ApplicationResourceManager
46  */

47 public final class ApplicationResources {
48
49     protected transient final Log log = LogFactory.getLog(ApplicationResources.class);
50
51     /**
52      * Servlet context key under which instance of this class is saved
53      */

54     private static final String JavaDoc INSTANCE_KEY = "com.blandware.atleap.webapp.util.ApplicationResources.INSTANCE";
55
56     /**
57      * Application resource storage
58      */

59     private ApplicationResourceStorage applicationResourceStorage;
60
61     /**
62      * Application resource manager we're using
63      */

64     private ApplicationResourceManager applicationResourceManager;
65
66     /**
67      * Servlet context of our application
68      */

69     private ServletContext JavaDoc servletContext;
70
71     /**
72      * Creates new instance of ApplicationResources and initializes internal fields
73      *
74      * @param servletContext the servlet context
75      */

76     private ApplicationResources(ServletContext JavaDoc servletContext) {
77         this.servletContext = servletContext;
78         ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
79         applicationResourceManager = (ApplicationResourceManager) applicationContext.getBean(Constants.APPLICATION_RESOURCE_MANAGER_BEAN);
80         loadResources();
81     }
82
83     /**
84      * Returns instance of GlobalPropertyManager
85      *
86      * @param servletContext the servlet context for which to obtain instance
87      * @return Instance of GlobalPropertyManager
88      */

89     public static ApplicationResources getInstance(ServletContext JavaDoc servletContext) {
90         ApplicationResources applicationResources = (ApplicationResources) servletContext.getAttribute(INSTANCE_KEY);
91         if ( applicationResources == null ) {
92             applicationResources = new ApplicationResources(servletContext);
93             servletContext.setAttribute(INSTANCE_KEY, applicationResources);
94         }
95         return applicationResources;
96     }
97
98     /**
99      * Updates resource in cache and calls to delegate in order to update resource in persistence layer
100      *
101      * @param localeIdentifier Identifier of locale
102      * @param key Key to associate value with
103      * @param value Value to associate with key
104      * @param version Version of application resource
105      * @throws com.blandware.atleap.service.exception.BeanNotFoundException
106      * if resource couldn't be found
107      */

108     public synchronized void updateResource(String JavaDoc localeIdentifier, String JavaDoc key, String JavaDoc value, Long JavaDoc version) throws BeanNotFoundException {
109         applicationResourceManager.updateApplicationResource(localeIdentifier, key, value, version);
110         applicationResourceStorage.putMessage(new Locale(localeIdentifier), key, value);
111     }
112
113     /**
114      * Loads bundle for specified locale and merges with existent
115      *
116      * @param locale Content locale to load bundle for
117      */

118     public void loadBundle(ContentLocale locale) {
119         ApplicationResourceStorage.Bundle bundle = applicationResourceManager.loadBundle(locale);
120         applicationResourceStorage.mergeBundle(bundle);
121     }
122
123     /**
124      * Drops bundle for locale with specified identifier
125      *
126      * @param localeIdentifier Identifier of locale to flush bundle
127      */

128     public void dropBundle(String JavaDoc localeIdentifier) {
129         applicationResourceStorage.dropBundle(new Locale(localeIdentifier));
130         applicationResourceManager.dropBundle(localeIdentifier);
131     }
132
133     // ~ Methods for retrieving message from bundle
134

135     /**
136      * Returns message for specified key and locale that is stored in session
137      *
138      * @param request The request by which user locale is found
139      * @param key Key to get message for
140      * @return Message associated with specified key and locale that is stored in session
141      */

142     public String JavaDoc getMessage(HttpServletRequest JavaDoc request, String JavaDoc key) {
143         return getMessage(request, key, null);
144     }
145
146     /**
147      * Returns message for specified key and locale that is stored in session
148      *
149      * @param request The request by which user locale is found
150      * @param key Key to get message for
151      * @param args Arguments to insert into placeholders
152      * @return Message for specified key and locale stored that is stored in session
153      */

154     public String JavaDoc getMessage(HttpServletRequest JavaDoc request, String JavaDoc key, Object JavaDoc[] args) {
155         Locale locale = RequestUtils.getUserLocale(request, null);
156         return getMessage(locale, key, args);
157     }
158
159     /**
160      * Returns message for specified key and given locale
161      *
162      * @param locale The locale to get message for
163      * @param key Key to get message for
164      * @return Message associated with specified key and given locale
165      */

166     public String JavaDoc getMessage(Locale locale, String JavaDoc key) {
167         return getMessage(locale, key, null);
168     }
169
170     /**
171      * Returns message for specified key and given locale
172      *
173      * @param locale The locale to get message for
174      * @param key Key to get message for
175      * @param args Arguments to insert into placeholders
176      * @return Message associated with specified key and given locale
177      */

178     public String JavaDoc getMessage(Locale locale, String JavaDoc key, Object JavaDoc[] args) {
179         String JavaDoc msg = applicationResourceStorage.getMessage(locale, key, args);
180         if ( msg == null ) {
181             String JavaDoc language = locale.getLanguage();
182             // get message for default locale
183
LocaleUtil localeUtil = LocaleUtil.getInstance(servletContext);
184             ContentLocale defaultLocale = localeUtil.getDefaultLocale();
185             if ( !defaultLocale.getIdentifier().equalsIgnoreCase(language) ) {
186                 msg = applicationResourceStorage.getMessage(new Locale(defaultLocale.getIdentifier()), key, args);
187             }
188
189             // if still null, search for the rest of locales
190
if ( msg == null ) {
191                 List JavaDoc activeLocales = localeUtil.getActiveLocales();
192                 for ( Iterator JavaDoc i = activeLocales.iterator(); i.hasNext() && msg == null; ) {
193                     ContentLocale contentLocale = (ContentLocale) i.next();
194
195                     // skip search for specified locale and for default locale because search for them has already been performed
196
String JavaDoc identifier = contentLocale.getIdentifier();
197                     if ( !identifier.equalsIgnoreCase(language) && !contentLocale.getDefaultInstance().booleanValue() ) {
198                         msg = applicationResourceStorage.getMessage(new Locale(contentLocale.getIdentifier()), key, args);
199                     }
200                 }
201             }
202         }
203         return msg;
204     }
205
206     /**
207      * Returns set of keys, stored in bundle, associated with specified locale
208      *
209      * @param contentLocale Locale to get bundle keys
210      * @return Set of keys, stored in bundle, associated with specified locale
211      */

212     public Set JavaDoc getKeys(ContentLocale contentLocale) {
213         String JavaDoc localeIdentifier = contentLocale.getIdentifier();
214         Locale locale = new Locale(localeIdentifier);
215         return applicationResourceStorage.getKeys(locale);
216     }
217
218     /**
219      * Returns set of keys, stored in bundle, associated with default locale
220      *
221      * @return Set of keys, stored in bundle, associated with default locale
222      */

223     public Set JavaDoc getKeys() {
224         ContentLocale defaultLocale = LocaleUtil.getInstance(servletContext).getDefaultLocale();
225         return getKeys(defaultLocale);
226     }
227
228     /**
229      * Reloads all resources from file system and override dynamic values
230      */

231     public void reloadResources() {
232         applicationResourceStorage = applicationResourceManager.loadResources(true);
233     }
234
235     /**
236      * Reloads all resources from file system and do not override dynamic values
237      */

238     public void loadResources() {
239         applicationResourceStorage = applicationResourceManager.loadResources(false);
240         if ( log.isInfoEnabled() ) {
241             log.info("Application resources synchronized");
242         }
243     }
244
245 }
246
Popular Tags