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;
020:
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Set;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.roller.config.RollerConfig;
027: import org.apache.roller.pojos.Template;
028:
029: /**
030: * A governing class for Rollers rendering system.
031: *
032: * The purpose of the RendererManager is to provide a level of abstraction
033: * between classes that are rendering content and the implementations of the
034: * rendering technology. This allows us to provide easily pluggable rendering
035: * implementations.
036: */
037: public class RendererManager {
038:
039: private static Log log = LogFactory.getLog(RendererManager.class);
040:
041: // a set of all renderer factories we are consulting
042: private static Set rendererFactories = new HashSet();
043:
044: static {
045: // lookup set of renderer factories we are going to use
046: String rollerFactories = RollerConfig
047: .getProperty("rendering.rollerRendererFactories");
048: String userFactories = RollerConfig
049: .getProperty("rendering.userRendererFactories");
050:
051: // instantiate user defined renderer factory classes
052: if (userFactories != null && userFactories.trim().length() > 0) {
053:
054: RendererFactory rendererFactory = null;
055: String[] uFactories = userFactories.split(",");
056: for (int i = 0; i < uFactories.length; i++) {
057: try {
058: Class factoryClass = Class.forName(uFactories[i]);
059: rendererFactory = (RendererFactory) factoryClass
060: .newInstance();
061: rendererFactories.add(rendererFactory);
062: } catch (ClassCastException cce) {
063: log.error(
064: "It appears that your factory does not implement "
065: + "the RendererFactory interface",
066: cce);
067: } catch (Exception e) {
068: log.error(
069: "Unable to instantiate renderer factory ["
070: + uFactories[i] + "]", e);
071: }
072: }
073: }
074:
075: // instantiate roller standard renderer factory classes
076: if (rollerFactories != null
077: && rollerFactories.trim().length() > 0) {
078:
079: RendererFactory rendererFactory = null;
080: String[] rFactories = rollerFactories.split(",");
081: for (int i = 0; i < rFactories.length; i++) {
082: try {
083: Class factoryClass = Class.forName(rFactories[i]);
084: rendererFactory = (RendererFactory) factoryClass
085: .newInstance();
086: rendererFactories.add(rendererFactory);
087: } catch (ClassCastException cce) {
088: log.error(
089: "It appears that your factory does not implement "
090: + "the RendererFactory interface",
091: cce);
092: } catch (Exception e) {
093: log.error(
094: "Unable to instantiate renderer factory ["
095: + rFactories[i] + "]", e);
096: }
097: }
098: }
099:
100: if (rendererFactories.size() < 1) {
101: // hmm ... failed to load any renderer factories?
102: log
103: .warn("Failed to load any renderer factories. "
104: + "Rendering probably won't function as you expect.");
105: }
106:
107: log.info("Renderer Manager Initialized.");
108: }
109:
110: // this class is non-instantiable
111: private RendererManager() {
112: }
113:
114: /**
115: * Find the appropriate Renderer for the given content.
116: *
117: * This method checks all renderer factories configured for the Roller
118: * instance and tries to find a Renderer for the content. If no Renderer
119: * can be found then we throw an exception.
120: */
121: public static Renderer getRenderer(Template template)
122: throws RenderingException {
123:
124: Renderer renderer = null;
125:
126: // iterate over our renderer factories and see if one of them
127: // wants to handle this content
128: Iterator factories = rendererFactories.iterator();
129: while (factories.hasNext()) {
130: renderer = ((RendererFactory) factories.next())
131: .getRenderer(template);
132:
133: if (renderer != null) {
134: return renderer;
135: }
136: }
137:
138: throw new RenderingException("No renderer found for template "
139: + template.getId() + "!");
140: }
141:
142: }
|