01: package org.caramba.spring;
02:
03: import org.caramba.CarambaContext;
04: import org.caramba.CarambaException;
05: import org.caramba.components.Page;
06: import org.caramba.config.PageConfig;
07: import org.caramba.config.PageFactory;
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: * PageFactory that creates Pages, as they are defined in a Spring application context. This way
16: * you can incect your pages with the needed dependencies.
17: * @author Pieter Degraeuwe
18: */
19: public class SpringPageFactory implements PageFactory {
20: public Page createPage(CarambaContext pContext,
21: PageConfig pPageConfig) throws CarambaException {
22: Object bean = getApplicatioContext(
23: pContext.getSession().getServletContext()).getBean(
24: pPageConfig.getName());
25: if (!(bean instanceof Page))
26: throw new CarambaException("Bean " + pPageConfig.getName()
27: + " is not an instanceof " + Page.class.getName());
28: return (Page) bean;
29: }
30:
31: /**
32: * Gets the ApplicationContext to be used to get the page / panel beans.
33: * This method makes use of {@link WebApplicationContextUtils#getRequiredWebApplicationContext(javax.servlet.ServletContext)}.
34: * <b>Note that these pages and panels must be declared as non-singletons (singleton=false) !/b>
35: * @param pServletContext the ServletContext of the WebApplication
36: * @return the requested ApplicationContext
37: */
38: protected ApplicationContext getApplicatioContext(
39: ServletContext pServletContext) {
40: WebApplicationContext ctx = WebApplicationContextUtils
41: .getRequiredWebApplicationContext(pServletContext);
42: return ctx;
43:
44: }
45: }
|