1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.util;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.Enumeration;
23 import java.util.Locale;
24 import java.util.Properties;
25 import java.util.ResourceBundle;
26
27
28
29
30 public class CombinedResources extends ResourceBundle {
31
32
33
34
35
36 private final String resourceName;
37 private volatile boolean inited;
38 private final Properties properties = new Properties();
39
40 public CombinedResources(final String resourceName) {
41 this.resourceName = resourceName;
42 init();
43 }
44
45 @Override
46 public Enumeration<String> getKeys() {
47 return new Enumeration<String>() {
48 @Override
49 public boolean hasMoreElements() {
50 return properties.keys().hasMoreElements();
51 }
52
53 @Override
54 public String nextElement() {
55
56 return (String) properties.keys().nextElement();
57 }
58
59 };
60 }
61
62 public String getResourceName() {
63 return resourceName;
64 }
65
66 @Override
67 protected Object handleGetObject(final String key) {
68 return properties.get(key);
69 }
70
71 protected void init() {
72 if (inited) {
73 return;
74 }
75
76 loadResources(getResourceName());
77 loadResources(Locale.getDefault());
78 loadResources(getLocale());
79 inited = true;
80 }
81
82 protected void loadResources(final Locale locale) {
83 if (locale == null) {
84 return;
85 }
86 final String[] parts = new String[] {locale.getLanguage(), locale.getCountry(), locale.getVariant()};
87 final StringBuilder sb = new StringBuilder();
88 for (int i = 0; i < 3; i++) {
89 sb.append(getResourceName());
90 for (int j = 0; j < i; j++) {
91 sb.append('_').append(parts[j]);
92 }
93 if (!parts[i].isEmpty()) {
94 sb.append('_').append(parts[i]);
95 loadResources(sb.toString());
96 }
97 sb.setLength(0);
98 }
99 }
100
101 protected void loadResources(String resourceName) {
102 ClassLoader loader = getClass().getClassLoader();
103 if (loader == null) {
104 loader = ClassLoader.getSystemClassLoader();
105 }
106 if (loader != null) {
107 resourceName = resourceName.replace('.', '/') + ".properties";
108 try {
109 final Enumeration<URL> resources = loader.getResources(resourceName);
110 while (resources.hasMoreElements()) {
111 final URL resource = resources.nextElement();
112 try (final InputStream inputStream = resource.openConnection().getInputStream()) {
113 properties.load(inputStream);
114 } catch (final IOException ignored) {
115
116 }
117 }
118 } catch (final IOException ignored) {
119
120 }
121 }
122 }
123 }