1 18 19 package org.apache.roller.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.RollerException; 28 import org.apache.roller.config.RollerConfig; 29 import org.apache.roller.pojos.WebsiteData; 30 import org.apache.roller.ui.rendering.util.WeblogPageRequest; 31 import org.apache.roller.ui.rendering.velocity.deprecated.ContextLoader; 32 import org.apache.roller.util.Utilities; 33 34 35 38 public class ModelLoader { 39 40 private static Log log = LogFactory.getLog(ModelLoader.class); 41 42 43 46 public static void loadOldModels( 47 Map model, 48 HttpServletRequest request, 49 HttpServletResponse response, 50 PageContext pageContext, 51 WeblogPageRequest pageRequest) throws RollerException { 52 53 if (RollerConfig.getBooleanProperty("rendering.legacyModels.enabled")) { 55 ContextLoader.setupContext(model, request, response, pageContext, pageRequest); 56 } 57 } 58 59 60 65 public static void loadCustomModels(WebsiteData weblog, Map model, Map initData) { 66 67 if (weblog.getPageModels() != null) { 68 try { 69 loadModels(weblog.getPageModels(), model, initData, false); 70 } catch(RollerException ex) { 71 log.error("Error loading weblog custom models", ex); 73 } 74 } 75 } 76 77 78 84 public static void loadModels(String modelsString, Map model, 85 Map initData, boolean fail) 86 throws RollerException { 87 88 String [] models = Utilities.stringToStringArray(modelsString, ","); 89 for(int i=0; i < models.length; i++) { 90 try { 91 Class modelClass = Class.forName(models[i]); 92 Model pageModel = (Model) modelClass.newInstance(); 93 pageModel.init(initData); 94 model.put(pageModel.getModelName(), pageModel); 95 } catch (RollerException re) { 96 if(fail) { 97 throw re; 98 } else { 99 log.warn("Error initializing model: " + models[i]); 100 } 101 } catch (ClassNotFoundException cnfe) { 102 if(fail) { 103 throw new RollerException("Error finding model: " + models[i], cnfe); 104 } else { 105 log.warn("Error finding model: " + models[i]); 106 } 107 } catch (InstantiationException ie) { 108 if(fail) { 109 throw new RollerException("Error insantiating model: " + models[i], ie); 110 } else { 111 log.warn("Error insantiating model: " + models[i]); 112 } 113 } catch (IllegalAccessException iae) { 114 if(fail) { 115 throw new RollerException("Error accessing model: " + models[i], iae); 116 } else { 117 log.warn("Error accessing model: " + models[i]); 118 } 119 } 120 } 121 } 122 123 } 124 | Popular Tags |