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.net.URL;
21  import java.util.Enumeration;
22  import java.util.Locale;
23  import java.util.Properties;
24  import java.util.ResourceBundle;
25  
26  /**
27   * @since 2.0
28   */
29  public class CombinedResources extends ResourceBundle {
30  
31      // locale.getLanguage()
32      // locale.getCountry()
33      // locale.getVariant()
34  
35      private final String resourceName;
36      private boolean inited;
37      private final Properties properties = new Properties();
38  
39      public CombinedResources(final String resourceName) {
40          this.resourceName = resourceName;
41      }
42  
43      protected void init() {
44          if (inited) {
45              return;
46          }
47  
48          loadResources(getResourceName());
49          loadResources(Locale.getDefault());
50          loadResources(getLocale());
51          inited = true;
52      }
53  
54      protected void loadResources(final Locale locale) {
55          if (locale == null) {
56              return;
57          }
58          final String[] parts = new String[] { locale.getLanguage(), locale.getCountry(), locale.getVariant() };
59          final StringBuilder sb = new StringBuilder();
60          for (int i = 0; i < 3; i++) {
61              sb.append(getResourceName());
62              for (int j = 0; j < i; j++) {
63                  sb.append('_').append(parts[j]);
64              }
65              if (parts[i].length() != 0) {
66                  sb.append('_').append(parts[i]);
67                  loadResources(sb.toString());
68              }
69              sb.setLength(0);
70          }
71      }
72  
73      protected void loadResources(String resourceName) {
74          ClassLoader loader = getClass().getClassLoader();
75          if (loader == null) {
76              loader = ClassLoader.getSystemClassLoader();
77          }
78          if (loader != null) {
79              resourceName = resourceName.replace('.', '/') + ".properties";
80              try {
81                  final Enumeration<URL> resources = loader.getResources(resourceName);
82                  while (resources.hasMoreElements()) {
83                      final URL resource = resources.nextElement();
84                      try {
85                          properties.load(resource.openConnection().getInputStream());
86                      } catch (final IOException ignored) {
87                          // Ignore
88                      }
89                  }
90              } catch (final IOException ignored) {
91                  // Ignore
92              }
93          }
94      }
95  
96      public String getResourceName() {
97          return resourceName;
98      }
99  
100     @Override
101     public Enumeration<String> getKeys() {
102         if (!inited) {
103             init();
104         }
105         return new Enumeration<String>() {
106             @Override
107             public boolean hasMoreElements() {
108                 return properties.keys().hasMoreElements();
109             }
110 
111             @Override
112             public String nextElement() {
113                 // We know that our properties will only ever contain Strings
114                 return (String) properties.keys().nextElement();
115             }
116 
117         };
118     }
119 
120     @Override
121     protected Object handleGetObject(final String key) {
122         if (!inited) {
123             init();
124         }
125         return properties.get(key);
126     }
127 }