Author: ajaquith
Date: Mon Mar  8 23:43:12 2010
New Revision: 920569

URL: http://svn.apache.org/viewvc?rev=920569&view=rev
Log:
Fixed feature that was broken: localized strings needed by Dirk's JavaScript 
works again. The code has also been refactored slightly so that it cached 
rather than having to continually hit the disk looking for resource bundles on 
every request.

Modified:
    incubator/jspwiki/trunk/ChangeLog
    incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java
    
incubator/jspwiki/trunk/src/java/org/apache/wiki/content/jcr/JCRWikiPage.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java
    
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/PreferencesLocalePicker.java
    
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java

Modified: incubator/jspwiki/trunk/ChangeLog
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/ChangeLog?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- incubator/jspwiki/trunk/ChangeLog (original)
+++ incubator/jspwiki/trunk/ChangeLog Mon Mar  8 23:43:12 2010
@@ -1,3 +1,12 @@
+2010-03-08 Andrew Jaquith <ajaquith AT apache DOT org>
+
+        * 3.0.0-svn-214
+
+        * Fixed feature that was broken: localized strings needed by
+        Dirk's JavaScript works again. The code has also been refactored
+        slightly so that it cached rather than having to continually
+        hit the disk looking for resource bundles on every request.
+
 2010-03-03 Andrew Jaquith <ajaquith AT apache DOT org>
 
         * 3.0.0-svn-213

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java Mon Mar  8 
23:43:12 2010
@@ -77,7 +77,7 @@
      *  <p>
      *  If the build identifier is empty, it is not added.
      */
-    public static final String     BUILD         = "213";
+    public static final String     BUILD         = "214";
 
     /**
      *  This is the generic version string you should use

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/content/jcr/JCRWikiPage.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/content/jcr/JCRWikiPage.java?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- 
incubator/jspwiki/trunk/src/java/org/apache/wiki/content/jcr/JCRWikiPage.java 
(original)
+++ 
incubator/jspwiki/trunk/src/java/org/apache/wiki/content/jcr/JCRWikiPage.java 
Mon Mar  8 23:43:12 2010
@@ -119,16 +119,16 @@
      *  a temporary storage for workflows.
      *  
      *  @param engine a reference to the {...@link org.apache.wiki.WikiEngine}
-     *  @param name the {...@link org.apache.wiki.content.WikiPath}
+     *  @param path the {...@link org.apache.wiki.content.WikiPath}
      *  @param node the JCR {...@link javax.jcr.Node}
      *  @throws RepositoryException If the page cannot be located.
      */
-    public JCRWikiPage(WikiEngine engine, WikiPath name, Node node) 
+    public JCRWikiPage(WikiEngine engine, WikiPath path, Node node) 
         throws RepositoryException
     {
         m_engine  = engine;
         m_jcrPath = node.getPath();
-        m_path    = name;
+        m_path    = path;
     }
     
     /**

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java 
(original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java 
Mon Mar  8 23:43:12 2010
@@ -24,16 +24,21 @@
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 import javax.servlet.jsp.PageContext;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.wiki.InternalWikiException;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
+import org.apache.wiki.event.WikiEngineEvent;
+import org.apache.wiki.event.WikiEvent;
+import org.apache.wiki.i18n.InternationalizationManager;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
 import org.apache.wiki.modules.ModuleManager;
 import org.apache.wiki.modules.WikiModuleInfo;
+import org.apache.wiki.preferences.Preferences;
 
 
 /**
@@ -53,6 +58,12 @@
      */
     private static final String RESOURCE_RESOLVER = "resourceResolver";
 
+    /**
+     * Attribute name for the localized JavaScript template strings. Stored in 
the
+     * servlet context as an attribute.
+     */
+    public static final String TEMPLATE_JAVASCRIPT_STRINGS = 
"templateJsStrings";
+
     /** The default directory for template resources. Value is {...@value}. */
     public static final String TEMPLATE_DIR = "templates";
 
@@ -80,6 +91,9 @@
      * even if the WikiEngine cannot initialize for some reason.
      * If the WikiEngine does not initialize, the default template
      * {...@link #DEFAULT_TEMPLATE} will be used for all resource requests.</p>
+     * <p>Note that because the resource resolver is stashed as a 
ServletContext
+     * attribute, it is (effectively) lazily initialized once per 
ServletContext.
+     * The stashed resolver is 
      * @param servletContext the servlet context
      * @return the unmodifiable map
      */
@@ -118,6 +132,71 @@
     }
     
     /**
+     * Returns all message keys and values for all keys prefixed with
+     * {...@code javascript} from the {...@code templates/default*.properties}
+     * resource bundle files. The return value is a JavaScript snippet that
+     * defines the LocalizedStings array. This method does not depend on
+     * WikiEngine initialization. Assuming that the resource bundle is
+     * available on the classpath, it will return non-null String even if
+     * the WikiEngine does not initialize.
+     * 
+     * @param session the HTTP session
+     * @param locale the Locale for which localized strings are sought
+     * @return JavaScript snippet which defines the LocalizedStrings array
+    */
+    @SuppressWarnings("unchecked")
+    public static String getTemplateJSStrings( HttpSession session, Locale 
locale )
+    {
+        // If not Locale we support, bail now
+        if ( !Preferences.AVAILABLE_LOCALES.containsKey( locale ) )
+        {
+            // This is probably not the way we should do it.
+            return "";
+        }
+        
+        // Retrieve the ServletContext stash
+        ServletContext context = session.getServletContext();
+        Map<Locale,String> templateStrings = 
(Map<Locale,String>)context.getAttribute( TEMPLATE_JAVASCRIPT_STRINGS );
+        if ( templateStrings == null )
+        {
+            templateStrings = new HashMap<Locale,String>();
+            context.setAttribute( TEMPLATE_JAVASCRIPT_STRINGS, templateStrings 
);
+        }
+        
+        // Retrieve the JavaScript string for the Locale we want
+        String templateString = templateStrings.get( locale );
+        if ( templateString == null )
+        {
+            // Not built yet; go do that now/
+            StringBuilder sb = new StringBuilder();
+            sb.append( "var LocalizedStrings = {\n" );
+            ResourceBundle rb = ResourceBundle.getBundle( 
InternationalizationManager.TEMPLATES_BUNDLE, locale );
+
+            boolean first = true;
+            for( Enumeration<String> en = rb.getKeys(); en.hasMoreElements(); )
+            {
+                String key = en.nextElement();
+                if( key.startsWith( "javascript" ) )
+                {
+                    if( first )
+                    {
+                        first = false;
+                    }
+                    else
+                    {
+                        sb.append( ",\n" );
+                    }
+                    sb.append( "\"" + key + "\":\"" + rb.getString( key ) + 
"\"" );
+                }
+            }
+            sb.append( "\n};\n" );
+            templateString = sb.toString();
+            templateStrings.put( locale, templateString );
+        }
+        return templateString;
+    }
+    
+    /**
      * Adds all of the resources under a specified path prefix to the
      * resource resolver map, with the "short name" of the path as the
      * key, and the full path as the value. The short name is the portion
@@ -160,7 +239,7 @@
             }
         }
     }
-    
+
     /**
      * Tries to locate a given resource from the template directory, relative 
to
      * the root of the JSPWiki webapp context (for example, relative to
@@ -229,6 +308,29 @@
     }
 
     /**
+     * Listens for the WikiEngine shutdown event, and
+     * when received, flushes the stashed localized JavaScript strings
+     * and the resource resolver.
+     * 
+     * @param event The wiki event to inspect.
+     */
+    public void actionPerformed( WikiEvent event )
+    {
+
+        if( event instanceof WikiEngineEvent )
+        {
+            if( event.getType() == WikiEngineEvent.SHUTDOWN )
+            {
+                if ( m_engine != null )
+                {
+                    m_engine.getServletContext().removeAttribute( 
RESOURCE_RESOLVER );
+                    m_engine.getServletContext().removeAttribute( 
TEMPLATE_JAVASCRIPT_STRINGS );
+                }
+            }
+        }
+    }
+
+    /**
      * An utility method for finding a JSP page. It searches only under either
      * current context or by the absolute name.
      * 
@@ -337,7 +439,7 @@
 
         return resultSet;
     }
-    
+
     /**
      * Returns an empty collection, since at the moment the TemplateManager 
does
      * not manage any modules.

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/PreferencesLocalePicker.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/PreferencesLocalePicker.java?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/PreferencesLocalePicker.java
 (original)
+++ 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/PreferencesLocalePicker.java
 Mon Mar  8 23:43:12 2010
@@ -31,6 +31,7 @@
 
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.preferences.Preferences;
+import org.apache.wiki.ui.TemplateManager;
 
 /**
  * This is a simple Stripes LocalePicker which uses
@@ -89,16 +90,23 @@
         }
 
         // See if we can match the user's locale against one we support
-        Locale match = isSupported( locale.getLanguage(), locale.getCountry(), 
locale.getVariant() );
-        if( match == null )
+        Locale match;
+        Locale closestMatch = isSupported( locale.getLanguage(), null, null );
+        if( closestMatch != null )
         {
-            match = isSupported( locale.getLanguage(), locale.getCountry(), 
null );
-            if( match == null )
+            match = closestMatch;
+            closestMatch = isSupported( locale.getLanguage(), 
locale.getCountry(), null );
+            if( closestMatch != null )
             {
-                match = isSupported( locale.getLanguage(), null, null );
+                match = closestMatch;
+                closestMatch = isSupported( locale.getLanguage(), 
locale.getCountry(), locale.getVariant() );
+                if ( closestMatch != null )
+                {
+                    match = closestMatch;
+                }
             }
         }
-        if( match == null )
+        else
         {
             // If we can't, use the WikiEngine's default locale
             WikiEngine engine = WikiEngine.getInstance( 
request.getSession().getServletContext(), null );
@@ -110,6 +118,7 @@
         // Set the preferred locale in Prefs
         Preferences prefs = Preferences.getPreferences( request );
         prefs.put( Preferences.PREFS_LOCALE, match );
+        
         return match;
     }
 

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java?rev=920569&r1=920568&r2=920569&view=diff
==============================================================================
--- 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java
 (original)
+++ 
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java
 Mon Mar  8 23:43:12 2010
@@ -23,6 +23,7 @@
 
 import java.lang.reflect.Method;
 import java.security.Permission;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 
@@ -46,6 +47,7 @@
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
 import org.apache.wiki.preferences.Preferences;
+import org.apache.wiki.ui.TemplateManager;
 import org.slf4j.MDC;
 
 /**
@@ -274,8 +276,10 @@
      * <code>null</code>. After the action bean is injected, downstream classes
      * like WikiTagBase can use it. The attribute can also be accessed as 
variables
      * using the JSP Expression Language (example: 
<code>${wikiActionBean}</code>).
-     * User preferences are also set up.
-     * 
+     * User preferences are also set up. In addition, the HttpSession attribute
+     * {...@value TemplateManager#TEMPLATE_JAVASCRIPT_STRINGS} is set to the 
value
+     * of the localized JavaScript string array as described in
+     * {...@link 
TemplateManager#getTemplateJSStrings(javax.servlet.http.HttpSession, Locale)}.
      * @param context the execution context
      * @return a Resolution if the
      *         {...@link 
net.sourceforge.stripes.controller.LifecycleStage#ActionBeanResolution}
@@ -321,6 +325,10 @@
         // Stash the WikiContext
         WikiContextFactory.saveContext( request, actionBean.getContext() );
 
+        // Stash the JS localized strings
+        String templateString = TemplateManager.getTemplateJSStrings( 
request.getSession(), request.getLocale() );
+        request.getSession().setAttribute( 
TemplateManager.TEMPLATE_JAVASCRIPT_STRINGS, templateString );
+
         if( log.isDebugEnabled() )
         {
             log.debug( "WikiInterceptor resolved ActionBean: " + actionBean );


Reply via email to