View Javadoc
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 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 org.apache.juddi.webconsole.resources;
17  
18  import java.util.HashMap;
19  import java.util.Locale;
20  import java.util.Map;
21  import java.util.MissingResourceException;
22  import java.util.ResourceBundle;
23  import javax.servlet.http.HttpSession;
24  
25  /**
26   * This a resource loader for specific locales for internationalization,
27   * provides some basic caching to prevent round trip disk access
28   *
29   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
30   */
31  public class ResourceLoader {
32  
33          private static Map map = new HashMap();
34  
35          /**
36           * returns a localized string in the locale defined within
37           * session.getAttribute("locale") or in the default locale, en
38           *
39           * @param session
40           * @param key
41           * @return a localized string
42           * @throws IllegalArgumentException if the key is null
43           * @throws MissingResourceException if the resource bundle can't be
44           * found
45           */
46          public static String GetResource(HttpSession session, String key) throws MissingResourceException {
47                  if (key == null) {
48                          throw new IllegalArgumentException("key");
49                  }
50                  String locale = "en";
51                  if (session != null) {
52                          locale = (String) session.getAttribute("locale");
53                  }
54                  if (locale==null)
55                          locale = "en";
56                  return GetResource(locale, key);
57          }
58  
59          /**
60           * returns a localized string in the locale defined within locale or in
61           * the default locale, en
62           *
63           * @param locale
64           * @param key
65           * @return a localized string
66           * @throws IllegalArgumentException if the key is null
67           * @throws MissingResourceException if the resource bundle can't be
68           * found
69           */
70          public static String GetResource(String locale, String key) throws MissingResourceException {
71                  if (key == null) {
72                          throw new IllegalArgumentException("key");
73                  }
74  
75                  ResourceBundle bundle = (ResourceBundle) map.get(locale);
76                  if (bundle == null) {
77                          bundle = ResourceBundle.getBundle("org.apache.juddi.webconsole.resources.web", new Locale(locale));
78                          map.put(locale, bundle);
79                  }
80                  try {
81                          return bundle.getString(key.trim());
82                  } catch (Exception ex) {
83                          return "key " + key + " not found " + ex.getMessage();
84                  }
85          }
86  }