01: package org.caramba.spring;
02:
03: import org.caramba.CarambaContext;
04: import org.caramba.CarambaException;
05: import org.caramba.components.Panel;
06: import org.caramba.config.PanelConfig;
07: import org.caramba.config.PanelFactory;
08: import org.springframework.context.ApplicationContext;
09: import org.springframework.web.context.WebApplicationContext;
10: import org.springframework.web.context.support.WebApplicationContextUtils;
11:
12: import javax.servlet.ServletContext;
13:
14: /**
15: * A PageFactory implementation is responsible to create a Page instance with the given Pageconfig.
16: *
17: * @author Pieter Degraeuwe
18: * @see org.caramba.config.DefaultPanelFactory
19: */
20: public class SpringPanelFactory implements PanelFactory {
21: public Panel createPanel(CarambaContext pContext,
22: PanelConfig pPanelConfig) throws CarambaException {
23: Object bean = getApplicatioContext(
24: pContext.getSession().getServletContext()).getBean(
25: pPanelConfig.getName());
26: if (!(bean instanceof Panel))
27: throw new CarambaException("Bean " + pPanelConfig.getName()
28: + " is not an instanceof " + Panel.class.getName());
29: return (Panel) bean;
30: }
31:
32: /**
33: * Gets the ApplicationContext to be used to get the page / panel beans.
34: * This method makes use of {@link org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext(javax.servlet.ServletContext)}.
35: * <b>Note that these pages and panels must be declared as non-singletons (singleton=false) !/b>
36: *
37: * @param pServletContext the ServletContext of the WebApplication
38: * @return the requested ApplicationContext
39: */
40: protected ApplicationContext getApplicatioContext(
41: ServletContext pServletContext) {
42: WebApplicationContext ctx = WebApplicationContextUtils
43: .getRequiredWebApplicationContext(pServletContext);
44: return ctx;
45:
46: }
47: }
|