KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > ComponentRepository


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.ServletContext JavaDoc;
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 /**
50  * Repository containing component implementations.
51  */

52 public class ComponentRepository implements ServletContextAware,
53         ApplicationContextAware, InitializingBean, ConfigurableBean {
54
55     private Log log = LogFactory.getLog(ComponentRepository.class);
56
57     private String JavaDoc[] configLocations;
58
59     private ServletContext JavaDoc servletContext;
60
61     private ApplicationContext applicationContext;
62
63     private XmlWebApplicationContext context;
64
65     private Map JavaDoc componentMap = new HashMap JavaDoc();
66
67     private String JavaDoc viewNamePrefix;
68
69     private String JavaDoc viewNameSuffix;
70
71     private BeanConfigurationWatcher configWatcher =
72             new BeanConfigurationWatcher(this);
73
74     private Map JavaDoc controllers;
75
76     private List JavaDoc locators;
77
78     private Map JavaDoc configuredPropertyProcessors;
79
80     private ComponentFormRepository formRepository;
81
82     public void setConfigLocations(String JavaDoc[] configLocations) {
83         this.configLocations = configLocations;
84     }
85
86     public void setServletContext(ServletContext JavaDoc 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 JavaDoc resources = new ArrayList JavaDoc();
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 JavaDoc();
129         Iterator JavaDoc it = componentMap.values().iterator();
130         while (it.hasNext()) {
131             Component component = (Component) it.next();
132             configuredPropertyProcessors.put(component, new ArrayList JavaDoc(
133                     component.getPropertyProcessors()));
134         }
135     }
136
137     public void resetPropertyProcessors() {
138         Iterator JavaDoc it = configuredPropertyProcessors.entrySet().iterator();
139         while (it.hasNext()) {
140             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
141             Component component = (Component) entry.getKey();
142             List JavaDoc processors = (List JavaDoc) entry.getValue();
143             if (processors == null) {
144                 processors = new ArrayList JavaDoc();
145             }
146             component.setPropertyProcessors(processors);
147         }
148     }
149
150     public void addComponent(String JavaDoc type, Component component) {
151         componentMap.put(type, component);
152     }
153
154     public Component getComponent(String JavaDoc type) {
155         configWatcher.checkForModifications();
156         if (componentMap.get(type) == null) {
157             ViewComponent viewComponent = new ViewComponent();
158             String JavaDoc 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 JavaDoc defaultViewLocation) {
170         this.viewNamePrefix = defaultViewLocation;
171     }
172
173     public void setViewNameSuffix(String JavaDoc viewSuffix) {
174         this.viewNameSuffix = viewSuffix;
175     }
176
177     public ComponentListConfiguration getListConfiguration(String JavaDoc controllerId) {
178         return (ComponentListController) controllers.get(controllerId);
179     }
180
181     public void clearControllers() {
182         controllers = new HashMap JavaDoc();
183         locators = new ArrayList JavaDoc();
184     }
185
186     public void registerController(String JavaDoc name, ComponentListController controller) {
187         controllers.put(name, controller);
188         ComponentListLocator locator = controller.getLocator();
189         locators.add(locator);
190     }
191
192     public String JavaDoc getUrl(ComponentList componentList) {
193         Location location = componentList.getLocation();
194         Iterator JavaDoc 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 JavaDoc getFormUrl(String JavaDoc formId, Long JavaDoc containerId) {
209         if (formRepository.containsForm(formId)) {
210             return "/components/form/" + formId + "/" + containerId;
211         }
212         return null;
213     }
214
215 }
216
Popular Tags