View Javadoc

1   /*
2    * $Id: StrutsSpringObjectFactory.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.spring;
23  
24  import javax.servlet.ServletContext;
25  
26  import org.apache.struts2.StrutsConstants;
27  import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
28  import org.springframework.context.ApplicationContext;
29  import org.springframework.web.context.support.WebApplicationContextUtils;
30  
31  import com.opensymphony.xwork2.inject.Inject;
32  import com.opensymphony.xwork2.spring.SpringObjectFactory;
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  
36  
37  
38  /***
39   * Struts object factory that integrates with Spring.
40   * <p/>
41   * Spring should be loaded using a web context listener
42   * <code>org.springframework.web.context.ContextLoaderListener</code> defined in <code>web.xml</code>.
43   *
44   */
45  public class StrutsSpringObjectFactory extends SpringObjectFactory {
46      private static final Logger LOG = LoggerFactory.getLogger(StrutsSpringObjectFactory.class);
47  
48      @Inject
49      public StrutsSpringObjectFactory(
50              @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
51              @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
52              @Inject ServletContext servletContext) {
53            
54          super();
55          boolean useClassCache = "true".equals(useClassCacheStr);
56          LOG.info("Initializing Struts-Spring integration...");
57  
58          ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
59          if (appContext == null) {
60              // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
61              String message = "********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********\n" +
62                      "Looks like the Spring listener was not configured for your web app! \n" +
63                      "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n" +
64                      "You might need to add the following to web.xml: \n" +
65                      "    <listener>\n" +
66                      "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n" +
67                      "    </listener>";
68              LOG.fatal(message);
69              return;
70          }
71  
72          this.setApplicationContext(appContext);
73  
74          int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;   // default
75          if ("name".equals(autoWire)) {
76              type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
77          } else if ("type".equals(autoWire)) {
78              type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
79          } else if ("auto".equals(autoWire)) {
80              type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
81          } else if ("constructor".equals(autoWire)) {
82              type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
83          }
84          this.setAutowireStrategy(type);
85  
86          this.setUseClassCache(useClassCache);
87  
88          LOG.info("... initialized Struts-Spring integration successfully");
89      }
90  }