You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ja...@apache.org on 2009/03/12 23:14:03 UTC

svn commit: r753038 - in /incubator/jspwiki/trunk: ChangeLog src/java/org/apache/wiki/PageManager.java src/java/org/apache/wiki/Release.java src/java/org/apache/wiki/content/ContentManager.java src/java/org/apache/wiki/content/PageNotFoundException.java

Author: jalkanen
Date: Thu Mar 12 22:14:02 2009
New Revision: 753038

URL: http://svn.apache.org/viewvc?rev=753038&view=rev
Log:
ContentManager now throws a PageNotFoundException when
a page cannot be found, instead of returning null. This is
to reduce the amount of NPEs that might otherwise appear.

Added:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/content/PageNotFoundException.java
Modified:
    incubator/jspwiki/trunk/ChangeLog
    incubator/jspwiki/trunk/src/java/org/apache/wiki/PageManager.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/content/ContentManager.java

Modified: incubator/jspwiki/trunk/ChangeLog
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/ChangeLog?rev=753038&r1=753037&r2=753038&view=diff
==============================================================================
--- incubator/jspwiki/trunk/ChangeLog (original)
+++ incubator/jspwiki/trunk/ChangeLog Thu Mar 12 22:14:02 2009
@@ -4,6 +4,10 @@
         
         * Fixed issue in ContentManager.getPage() not properly
         catching exceptions.
+        
+        * ContentManager now throws a PageNotFoundException when
+        a page cannot be found, instead of returning null. This is
+        to reduce the amount of NPEs that might otherwise appear.
 
 2009-02-22  Andrew Jaquith <ajaquith AT apache DOT org>
 

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/PageManager.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/PageManager.java?rev=753038&r1=753037&r2=753038&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/PageManager.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/PageManager.java Thu Mar 12 22:14:02 2009
@@ -144,7 +144,11 @@
     public String getPageText( String pageName, int version )
         throws ProviderException
     {
-        return m_engine.getContentManager().getPage( WikiName.valueOf( pageName ), version ).getContentAsString();
+        WikiPage p = m_engine.getContentManager().getPage( WikiName.valueOf( pageName ), version );
+        
+        if( p != null ) return p.getContentAsString();
+        
+        return null;
     }
 
     /**
@@ -278,7 +282,14 @@
      */
     public int getTotalPageCount()
     {
-        return m_engine.getContentManager().getTotalPageCount( null );
+        try
+        {
+            return m_engine.getContentManager().getTotalPageCount( null );
+        }
+        catch( ProviderException e )
+        {
+            return -1;
+        }
     }
 
     /**

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=753038&r1=753037&r2=753038&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java Thu Mar 12 22:14:02 2009
@@ -77,7 +77,7 @@
      *  <p>
      *  If the build identifier is empty, it is not added.
      */
-    public static final String     BUILD         = "81";
+    public static final String     BUILD         = "82";
     
     /**
      *  This is the generic version string you should use

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/content/ContentManager.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/content/ContentManager.java?rev=753038&r1=753037&r2=753038&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/content/ContentManager.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/content/ContentManager.java Thu Mar 12 22:14:02 2009
@@ -72,6 +72,11 @@
  *  and "jackrabbit" for <a href="http://jackrabbit.apache.org">Apache Jackrabbit</a>.
  *  <p>
  *  If there is no property defined, defaults to "priha".
+ *  <p>
+ *  The methods in this class always return valid values. In case they cannot
+ *  be acquired by some means, it throws an Exception.  This is different from
+ *  the way the old PageManager used to work, so you can go ahead and get rid
+ *  of all the null-checks from your old code.
  *
  *  FIXME:
  *    * This class is currently designed to be a drop-in replacement for PageManager.
@@ -554,7 +559,7 @@
      */
 
     public List<WikiPage> getVersionHistory( WikiName path )
-        throws ProviderException
+        throws ProviderException, PageNotFoundException
     {
         List<WikiPage> result = new ArrayList<WikiPage>();
         JCRWikiPage base = getPage(path);
@@ -572,6 +577,10 @@
                 result.add( new JCRWikiPage(m_engine,v) );
             }
         }
+        catch( PathNotFoundException e )
+        {
+            throw new PageNotFoundException(path);
+        }
         catch( RepositoryException e )
         {
             throw new ProviderException("Unable to get version history",e);
@@ -602,21 +611,14 @@
      *  
      *  @param space Name of the Wiki space.  May be null, in which
      *  case all spaces will be counted
-     *  @return The number of pages, or -1, if there is an error.
+     *  @return The number of pages.
+     *  @throws ProviderException If there was an error.
      */
     // FIXME: Unfortunately this method is very slow, since it involves gobbling
     //        up the entire repo.
-    public int getTotalPageCount(String space)
+    public int getTotalPageCount(String space) throws ProviderException
     {
-        try
-        {
-            return getAllPages(space).size();
-        }
-        catch( ProviderException e )
-        {
-            log.error( "Unable to count pages: ",e );
-            return -1;
-        }
+        return getAllPages(space).size();
     }
     /**
      *  Returns true, if the page exists (any version).
@@ -625,7 +627,6 @@
      *  @return A boolean value describing the existence of a page
      *  @throws ProviderException If the backend fails or the wikiPath is illegal.
      */
- 
     public boolean pageExists( WikiName wikiPath )
         throws ProviderException
     {
@@ -682,9 +683,10 @@
      *  
      *  @param page The page to delete.
      *  @throws ProviderException if the page fails
+     *  @throws PageNotFoundException If the page in question does not exist.
      */
     public void deleteVersion( WikiPage page )
-        throws ProviderException
+        throws ProviderException, PageNotFoundException
     {
         fireEvent( WikiPageEvent.PAGE_DELETE_REQUEST, page.getName() );
 
@@ -696,6 +698,10 @@
             
             fireEvent( WikiPageEvent.PAGE_DELETED, jcrPage.getName() );
         }
+        catch( PathNotFoundException e )
+        {
+            throw new PageNotFoundException(page.getQualifiedName());
+        }
         catch( RepositoryException e )
         {
             throw new ProviderException("Unable to delete a page",e);
@@ -707,10 +713,11 @@
      *  
      *  @param page The WikiPage to delete
      *  @throws ProviderException If the backend fails or the page is illegal.
+     *  @throws PageNotFoundException If the page has already disappeared.
      */
     
     public void deletePage( WikiPage page )
-        throws ProviderException
+        throws ProviderException, PageNotFoundException
     {
         fireEvent( WikiPageEvent.PAGE_DELETE_REQUEST, page.getName() );
 
@@ -741,6 +748,10 @@
             
             fireEvent( WikiPageEvent.PAGE_DELETED, page.getName() );
         }
+        catch( PathNotFoundException e )
+        {
+            throw new PageNotFoundException(page.getQualifiedName());
+        }
         catch( RepositoryException e )
         {
             throw new ProviderException("Deletion of pages failed.",e);
@@ -1001,8 +1012,9 @@
      *  @param path the path
      *  @return the {@link JCRWikiPage} 
      *  @throws ProviderException If the backend fails.
+     *  @throws PageNotFoundException If the page does not exist.
      */
-    public JCRWikiPage getPage( WikiName path ) throws ProviderException
+    public JCRWikiPage getPage( WikiName path ) throws ProviderException, PageNotFoundException
     {
         try
         {
@@ -1015,7 +1027,7 @@
         }
         catch( PathNotFoundException e )
         {
-            return null;
+            throw new PageNotFoundException( path );
         }
         catch( RepositoryException e )
         {
@@ -1027,7 +1039,7 @@
         }
     }
 
-    public JCRWikiPage getPage( WikiName path, int version ) throws ProviderException
+    public JCRWikiPage getPage( WikiName path, int version ) throws ProviderException, PageNotFoundException
     {
         try
         {
@@ -1056,8 +1068,7 @@
         }
         catch( PathNotFoundException e )
         {
-            // Page was not found at all.
-            return null;
+            throw new PageNotFoundException( path );
         }
         catch( RepositoryException e )
         {

Added: incubator/jspwiki/trunk/src/java/org/apache/wiki/content/PageNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/content/PageNotFoundException.java?rev=753038&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/content/PageNotFoundException.java (added)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/content/PageNotFoundException.java Thu Mar 12 22:14:02 2009
@@ -0,0 +1,52 @@
+/*
+    JSPWiki - a JSP-based WikiWiki clone.
+
+    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.wiki.content;
+
+import org.apache.wiki.providers.ProviderException;
+
+/**
+ *  A particular kind of exception noting that the WikiPage or attachment 
+ *  in question was not found.
+ *  
+ *  @since 3.0
+ */
+public class PageNotFoundException extends ProviderException
+{
+    private static final long serialVersionUID = 1L;
+
+    /**
+     *  Construct an exception from a String path. 
+     *  @param path The path to the nonexistant WikiPage.
+     */
+    public PageNotFoundException( String path )
+    {
+        super( path );
+    }
+
+    /**
+     *  Construct an exception from a WikiName path. 
+     *  @param path The path to the nonexistant WikiPage.
+     */
+    public PageNotFoundException( WikiName path )
+    {
+        super( path.toString() );
+    }
+}