Author: imario
Date: Mon May 14 12:31:08 2007
New Revision: 537957

URL: http://svn.apache.org/viewvc?view=rev&rev=537957
Log:
VFS-154: deal with multiple VFS Resources.properties in the classpath - Thanks 
to Adam Heath for the patch!

Added:
    
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CombinedResources.java
Modified:
    
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Messages.java

Added: 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CombinedResources.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CombinedResources.java?view=auto&rev=537957
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CombinedResources.java
 (added)
+++ 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CombinedResources.java
 Mon May 14 12:31:08 2007
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.util;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Properties;
+import java.util.ResourceBundle;
+
+public class CombinedResources extends ResourceBundle
+{
+       // locale.getLanguage()
+       // locale.getCountry()
+       // locale.getVariant()
+
+       private final String resourceName;
+       private boolean inited;
+       private final Properties properties = new Properties();
+
+       public CombinedResources(String resourceName)
+       {
+               this.resourceName = resourceName;
+       }
+
+       protected void init()
+       {
+               if (inited)
+               {                       
+                       return;
+               }
+
+               loadResources(getResourceName());
+               loadResources(Locale.getDefault());
+               loadResources(getLocale());
+               inited = true;
+       }
+
+       protected void loadResources(Locale locale)
+       {
+               if (locale == null)
+               {
+                       return;
+               }
+               String[] parts = new String[]{locale.getLanguage(), 
locale.getCountry(), locale.getVariant()};
+               StringBuffer sb = new StringBuffer();
+               for (int i = 0; i < 3; i++)
+               {
+                       sb.append(getResourceName());
+                       for (int j = 0; j < i; j++)
+                       {
+                               sb.append('_').append(parts[j]);
+                       }
+                       if (parts[i].length() != 0)
+                       {
+                               sb.append('_').append(parts[i]);
+                               loadResources(sb.toString());
+                       }
+                       sb.setLength(0);
+               }
+       }
+
+       protected void loadResources(String resourceName)
+       {
+               ClassLoader loader = getClass().getClassLoader();
+               if (loader == null)
+               {
+                       loader = ClassLoader.getSystemClassLoader();
+               }
+               resourceName = resourceName.replace('.', '/') + ".properties";
+               try
+               {
+                       Enumeration resources = 
loader.getResources(resourceName);
+                       while (resources.hasMoreElements())
+                       {
+                               URL resource = (URL) resources.nextElement();
+                               try
+                               {
+                                       
properties.load(resource.openConnection().getInputStream());
+                               }
+                               catch (IOException e)
+                               {
+                                       // ignore
+                               }
+                       }
+               }
+               catch (IOException e)
+               {
+                       // ignore
+               }
+       }
+
+       public String getResourceName()
+       {
+               return resourceName;
+       }
+
+       public Enumeration getKeys()
+       {
+               if (!inited)
+               {
+                       init();
+               }
+               return properties.keys();
+       }
+
+       protected Object handleGetObject(String key)
+       {
+               if (!inited)
+               {
+                       init();
+               }
+               return properties.get(key);
+       }
+}

Modified: 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Messages.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Messages.java?view=diff&rev=537957&r1=537956&r2=537957
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Messages.java
 (original)
+++ 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Messages.java
 Mon May 14 12:31:08 2007
@@ -104,7 +104,7 @@
         // Locate the message
         if (resources == null)
         {
-            resources = 
ResourceBundle.getBundle("org.apache.commons.vfs.Resources");
+            resources = new 
CombinedResources("org.apache.commons.vfs.Resources");
         }
         final String msgText = resources.getString(code);
         msg = new MessageFormat(msgText);



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to