1 48 49 package org.jpublish.component; 50 51 import java.io.IOException ; 52 import java.io.StringWriter ; 53 import java.util.HashMap ; 54 import java.util.Map ; 55 56 import com.anthonyeden.lib.config.Configuration; 57 import com.anthonyeden.lib.config.ConfigurationException; 58 import com.anthonyeden.lib.util.ClassUtilities; 59 import com.anthonyeden.lib.util.MessageUtilities; 60 import org.apache.commons.logging.Log; 61 import org.apache.commons.logging.LogFactory; 62 import org.jpublish.JPublishEngine; 63 import org.jpublish.RequestContext; 64 import org.jpublish.SiteContext; 65 import org.jpublish.repository.Repository; 66 import org.jpublish.view.ViewRenderException; 67 import org.jpublish.view.ViewRenderer; 68 69 75 76 public abstract class AbstractComponent implements Component { 77 78 private Log log = LogFactory.getLog(AbstractComponent.class); 79 80 protected String name = null; 81 protected String description = null; 82 protected Map properties = new HashMap (); 83 84 protected Repository viewRepository = null; 85 protected ViewRenderer viewRenderer = null; 86 87 protected SiteContext siteContext = null; 88 89 92 93 protected AbstractComponent() { 94 95 } 96 97 102 103 public String getName() { 104 return name; 105 } 106 107 112 113 public String getDescription() { 114 return description; 115 } 116 117 122 123 public Map getProperties() { 124 return properties; 125 } 126 127 132 133 public SiteContext getSiteContext() { 134 return siteContext; 135 } 136 137 142 143 public void setSiteContext(SiteContext siteContext) { 144 this.siteContext = siteContext; 145 } 146 147 152 153 public Repository getViewRepository() { 154 return viewRepository; 155 } 156 157 162 163 public void setViewRepository(Repository viewRepository) { 164 this.viewRepository = viewRepository; 165 } 166 167 173 174 public ViewRenderer getViewRenderer() { 175 return viewRenderer; 176 } 177 178 184 185 public void setViewRenderer(ViewRenderer viewRenderer) { 186 this.viewRenderer = viewRenderer; 187 } 188 189 197 198 public void loadConfiguration(Configuration configuration) 199 throws ConfigurationException { 200 Configuration viewRendererConfiguration = 202 configuration.getChild("view-renderer"); 203 if (viewRendererConfiguration != null) { 204 String viewRendererClass = 205 viewRendererConfiguration.getAttribute("classname"); 206 207 try { 208 viewRenderer = (ViewRenderer) ClassUtilities.loadClass(viewRendererClass).newInstance(); 209 viewRenderer.setSiteContext(siteContext); 210 } catch (ClassNotFoundException e) { 211 Object [] args = {viewRendererClass}; 212 String msg = MessageUtilities.getMessage(getClass(), 213 JPublishEngine.MESSAGE_PACKAGE, 214 "viewRendererClassNotFoundError", args); 215 throw new ConfigurationException(msg, 216 viewRendererConfiguration); 217 } catch (InstantiationException e) { 218 Object [] args = {viewRendererClass}; 219 String msg = MessageUtilities.getMessage(getClass(), 220 JPublishEngine.MESSAGE_PACKAGE, 221 "viewRendererInstantiationError", args); 222 throw new ConfigurationException(msg, 223 viewRendererConfiguration); 224 } catch (IllegalAccessException e) { 225 Object [] args = {viewRendererClass}; 226 String msg = MessageUtilities.getMessage(getClass(), 227 JPublishEngine.MESSAGE_PACKAGE, 228 "viewRendererIllegalAccessError", args); 229 throw new ConfigurationException(msg, 230 viewRendererConfiguration); 231 } 232 viewRenderer.loadConfiguration(viewRendererConfiguration); 237 try { 238 viewRenderer.init(); 239 } catch (Exception e) { 240 Object [] args = { 241 viewRenderer.getClass().getName(), 242 e.getMessage() 243 }; 244 String msg = MessageUtilities.getMessage(getClass(), 245 JPublishEngine.MESSAGE_PACKAGE, "viewRendererInitError", 246 args); 247 throw new ConfigurationException(msg, e, configuration); 248 } 249 } 250 251 if (viewRenderer == null) { 252 viewRenderer = siteContext.getViewRenderer(); 253 } 254 255 Configuration repositoryConfiguration = 257 configuration.getChild("repository"); 258 if (repositoryConfiguration != null) { 259 String name = repositoryConfiguration.getAttribute("name"); 260 if (name == null) { 261 Object [] args = {}; 262 String msg = MessageUtilities.getMessage(getClass(), 263 JPublishEngine.MESSAGE_PACKAGE, "repositoryNameRequired", 264 args); 265 throw new ConfigurationException(msg, repositoryConfiguration); 266 } else { 267 if (log.isDebugEnabled()) { 268 log.debug("Using repository name: " + name); 269 } 270 viewRepository = siteContext.getRepository(name); 271 } 272 } else { 273 log.error("Repository configuration element not found"); 274 } 275 276 if (viewRepository == null) { 277 Object [] args = {getName()}; 278 String msg = MessageUtilities.getMessage(getClass(), 279 JPublishEngine.MESSAGE_PACKAGE, "repositoryRequired", args); 280 throw new ConfigurationException(msg, configuration); 281 } 282 } 283 284 292 293 protected String renderView(String path, RequestContext context) 294 throws ViewRenderException, IOException { 295 ViewRenderer viewRenderer = getViewRenderer(); 296 StringWriter out = new StringWriter (); 297 viewRenderer.render(context, path, out); 298 return out.toString(); 299 } 300 301 } 302 | Popular Tags |