Skip to content

Commit 73380f3

Browse files
authored
Move Plexus Component to JSR330 (#11)
1 parent fba00ba commit 73380f3

File tree

5 files changed

+124
-187
lines changed

5 files changed

+124
-187
lines changed

pom.xml

+18-12
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,32 @@
3232
</distributionManagement>
3333

3434
<properties>
35+
<slf4jVersion>1.7.36</slf4jVersion>
3536
<project.build.outputTimestamp>2020-01-20T18:52:37Z</project.build.outputTimestamp>
3637
</properties>
3738

3839
<dependencies>
3940
<dependency>
40-
<groupId>org.codehaus.plexus</groupId>
41-
<artifactId>plexus-container-default</artifactId>
42-
<version>2.1.1</version>
41+
<groupId>javax.inject</groupId>
42+
<artifactId>javax.inject</artifactId>
43+
<version>1</version>
4344
</dependency>
4445
<dependency>
45-
<groupId>org.codehaus.plexus</groupId>
46-
<artifactId>plexus-component-annotations</artifactId>
47-
<version>2.2.0</version>
48-
<scope>provided</scope>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-api</artifactId>
48+
<version>${slf4jVersion}</version>
4949
</dependency>
5050
<dependency>
51-
<groupId>org.codehaus.plexus</groupId>
52-
<artifactId>plexus-utils</artifactId>
53-
<version>4.0.0</version>
51+
<groupId>org.slf4j</groupId>
52+
<artifactId>slf4j-simple</artifactId>
53+
<version>${slf4jVersion}</version>
54+
<scope>test</scope>
5455
</dependency>
5556
<dependency>
5657
<groupId>org.codehaus.plexus</groupId>
57-
<artifactId>plexus-xml</artifactId>
58-
<version>3.0.0</version>
58+
<artifactId>plexus-testing</artifactId>
59+
<version>1.3.0</version>
60+
<scope>test</scope>
5961
</dependency>
6062
</dependencies>
6163

@@ -79,6 +81,10 @@
7981
</execution>
8082
</executions>
8183
</plugin>
84+
<plugin>
85+
<groupId>org.eclipse.sisu</groupId>
86+
<artifactId>sisu-maven-plugin</artifactId>
87+
</plugin>
8288
</plugins>
8389
</build>
8490
</project>

src/main/java/org/codehaus/plexus/i18n/DefaultI18N.java

+56-102
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,63 @@
1616
* limitations under the License.
1717
*/
1818

19-
import java.lang.reflect.Field;
19+
import javax.inject.Named;
20+
import javax.inject.Singleton;
21+
2022
import java.text.MessageFormat;
2123
import java.util.HashMap;
2224
import java.util.Locale;
2325
import java.util.Map;
2426
import java.util.MissingResourceException;
2527
import java.util.ResourceBundle;
2628

27-
import org.codehaus.plexus.component.annotations.Component;
28-
import org.codehaus.plexus.logging.AbstractLogEnabled;
29-
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
30-
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
31-
import org.codehaus.plexus.util.StringUtils;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
3231

3332
/**
3433
*
3534
*/
36-
@Component(role = I18N.class)
37-
public class DefaultI18N extends AbstractLogEnabled implements I18N, Initializable {
35+
@Named
36+
@Singleton
37+
public class DefaultI18N implements I18N {
38+
39+
private final Logger log = LoggerFactory.getLogger(DefaultI18N.class);
3840
private static final Object[] NO_ARGS = new Object[0];
3941

40-
private HashMap bundles;
42+
private Map<String, Map<Locale, ResourceBundle>> bundles;
4143

4244
private String[] bundleNames;
4345

4446
private String defaultBundleName;
4547

46-
private Locale defaultLocale = Locale.getDefault();
47-
48-
private String defaultLanguage = Locale.getDefault().getLanguage();
49-
50-
private String defaultCountry = Locale.getDefault().getCountry();
51-
5248
private boolean devMode;
5349

50+
public DefaultI18N() {
51+
initialize();
52+
}
53+
54+
public DefaultI18N(String[] bundleNames) {
55+
this.bundleNames = bundleNames != null ? bundleNames.clone() : new String[0];
56+
initialize();
57+
}
5458
// ----------------------------------------------------------------------
5559
// Accessors
5660
// ----------------------------------------------------------------------
5761

5862
public String getDefaultLanguage() {
59-
return defaultLanguage;
63+
return Locale.getDefault().getLanguage();
6064
}
6165

6266
public String getDefaultCountry() {
63-
return defaultCountry;
67+
return Locale.getDefault().getCountry();
6468
}
6569

6670
public String getDefaultBundleName() {
6771
return defaultBundleName;
6872
}
6973

7074
public String[] getBundleNames() {
71-
return (String[]) bundleNames.clone();
75+
return bundleNames.clone();
7276
}
7377

7478
public ResourceBundle getBundle() {
@@ -111,25 +115,7 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
111115
// ----------------------------------------------------------------------
112116

113117
if (devMode) {
114-
try {
115-
Class klass = ResourceBundle.getBundle(bundleName).getClass().getSuperclass();
116-
117-
Field field = klass.getDeclaredField("cacheList");
118-
119-
field.setAccessible(true);
120-
121-
// SoftCache cache = (SoftCache) field.get( null );
122-
//
123-
// cache.clear();
124-
125-
Object cache = field.get(null);
126-
127-
cache.getClass().getDeclaredMethod("clear", null).invoke(cache, null);
128-
129-
field.setAccessible(false);
130-
} catch (Exception e) {
131-
// Intentional
132-
}
118+
ResourceBundle.clearCache();
133119
}
134120

135121
if (locale == null) {
@@ -139,13 +125,12 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
139125
// Find/retrieve/cache bundle.
140126
ResourceBundle rb;
141127

142-
HashMap bundlesByLocale = (HashMap) bundles.get(bundleName);
128+
Map<Locale, ResourceBundle> bundlesByLocale = bundles.get(bundleName);
143129

144130
if (bundlesByLocale != null) {
145131
// Cache of bundles by locale for the named bundle exists.
146132
// Check the cache for a bundle corresponding to locale.
147-
rb = (ResourceBundle) bundlesByLocale.get(locale);
148-
133+
rb = bundlesByLocale.get(locale);
149134
if (rb == null) {
150135
// Not yet cached.
151136
rb = cacheBundle(bundleName, locale);
@@ -161,16 +146,15 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
161146
* @see I18N#getLocale(String)
162147
*/
163148
public Locale getLocale(String header) {
164-
if (!StringUtils.isEmpty(header)) {
149+
if (header != null && !header.isEmpty()) {
165150
I18NTokenizer tok = new I18NTokenizer(header);
166-
167151
if (tok.hasNext()) {
168-
return (Locale) tok.next();
152+
return tok.next();
169153
}
170154
}
171155

172156
// Couldn't parse locale.
173-
return defaultLocale;
157+
return Locale.getDefault();
174158
}
175159

176160
public String getString(String key) {
@@ -187,7 +171,6 @@ public String getString(String key, Locale locale) {
187171
*/
188172
public String getString(String bundleName, Locale locale, String key) {
189173
String value;
190-
191174
if (locale == null) {
192175
locale = getLocale(null);
193176
}
@@ -198,11 +181,8 @@ public String getString(String bundleName, Locale locale, String key) {
198181
value = getStringOrNull(rb, key);
199182

200183
// Look for text in list of default bundles.
201-
if (value == null && bundleNames.length > 0) {
202-
String name;
203-
for (int i = 0; i < bundleNames.length; i++) {
204-
name = bundleNames[i];
205-
184+
if (value == null) {
185+
for (String name : bundleNames) {
206186
if (!name.equals(bundleName)) {
207187
rb = getBundle(name, locale);
208188

@@ -218,27 +198,20 @@ public String getString(String bundleName, Locale locale, String key) {
218198
}
219199

220200
if (value == null) {
221-
String loc = locale.toString();
222-
223-
String mesg =
224-
"Noticed missing resource: " + "bundleName=" + bundleName + ", locale=" + loc + ", key=" + key;
225-
226-
getLogger().debug(mesg);
227-
201+
log.debug("Noticed missing resource: bundleName={}, locale={}, key={}", bundleName, locale, key);
228202
// Just send back the key, we don't need to throw an exception.
229-
230203
value = key;
231204
}
232205

233206
return value;
234207
}
235208

236209
public String format(String key, Object arg1) {
237-
return format(defaultBundleName, defaultLocale, key, new Object[] {arg1});
210+
return format(defaultBundleName, Locale.getDefault(), key, new Object[] {arg1});
238211
}
239212

240213
public String format(String key, Object arg1, Object arg2) {
241-
return format(defaultBundleName, defaultLocale, key, new Object[] {arg1, arg2});
214+
return format(defaultBundleName, Locale.getDefault(), key, new Object[] {arg1, arg2});
242215
}
243216

244217
/**
@@ -275,28 +248,15 @@ public String format(String bundleName, Locale locale, String key, Object[] args
275248
if (args == null) {
276249
args = NO_ARGS;
277250
}
278-
279-
// FIXME: after switching to JDK 1.4, it will be possible to clean
280-
// this up by providing the Locale along with the string in the
281-
// constructor to MessageFormat. Until 1.4, the following workaround
282-
// is required for constructing the format with the appropriate locale:
283-
MessageFormat messageFormat = new MessageFormat("");
284-
messageFormat.setLocale(locale);
285-
messageFormat.applyPattern(value);
286-
287-
return messageFormat.format(args);
251+
return new MessageFormat(value, locale).format(args);
288252
}
289253

290254
/**
291255
* Called the first time the Service is used.
292256
*/
293-
public void initialize() throws InitializationException {
294-
bundles = new HashMap();
295-
296-
defaultLocale = new Locale(defaultLanguage, defaultCountry);
297-
257+
public void initialize() {
258+
bundles = new HashMap<>();
298259
initializeBundleNames();
299-
300260
if ("true".equals(System.getProperty("PLEXUS_DEV_MODE"))) {
301261
devMode = true;
302262
}
@@ -307,8 +267,7 @@ public void initialize() throws InitializationException {
307267
// ----------------------------------------------------------------------
308268

309269
protected void initializeBundleNames() {
310-
// System.err.println("cfg=" + getConfiguration());
311-
if (defaultBundleName != null && defaultBundleName.length() > 0) {
270+
if (defaultBundleName != null && !defaultBundleName.isEmpty()) {
312271
// Using old-style single bundle name property.
313272
if (bundleNames == null || bundleNames.length <= 0) {
314273
bundleNames = new String[] {defaultBundleName};
@@ -333,18 +292,15 @@ protected void initializeBundleNames() {
333292
* @throws MissingResourceException Bundle not found.
334293
*/
335294
private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale) throws MissingResourceException {
336-
HashMap bundlesByLocale = (HashMap) bundles.get(bundleName);
337-
338-
ResourceBundle rb = (bundlesByLocale == null ? null : (ResourceBundle) bundlesByLocale.get(locale));
295+
Map<Locale, ResourceBundle> bundlesByLocale = bundles.get(bundleName);
339296

297+
ResourceBundle rb = (bundlesByLocale == null ? null : bundlesByLocale.get(locale));
340298
if (rb == null) {
341-
bundlesByLocale = (bundlesByLocale == null ? new HashMap(3) : new HashMap(bundlesByLocale));
342-
299+
bundlesByLocale = (bundlesByLocale == null ? new HashMap<>(3) : new HashMap<>(bundlesByLocale));
343300
try {
344301
rb = ResourceBundle.getBundle(bundleName, locale);
345302
} catch (MissingResourceException e) {
346303
rb = findBundleByLocale(bundleName, locale, bundlesByLocale);
347-
348304
if (rb == null) {
349305
throw (MissingResourceException) e.fillInStackTrace();
350306
}
@@ -353,11 +309,8 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
353309
if (rb != null) {
354310
// Cache bundle.
355311
bundlesByLocale.put(rb.getLocale(), rb);
356-
357-
HashMap bundlesByName = new HashMap(bundles);
358-
312+
Map<String, Map<Locale, ResourceBundle>> bundlesByName = new HashMap<>(bundles);
359313
bundlesByName.put(bundleName, bundlesByLocale);
360-
361314
this.bundles = bundlesByName;
362315
}
363316
}
@@ -380,30 +333,31 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
380333
* <p>Since we're really just guessing at possible bundles to use,
381334
* we don't ever throw <code>MissingResourceException</code>.</p>
382335
*/
383-
private ResourceBundle findBundleByLocale(String bundleName, Locale locale, Map bundlesByLocale) {
336+
private ResourceBundle findBundleByLocale(
337+
String bundleName, Locale locale, Map<Locale, ResourceBundle> bundlesByLocale) {
384338
ResourceBundle rb = null;
385339

386-
if (!StringUtils.isNotEmpty(locale.getCountry()) && defaultLanguage.equals(locale.getLanguage())) {
387-
/*
388-
category.debug("Requested language '" + locale.getLanguage() +
389-
"' matches default: Attempting to guess bundle " +
390-
"using default country '" + defaultCountry + '\'');
391-
*/
392-
Locale withDefaultCountry = new Locale(locale.getLanguage(), defaultCountry);
393-
rb = (ResourceBundle) bundlesByLocale.get(withDefaultCountry);
340+
if (locale.getCountry() != null
341+
&& !locale.getCountry().isEmpty()
342+
&& Locale.getDefault().getLanguage().equals(locale.getLanguage())) {
343+
Locale withDefaultCountry =
344+
new Locale(locale.getLanguage(), Locale.getDefault().getCountry());
345+
rb = bundlesByLocale.get(withDefaultCountry);
394346
if (rb == null) {
395347
rb = getBundleIgnoreException(bundleName, withDefaultCountry);
396348
}
397-
} else if (!StringUtils.isNotEmpty(locale.getLanguage()) && defaultCountry.equals(locale.getCountry())) {
398-
Locale withDefaultLanguage = new Locale(defaultLanguage, locale.getCountry());
399-
rb = (ResourceBundle) bundlesByLocale.get(withDefaultLanguage);
349+
} else if (locale.getLanguage() != null
350+
&& !locale.getLanguage().isEmpty()
351+
&& Locale.getDefault().getCountry().equals(locale.getCountry())) {
352+
Locale withDefaultLanguage = new Locale(Locale.getDefault().getLanguage(), locale.getCountry());
353+
rb = bundlesByLocale.get(withDefaultLanguage);
400354
if (rb == null) {
401355
rb = getBundleIgnoreException(bundleName, withDefaultLanguage);
402356
}
403357
}
404358

405-
if (rb == null && !defaultLocale.equals(locale)) {
406-
rb = getBundleIgnoreException(bundleName, defaultLocale);
359+
if (rb == null && !Locale.getDefault().equals(locale)) {
360+
rb = getBundleIgnoreException(bundleName, Locale.getDefault());
407361
}
408362

409363
return rb;

0 commit comments

Comments
 (0)