You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2010/02/23 23:00:48 UTC

svn commit: r915543 - in /incubator/jspwiki/trunk/src/java/org/apache/wiki: auth/SessionMonitor.java ui/TemplateManager.java ui/stripes/TemplateResolution.java

Author: ajaquith
Date: Tue Feb 23 22:00:48 2010
New Revision: 915543

URL: http://svn.apache.org/viewvc?rev=915543&view=rev
Log:
Tweaked TemplateManager to make the "resource resolver" initialize safely even when the WikiEngine does not. This means the 'templates' JSTL scripting variable can be used everywhere, for example in Install.jsp, Error.jsp or any pages that must load safely.

Modified:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/auth/SessionMonitor.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/TemplateManager.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/TemplateResolution.java

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/auth/SessionMonitor.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/auth/SessionMonitor.java?rev=915543&r1=915542&r2=915543&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/auth/SessionMonitor.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/auth/SessionMonitor.java Tue Feb 23 22:00:48 2010
@@ -36,8 +36,8 @@
 import org.apache.wiki.event.*;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
-import org.apache.wiki.preferences.Preferences;
 import org.apache.wiki.rpc.json.JSONRPCManager;
+import org.apache.wiki.ui.TemplateManager;
 
 
 
@@ -294,21 +294,21 @@
     {
         HttpSession session = event.getSession();
         
+        // Stash 'templates' attribute for scripting
+        ServletContext servletContext = session.getServletContext();
+        session.setAttribute( "templates", TemplateManager.getResourceResolver( servletContext ) );
+
         JSONRPCManager.sessionCreated(session);
         
         // Go get the WikiEngine; note that m_engine is NOT set
         // yet because this method is called by the container,
         // and it has no knowledge of getInstance(WikiEngine).
-        ServletContext servletContext = session.getServletContext();
         WikiEngine engine = WikiEngine.getInstance( servletContext, null );
         if ( m_engine == null )
         {
             m_engine = engine;
         }
         
-        // Stash 'templates' attribute for scripting
-        session.setAttribute( "templates", engine.getTemplateManager().getTemplateResources() );
-        
         // Stash the WikiEngine and WikiSession for scripting
         session.setAttribute( WikiContextFactory.ATTR_WIKIENGINE, engine );
         session.setAttribute( WikiContextFactory.ATTR_WIKISESSION, find( session ) );

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=915543&r1=915542&r2=915543&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 Tue Feb 23 22:00:48 2010
@@ -49,6 +49,13 @@
     private static final String SKIN_DIRECTORY = "skins";
 
     /**
+     * Attribute name for the resource resolver map returned by
+     * {@link #getResourceResolver(ServletContext)}. Stored in the
+     * servlet context as an attribute.
+     */
+    private static final String RESOURCE_RESOLVER = "resourceResolver";
+
+    /**
      * Requests a JavaScript function to be called during window.onload. Value
      * is {@value}.
      */
@@ -104,14 +111,7 @@
     private static final List<WikiModuleInfo> EMPTY_MODULE_LIST = Collections.emptyList();
 
     /**
-     * Map that resolves resource requests relative to the templates directory
-     * with the actual resources.
-     * @see #getTemplateResources()
-     */
-    private Map<String,String> m_resources = Collections.emptyMap();
-    
-    /**
-     * Resolves requests for resources relative to the
+     * <p>Resolves requests for resources relative to the
      * <code>templates/<var>template</var></code> path to the actual resources,
      * where <var>template</var> is the configured template returned by
      * {@link WikiEngine#getTemplateDir()}, for example <code>default</code>.
@@ -124,11 +124,46 @@
      * <code>/templates/default/FindContent.jsp</code>. It is also possible
      * for certain keys to return <code>null</code>. The map itself is
      * immutable.
+     * </p>
+     * <p>The resource resolver is guaranteed to initialize if the ServletContext
+     * is active, 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>
+     * @param servletContext the servlet context
      * @return the unmodifiable map
      */
-    public Map<String,String> getTemplateResources()
+    @SuppressWarnings("unchecked")
+    public static Map<String,String> getResourceResolver( ServletContext context )
     {
-        return m_resources;
+        Map<String,String> resolver = (Map<String,String>)context.getAttribute( RESOURCE_RESOLVER );
+        if ( resolver == null )
+        {
+            // Get the WikiEngine template (or use the default if not available)
+            String template = null;
+            try
+            {
+                WikiEngine engine = WikiEngine.getInstance( context, null );
+                template = engine.getTemplateDir();
+            }
+            catch ( Exception e )
+            {
+                // WikiEngine didn't init!
+            }
+            if ( template == null )
+            {
+                template = DEFAULT_TEMPLATE;
+            }
+            
+            // Add all of the resources the template contains
+            resolver = new HashMap<String,String>();
+            addResources( context, resolver, "/" + DIRECTORY + "/" + template + "/", null );
+            
+            // Add resources the template does not contain, but default does
+            addResources( context, resolver, "/" + DIRECTORY + "/" + DEFAULT_TEMPLATE + "/", null );
+            resolver = Collections.unmodifiableMap( resolver );
+            context.setAttribute( RESOURCE_RESOLVER, resolver );
+        }
+        return resolver;
     }
     
     /**
@@ -141,7 +176,7 @@
     {
         super( engine );
         m_engine = engine;
-        initTemplateResources( engine.getTemplateDir() );
+        getResourceResolver( engine.getServletContext() );
     }
 
     /**
@@ -600,26 +635,6 @@
     }
     
     /**
-     * Initializes the template resources resolver map so that requests
-     * for a particular resource will be found regardless of whether the
-     * template contains that resource, or the default template does.
-     * @param template the template whose resources provide the defaults
-     */
-    private void initTemplateResources( String template )
-    {
-        Map<String,String> resolver = new HashMap<String,String>();
-        
-        // Add all of the resources the template contains
-        addResources( resolver, "/" + DIRECTORY + "/" + template + "/", null );
-        
-        // Add resources the template does not contain, but default does
-        addResources( resolver, "/" + DIRECTORY + "/" + DEFAULT_TEMPLATE + "/", null );
-
-        // We're done! Make the map immutable
-        m_resources = Collections.unmodifiableMap( resolver );
-    }
-
-    /**
      * 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
@@ -627,17 +642,17 @@
      * has already been added to the resource map, it will not be added
      * again. Any resources ending in {@code /} (i.e., a directory path)
      * will be processed recursively.
+     * @param context the servlet context
      * @param resolver the resource resolver map
      * @param prefix the path prefix that the search initiates from
      * @param dir the directory to search relative to the path prefix. If not
      * supplied, the path prefix directory itself will be searched
      */
     @SuppressWarnings("unchecked")
-    private void addResources( Map<String,String> resolver, String prefix, String dir )
+    private static void addResources( ServletContext context, Map<String,String> resolver, String prefix, String dir )
     {
-        ServletContext servletContext = m_engine.getServletContext();
         String searchPath = dir == null ? prefix : prefix + dir;
-        Set<String> resources = servletContext.getResourcePaths( searchPath );
+        Set<String> resources = context.getResourcePaths( searchPath );
         if ( resources != null )
         {
             for ( String resource : resources )
@@ -647,7 +662,7 @@
                 // Directory: process these entries too
                 if ( shortName.endsWith( "/" ) )
                 {
-                    addResources( resolver, prefix, shortName );
+                    addResources( context, resolver, prefix, shortName );
                 }
 
                 // Regular resource: add it if we don't have it already

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/TemplateResolution.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/TemplateResolution.java?rev=915543&r1=915542&r2=915543&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/TemplateResolution.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/TemplateResolution.java Tue Feb 23 22:00:48 2010
@@ -1,11 +1,11 @@
 package org.apache.wiki.ui.stripes;
 
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import net.sourceforge.stripes.action.OnwardResolution;
 
-import org.apache.wiki.WikiEngine;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
 import org.apache.wiki.ui.TemplateManager;
@@ -37,12 +37,11 @@
     public void execute( HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
         // Figure out what the resolved template path should be
-        WikiEngine engine = WikiEngine.getInstance( request.getSession().getServletContext(), null );
-        TemplateManager templates = engine.getTemplateManager();
-        String path = templates.getTemplateResources().get( getPath() );
+        ServletContext servletContext = request.getSession().getServletContext();
+        String path = TemplateManager.getResourceResolver( servletContext ).get( getPath() );
         if( path == null )
         {
-            path = "/templates/default/" + getPath();
+            path = "/templates/" + TemplateManager.DEFAULT_TEMPLATE + "/" + getPath();
         }
         setPath( path );