View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.controller.action;
18  
19  import javax.servlet.ServletContext;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24  import org.springframework.web.context.WebApplicationContext;
25  import org.springframework.web.context.support.WebApplicationContextUtils;
26  
27  import com.fatwire.gst.foundation.controller.action.support.NullActionNameResolver;
28  import com.fatwire.gst.foundation.controller.support.WebContextUtil;
29  
30  /**
31   * @author Dolf Dijkstra
32   * @since May 27, 2011
33   */
34  public final class ActionNameResolverUtils {
35      private static final Log LOG = LogFactory.getLog(ActionNameResolverUtils.class.getPackage().getName());
36      public static final String ACTION_NAME_RESOLVER_BEAN = "gsfActionNameResolver";
37  
38      private static final ActionNameResolver nullActionNameResolver = new NullActionNameResolver();
39  
40      /**
41       * 
42       */
43      private ActionNameResolverUtils() {
44      }
45  
46      /**
47       * Returns the ActionNameResolver as configured by spring framework on the
48       * WebApplicationContext bean by the name of gsfActionLocator.
49       * 
50       * @param servletContext the servlet context.
51       * @return the ActionNameResolver that is configured via the servletContext.
52       */
53      public static ActionNameResolver getActionNameResolver(final ServletContext servletContext) {
54          return getActionNameResolver(servletContext, ACTION_NAME_RESOLVER_BEAN);
55      }
56  
57      private static ActionNameResolver getActionNameResolver(ServletContext servletContext, String actionNameResolverBean) {
58          return WebContextUtil.getWebAppContext(servletContext)
59                  .getBean(actionNameResolverBean, ActionNameResolver.class);
60      }
61  
62      /**
63       * Returns the ActionNameResolver as configured by spring framework on the
64       * WebApplicationContext bean by the name 'beanName'.
65       * 
66       * @param servletContext the servlet context.
67       * @return the ActionNameResolver that is configured via the servletContext.
68       */
69  
70      public static ActionNameResolver getActionNameResolverX(final ServletContext servletContext, String beanName) {
71  
72          // get the spring web application context
73          final WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
74  
75          // get the bean.
76  
77          ActionNameResolver resolver;
78          try {
79              resolver = (ActionNameResolver) wac.getBean(beanName, ActionNameResolver.class);
80              if (LOG.isTraceEnabled()) {
81                  LOG.trace("Using ActionNameResolver as configured: " + resolver.getClass().getName());
82              }
83          } catch (NoSuchBeanDefinitionException e) {
84              return nullActionNameResolver;
85          }
86  
87          return resolver;
88      }
89  
90  }