KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.core.ContentLocale;
20 import com.blandware.atleap.persistence.exception.DeleteException;
21 import com.blandware.atleap.service.core.ContentLocaleManager;
22 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
23 import com.blandware.atleap.service.exception.BeanNotFoundException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.web.context.support.WebApplicationContextUtils;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * <p>Serves as client for content locale manager.
33  * Caches lists of available and default locales and default locale in system.
34  * Added in performance reasons, in purpose to avoid redundant calls of methods of ContentLocaleManager, which
35  * are controlled by Spring, because such calls often talk to persistence layer even in cases when it is not necessary.
36  * </p>
37  * <p><a HREF="LocaleUtil.java.htm"><i>View Source</i></a></p>
38  *
39  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
40  * @version $Revision: 1.5 $ $Date: 2005/11/26 14:51:26 $
41  */

42 public final class LocaleUtil {
43
44     /**
45      * Servlet context key under which instance of this class is saved
46      */

47     private static final String JavaDoc INSTANCE_KEY = "com.blandware.atleap.webapp.util.LocaleUtil.INSTANCE";
48
49     /**
50      * All locales available in system
51      */

52     private List JavaDoc availableLocales;
53
54     /**
55      * Active locales. This is sublist of available locales
56      */

57     private List JavaDoc activeLocales;
58
59     /**
60      * Default locale
61      */

62     private ContentLocale defaultLocale;
63
64     /**
65      * Content locale manager we're using
66      */

67     private ContentLocaleManager contentLocaleManager = null;
68
69     /**
70      * Creates new instance of LocaleUtil and initializes internal fields
71      *
72      * @param servletContext Servlet context
73      */

74     private LocaleUtil(ServletContext JavaDoc servletContext) {
75         ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
76         contentLocaleManager = (ContentLocaleManager) applicationContext.getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN);
77         synchronizeCache();
78     }
79
80     /**
81      * Returns instance of LocaleUtil corresponding to a given servlet context
82      *
83      * @param servletContext Servlet context
84      * @return Instance of LocaleUtil
85      */

86     public static LocaleUtil getInstance(ServletContext JavaDoc servletContext) {
87         LocaleUtil localeUtil = (LocaleUtil) servletContext.getAttribute(INSTANCE_KEY);
88         if ( localeUtil == null ) {
89             localeUtil = new LocaleUtil(servletContext);
90             servletContext.setAttribute(INSTANCE_KEY, localeUtil);
91         }
92         return localeUtil;
93     }
94
95     /**
96      * Creates new locale
97      *
98      * @param locale Content locale to create
99      * @throws BeanAlreadyExistsException if locale with the same identifier already exists
100      */

101     public void createContentLocale(ContentLocale locale) throws BeanAlreadyExistsException {
102         contentLocaleManager.createContentLocale(locale);
103         synchronizeCache();
104     }
105
106     /**
107      * Retrieves content locale with specified identifier
108      *
109      * @param identifier Identifier of locale to retrieve
110      * @return Locale with specified identifier or <code>null</code> if none found
111      */

112     public ContentLocale retrieveContentLocale(String JavaDoc identifier) {
113         return contentLocaleManager.retrieveContentLocale(identifier);
114     }
115
116     /**
117      * Updates content locale
118      *
119      * @param locale Content locale to update
120      */

121     public void updateContentLocale(ContentLocale locale) {
122         contentLocaleManager.updateContentLocale(locale);
123         synchronizeCache();
124     }
125
126     /**
127      * Deletes locale with specified identifier
128      *
129      * @param identifier Identifier of content locale to delete
130      * @throws BeanNotFoundException if locale with specified identifeir does not exist
131      * @throws DeleteException if locale could not be deleted
132      */

133     public void deleteContentLocale(String JavaDoc identifier) throws BeanNotFoundException, DeleteException {
134         contentLocaleManager.deleteContentLocale(identifier);
135         synchronizeCache();
136     }
137
138     /**
139      * Synchronizes cache with ContentLocaleManager. This method is called after
140      * creating, updating or deleting locale. Such methods are called rarely in production time,
141      * and therefore there is no performance problem
142      */

143     public void synchronizeCache() {
144         availableLocales = contentLocaleManager.getAvailableLocales();
145         activeLocales = contentLocaleManager.getActiveLocales();
146         defaultLocale = contentLocaleManager.getDefaultLocale();
147
148         // sort lists
149
Collections.sort(availableLocales);
150         Collections.sort(activeLocales);
151     }
152
153     /**
154      * Returns list of locales, available in system
155      *
156      * @return Available locales
157      */

158     public List JavaDoc getAvailableLocales() {
159         return availableLocales;
160     }
161
162     /**
163      * Returns list of active locales
164      *
165      * @return Active locales
166      */

167     public List JavaDoc getActiveLocales() {
168         return activeLocales;
169     }
170
171     /**
172      * Returns default locale
173      *
174      * @return Default locale
175      */

176     public ContentLocale getDefaultLocale() {
177         return defaultLocale;
178     }
179
180 }
181
Popular Tags