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