View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @since 2.0
29   */
30  public class CombinedResources extends ResourceBundle {
31  
32      // locale.getLanguage()
33      // locale.getCountry()
34      // locale.getVariant()
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                  // We know that our properties will only ever contain Strings
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                         // Ignore
116                     }
117                 }
118             } catch (final IOException ignored) {
119                 // Ignore
120             }
121         }
122     }
123 }