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 *16 */17package org.apache.juddi.adminconsole.resources;
1819import java.util.HashMap;
20import java.util.Locale;
21import java.util.Map;
22import java.util.MissingResourceException;
23import java.util.ResourceBundle;
24import javax.servlet.http.HttpSession;
2526/**27 * This a resource loader for specific locales for internationalization,28 * provides some basic caching to prevent round trip disk access29 *30 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>31 */32publicclassResourceLoader {
3334privatestatic Map map = new HashMap();
3536/**37 * returns a localized string in the locale defined within38 * session.getAttribute("locale") or in the default locale, en39 *40 * @param session41 * @param key42 * @return a localized string43 * @throws IllegalArgumentException if the key is null44 * @throws MissingResourceException if the resource bundle can't be45 * found46 */47publicstatic String GetResource(HttpSession session, String key) throws MissingResourceException {
48if (key == null) {
49thrownew IllegalArgumentException("key");
50 }
51 String locale = (String) session.getAttribute("locale");
52return GetResource(locale, key);
53 }
5455/**56 * returns a localized string in the locale defined within locale or in57 * the default locale, en58 *59 * @param locale60 * @param key61 * @return a localized string62 * @throws IllegalArgumentException if the key is null63 * @throws MissingResourceException if the resource bundle can't be64 * found65 */66publicstatic String GetResource(String locale, String key) throws MissingResourceException {
67if (key == null) {
68thrownew IllegalArgumentException("key");
69 }
7071 ResourceBundle bundle = (ResourceBundle) map.get(locale);
72if (bundle == null) {
73 bundle = ResourceBundle.getBundle("org.apache.juddi.adminconsole.resources.web", new Locale(locale));
74 map.put(locale, bundle);
75 }
76try {
77return bundle.getString(key.trim());
78 } catch (Exception ex) {
79return"key " + key + " not found " + ex.getMessage();
80 }
81 }
82 }