001: package org.caramba.spring;
002:
003: import org.caramba.DestroyMode;
004: import org.caramba.components.Page;
005: import org.caramba.components.Panel;
006: import org.caramba.config.AbstractCarambaConfigFactory;
007: import org.caramba.config.CarambaConfig;
008: import org.caramba.config.PageConfig;
009: import org.caramba.config.PanelConfig;
010: import org.caramba.plugin.PlugIn;
011: import org.springframework.beans.factory.config.BeanDefinition;
012: import org.springframework.web.context.WebApplicationContext;
013: import org.springframework.web.context.support.WebApplicationContextUtils;
014: import org.springframework.web.context.support.XmlWebApplicationContext;
015:
016: import javax.servlet.ServletConfig;
017: import javax.servlet.ServletException;
018:
019: /**
020: * This CarambaConfigFactory implementation constructs and initializes the CarambaConfig, based on
021: * the loaded Spring {@link XmlWebApplicationContext}.</p>
022: * The extra caramba metadata is added by using the caramba namespace 'http://www.carambacomponents.org/caramba/spring/schema'.
023: * Use this CarambaConfigFactory if you want to inject your pages with other beans, defined in your regular Spring ApplicationContext
024: *
025: * @author Pieter Degraeuwe
026: */
027: public class SpringCarambaConfigFactory extends
028: AbstractCarambaConfigFactory {
029: public CarambaConfig createCarambaConfig(
030: ServletConfig pServletConfig) throws ServletException {
031:
032: //This ConfigFactory is actually an empty implementation, since the CarambaConfig is already
033: //initialized by the CarambaSpringContextLoader
034:
035: WebApplicationContext webApplicationContext = WebApplicationContextUtils
036: .getWebApplicationContext(pServletConfig
037: .getServletContext());
038:
039: if (webApplicationContext == null)
040: throw new ServletException(
041: "No Spring applicationContext found");
042:
043: if (webApplicationContext instanceof XmlWebApplicationContext) {
044: XmlWebApplicationContext ctx = (XmlWebApplicationContext) webApplicationContext;
045: //Basic setup of the carambaConfig
046: CarambaConfig carambaConfig = new CarambaConfig();
047: carambaConfig.setPageFactory(new SpringPageFactory());
048: carambaConfig.setPanelFactory(new SpringPanelFactory());
049:
050: //Add PageConfigs
051: String[] pageBeanNames = webApplicationContext
052: .getBeanNamesForType(Page.class);
053: for (int i = 0; i < pageBeanNames.length; i++) {
054: String pageBeanName = pageBeanNames[i];
055: BeanDefinition beanDefinition = ctx.getBeanFactory()
056: .getBeanDefinition(pageBeanName);
057: String resource = (String) beanDefinition
058: .getAttribute(CarambaNamespaceHandler.PAGE_ATTRIBUTE_RESOURCE);
059:
060: String pageClassName = webApplicationContext.getType(
061: pageBeanName).getName();
062: PageConfig pageConfig = constructPageConfig(
063: carambaConfig, pageBeanName, resource,
064: pageClassName);
065: DestroyMode dm = (DestroyMode) beanDefinition
066: .getAttribute(CarambaNamespaceHandler.PAGE_ATTRIBUTE_DESTROY_MODE);
067: if (dm != null) {
068: pageConfig.setDestroyMode(dm);
069: }
070: carambaConfig.addPageConfig(pageConfig);
071: }
072:
073: //Add PanelConfigs
074: String[] panelBeanNames = webApplicationContext
075: .getBeanNamesForType(Panel.class);
076: for (int i = 0; i < panelBeanNames.length; i++) {
077: String panelBeanName = panelBeanNames[i];
078: BeanDefinition beanDefinition = ctx.getBeanFactory()
079: .getBeanDefinition(panelBeanName);
080: String resource = (String) beanDefinition
081: .getAttribute(CarambaNamespaceHandler.PANEL_ATTRIBUTE_RESOURCE);
082: String panelClassName = webApplicationContext.getType(
083: panelBeanName).getName();
084: PanelConfig panelConfig = constructPanelConfig(
085: carambaConfig, panelBeanName, resource,
086: panelClassName);
087: carambaConfig.addPanelConfig(panelConfig);
088: }
089:
090: //Add plugIns
091: String[] plugInBeanNames = webApplicationContext
092: .getBeanNamesForType(PlugIn.class);
093: for (int i = 0; i < plugInBeanNames.length; i++) {
094: String plugInBeanName = plugInBeanNames[i];
095: carambaConfig.addPlugIn((PlugIn) ctx
096: .getBean(plugInBeanName));
097: }
098:
099: //todo Action and forward support
100:
101: //Message Resources
102: carambaConfig
103: .setMessageResources(new SpringMessageResources(ctx));
104:
105: return carambaConfig;
106:
107: } else {
108: throw new ServletException(
109: "The found WebApplicationContext is not an instance of "
110: + XmlWebApplicationContext.class.getName());
111: }
112: }
113:
114: }
|