1 24 package org.riotfamily.components; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.servlet.ServletContext ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.riotfamily.common.xml.BeanConfigurationWatcher; 37 import org.riotfamily.common.xml.ConfigurableBean; 38 import org.riotfamily.common.xml.ConfigurationEventListener; 39 import org.riotfamily.components.component.ViewComponent; 40 import org.riotfamily.components.config.ComponentListConfiguration; 41 import org.riotfamily.components.editor.ComponentFormRepository; 42 import org.springframework.beans.factory.InitializingBean; 43 import org.springframework.context.ApplicationContext; 44 import org.springframework.context.ApplicationContextAware; 45 import org.springframework.context.MessageSource; 46 import org.springframework.web.context.ServletContextAware; 47 import org.springframework.web.context.support.XmlWebApplicationContext; 48 49 52 public class ComponentRepository implements ServletContextAware, 53 ApplicationContextAware, InitializingBean, ConfigurableBean { 54 55 private Log log = LogFactory.getLog(ComponentRepository.class); 56 57 private String [] configLocations; 58 59 private ServletContext servletContext; 60 61 private ApplicationContext applicationContext; 62 63 private XmlWebApplicationContext context; 64 65 private Map componentMap = new HashMap (); 66 67 private String viewNamePrefix; 68 69 private String viewNameSuffix; 70 71 private BeanConfigurationWatcher configWatcher = 72 new BeanConfigurationWatcher(this); 73 74 private Map controllers; 75 76 private List locators; 77 78 private Map configuredPropertyProcessors; 79 80 private ComponentFormRepository formRepository; 81 82 public void setConfigLocations(String [] configLocations) { 83 this.configLocations = configLocations; 84 } 85 86 public void setServletContext(ServletContext servletContext) { 87 this.servletContext = servletContext; 88 } 89 90 public void setApplicationContext(ApplicationContext applicationContext) { 91 this.applicationContext = applicationContext; 92 } 93 94 public void addListener(ConfigurationEventListener listener) { 95 configWatcher.addListener(listener); 96 } 97 98 public void afterPropertiesSet() { 99 context = new XmlWebApplicationContext(); 100 context.setParent(applicationContext); 101 context.setServletContext(servletContext); 102 context.setConfigLocations(configLocations); 103 ArrayList resources = new ArrayList (); 104 for (int i = 0; i < configLocations.length; i++) { 105 resources.add(applicationContext.getResource(configLocations[i])); 106 } 107 configWatcher.setResources(resources); 108 109 configure(); 110 } 111 112 public MessageSource getMessageSource() { 113 return applicationContext; 114 } 115 116 public boolean isReloadable() { 117 return true; 118 } 119 120 public void configure() { 121 context.refresh(); 122 componentMap = context.getBeansOfType(Component.class); 123 log.debug("Components: " + componentMap); 124 storeConfiguredPropertyProcessors(); 125 } 126 127 private void storeConfiguredPropertyProcessors() { 128 configuredPropertyProcessors = new HashMap (); 129 Iterator it = componentMap.values().iterator(); 130 while (it.hasNext()) { 131 Component component = (Component) it.next(); 132 configuredPropertyProcessors.put(component, new ArrayList ( 133 component.getPropertyProcessors())); 134 } 135 } 136 137 public void resetPropertyProcessors() { 138 Iterator it = configuredPropertyProcessors.entrySet().iterator(); 139 while (it.hasNext()) { 140 Map.Entry entry = (Map.Entry ) it.next(); 141 Component component = (Component) entry.getKey(); 142 List processors = (List ) entry.getValue(); 143 if (processors == null) { 144 processors = new ArrayList (); 145 } 146 component.setPropertyProcessors(processors); 147 } 148 } 149 150 public void addComponent(String type, Component component) { 151 componentMap.put(type, component); 152 } 153 154 public Component getComponent(String type) { 155 configWatcher.checkForModifications(); 156 if (componentMap.get(type) == null) { 157 ViewComponent viewComponent = new ViewComponent(); 158 String viewName = viewNamePrefix + type + viewNameSuffix; 159 viewComponent.setViewName(viewName); 160 componentMap.put(type, viewComponent); 161 } 162 return (Component) componentMap.get(type); 163 } 164 165 public Component getComponent(ComponentVersion version) { 166 return getComponent(version.getType()); 167 } 168 169 public void setViewNamePrefix(String defaultViewLocation) { 170 this.viewNamePrefix = defaultViewLocation; 171 } 172 173 public void setViewNameSuffix(String viewSuffix) { 174 this.viewNameSuffix = viewSuffix; 175 } 176 177 public ComponentListConfiguration getListConfiguration(String controllerId) { 178 return (ComponentListController) controllers.get(controllerId); 179 } 180 181 public void clearControllers() { 182 controllers = new HashMap (); 183 locators = new ArrayList (); 184 } 185 186 public void registerController(String name, ComponentListController controller) { 187 controllers.put(name, controller); 188 ComponentListLocator locator = controller.getLocator(); 189 locators.add(locator); 190 } 191 192 public String getUrl(ComponentList componentList) { 193 Location location = componentList.getLocation(); 194 Iterator it = locators.iterator(); 195 while (it.hasNext()) { 196 ComponentListLocator locator = (ComponentListLocator) it.next(); 197 if (locator.supports(location.getType())) { 198 return locator.getUrl(location); 199 } 200 } 201 return null; 202 } 203 204 public void setFormRepository(ComponentFormRepository formRepository) { 205 this.formRepository = formRepository; 206 } 207 208 public String getFormUrl(String formId, Long containerId) { 209 if (formRepository.containsForm(formId)) { 210 return "/components/form/" + formId + "/" + containerId; 211 } 212 return null; 213 } 214 215 } 216 | Popular Tags |