001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.util; 018 019import java.io.IOException; 020import java.net.URL; 021import java.util.Enumeration; 022import java.util.Locale; 023import java.util.Properties; 024import java.util.ResourceBundle; 025 026/** 027 * @since 2.0 028 */ 029public class CombinedResources extends ResourceBundle { 030 // locale.getLanguage() 031 // locale.getCountry() 032 // locale.getVariant() 033 034 private final String resourceName; 035 private boolean inited; 036 private final Properties properties = new Properties(); 037 038 public CombinedResources(final String resourceName) { 039 this.resourceName = resourceName; 040 } 041 042 protected void init() { 043 if (inited) { 044 return; 045 } 046 047 loadResources(getResourceName()); 048 loadResources(Locale.getDefault()); 049 loadResources(getLocale()); 050 inited = true; 051 } 052 053 protected void loadResources(final Locale locale) { 054 if (locale == null) { 055 return; 056 } 057 final String[] parts = new String[] { locale.getLanguage(), locale.getCountry(), locale.getVariant() }; 058 final StringBuilder sb = new StringBuilder(); 059 for (int i = 0; i < 3; i++) { 060 sb.append(getResourceName()); 061 for (int j = 0; j < i; j++) { 062 sb.append('_').append(parts[j]); 063 } 064 if (parts[i].length() != 0) { 065 sb.append('_').append(parts[i]); 066 loadResources(sb.toString()); 067 } 068 sb.setLength(0); 069 } 070 } 071 072 protected void loadResources(String resourceName) { 073 ClassLoader loader = getClass().getClassLoader(); 074 if (loader == null) { 075 loader = ClassLoader.getSystemClassLoader(); 076 } 077 resourceName = resourceName.replace('.', '/') + ".properties"; 078 try { 079 final Enumeration<URL> resources = loader.getResources(resourceName); 080 while (resources.hasMoreElements()) { 081 final URL resource = resources.nextElement(); 082 try { 083 properties.load(resource.openConnection().getInputStream()); 084 } catch (final IOException ignored) { 085 /* Ignored. */ 086 } 087 } 088 } catch (final IOException ignored) { 089 /* Ignored. */ 090 } 091 } 092 093 public String getResourceName() { 094 return resourceName; 095 } 096 097 @Override 098 public Enumeration<String> getKeys() { 099 if (!inited) { 100 init(); 101 } 102 return new Enumeration<String>() { 103 @Override 104 public boolean hasMoreElements() { 105 return properties.keys().hasMoreElements(); 106 } 107 108 @Override 109 public String nextElement() { 110 // We know that our properties will only ever contain Strings 111 return (String) properties.keys().nextElement(); 112 } 113 114 }; 115 } 116 117 @Override 118 protected Object handleGetObject(final String key) { 119 if (!inited) { 120 init(); 121 } 122 return properties.get(key); 123 } 124}