File Source: WebappResourceLoader.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  
    19  package org.apache.roller.weblogger.ui.rendering.velocity;
    20  
    21  import java.io.InputStream;
    22  import javax.servlet.ServletContext;
    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.roller.weblogger.ui.core.RollerContext;
    27  import org.apache.velocity.exception.ResourceNotFoundException;
    28  import org.apache.velocity.runtime.resource.Resource;
    29  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
    30  
    31  
    32  /**
    33   * Loads Velocity resources from the webapp.
    34   *
    35   * All resource urls begin from the root of the webapp.  If a resource path
    36   * is relative (does not begin with a /) then it is prefixed with the path
    37   * /WEB-INF/velocity/, which is where Roller keeps its velocity files.
    38   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.rendering.velocity.WebappResourceLoader()
          * 
          *  Postconditions:
          *    this.mContext == null
          */
    39  public class WebappResourceLoader extends ResourceLoader {
    40      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.velocity.WebappResourceLoader__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    41      private static Log log = LogFactory.getLog(WebappResourceLoader.class);
    42      
    43      private ServletContext mContext = null;
    44      
    45      
    46      /**
    47       * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
    48       */
    49      public void init(ExtendedProperties config) {
    50          
                 /* 
    P/P           *  Method: void init(ExtendedProperties)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    init'ed(this.mContext)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.ui.core.RollerContext:getServletContext(...)@54 != null
                  * 
                  *  Postconditions:
                  *    (soft) this.mContext != null
                  * 
                  *  Test Vectors:
                  *    this.mContext: Inverse{null}, Addr_Set{null}
                  */
    51          log.debug("WebappResourceLoader : initialization starting.");
    52          
    53          if (mContext == null) {
    54              mContext = RollerContext.getServletContext();
    55              log.debug("Servlet Context = "+mContext.getRealPath("/WEB-INF/velocity/"));
    56          }
    57          
    58          log.debug(config);
    59          
    60          log.debug("WebappResourceLoader : initialization complete.");
    61      }
    62      
    63      
    64      /**
    65       * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getResourceStream(java.lang.String)
    66       */
    67      public InputStream getResourceStream(String name) 
    68              throws ResourceNotFoundException {
    69          
                 /* 
    P/P           *  Method: InputStream getResourceStream(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    name != null
                  *    this.mContext != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:length(...)@72 >= 1
                  *    javax.servlet.ServletContext:getResourceAsStream(...)@82 != null
                  * 
                  *  Postconditions:
                  *    (soft) return_value != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@79: {1}, {0}
                  */
    70          log.debug("Looking up resource named ... "+name);
    71          
    72          if (name == null || name.length() == 0) {
    73              throw new ResourceNotFoundException("No template name provided");
    74          }
    75          
    76          InputStream result = null;
    77          
    78          try {
    79              if(!name.startsWith("/"))
    80                  name = "/WEB-INF/velocity/" + name;
    81              
    82              result = this.mContext.getResourceAsStream(name);
    83              
    84          } catch(Exception e) {
    85              throw new ResourceNotFoundException(e.getMessage());
    86          }
    87          
    88          if(result == null) {
    89              throw new ResourceNotFoundException("Couldn't find "+name);
    90          }
    91          
    92          return result;
    93      }
    94      
    95      
    96      /**
    97       * Files loaded by this resource loader are considered static, so they are
    98       * never reloaded by velocity.
    99       *
   100       * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
   101       */
   102      public boolean isSourceModified(Resource arg0) {
                 /* 
    P/P           *  Method: bool isSourceModified(Resource)
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
   103          return false;
   104      }
   105      
   106      
   107      /**
   108       * Defaults to return 0.
   109       *
   110       * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
   111       */
   112      public long getLastModified(Resource arg0) {
                 /* 
    P/P           *  Method: long getLastModified(Resource)
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
   113          return 0;
   114      }
   115      
   116  }








SofCheck Inspector Build Version : 2.18479
WebappResourceLoader.java 2009-Jan-02 14:24:58
WebappResourceLoader.class 2009-Sep-04 03:12:45