jspwiki-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From juanpa...@apache.org
Subject svn commit: r1478935 - in /incubator/jspwiki/trunk: ./ src/main/java/org/apache/wiki/ src/main/java/org/apache/wiki/ui/ src/main/java/org/apache/wiki/util/ src/test/java/org/apache/wiki/util/
Date Fri, 03 May 2013 19:23:29 GMT
Author: juanpablo
Date: Fri May  3 19:23:17 2013
New Revision: 1478935

URL: http://svn.apache.org/r1478935
Log:
 * 2.9.1-incubating-9
       
 * Refactored TemplateManager#listLanguages(PageContext pageContext) so it doesn't search
for i18n files under a specific jar filename

Modified:
    incubator/jspwiki/trunk/ChangeLog
    incubator/jspwiki/trunk/pom.xml
    incubator/jspwiki/trunk/src/main/java/org/apache/wiki/Release.java
    incubator/jspwiki/trunk/src/main/java/org/apache/wiki/ui/TemplateManager.java
    incubator/jspwiki/trunk/src/main/java/org/apache/wiki/util/ClassUtil.java
    incubator/jspwiki/trunk/src/test/java/org/apache/wiki/util/ClassUtilTest.java

Modified: incubator/jspwiki/trunk/ChangeLog
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/ChangeLog?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/ChangeLog (original)
+++ incubator/jspwiki/trunk/ChangeLog Fri May  3 19:23:17 2013
@@ -1,3 +1,10 @@
+2013-05-03  Juan Pablo Santos (juanpablo AT apache DOT org)
+
+       * 2.9.1-incubating-9
+       
+       * Refactored TemplateManager#listLanguages(PageContext pageContext) so it doesn't
search for 
+         i18n files under a specific jar filename
+       
 2013-05-03 Glen Mazza (glenmazza AT apache DOT org)
 
        * 2.9.1-incubating-8

Modified: incubator/jspwiki/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/pom.xml?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/pom.xml (original)
+++ incubator/jspwiki/trunk/pom.xml Fri May  3 19:23:17 2013
@@ -146,7 +146,7 @@
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
             <version>1.4</version>
-            <scope>runtime</scope>
+            <scope>compile</scope>
         </dependency>
         <dependency>
             <groupId>nekohtml</groupId>

Modified: incubator/jspwiki/trunk/src/main/java/org/apache/wiki/Release.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/main/java/org/apache/wiki/Release.java?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/main/java/org/apache/wiki/Release.java (original)
+++ incubator/jspwiki/trunk/src/main/java/org/apache/wiki/Release.java Fri May  3 19:23:17
2013
@@ -75,7 +75,7 @@ public final class Release
      *  <p>
      *  If the build identifier is empty, it is not added.
      */
-    public static final String     BUILD         = "8";
+    public static final String     BUILD         = "9";
     
     /**
      *  This is the generic version string you should use

Modified: incubator/jspwiki/trunk/src/main/java/org/apache/wiki/ui/TemplateManager.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/main/java/org/apache/wiki/ui/TemplateManager.java?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/main/java/org/apache/wiki/ui/TemplateManager.java (original)
+++ incubator/jspwiki/trunk/src/main/java/org/apache/wiki/ui/TemplateManager.java Fri May
 3 19:23:17 2013
@@ -22,8 +22,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.*;
-import java.util.jar.JarEntry;
-import java.util.jar.JarInputStream;
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
@@ -32,7 +30,6 @@ import javax.servlet.jsp.jstl.fmt.Locale
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
-
 import org.apache.wiki.InternalWikiException;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
@@ -40,6 +37,7 @@ import org.apache.wiki.i18n.Internationa
 import org.apache.wiki.modules.ModuleManager;
 import org.apache.wiki.preferences.Preferences;
 import org.apache.wiki.preferences.Preferences.TimeFormat;
+import org.apache.wiki.util.ClassUtil;
 
 /**
  *  This class takes care of managing JSPWiki templates.  This class also provides
@@ -89,8 +87,6 @@ public class TemplateManager
 
     /** Location of I18N Resource bundles, and path prefix and suffixes */
 
-    public static final String I18NRESOURCE_PATH = "/WEB-INF/lib/JSPWiki.jar";
-
     public static final String I18NRESOURCE_PREFIX = "templates/default_";
 
     public static final String I18NRESOURCE_SUFFIX = ".properties";
@@ -420,58 +416,26 @@ public class TemplateManager
      * List all installed i18n language properties
      * 
      * @param pageContext
-     * @return map of installed Languages (with help of Juan Pablo Santos Rodriguez)
+     * @return map of installed Languages
      * @since 2.7.x
      */
     public Map listLanguages(PageContext pageContext)
     {
-        LinkedHashMap<String,String> resultMap = new LinkedHashMap<String,String>();
-
+        Map< String, String > resultMap = new LinkedHashMap< String, String >();
         String clientLanguage = ((HttpServletRequest) pageContext.getRequest()).getLocale().toString();
-        JarInputStream jarStream = null;
         
-        try
-        {
-            JarEntry entry;
-            InputStream inputStream = pageContext.getServletContext().getResourceAsStream(I18NRESOURCE_PATH);
-            jarStream = new JarInputStream(inputStream);
-
-            while ((entry = jarStream.getNextJarEntry()) != null)
-            {
-                String name = entry.getName();
-
-                if (!entry.isDirectory() && name.startsWith(I18NRESOURCE_PREFIX)
&& name.endsWith(I18NRESOURCE_SUFFIX))
+        List< String > entries = ClassUtil.classpathEntriesUnder( DIRECTORY );
+        for( String name : entries ) {
+            if ( name.startsWith( I18NRESOURCE_PREFIX ) && name.endsWith( I18NRESOURCE_SUFFIX
) )
+            {
+                name = name.substring(I18NRESOURCE_PREFIX.length(), name.lastIndexOf(I18NRESOURCE_SUFFIX));
+                Locale locale = new Locale(name.substring(0, 2), ((name.indexOf("_") == -1)
? "" : name.substring(3, 5)));
+                String defaultLanguage = "";
+                if (clientLanguage.startsWith(name))
                 {
-                    name = name.substring(I18NRESOURCE_PREFIX.length(), name.lastIndexOf(I18NRESOURCE_SUFFIX));
-
-                    Locale locale = new Locale(name.substring(0, 2), ((name.indexOf("_")
== -1) ? "" : name.substring(3, 5)));
-
-                    String defaultLanguage = "";
-
-                    if (clientLanguage.startsWith(name))
-                    {
-                        defaultLanguage = LocaleSupport.getLocalizedMessage(pageContext,
I18NDEFAULT_LOCALE);
-                    }
-
-                    resultMap.put(name, locale.getDisplayName(locale) + " " + defaultLanguage);
+                    defaultLanguage = LocaleSupport.getLocalizedMessage(pageContext, I18NDEFAULT_LOCALE);
                 }
-            }
-        }
-        catch (IOException ioe)
-        {
-            if (log.isDebugEnabled())
-                log.debug("Could not search jar file '" + I18NRESOURCE_PATH + 
-                          "'for properties files due to an IOException: \n" + ioe.getMessage());
-        }
-        finally
-        {
-            if( jarStream != null ) 
-            {
-                try 
-                { 
-                    jarStream.close(); 
-                } 
-                catch(IOException e) {}
+                resultMap.put(name, locale.getDisplayName(locale) + " " + defaultLanguage);
             }
         }
         

Modified: incubator/jspwiki/trunk/src/main/java/org/apache/wiki/util/ClassUtil.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/main/java/org/apache/wiki/util/ClassUtil.java?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/main/java/org/apache/wiki/util/ClassUtil.java (original)
+++ incubator/jspwiki/trunk/src/main/java/org/apache/wiki/util/ClassUtil.java Fri May  3 19:23:17
2013
@@ -18,19 +18,26 @@
  */
 package org.apache.wiki.util;
 
+import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.net.JarURLConnection;
+import java.net.URL;
 import java.util.*;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.apache.wiki.api.exceptions.WikiException;
 import org.jdom.Document;
 import org.jdom.Element;
 import org.jdom.input.SAXBuilder;
 import org.jdom.xpath.XPath;
 
-import org.apache.wiki.api.exceptions.WikiException;
-
 /**
  *  Contains useful utilities for class file manipulation.  This is a static class,
  *  so there is no need to instantiate it.
@@ -170,6 +177,120 @@ public final class ClassUtil
     }
     
     /**
+     * Lists all the files in classpath under a given package.
+     * 
+     * @param rootPackage the base package. Can be {code null}.
+     * @return all files entries in classpath under the given package
+     */
+    public static List< String > classpathEntriesUnder( final String rootPackage )

+    {
+        List< String > results = new ArrayList< String >();
+        Enumeration< URL > en = null;
+        if( StringUtils.isNotEmpty( rootPackage ) ) {
+            try
+            {
+                en = ClassUtil.class.getClassLoader().getResources( rootPackage );
+            }
+            catch( IOException e )
+            {
+                log.error( e.getMessage(), e );
+            }
+        }
+        
+        while( en != null && en.hasMoreElements() )
+        {
+            URL url = en.nextElement();
+            try
+            {
+                if( "jar".equals( url.getProtocol() ) ) 
+                {
+                    jarEntriesUnder( results, ( JarURLConnection )url.openConnection(), rootPackage
);
+                } 
+                else if( "file".equals( url.getProtocol() ) ) 
+                {
+                    fileEntriesUnder( results, new File( url.getFile() ), rootPackage );
+                }
+                
+            }
+            catch (IOException ioe)
+            {
+                log.error( ioe.getMessage(), ioe );
+            }
+        }
+        return results;
+    }
+    
+    /**
+     * Searchs for all the files in classpath under a given package, for a given {@link File}.
If the 
+     * {@link File} is a directory all files inside it are stored, otherwise the {@link File}
itself is
+     * stored
+     * 
+     * @param results collection in which the found entries are stored
+     * @param file given {@link File} to search in.
+     * @param rootPackage base package.
+     */
+    static void fileEntriesUnder( List< String > results, File file, String rootPackage
) 
+    {
+        log.debug( "scanning [" + file.getName() +"]" );
+        if( file.isDirectory() ) {
+            @SuppressWarnings( "unchecked" )Iterator< File > files = FileUtils.iterateFiles(
file, null, true );
+            while( files.hasNext() ) 
+            {
+                File subfile = files.next();
+                // store an entry similar to the jarSearch(..) below ones
+                String entry = StringUtils.replace( subfile.getAbsolutePath(), file.getAbsolutePath()
+ File.separatorChar, StringUtils.EMPTY );
+                results.add( rootPackage + "/" + entry );
+            }
+        } else {
+            results.add( file.getName() );
+        }
+    }
+    
+    /**
+     * Searchs for all the files in classpath under a given package, for a given {@link JarURLConnection}.
+     * 
+     * @param results collection in which the found entries are stored
+     * @param jurlcon given {@link JarURLConnection} to search in.
+     * @param rootPackage base package.
+     */
+    static void jarEntriesUnder( List< String > results, JarURLConnection jurlcon,
String rootPackage )
+    {
+        JarFile jar = null;
+        try
+        {
+            jar = jurlcon.getJarFile();
+            log.debug( "scanning [" + jar.getName() +"]" );
+            Enumeration< JarEntry > entries = jar.entries();
+            while( entries.hasMoreElements() )
+            {
+                JarEntry entry = entries.nextElement();
+                if( entry.getName().startsWith( rootPackage ) && !entry.isDirectory()
) 
+                {
+                    results.add( entry.getName() );
+                }
+            }
+        }
+        catch( IOException ioe )
+        {
+            log.error( ioe.getMessage(), ioe );
+        }
+        finally 
+        {
+            if (jar != null)
+            {
+                try
+                {
+                    jar.close();
+                }
+                catch( IOException ioe )
+                {
+                    log.error( ioe.getMessage(), ioe );
+                }
+            }
+        }
+    }
+    
+    /**
      *  This method is used to locate and instantiate a mapped class.
      *  You may redefine anything in the resource file which is located in your classpath
      *  under the name <code>ClassUtil.MAPPINGS ({@value #MAPPINGS})</code>.

Modified: incubator/jspwiki/trunk/src/test/java/org/apache/wiki/util/ClassUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/test/java/org/apache/wiki/util/ClassUtilTest.java?rev=1478935&r1=1478934&r2=1478935&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/test/java/org/apache/wiki/util/ClassUtilTest.java (original)
+++ incubator/jspwiki/trunk/src/test/java/org/apache/wiki/util/ClassUtilTest.java Fri May
 3 19:23:17 2013
@@ -19,7 +19,11 @@
 
 package org.apache.wiki.util;
 
-import junit.framework.*;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
 
 public class ClassUtilTest extends TestCase
 {
@@ -27,6 +31,28 @@ public class ClassUtilTest extends TestC
     {
         super( s );
     }
+    
+    /**
+     * tests various kinds of searches on classpath items
+     */
+    public void testClasspathSearch() throws Exception
+    {
+        List< String > jarSearch = ClassUtil.classpathEntriesUnder( "META-INF" );
+        assertNotNull( jarSearch );
+        assertTrue( jarSearch.size() > 0 );
+        
+        List< String > fileSearch = ClassUtil.classpathEntriesUnder( "templates" );
+        assertNotNull( fileSearch );
+        assertTrue( fileSearch.size() > 0 );
+        
+        List< String > nullSearch = ClassUtil.classpathEntriesUnder( "blurb" );
+        assertNotNull( nullSearch );
+        assertTrue( nullSearch.size() == 0 );
+        
+        List< String > nullInputSearch = ClassUtil.classpathEntriesUnder( null );
+        assertNotNull( nullInputSearch );
+        assertTrue( nullSearch.size() == 0 );
+    }
 
     /**
      *  Tries to find an existing class.



Mime
View raw message