File Source: ModelLoader.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.model;
    20  
    21  import java.util.Map;
    22  import javax.servlet.http.HttpServletRequest;
    23  import javax.servlet.http.HttpServletResponse;
    24  import javax.servlet.jsp.PageContext;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.weblogger.WebloggerException;
    28  import org.apache.roller.weblogger.business.URLStrategy;
    29  import org.apache.roller.weblogger.config.WebloggerConfig;
    30  import org.apache.roller.weblogger.pojos.Weblog;
    31  import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
    32  import org.apache.roller.weblogger.ui.rendering.velocity.deprecated.ContextLoader;
    33  import org.apache.roller.weblogger.util.Utilities;
    34  
    35  
    36  /**
    37   * Helps with model loading process.
    38   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.rendering.model.ModelLoader()
          */
    39  public class ModelLoader {
    40      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.model.ModelLoader__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    41      private static Log log = LogFactory.getLog(ModelLoader.class);
    42      
    43      
    44      /**
    45       * Load old page models, but only if they are enabled.
    46       */
    47      public static void loadOldModels(
    48              Map model,
    49              HttpServletRequest  request,
    50              HttpServletResponse response,
    51              PageContext pageContext,
    52              WeblogPageRequest pageRequest,
    53              URLStrategy urlStrategy) throws WebloggerException {
    54          
    55          // Only load old models if it's enabled     
                 /* 
    P/P           *  Method: void loadOldModels(Map, HttpServletRequest, HttpServletResponse, PageContext, WeblogPageRequest, URLStrategy)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@56: {0}, {1}
                  */
    56          if (WebloggerConfig.getBooleanProperty("rendering.legacyModels.enabled")) { 
    57              ContextLoader.setupContext(model, request, response, pageContext, pageRequest, urlStrategy);            
    58          }
    59      }
    60      
    61      
    62      /**
    63       * Load set of custom models set for the given weblog.
    64       *
    65       * Does not fail if there is a problem with one of the models.
    66       */
    67      public static void loadCustomModels(Weblog weblog, Map model, Map initData) {
    68          
                 /* 
    P/P           *  Method: void loadCustomModels(Weblog, Map, Map)
                  * 
                  *  Preconditions:
                  *    weblog != null
                  *    (soft) log != null
                  *    (soft) model != null
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.pojos.Weblog:getPageModels(...)@69: Addr_Set{null}, Inverse{null}
                  */
    69          if (weblog.getPageModels() != null) {
    70              try {
    71                  loadModels(weblog.getPageModels(), model, initData, false);
    72              } catch(WebloggerException ex) {
    73                  // shouldn't happen, but log it just in case
    74                  log.error("Error loading weblog custom models", ex);
    75              }
    76          }     
    77      }
    78      
    79      
    80      /**
    81       * Convenience method to load a comma-separated list of page models.
    82       *
    83       * Optionally fails if any exceptions are thrown when initializing
    84       * the Model instances.
    85       */
    86      public static void loadModels(String modelsString, Map model, 
    87                                     Map initData, boolean fail) 
    88              throws WebloggerException {
    89          
                 /* 
    P/P           *  Method: void loadModels(String, Map, Map, bool)
                  * 
                  *  Preconditions:
                  *    (soft) fail == 0
                  *    (soft) log != null
                  *    (soft) model != null
                  * 
                  *  Presumptions:
                  *    java.lang.Class:forName(...)@93 != null
                  */
    90          String[] models = Utilities.stringToStringArray(modelsString, ",");
    91          for(int i=0; i < models.length; i++) {
    92              try {
    93                  Class modelClass = Class.forName(models[i]);
    94                  Model pageModel = (Model) modelClass.newInstance();
    95                  pageModel.init(initData);
    96                  model.put(pageModel.getModelName(), pageModel);
    97              } catch (WebloggerException re) {
    98                  if(fail) {
    99                      throw re;
   100                  } else {
   101                      log.warn("Error initializing model: " + models[i]);
   102                  }
   103              } catch (ClassNotFoundException cnfe) {
   104                  if(fail) {
   105                      throw new WebloggerException("Error finding model: " + models[i], cnfe);
   106                  } else {
   107                      log.warn("Error finding model: " + models[i]);
   108                  }
   109              } catch (InstantiationException ie) {
   110                  if(fail) {
   111                      throw new WebloggerException("Error insantiating model: " + models[i], ie);
   112                  } else {
   113                      log.warn("Error insantiating model: " + models[i]);
   114                  }
   115              } catch (IllegalAccessException iae) {
   116                  if(fail) {
   117                      throw new WebloggerException("Error accessing model: " + models[i], iae);
   118                  } else {
   119                      log.warn("Error accessing model: " + models[i]);
   120                  }
   121              }
   122          }
   123      }
   124      
   125  }








SofCheck Inspector Build Version : 2.18479
ModelLoader.java 2009-Jan-02 14:24:46
ModelLoader.class 2009-Sep-04 03:12:44