KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > model > ModelLoader


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.ui.rendering.model;
20
21 import java.util.Map JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
36  * Helps with model loading process.
37  */

38 public class ModelLoader {
39     
40     private static Log log = LogFactory.getLog(ModelLoader.class);
41     
42     
43     /**
44      * Load old page models, but only if they are enabled.
45      */

46     public static void loadOldModels(
47             Map JavaDoc model,
48             HttpServletRequest JavaDoc request,
49             HttpServletResponse JavaDoc response,
50             PageContext JavaDoc pageContext,
51             WeblogPageRequest pageRequest) throws RollerException {
52         
53         // Only load old models if it's enabled
54
if (RollerConfig.getBooleanProperty("rendering.legacyModels.enabled")) {
55             ContextLoader.setupContext(model, request, response, pageContext, pageRequest);
56         }
57     }
58     
59     
60     /**
61      * Load set of custom models set for the given weblog.
62      *
63      * Does not fail if there is a problem with one of the models.
64      */

65     public static void loadCustomModels(WebsiteData weblog, Map JavaDoc model, Map JavaDoc initData) {
66         
67         if (weblog.getPageModels() != null) {
68             try {
69                 loadModels(weblog.getPageModels(), model, initData, false);
70             } catch(RollerException ex) {
71                 // shouldn't happen, but log it just in case
72
log.error("Error loading weblog custom models", ex);
73             }
74         }
75     }
76     
77     
78     /**
79      * Convenience method to load a comma-separated list of page models.
80      *
81      * Optionally fails if any exceptions are thrown when initializing
82      * the Model instances.
83      */

84     public static void loadModels(String JavaDoc modelsString, Map JavaDoc model,
85                                    Map JavaDoc initData, boolean fail)
86             throws RollerException {
87         
88         String JavaDoc[] models = Utilities.stringToStringArray(modelsString, ",");
89         for(int i=0; i < models.length; i++) {
90             try {
91                 Class JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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