001: /*
002: * $Id: SpringWebApplicationFactory.java 461368 2006-07-19 09:04:23Z janne $
003: * $Revision: 461368 $
004: * $Date: 2006-07-19 11:04:23 +0200 (Wed, 19 Jul 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.spring;
020:
021: import java.util.Map;
022:
023: import javax.servlet.ServletContext;
024:
025: import org.springframework.context.ApplicationContext;
026: import org.springframework.web.context.support.WebApplicationContextUtils;
027:
028: import wicket.protocol.http.IWebApplicationFactory;
029: import wicket.protocol.http.WebApplication;
030: import wicket.protocol.http.WicketServlet;
031:
032: /**
033: * Implementation of IWebApplicationFactory that pulls the WebApplication object
034: * out of spring application context.
035: *
036: * Configuration example:
037: *
038: * <pre>
039: * <servlet>
040: * <servlet-name>phonebook</servlet-name>
041: * <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
042: * <init-param>
043: * <param-name>applicationFactoryClassName</param-name>
044: * <param-value>wicket.contrib.spring.SpringWebApplicationFactory</param-value>
045: * </init-param>
046: * <load-on-startup>1</load-on-startup>
047: * </servlet>
048: * </pre>
049: *
050: * beanName init parameter can be used if there are multiple WebApplications defined
051: * on the spring application context.
052: *
053: * Example:
054: *
055: * <pre>
056: * <servlet>
057: * <servlet-name>phonebook</servlet-name>
058: * <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
059: * <init-param>
060: * <param-name>applicationFactoryClassName</param-name>
061: * <param-value>wicket.contrib.spring.SpringWebApplicationFactory</param-value>
062: * </init-param>
063: * <init-param>
064: * <param-name>beanName</param-name>
065: * <param-value>phonebookApplication</param-value>
066: * </init-param>
067: * <load-on-startup>1</load-on-startup>
068: * </servlet>
069: * </pre>
070: *
071: *
072: * @author Igor Vaynberg (ivaynberg)
073: * @author Janne Hietamäki (jannehietamaki)
074: *
075: */
076: public class SpringWebApplicationFactory implements
077: IWebApplicationFactory {
078: /**
079: * @see wicket.protocol.http.IWebApplicationFactory#createApplication(wicket.protocol.http.WicketServlet)
080: */
081: public WebApplication createApplication(WicketServlet servlet) {
082: ServletContext sc = servlet.getServletContext();
083: ApplicationContext ac = WebApplicationContextUtils
084: .getRequiredWebApplicationContext(sc);
085:
086: String beanName = servlet.getInitParameter("applicationBean");
087: if (beanName != null) {
088: WebApplication application = (WebApplication) ac
089: .getBean(beanName);
090: if (application == null) {
091: throw new IllegalArgumentException(
092: "Unable to find WebApplication bean with name ["
093: + beanName + "]");
094: }
095: return application;
096: } else {
097: Map beans = ac.getBeansOfType(WebApplication.class, false,
098: false);
099: if (beans.size() == 0) {
100: throw new IllegalStateException("bean of type ["
101: + WebApplication.class.getName()
102: + "] not found");
103: }
104: if (beans.size() > 1) {
105: throw new IllegalStateException(
106: "more then one bean of type ["
107: + WebApplication.class.getName()
108: + "] found, must have only one");
109: }
110: return (WebApplication) beans.values().iterator().next();
111: }
112: }
113: }
|