001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ParticipantSpringWeb.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.rep.participants;
009:
010: import java.io.InputStream;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.util.Enumeration;
014: import java.util.Set;
015: import java.util.Vector;
016:
017: import javax.servlet.RequestDispatcher;
018: import javax.servlet.Servlet;
019: import javax.servlet.ServletContext;
020: import javax.servlet.ServletException;
021:
022: import org.springframework.beans.factory.BeanFactory;
023: import org.springframework.web.context.ContextLoader;
024: import org.springframework.web.context.support.WebApplicationContextUtils;
025:
026: import com.uwyn.rife.rep.BlockingParticipant;
027:
028: public class ParticipantSpringWeb extends BlockingParticipant {
029: private BeanFactory mBeanFactory = null;
030:
031: public ParticipantSpringWeb() {
032: setInitializationMessage("Obtaining the Spring web application context ...");
033: setCleanupMessage("Releasing the Spring web application context ...");
034: }
035:
036: protected void initialize() {
037: ServletContext context = (ServletContext) getRepository()
038: .getContext();
039: if (getParameter() != null && !getParameter().equals("")) {
040: ContextLoader loader = new ContextLoader();
041: mBeanFactory = loader
042: .initWebApplicationContext(new ServletContextWrapper(
043: context));
044: } else {
045: mBeanFactory = WebApplicationContextUtils
046: .getWebApplicationContext(context);
047: }
048: }
049:
050: protected Object _getObject() {
051: return mBeanFactory;
052: }
053:
054: protected Object _getObject(Object key) {
055: if (null == key) {
056: return null;
057: }
058:
059: return mBeanFactory.getBean(String.valueOf(key));
060: }
061:
062: public class ServletContextWrapper implements ServletContext {
063: private ServletContext mDelegate;
064: private Vector mInitParamNames = new Vector();
065:
066: ServletContextWrapper(ServletContext delegate) {
067: mDelegate = delegate;
068:
069: Enumeration names_enum = mDelegate.getInitParameterNames();
070: while (names_enum.hasMoreElements()) {
071: mInitParamNames.add(names_enum.nextElement());
072: }
073: if (!mInitParamNames
074: .contains(ContextLoader.CONFIG_LOCATION_PARAM)) {
075: mInitParamNames
076: .add(ContextLoader.CONFIG_LOCATION_PARAM);
077: }
078: }
079:
080: public Object getAttribute(String name) {
081: return mDelegate.getAttribute(name);
082: }
083:
084: public Enumeration getAttributeNames() {
085: return mDelegate.getAttributeNames();
086: }
087:
088: public ServletContext getContext(String uripath) {
089: return mDelegate.getContext(uripath);
090: }
091:
092: public String getInitParameter(String name) {
093: if (ContextLoader.CONFIG_LOCATION_PARAM.equals(name)) {
094: return getParameter();
095: }
096:
097: return mDelegate.getInitParameter(name);
098: }
099:
100: public Enumeration getInitParameterNames() {
101: return mInitParamNames.elements();
102: }
103:
104: public int getMajorVersion() {
105: return mDelegate.getMajorVersion();
106: }
107:
108: public String getMimeType(String file) {
109: return mDelegate.getMimeType(file);
110: }
111:
112: public int getMinorVersion() {
113: return mDelegate.getMinorVersion();
114: }
115:
116: public RequestDispatcher getNamedDispatcher(String name) {
117: return mDelegate.getNamedDispatcher(name);
118: }
119:
120: public String getRealPath(String path) {
121: return mDelegate.getRealPath(path);
122: }
123:
124: public RequestDispatcher getRequestDispatcher(String path) {
125: return mDelegate.getRequestDispatcher(path);
126: }
127:
128: public URL getResource(String path)
129: throws MalformedURLException {
130: return mDelegate.getResource(path);
131: }
132:
133: public InputStream getResourceAsStream(String path) {
134: return mDelegate.getResourceAsStream(path);
135: }
136:
137: public Set getResourcePaths(String path) {
138: return mDelegate.getResourcePaths(path);
139: }
140:
141: public String getServerInfo() {
142: return mDelegate.getServerInfo();
143: }
144:
145: public Servlet getServlet(String name) throws ServletException {
146: return mDelegate.getServlet(name);
147: }
148:
149: public String getServletContextName() {
150: return mDelegate.getServletContextName();
151: }
152:
153: public Enumeration getServletNames() {
154: return mDelegate.getServletNames();
155: }
156:
157: public Enumeration getServlets() {
158: return mDelegate.getServlets();
159: }
160:
161: public void log(Exception exception, String msg) {
162: mDelegate.log(exception, msg);
163: }
164:
165: public void log(String message, Throwable throwable) {
166: mDelegate.log(message, throwable);
167: }
168:
169: public void log(String msg) {
170: mDelegate.log(msg);
171: }
172:
173: public void removeAttribute(String name) {
174: mDelegate.removeAttribute(name);
175: }
176:
177: public void setAttribute(String name, Object object) {
178: mDelegate.setAttribute(name, object);
179: }
180: }
181: }
|