blob: 354153cc7eca96831e1273961279941cee884fb3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26#warn This file is preprocessed before being compiled
27
28package sun.util;
29
30import java.util.Arrays;
31import java.util.Collection;
32import java.util.Collections;
33import java.util.HashSet;
34import java.util.List;
35import java.util.Locale;
36import java.util.ResourceBundle;
37import java.util.ResourceBundle.Control;
38
39/**
40 * This is a convenient class for loading some of internal resources faster
41 * if they are built with Resources.gmk defined in J2SE workspace. Also,
42 * they have to be in class file format.
43 *
44 * "LOCALE_LIST" will be replaced at built time by a list of locales we
45 * defined in Defs.gmk. We want to exclude these locales from search to
46 * gain better performance. For example, since we know if the resource
47 * is built with Resources.gmk, they are not going to provide basename_en.class
48 * & basename_en_US.class resources, in that case, continuing searching them
49 * is expensive. By excluding them from the candidate locale list, these
50 * resources won't be searched.
51 *
52 * @since 1.6.
53 */
54public class CoreResourceBundleControl extends ResourceBundle.Control {
55 /* the candidate locale list to search */
56 private final Collection<Locale> excludedJDKLocales;
57 /* singlton instance of the resource bundle control. */
58 private static CoreResourceBundleControl resourceBundleControlInstance =
59 new CoreResourceBundleControl();
60
61 protected CoreResourceBundleControl() {
62 excludedJDKLocales = Arrays.asList(#LOCALE_LIST#);
63 }
64
65 /**
66 * This method is to provide a customized ResourceBundle.Control to speed
67 * up the search of resources in JDK.
68 *
69 * @return the instance of resource bundle control.
70 */
71 public static CoreResourceBundleControl getRBControlInstance() {
72 return resourceBundleControlInstance;
73 }
74
75 /**
76 * This method is to provide a customized ResourceBundle.Control to speed
77 * up the search of resources in JDK, with the bundle's package name check.
78 *
79 * @param bundleName bundle name to check
80 * @return the instance of resource bundle control if the bundle is JDK's,
81 * otherwise returns null.
82 */
83 public static CoreResourceBundleControl getRBControlInstance(String bundleName) {
84 if (bundleName.startsWith("com.sun.") ||
85 bundleName.startsWith("java.") ||
86 bundleName.startsWith("javax.") ||
87 bundleName.startsWith("sun.")) {
88 return resourceBundleControlInstance;
89 } else {
90 return null;
91 }
92 }
93
94 /**
95 * @returns a list of candidate locales to search from.
96 * @exception NullPointerException if baseName or locale is null.
97 */
98 @Override
99 public List<Locale> getCandidateLocales(String baseName, Locale locale) {
100 List<Locale> candidates = super.getCandidateLocales(baseName, locale);
101 candidates.removeAll(excludedJDKLocales);
102 return candidates;
103 }
104
105 /**
106 * @ returns TTL_DONT_CACHE so that ResourceBundle instance won't be cached.
107 * User of this CoreResourceBundleControl should probably maintain a hard reference
108 * to the ResourceBundle object themselves.
109 */
110 @Override
111 public long getTimeToLive(String baseName, Locale locale) {
112 return ResourceBundle.Control.TTL_DONT_CACHE;
113 }
114}