1 48 49 package org.jpublish.component; 50 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.Map ; 54 55 import com.anthonyeden.lib.config.Configuration; 56 import com.anthonyeden.lib.config.ConfigurationException; 57 import com.anthonyeden.lib.util.ClassUtilities; 58 import com.anthonyeden.lib.util.MessageUtilities; 59 import org.apache.commons.logging.Log; 60 import org.apache.commons.logging.LogFactory; 61 import org.jpublish.JPublishEngine; 62 import org.jpublish.SiteContext; 63 64 69 70 public class DefaultComponentManager implements ComponentManager { 71 72 private static Log log = LogFactory.getLog(DefaultComponentManager.class); 73 74 private Map components = new HashMap (); 75 private SiteContext siteContext; 76 77 82 83 public void setSiteContext(SiteContext siteContext) { 84 this.siteContext = siteContext; 85 } 86 87 94 95 public Component getComponent(String id) throws ComponentNotFoundException { 96 return (Component) components.get(id); 97 } 98 99 104 105 public Iterator getComponents() { 106 return components.values().iterator(); 107 } 108 109 115 116 public void loadConfiguration(Configuration configuration) 117 throws ConfigurationException { 118 Configuration componentsElement = configuration.getChild("components"); 119 Iterator componentElements = 120 componentsElement.getChildren("component").iterator(); 121 while (componentElements.hasNext()) { 122 String componentClassName = null; 123 String componentId = null; 124 try { 125 Configuration componentElement = 126 (Configuration) componentElements.next(); 127 128 componentId = componentElement.getAttribute("id"); 129 componentClassName = componentElement.getAttribute("classname"); 130 131 Component comp = (Component) ClassUtilities.loadClass(componentClassName).newInstance(); 132 comp.setSiteContext(siteContext); 133 134 log.info("Loaded component " + componentId + " [" + 135 componentClassName + "]"); 136 137 comp.loadConfiguration(componentElement); 138 if (log.isDebugEnabled()) { 139 log.debug("Component " + componentId + 140 " configuration loaded"); 141 } 142 143 components.put(componentId, comp); 144 } catch (ClassNotFoundException e) { 145 Object [] args = {componentId, componentClassName}; 146 String msg = MessageUtilities.getMessage(getClass(), 147 JPublishEngine.MESSAGE_PACKAGE, 148 "componentClassNotFound", args); 149 throw new ConfigurationException(msg, e); 150 } catch (Exception e) { 151 Object [] args = {componentId}; 152 String msg = MessageUtilities.getMessage(getClass(), 153 JPublishEngine.MESSAGE_PACKAGE, "componentLoadError", args); 154 log.error(msg); 155 throw new ConfigurationException(msg, e); 156 } 157 } 158 } 159 160 } 161 | Popular Tags |