View Javadoc

1   package org.webmacro.adapter.spring;
2   
3   
4   import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
5   import org.springframework.web.servlet.view.AbstractUrlBasedView;
6   import org.springframework.web.servlet.View;
7   import org.springframework.beans.BeansException;
8   import org.springframework.beans.factory.ListableBeanFactory;
9   import org.springframework.beans.factory.BeanFactoryUtils;
10  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
11  import org.springframework.context.ApplicationContextException;
12  import org.webmacro.WM;
13  import org.webmacro.WebMacro;
14  import org.webmacro.InitException;
15  
16  import javax.servlet.ServletContext;
17  import java.util.Locale;
18  
19  /***
20   * User: mpalmer
21   */
22  public class WebMacroViewResolver extends AbstractTemplateViewResolver
23  {
24      private WebMacro webMacro;
25  
26      public WebMacroViewResolver()
27      {
28          setViewClass(WebMacroView.class);
29      }
30  
31      /***
32       * Requires WebMacroView.
33       * @see WebMacroView
34       */
35      protected Class requiredViewClass()
36      {
37          return WebMacroView.class;
38      }
39  
40      protected AbstractUrlBasedView buildView(String viewName) throws Exception {
41          WebMacroView view = (WebMacroView) super.buildView(viewName);
42  
43          // Tell it which WebMacro to use
44          // Fetch our WebMacro in advance so we don't need to lock for
45          // every render
46          synchronized (this)
47          {
48              if (webMacro == null)
49              {
50                  webMacro = findWebMacro(getApplicationContext(),
51                      getServletContext(), this.getClass().getClassLoader());
52              }
53          }
54  
55          view.setWebMacro( webMacro);
56  
57          view.checkTemplate();
58  
59          // do we need to do anything here?
60  
61          return view;
62      }
63  
64      public WebMacro getWebMacro()
65      {
66          return webMacro;
67      }
68  
69      /***
70       * You can programmatically supply a WebMacro instance if your configuration
71       * is performed during execution rather than exclusively from a Spring context
72       * @param webMacro
73       */
74      public void setWebMacro(WebMacro webMacro)
75      {
76          this.webMacro = webMacro;
77      }
78  
79      /***
80       * Attempt to load WebMacro instance from Spring application context, or fall back
81       * to a plain new WM() (note: no webapp classpath template loading!)
82       *
83       * @return
84       * @throws org.springframework.beans.BeansException
85       */
86      protected WebMacro findWebMacro(
87          ListableBeanFactory beanFactory, ServletContext servletcontext, ClassLoader classLoader) throws BeansException
88      {
89          try
90          {
91              WebMacro wm = (WebMacro) BeanFactoryUtils.beanOfTypeIncludingAncestors(
92                  beanFactory, WebMacro.class, true, false);
93              return wm;
94          }
95          catch (NoSuchBeanDefinitionException e)
96          {
97              try
98              {
99                  return webMacro == null
100                     ? webMacro = new WM(servletcontext,classLoader, null)
101                     : webMacro; // Can we find the servlet here using a bean context?
102             }
103             catch (InitException initEx)
104             {
105                 throw new ApplicationContextException("Unable to init default WebMacro" +
106                     "instance. Fix the problem or create an explicit WebMacro (WM)" +
107                     "bean in the application context.");
108             }
109         }
110     }
111 }