author | mchung |
Thu, 21 Dec 2017 15:18:49 -0800 | |
changeset 48524 | b6fc9a193661 |
parent 47216 | 71c04702a3d5 |
child 54206 | 003cc64366da |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
45439
5673d77a787b
8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents:
43712
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36511 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.util.spi; |
|
27 |
||
28 |
import java.util.Locale; |
|
29 |
import java.util.ResourceBundle; |
|
30 |
||
31 |
/** |
|
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
32 |
* {@code ResourceBundleProvider} is a service provider interface for |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
33 |
* resource bundles. It is used by |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
34 |
* {@link ResourceBundle#getBundle(String) ResourceBundle.getBundle} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
35 |
* factory methods to locate and load the service providers that are deployed as |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
36 |
* modules via {@link java.util.ServiceLoader ServiceLoader}. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
37 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
38 |
* <h3>Developing resource bundle services</h3> |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
39 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
40 |
* A service for a resource bundle of a given <em>{@code baseName}</em> must have |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
41 |
* a fully-qualified class name of the form: |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
42 |
* <blockquote> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
43 |
* {@code <package of baseName> + ".spi." + <simple name of baseName> + "Provider"} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
44 |
* </blockquote> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
45 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
46 |
* The service type is in a {@code spi} subpackage as it may be packaged in |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
47 |
* a module separate from the resource bundle providers. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
48 |
* For example, the service for a resource bundle named |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
49 |
* {@code com.example.app.MyResources} must be |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
50 |
* {@code com.example.app.spi.MyResourcesProvider}: |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
51 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
52 |
* <blockquote><pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
53 |
* {@code package com.example.app.spi; |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
54 |
* public interface MyResourcesProvider extends ResourceBundleProvider { |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
55 |
* } |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
56 |
* }</pre></blockquote> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
57 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
58 |
* <h3>Deploying resource bundle service providers</h3> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
59 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
60 |
* Resource bundles can be deployed in one or more service providers |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
61 |
* in modules. For example, a provider for a service |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
62 |
* named "{@code com.example.app.spi.MyResourcesProvider}" |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
63 |
* has the following implementation class: |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
64 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
65 |
* <blockquote><pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
66 |
* {@code import com.example.app.spi.MyResourcesProvider; |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
67 |
* class MyResourcesProviderImpl extends AbstractResourceBundleProvider |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
68 |
* implements MyResourcesProvider |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
69 |
* { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
70 |
* public MyResourcesProviderImpl() { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
71 |
* super("java.properties"); |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
72 |
* } |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
73 |
* // this provider maps the resource bundle to per-language package |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
74 |
* protected String toBundleName(String baseName, Locale locale) { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
75 |
* return "p." + locale.getLanguage() + "." + baseName; |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
76 |
* } |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
77 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
78 |
* public ResourceBundle getBundle(String baseName, Locale locale) { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
79 |
* // this module only provides bundles in French |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
80 |
* if (locale.equals(Locale.FRENCH)) { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
81 |
* return super.getBundle(baseName, locale); |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
82 |
* } |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
83 |
* // otherwise return null |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
84 |
* return null; |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
85 |
* } |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
86 |
* }}</pre></blockquote> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
87 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
88 |
* This example provides "{@code com.example.app.MyResources}" |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
89 |
* resource bundle of the French locale. Traditionally resource bundles of |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
90 |
* all locales are packaged in the same package as the resource bundle base name. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
91 |
* When deploying resource bundles in more than one modules and two modules |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
92 |
* containing a package of the same name, <em>split package</em>, |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
93 |
* is not supported, resource bundles in each module can be packaged in |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
94 |
* a different package as shown in this example where this provider packages |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
95 |
* the resource bundles in per-language package, i.e. {@code com.example.app.fr} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
96 |
* for French locale. |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
97 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
98 |
* <p> A provider can provide more than one services, each of which is a service |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
99 |
* for a resource bundle of a different base name. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
100 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
101 |
* <p>{@link AbstractResourceBundleProvider} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
102 |
* provides the basic implementation for {@code ResourceBundleProvider} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
103 |
* and a subclass can override the {@link |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
104 |
* AbstractResourceBundleProvider#toBundleName(String, Locale) toBundleName} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
105 |
* method to return a provider-specific location of the resource to be loaded, |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
106 |
* for example, per-language package. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
107 |
* A provider can override {@link #getBundle ResourceBundleProvider.getBundle} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
108 |
* method for example to only search the known supported locales or |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
109 |
* return resource bundles in other formats such as XML. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
110 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
111 |
* <p>The module declaration of this provider module specifies the following |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
112 |
* directive: |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
113 |
* <pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
114 |
* provides com.example.app.spi.MyResourcesProvider with com.example.impl.MyResourcesProviderImpl; |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
115 |
* </pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
116 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
117 |
* <h3><a id="obtain-resource-bundle">Obtaining resource bundles from providers</a></h3> |
36511 | 118 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
119 |
* The module declaration of the <em>consumer module</em> that calls one of the |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
120 |
* {@code ResourceBundle.getBundle} factory methods to obtain a resource |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
121 |
* bundle from service providers must specify the following directive: |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
122 |
* <pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
123 |
* uses com.example.app.spi.MyResourcesProvider; |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
124 |
* </pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
125 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
126 |
* {@link ResourceBundle#getBundle(String, Locale) |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
127 |
* ResourceBundle.getBundle("com.example.app.MyResource", locale)} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
128 |
* locates and loads the providers for {@code com.example.app.spi.MyResourcesProvider} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
129 |
* service and then invokes {@link #getBundle(String, Locale) |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
130 |
* ResourceBundleProvider.getBundle("com.example.app.MyResource", locale)} to |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
131 |
* find the resource bundle of the given base name and locale. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
132 |
* If the consumer module is a resource bundle service provider for |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
133 |
* {@code com.example.app.spi.MyResourcesProvider}, {@code ResourceBundle.getBundle} |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
134 |
* will locate resource bundles only from service providers. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
135 |
* Otherwise, {@code ResourceBundle.getBundle} may continue the search of |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
136 |
* the resource bundle in other modules and class path per the specification |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
137 |
* of the {@code ResourceBundle.getBundle} method being called. |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
138 |
* |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
139 |
* @see AbstractResourceBundleProvider |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
140 |
* @see <a href="../ResourceBundle.html#resource-bundle-modules"> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
141 |
* Resource Bundles and Named Modules</a> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
142 |
* @see java.util.ServiceLoader |
36511 | 143 |
* @since 9 |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
38782
diff
changeset
|
144 |
* @spec JPMS |
36511 | 145 |
*/ |
146 |
public interface ResourceBundleProvider { |
|
147 |
/** |
|
148 |
* Returns a {@code ResourceBundle} for the given bundle name and locale. |
|
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
149 |
* This method returns {@code null} if there is no {@code ResourceBundle} |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
150 |
* found for the given parameters. |
36511 | 151 |
* |
152 |
* |
|
153 |
* @param baseName |
|
154 |
* the base bundle name of the resource bundle, a fully |
|
155 |
* qualified class name |
|
156 |
* @param locale |
|
157 |
* the locale for which the resource bundle should be loaded |
|
158 |
* @return the ResourceBundle created for the given parameters, or null if no |
|
159 |
* {@code ResourceBundle} for the given parameters is found |
|
160 |
*/ |
|
161 |
public ResourceBundle getBundle(String baseName, Locale locale); |
|
162 |
} |