1/*2 * Copyright 2001-2008 The Apache Software Foundation.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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16package org.apache.juddi.webconsole.resources;
1718import java.util.HashMap;
19import java.util.Locale;
20import java.util.Map;
21import java.util.MissingResourceException;
22import java.util.ResourceBundle;
23import javax.servlet.http.HttpSession;
2425/**26 * This a resource loader for specific locales for internationalization,27 * provides some basic caching to prevent round trip disk access28 *29 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>30 */31publicclassResourceLoader {
3233privatestatic Map map = new HashMap();
3435/**36 * returns a localized string in the locale defined within37 * session.getAttribute("locale") or in the default locale, en38 *39 * @param session40 * @param key41 * @return a localized string42 * @throws IllegalArgumentException if the key is null43 * @throws MissingResourceException if the resource bundle can't be44 * found45 */46publicstatic String GetResource(HttpSession session, String key) throws MissingResourceException {
47if (key == null) {
48thrownew IllegalArgumentException("key");
49 }
50 String locale = "en";
51if (session != null) {
52 locale = (String) session.getAttribute("locale");
53 }
54if (locale==null)
55 locale = "en";
56return GetResource(locale, key);
57 }
5859/**60 * returns a localized string in the locale defined within locale or in61 * the default locale, en62 *63 * @param locale64 * @param key65 * @return a localized string66 * @throws IllegalArgumentException if the key is null67 * @throws MissingResourceException if the resource bundle can't be68 * found69 */70publicstatic String GetResource(String locale, String key) throws MissingResourceException {
71if (key == null) {
72thrownew IllegalArgumentException("key");
73 }
7475 ResourceBundle bundle = (ResourceBundle) map.get(locale);
76if (bundle == null) {
77 bundle = ResourceBundle.getBundle("org.apache.juddi.webconsole.resources.web", new Locale(locale));
78 map.put(locale, bundle);
79 }
80try {
81return bundle.getString(key.trim());
82 } catch (Exception ex) {
83return"key " + key + " not found " + ex.getMessage();
84 }
85 }
86 }