View Javadoc

1   /*
2    * $Id: StrutsSpringObjectFactory.java 558574 2007-07-23 01:01:37Z jholmes $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.spring;
22  
23  import javax.servlet.ServletContext;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts2.StrutsConstants;
28  import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.web.context.support.WebApplicationContextUtils;
31  
32  import com.opensymphony.xwork2.inject.Inject;
33  import com.opensymphony.xwork2.spring.SpringObjectFactory;
34  
35  
36  
37  /***
38   * Struts object factory that integrates with Spring.
39   * <p/>
40   * Spring should be loaded using a web context listener
41   * <code>org.springframework.web.context.ContextLoaderListener</code> defined in <code>web.xml</code>.
42   *
43   */
44  public class StrutsSpringObjectFactory extends SpringObjectFactory {
45      private static final Log log = LogFactory.getLog(StrutsSpringObjectFactory.class);
46  
47      @Inject
48      public StrutsSpringObjectFactory(
49              @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
50              @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
51              @Inject ServletContext servletContext) {
52            
53          super();
54          boolean useClassCache = "true".equals(useClassCacheStr);
55          log.info("Initializing Struts-Spring integration...");
56  
57          ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
58          if (appContext == null) {
59              // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
60              String message = "********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********\n" +
61                      "Looks like the Spring listener was not configured for your web app! \n" +
62                      "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n" +
63                      "You might need to add the following to web.xml: \n" +
64                      "    <listener>\n" +
65                      "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n" +
66                      "    </listener>";
67              log.fatal(message);
68              return;
69          }
70  
71          this.setApplicationContext(appContext);
72  
73          int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;   // default
74          if ("name".equals(autoWire)) {
75              type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
76          } else if ("type".equals(autoWire)) {
77              type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
78          } else if ("auto".equals(autoWire)) {
79              type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
80          } else if ("constructor".equals(autoWire)) {
81              type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
82          }
83          this.setAutowireStrategy(type);
84  
85          this.setUseClassCache(useClassCache);
86  
87          log.info("... initialized Struts-Spring integration successfully");
88      }
89  }