KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > RendererManager


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;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.config.RollerConfig;
27 import org.apache.roller.pojos.Template;
28
29
30 /**
31  * A governing class for Rollers rendering system.
32  *
33  * The purpose of the RendererManager is to provide a level of abstraction
34  * between classes that are rendering content and the implementations of the
35  * rendering technology. This allows us to provide easily pluggable rendering
36  * implementations.
37  */

38 public class RendererManager {
39     
40     private static Log log = LogFactory.getLog(RendererManager.class);
41     
42     // a set of all renderer factories we are consulting
43
private static Set JavaDoc rendererFactories = new HashSet JavaDoc();
44     
45     
46     static {
47         // lookup set of renderer factories we are going to use
48
String JavaDoc rollerFactories = RollerConfig.getProperty("rendering.rollerRendererFactories");
49         String JavaDoc userFactories = RollerConfig.getProperty("rendering.userRendererFactories");
50         
51         // instantiate user defined renderer factory classes
52
if(userFactories != null && userFactories.trim().length() > 0) {
53             
54             RendererFactory rendererFactory = null;
55             String JavaDoc[] uFactories = userFactories.split(",");
56             for(int i=0; i < uFactories.length; i++) {
57                 try {
58                     Class JavaDoc factoryClass = Class.forName(uFactories[i]);
59                     rendererFactory = (RendererFactory) factoryClass.newInstance();
60                     rendererFactories.add(rendererFactory);
61                 } catch(ClassCastException JavaDoc cce) {
62                     log.error("It appears that your factory does not implement "+
63                             "the RendererFactory interface", cce);
64                 } catch(Exception JavaDoc e) {
65                     log.error("Unable to instantiate renderer factory ["+uFactories[i]+"]", e);
66                 }
67             }
68         }
69         
70         // instantiate roller standard renderer factory classes
71
if(rollerFactories != null && rollerFactories.trim().length() > 0) {
72             
73             RendererFactory rendererFactory = null;
74             String JavaDoc[] rFactories = rollerFactories.split(",");
75             for(int i=0; i < rFactories.length; i++) {
76                 try {
77                     Class JavaDoc factoryClass = Class.forName(rFactories[i]);
78                     rendererFactory = (RendererFactory) factoryClass.newInstance();
79                     rendererFactories.add(rendererFactory);
80                 } catch(ClassCastException JavaDoc cce) {
81                     log.error("It appears that your factory does not implement "+
82                             "the RendererFactory interface", cce);
83                 } catch(Exception JavaDoc e) {
84                     log.error("Unable to instantiate renderer factory ["+rFactories[i]+"]", e);
85                 }
86             }
87         }
88         
89         if(rendererFactories.size() < 1) {
90             // hmm ... failed to load any renderer factories?
91
log.warn("Failed to load any renderer factories. "+
92                     "Rendering probably won't function as you expect.");
93         }
94         
95         log.info("Renderer Manager Initialized.");
96     }
97     
98     
99     // this class is non-instantiable
100
private RendererManager() {}
101     
102     
103     /**
104      * Find the appropriate Renderer for the given content.
105      *
106      * This method checks all renderer factories configured for the Roller
107      * instance and tries to find a Renderer for the content. If no Renderer
108      * can be found then we throw an exception.
109      */

110     public static Renderer getRenderer(Template template)
111             throws RenderingException {
112         
113         Renderer renderer = null;
114         
115         // iterate over our renderer factories and see if one of them
116
// wants to handle this content
117
Iterator JavaDoc factories = rendererFactories.iterator();
118         while(factories.hasNext()) {
119             renderer = ((RendererFactory)factories.next()).getRenderer(template);
120             
121             if(renderer != null) {
122                 return renderer;
123             }
124         }
125         
126         throw new RenderingException("No renderer found for template "+
127                 template.getId()+"!");
128     }
129     
130 }
131
Popular Tags