01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.spring;
06:
07: import com.opensymphony.webwork.config.Configuration;
08: import com.opensymphony.webwork.util.ObjectFactoryInitializable;
09: import com.opensymphony.webwork.WebWorkConstants;
10: import com.opensymphony.xwork.spring.SpringObjectFactory;
11: import org.apache.commons.logging.Log;
12: import org.apache.commons.logging.LogFactory;
13: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
14: import org.springframework.context.ApplicationContext;
15: import org.springframework.web.context.support.WebApplicationContextUtils;
16:
17: import javax.servlet.ServletContext;
18:
19: /**
20: * WebWork object factory that integrates with Spring.
21: * <p/>
22: * Spring should be loaded using a web context listener
23: * <code>org.springframework.web.context.ContextLoaderListener</code> defined in <code>web.xml</code>.
24: *
25: * @author plightbo
26: */
27: public class WebWorkSpringObjectFactory extends SpringObjectFactory
28: implements ObjectFactoryInitializable {
29: private static final Log log = LogFactory
30: .getLog(WebWorkSpringObjectFactory.class);
31:
32: public void init(ServletContext servletContext) {
33: log.info("Initializing WebWork-Spring integration...");
34:
35: ApplicationContext appContext = WebApplicationContextUtils
36: .getWebApplicationContext(servletContext);
37: if (appContext == null) {
38: // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
39: String message = "********** FATAL ERROR STARTING UP SPRING-WEBWORK INTEGRATION **********\n"
40: + "Looks like the Spring listener was not configured for your web app! \n"
41: + "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n"
42: + "You might need to add the following to web.xml: \n"
43: + " <listener>\n"
44: + " <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n"
45: + " </listener>";
46: log.fatal(message);
47: return;
48: }
49:
50: this .setApplicationContext(appContext);
51:
52: String autoWire = Configuration
53: .getString(WebWorkConstants.WEBWORK_OBJECTFACTORY_SPRING_AUTOWIRE);
54: int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; // default
55: if ("name".equals(autoWire)) {
56: type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
57: } else if ("type".equals(autoWire)) {
58: type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
59: } else if ("auto".equals(autoWire)) {
60: type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
61: } else if ("constructor".equals(autoWire)) {
62: type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
63: }
64: this .setAutowireStrategy(type);
65:
66: boolean useClassCache = "true"
67: .equals(Configuration
68: .getString(WebWorkConstants.WEBWORK_OBJECTFACTORY_SPRING_USE_CLASS_CACHE));
69: this .setUseClassCache(useClassCache);
70:
71: log
72: .info("... initialized WebWork-Spring integration successfully");
73: }
74: }
|