Author: svenmeier
Date: Wed Nov  9 16:40:09 2011
New Revision: 1199836

URL: http://svn.apache.org/viewvc?rev=1199836&view=rev
Log:
WICKET-4162 new string resource loader for default jar resources 
"wicket-jar.properties"

Added:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java
   (with props)
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java
   (with props)
    wicket/trunk/wicket-core/src/test/java/wicket-jar.properties   (with props)
    wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml   (with 
props)
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ApplicationSettingsTest.java

Added: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java?rev=1199836&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java
 (added)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java
 Wed Nov  9 16:40:09 2011
@@ -0,0 +1,138 @@
+/*
+ * 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.wicket.resource.loader;
+
+import java.util.Locale;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.resource.IPropertiesFactory;
+import org.apache.wicket.resource.Properties;
+import org.apache.wicket.util.resource.locator.ResourceNameIterator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This is one of Wicket's default string resource loaders. It is designed to 
let wicket extension
+ * modules contribute default resource bundles for their components.
+ * <p>
+ * The jar based string resource loader attempts to find the resource from a 
bundle that corresponds
+ * to the supplied component objects jar or one of its parent's jar.
+ * <p>
+ * This implementation is fully aware of both locale and style values when 
trying to obtain the
+ * appropriate resources.
+ * <p>
+ * Looks for a file called (by default) <code>wicket-jar.properties</code> at 
the root of the jar.
+ * 
+ * @author Bertrand Guay-Paquet
+ * @author Sven Meier
+ */
+public class JarStringResourceLoader extends ComponentStringResourceLoader
+{
+       /** Log. */
+       private static final Logger log = 
LoggerFactory.getLogger(JarStringResourceLoader.class);
+
+       /** The name (without extension) of the properties file */
+       private String filename = "wicket-jar";
+
+       /**
+        * Create and initialize the resource loader.
+        */
+       public JarStringResourceLoader()
+       {
+       }
+
+       /**
+        * 
+        * @see 
org.apache.wicket.resource.loader.ComponentStringResourceLoader#loadStringResource(java.lang.Class,
+        *      java.lang.String, java.util.Locale, java.lang.String, 
java.lang.String)
+        */
+       @Override
+       public String loadStringResource(Class<?> clazz, final String key, 
final Locale locale,
+               final String style, final String variation)
+       {
+               if (clazz == null)
+               {
+                       return null;
+               }
+
+               // Load the properties associated with the path
+               IPropertiesFactory propertiesFactory = getPropertiesFactory();
+
+               // Search the component's class and its parent classes
+               while (!isStopResourceSearch(clazz))
+               {
+                       // Iterator over all the combinations
+                       ResourceNameIterator iter = 
newResourceNameIterator(filename, locale, style, variation);
+                       while (iter.hasNext())
+                       {
+                               String newPath = iter.next();
+                               Properties props = 
propertiesFactory.load(clazz, newPath);
+                               if (props != null)
+                               {
+                                       // Lookup the value
+                                       String value = props.getString(key);
+                                       if (value != null)
+                                       {
+                                               return value;
+                                       }
+                               }
+                       }
+
+                       clazz = clazz.getSuperclass();
+               }
+
+               // not found
+               return null;
+       }
+
+       /**
+        * @see 
org.apache.wicket.resource.loader.ComponentStringResourceLoader#loadStringResource(org.apache.wicket.Component,
+        *      java.lang.String, java.util.Locale, java.lang.String, 
java.lang.String)
+        */
+       @Override
+       public String loadStringResource(final Component component, final 
String key,
+               final Locale locale, final String style, final String variation)
+       {
+               if (component == null)
+               {
+                       return null;
+               }
+               return loadStringResource(component.getClass(), key, locale, 
style, variation);
+       }
+
+       /**
+        * Gets the properties file filename (without extension)
+        * 
+        * @return filename
+        */
+       public String getFilename()
+       {
+               return filename;
+       }
+
+       /**
+        * Sets the properties filename (without extension)
+        * 
+        * @param filename
+        *            filename
+        */
+       public void setFilename(String filename)
+       {
+               this.filename = filename;
+       }
+}
\ No newline at end of file

Propchange: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/resource/loader/JarStringResourceLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java?rev=1199836&r1=1199835&r2=1199836&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ResourceSettings.java
 Wed Nov  9 16:40:09 2011
@@ -20,6 +20,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.wicket.Application;
+import org.apache.wicket.Component;
 import org.apache.wicket.IResourceFactory;
 import org.apache.wicket.Localizer;
 import org.apache.wicket.css.ICssCompressor;
@@ -39,6 +40,7 @@ import org.apache.wicket.resource.Proper
 import org.apache.wicket.resource.loader.ClassStringResourceLoader;
 import org.apache.wicket.resource.loader.ComponentStringResourceLoader;
 import org.apache.wicket.resource.loader.IStringResourceLoader;
+import org.apache.wicket.resource.loader.JarStringResourceLoader;
 import org.apache.wicket.resource.loader.PackageStringResourceLoader;
 import org.apache.wicket.resource.loader.ValidatorStringResourceLoader;
 import org.apache.wicket.settings.IResourceSettings;
@@ -127,7 +129,44 @@ public class ResourceSettings implements
        private final Application application;
 
        /**
-        * Construct
+        * Configures Wicket's default ResourceLoaders.<br>
+        * For an example in {@code FooApplication} let {@code bar.Foo} extend 
{@link Component}, this
+        * results in the following ordering:
+        * <dl>
+        * <dt>component specific</dt>
+        * <dd>
+        * <ul>
+        * <li>bar/Foo.properties</li>
+        * <li>org/apache/wicket/Component.properties</li>
+        * </ul>
+        * </dd>
+        * <dt>package specific</dt>
+        * <dd>
+        * <ul>
+        * <li>bar/package.properties</li>
+        * <li>package.properties (on Foo's class loader)</li>
+        * <li>org/apache/wicket/package.properties</li>
+        * <li>org/apache/package.properties</li>
+        * <li>org/package.properties</li>
+        * <li>package.properties (on Component's class loader)</li>
+        * </ul>
+        * </dd>
+        * <dt>application specific</dt>
+        * <dd>
+        * <ul>
+        * <li>FooApplication.properties</li>
+        * <li>Application.properties</li>
+        * </ul>
+        * </dd>
+        * <dt>validator specific</dt>
+        * <dt>jar specific</dt>
+        * <dd>
+        * <ul>
+        * <li>wicket-jar.properties (in jar containing Foo)</li>
+        * <li>wicket-jar.properties (in jar containing Component)</li>
+        * </ul>
+        * </dd>
+        * </dl>
         * 
         * @param application
         */
@@ -138,6 +177,7 @@ public class ResourceSettings implements
                stringResourceLoaders.add(new PackageStringResourceLoader());
                stringResourceLoaders.add(new 
ClassStringResourceLoader(application.getClass()));
                stringResourceLoaders.add(new ValidatorStringResourceLoader());
+               stringResourceLoaders.add(new JarStringResourceLoader());
        }
 
        /**

Modified: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ApplicationSettingsTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ApplicationSettingsTest.java?rev=1199836&r1=1199835&r2=1199836&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ApplicationSettingsTest.java
 (original)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/ApplicationSettingsTest.java
 Wed Nov  9 16:40:09 2011
@@ -28,6 +28,7 @@ import org.apache.wicket.resource.loader
 import org.apache.wicket.resource.loader.ClassStringResourceLoader;
 import org.apache.wicket.resource.loader.ComponentStringResourceLoader;
 import org.apache.wicket.resource.loader.IStringResourceLoader;
+import org.apache.wicket.resource.loader.JarStringResourceLoader;
 import org.apache.wicket.resource.loader.PackageStringResourceLoader;
 import org.apache.wicket.resource.loader.ValidatorStringResourceLoader;
 import org.apache.wicket.settings.IFrameworkSettings;
@@ -118,15 +119,17 @@ public class ApplicationSettingsTest
        {
                IResourceSettings settings = new ResourceSettings(new 
MockApplication());
                List<IStringResourceLoader> loaders = 
settings.getStringResourceLoaders();
-               Assert.assertEquals("There should be 4 default loaders", 4, 
loaders.size());
+               Assert.assertEquals("There should be 5 default loaders", 5, 
loaders.size());
                Assert.assertTrue("First loader one should be the component 
one",
                        loaders.get(0) instanceof 
ComponentStringResourceLoader);
-               Assert.assertTrue("Second loader should be the application one",
+               Assert.assertTrue("Second loader should be the package one",
                        loaders.get(1) instanceof PackageStringResourceLoader);
                Assert.assertTrue("Third loader should be the application one",
                        loaders.get(2) instanceof ClassStringResourceLoader);
                Assert.assertTrue("Fourth loader should be the validator one",
                        loaders.get(3) instanceof 
ValidatorStringResourceLoader);
+               Assert.assertTrue("Fifth should be the classpath one",
+                       loaders.get(4) instanceof JarStringResourceLoader);
        }
 
        /**

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java?rev=1199836&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java
 Wed Nov  9 16:40:09 2011
@@ -0,0 +1,81 @@
+/*
+ * 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.wicket.resource.loader;
+
+import java.util.Locale;
+
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.html.basic.Label;
+
+/**
+ * 
+ */
+public class JarStringResourceLoaderTest extends WicketTestCase
+{
+
+       /**
+        * 
+        */
+       public void testLoader_default()
+       {
+               JarStringResourceLoader loader = new JarStringResourceLoader();
+               MyLabel label = new MyLabel("id");
+
+               // must specify locale explitely or it will fail on French 
system
+               assertEquals("english", loader.loadStringResource(label,
+                       "JarStringResourceLoaderTest_text", Locale.ENGLISH, 
null, null));
+       }
+
+       /**
+        * 
+        */
+       public void testLoader_fr()
+       {
+               JarStringResourceLoader loader = new JarStringResourceLoader();
+               MyLabel label = new MyLabel("id");
+               assertEquals("french", loader.loadStringResource(label, 
"JarStringResourceLoaderTest_text",
+                       Locale.FRENCH, null, null));
+       }
+
+       /**
+        * 
+        */
+       public void testLoader_notFound()
+       {
+               JarStringResourceLoader loader = new JarStringResourceLoader();
+               MyLabel label = new MyLabel("id");
+               assertNull(loader.loadStringResource(label, "fhadfjksd", null, 
null, null));
+       }
+
+       /**
+        * 
+        */
+       private static class MyLabel extends Label
+       {
+               private static final long serialVersionUID = 1L;
+
+               /**
+                * Construct.
+                * 
+                * @param id
+                */
+               public MyLabel(String id)
+               {
+                       super(id);
+               }
+       }
+}

Propchange: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/resource/loader/JarStringResourceLoaderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket-core/src/test/java/wicket-jar.properties
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/wicket-jar.properties?rev=1199836&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/wicket-jar.properties (added)
+++ wicket/trunk/wicket-core/src/test/java/wicket-jar.properties Wed Nov  9 
16:40:09 2011
@@ -0,0 +1,2 @@
+# Jar level properties file
+JarStringResourceLoaderTest_text = english
\ No newline at end of file

Propchange: wicket/trunk/wicket-core/src/test/java/wicket-jar.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml?rev=1199836&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml (added)
+++ wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml Wed Nov 
 9 16:40:09 2011
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+-->
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd";>
+<properties>
+       <entry key="JarStringResourceLoaderTest_text">french</entry>
+</properties>
\ No newline at end of file

Propchange: wicket/trunk/wicket-core/src/test/java/wicket-jar_fr.properties.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to