001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.model;
020:
021: import java.util.Map;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.servlet.jsp.PageContext;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.RollerException;
028: import org.apache.roller.config.RollerConfig;
029: import org.apache.roller.pojos.WebsiteData;
030: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
031: import org.apache.roller.ui.rendering.velocity.deprecated.ContextLoader;
032: import org.apache.roller.util.Utilities;
033:
034: /**
035: * Helps with model loading process.
036: */
037: public class ModelLoader {
038:
039: private static Log log = LogFactory.getLog(ModelLoader.class);
040:
041: /**
042: * Load old page models, but only if they are enabled.
043: */
044: public static void loadOldModels(Map model,
045: HttpServletRequest request, HttpServletResponse response,
046: PageContext pageContext, WeblogPageRequest pageRequest)
047: throws RollerException {
048:
049: // Only load old models if it's enabled
050: if (RollerConfig
051: .getBooleanProperty("rendering.legacyModels.enabled")) {
052: ContextLoader.setupContext(model, request, response,
053: pageContext, pageRequest);
054: }
055: }
056:
057: /**
058: * Load set of custom models set for the given weblog.
059: *
060: * Does not fail if there is a problem with one of the models.
061: */
062: public static void loadCustomModels(WebsiteData weblog, Map model,
063: Map initData) {
064:
065: if (weblog.getPageModels() != null) {
066: try {
067: loadModels(weblog.getPageModels(), model, initData,
068: false);
069: } catch (RollerException ex) {
070: // shouldn't happen, but log it just in case
071: log.error("Error loading weblog custom models", ex);
072: }
073: }
074: }
075:
076: /**
077: * Convenience method to load a comma-separated list of page models.
078: *
079: * Optionally fails if any exceptions are thrown when initializing
080: * the Model instances.
081: */
082: public static void loadModels(String modelsString, Map model,
083: Map initData, boolean fail) throws RollerException {
084:
085: String[] models = Utilities.stringToStringArray(modelsString,
086: ",");
087: for (int i = 0; i < models.length; i++) {
088: try {
089: Class modelClass = Class.forName(models[i]);
090: Model pageModel = (Model) modelClass.newInstance();
091: pageModel.init(initData);
092: model.put(pageModel.getModelName(), pageModel);
093: } catch (RollerException re) {
094: if (fail) {
095: throw re;
096: } else {
097: log.warn("Error initializing model: " + models[i]);
098: }
099: } catch (ClassNotFoundException cnfe) {
100: if (fail) {
101: throw new RollerException("Error finding model: "
102: + models[i], cnfe);
103: } else {
104: log.warn("Error finding model: " + models[i]);
105: }
106: } catch (InstantiationException ie) {
107: if (fail) {
108: throw new RollerException(
109: "Error insantiating model: " + models[i],
110: ie);
111: } else {
112: log.warn("Error insantiating model: " + models[i]);
113: }
114: } catch (IllegalAccessException iae) {
115: if (fail) {
116: throw new RollerException("Error accessing model: "
117: + models[i], iae);
118: } else {
119: log.warn("Error accessing model: " + models[i]);
120: }
121: }
122: }
123: }
124:
125: }
|