01: package com.opensymphony.webwork.plexus;
02:
03: import com.opensymphony.webwork.util.ObjectFactoryInitializable;
04: import com.opensymphony.xwork.ObjectFactory;
05: import org.apache.commons.logging.Log;
06: import org.apache.commons.logging.LogFactory;
07: import org.codehaus.plexus.PlexusContainer;
08:
09: import javax.servlet.ServletContext;
10: import java.util.Map;
11:
12: /**
13: * Plexus integartion. You need three optional files: plexus-request.xml, plexus-session.xml, and
14: * plexus-application.xml.
15: *
16: * The syntax of these files is:
17: *
18: * <pre>
19: * <plexus>
20: * <components>
21: * <component>
22: * <role>com.acme.MyBean</role>
23: * <implementation>com.acme.MyBean|com.acme.MyBeanImpl</implementation>
24: * <componentComposer>field|setter|?</componentComposer>
25: * <requirements>
26: * <requirement>
27: * <role>com.acme.MyOtherBean</role>
28: * </requirement>
29: * </requirements>
30: * <configuration>
31: * <foo>123</foo>
32: * <bar>hello, world</bar>
33: * </configuration>
34: * </component>
35: * </components>
36: * </plexus>
37: * </pre>
38: */
39: public class PlexusObjectFactory extends ObjectFactory implements
40: ObjectFactoryInitializable {
41: private static final Log log = LogFactory
42: .getLog(PlexusObjectFactory.class);
43:
44: private PlexusContainer base;
45:
46: public void init(ServletContext servletContext) {
47: if (!PlexusLifecycleListener.loaded || !PlexusFilter.loaded) {
48: // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
49: String message = "********** FATAL ERROR STARTING UP PLEXUS-WEBWORK INTEGRATION **********\n"
50: + "Looks like the Plexus listener was not configured for your web app! \n"
51: + "You need to add the following to web.xml: \n"
52: + "\n"
53: + " <!-- this should be before the webwork filter -->\n"
54: + " <filter>\n"
55: + " <filter-name>plexus</filter-name>\n"
56: + " <filter-class>com.opensymphony.webwork.plexus.PlexusFilter</filter-class>\n"
57: + " </filter>\n"
58: + "\n"
59: + "...\n"
60: + "\n"
61: + " <!-- this should be before the webwork filter -->\n"
62: + " <filter-mapping>\n"
63: + " <filter-name>plexus</filter-name>\n"
64: + " <url-pattern>/*</url-pattern>\n"
65: + " </filter-mapping>\n"
66: + "\n"
67: + "...\n"
68: + "\n"
69: + " <listener>\n"
70: + " <listener-class>com.opensymphony.webwork.plexus.PlexusLifecycleListener</listener-class>\n"
71: + " </listener>";
72: log.fatal(message);
73: return;
74: }
75:
76: base = (PlexusContainer) servletContext
77: .getAttribute(PlexusLifecycleListener.KEY);
78: }
79:
80: public Object buildBean(String className, Map extraContext)
81: throws Exception {
82: PlexusContainer pc = PlexusThreadLocal.getPlexusContainer();
83: if (pc == null) {
84: pc = base;
85: }
86:
87: try {
88: return pc.lookup(className);
89: } catch (Exception e) {
90: Object o = super.buildBean(className, extraContext);
91: pc.autowire(o);
92: return o;
93: }
94: }
95: }
|