File Source: RollerResourceLoader.java

     1  /*
     2  * Licensed to the Apache Software Foundation (ASF) under one or more
     3  *  contributor license agreements.  The ASF licenses this file to You
     4  * under the Apache License, Version 2.0 (the "License"); you may not
     5  * use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *     http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.  For additional information regarding
    15  * copyright in this work, please see the NOTICE file in the top level
    16  * directory of this distribution.
    17  */
    18  package org.apache.roller.weblogger.ui.rendering.velocity;
    19  
    20  import java.io.ByteArrayInputStream;
    21  import java.io.InputStream;
    22  import java.io.UnsupportedEncodingException;
    23  import org.apache.commons.collections.ExtendedProperties;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.velocity.exception.ResourceNotFoundException;
    27  import org.apache.velocity.runtime.resource.Resource;
    28  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
    29  import org.apache.roller.weblogger.WebloggerException;
    30  import org.apache.roller.weblogger.business.WebloggerFactory;
    31  import org.apache.roller.weblogger.pojos.WeblogTemplate;
    32  
    33  
    34  /**
    35   * This is a simple template file loader that loads templates
    36   * from the Roller instance instead of plain files.
    37   * 
    38   * RollerResourceLoader makes use of WebloggerFactory.
    39   * 
    40   * @author <a href="mailto:lance@brainopolis.com">Lance Lavandowska</a>
    41   * @version $Id: RollerResourceLoader.java,v 1.9 2005/01/15 03:32:49 snoopdave Exp $
    42   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.rendering.velocity.RollerResourceLoader()
          */
    43  public class RollerResourceLoader extends ResourceLoader {
    44      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.velocity.RollerResourceLoader__static_init
              * 
              *  Postconditions:
              *    init'ed(mLogger)
              */
    45      private static Log mLogger = LogFactory.getLog(RollerResourceLoader.class);
    46      
    47      
    48      public void init(ExtendedProperties configuration) {
                 /* 
    P/P           *  Method: void init(ExtendedProperties)
                  * 
                  *  Preconditions:
                  *    mLogger != null
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@49: {0}, {1}
                  */
    49          if (mLogger.isDebugEnabled()) {
    50              mLogger.debug(configuration);
    51          }
    52      }
    53      
    54      
    55      public boolean isSourceModified(Resource resource) {
                 /* 
    P/P           *  Method: bool isSourceModified(Resource)
                  * 
                  *  Preconditions:
                  *    mLogger != null
                  *    resource != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    56          return (resource.getLastModified() !=
    57                  readLastModified(resource, "checking timestamp"));
    58      }
    59      
    60      
    61      public long getLastModified(Resource resource) {
                 /* 
    P/P           *  Method: long getLastModified(Resource)
                  * 
                  *  Preconditions:
                  *    mLogger != null
                  *    resource != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    62          return readLastModified(resource, "getting timestamp");
    63      }
    64      
    65      /**
    66       * Get an InputStream so that the Runtime can build a
    67       * template with it.
    68       *
    69       * @param name name of template
    70       * @return InputStream containing template
    71       */
    72      public InputStream getResourceStream(String name)
    73              throws ResourceNotFoundException {
    74          
                 /* 
    P/P           *  Method: InputStream getResourceStream(String)
                  * 
                  *  Preconditions:
                  *    name != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:length(...)@75 >= 1
                  *    org.apache.roller.weblogger.business.UserManager:getPage(...)@80 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@80 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@80 != null
                  *    org.apache.roller.weblogger.pojos.WeblogTemplate:getContents(...)@88 != null
                  * 
                  *  Postconditions:
                  *    return_value == &new ByteArrayInputStream(getResourceStream#3)
                  *    new ByteArrayInputStream(getResourceStream#3) num objects == 1
                  */
    75          if (name == null || name.length() == 0) {
    76              throw new ResourceNotFoundException("Need to specify a template name!");
    77          }
    78          
    79          try {
    80              WeblogTemplate page = 
    81                      WebloggerFactory.getWeblogger().getUserManager().getPage(name);
    82              
    83              if (page == null) {
    84                  throw new ResourceNotFoundException(
    85                          "RollerResourceLoader: page \"" +
    86                          name + "\" not found");
    87              }
    88              return new ByteArrayInputStream( page.getContents().getBytes("UTF-8") );
    89          } catch (UnsupportedEncodingException uex) {
    90              // This should never actually happen.  We expect UTF-8 in all JRE installation.
    91              // This rethrows as a Runtime exception after logging.
    92              mLogger.error(uex);
    93              throw new RuntimeException(uex);
    94          } catch (WebloggerException re) {
    95              String msg = "RollerResourceLoader Error: " +
    96                      "database problem trying to load resource " + name;
    97              mLogger.error( msg, re );
    98              throw new ResourceNotFoundException(msg);
    99          }
   100      }
   101      
   102      
   103      /**
   104       * Fetches the last modification time of the resource
   105       *
   106       * @param resource Resource object we are finding timestamp of
   107       * @param i_operation string for logging, indicating caller's intention
   108       *
   109       * @return timestamp as long
   110       */
   111      private long readLastModified(Resource resource, String i_operation) {
   112          
   113          /*
   114           *  get the template name from the resource
   115           */
                 /* 
    P/P           *  Method: long readLastModified(Resource, String)
                  * 
                  *  Preconditions:
                  *    mLogger != null
                  *    resource != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.UserManager:getPage(...)@118 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@118 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@118 != null
                  *    org.apache.roller.weblogger.pojos.WeblogTemplate:getLastModified(...)@122 != null
                  *    org.apache.roller.weblogger.pojos.WeblogTemplate:getLastModified(...)@125 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@121: {0}, {1}
                  */
   116          String name = resource.getName();
   117          try {
   118              WeblogTemplate page = 
   119                      WebloggerFactory.getWeblogger().getUserManager().getPage(name);
   120              
   121              if (mLogger.isDebugEnabled()) {
   122                  mLogger.debug(name + ": resource=" + resource.getLastModified() +
   123                          " vs. page=" + page.getLastModified().getTime());
   124              }
   125              return page.getLastModified().getTime();
   126          } catch (WebloggerException re) {
   127              mLogger.error( "Error " + i_operation, re );
   128          }
   129          return 0;
   130      }
   131      
   132  }








SofCheck Inspector Build Version : 2.18479
RollerResourceLoader.java 2009-Jan-02 14:25:48
RollerResourceLoader.class 2009-Sep-04 03:12:45