View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.module;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import javax.servlet.ServletContext;
39  import javax.servlet.ServletException;
40  
41  import org.springframework.util.StringUtils;
42  import org.springframework.web.context.ConfigurableWebApplicationContext;
43  import org.springframework.web.context.ContextLoader;
44  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
45  import org.springframework.web.servlet.DispatcherServlet;
46  
47  import info.magnolia.module.blossom.render.BlossomDispatcherServlet;
48  import info.magnolia.module.blossom.support.CustomServletConfig;
49  import info.magnolia.objectfactory.Components;
50  
51  /**
52   * Intended to be used as a base class for Magnolia modules that use Blossom. Provides support for initializing
53   * the root WebApplicationContext and DispatcherServlets. Initializing them like this makes them participate
54   * properly at module install time. Servlets apart from {@link BlossomDispatcherServlet} should be configured
55   * in your module's definition file along with their url mappings.
56   *
57   * @since 1.1
58   */
59  public class BlossomModuleSupport {
60  
61      private ContextLoader contextLoader;
62      private List<DispatcherServlet> dispatcherServlets = new ArrayList<DispatcherServlet>();
63  
64      /**
65       * Creates and initializes a root web application context using the provided configLocation. By default the
66       * implementation used is {@link org.springframework.web.context.support.XmlWebApplicationContext}.
67       */
68      public void initRootWebApplicationContext(final String configLocation) {
69  
70          initRootWebApplicationContext(new ContextLoader() {
71  
72              @Override
73              protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
74  
75                  applicationContext.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
76              }
77          });
78      }
79  
80      /**
81       * Creates and initializes a root web application context using the provided configuration classes. The
82       * implementation used is {@link AnnotationConfigWebApplicationContext}.
83       */
84      public void initRootWebApplicationContext(final Class<?>... configClasses) {
85  
86          initRootWebApplicationContext(new ContextLoader() {
87  
88              @Override
89              protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
90  
91                  applicationContext.setConfigLocations(classNamesToArray(configClasses));
92              }
93  
94              @Override
95              protected Class<?> determineContextClass(ServletContext servletContext) {
96                  return AnnotationConfigWebApplicationContext.class;
97              }
98          });
99      }
100 
101     /**
102      * Creates and initializes a root web application context using the provided {@link ContextLoader}.
103      */
104     public void initRootWebApplicationContext(ContextLoader contextLoader) {
105         if (this.contextLoader != null) {
106             throw new IllegalStateException("ContextLoader already set, root WebApplicationContext has already been initialized");
107         }
108         this.contextLoader = contextLoader;
109         this.contextLoader.initWebApplicationContext(getServletContext());
110     }
111 
112     /**
113      * Closes the root web application context.
114      */
115     public void closeRootWebApplicationContext() {
116         if (contextLoader != null) {
117             contextLoader.closeWebApplicationContext(getServletContext());
118             contextLoader = null;
119         }
120     }
121 
122     /**
123      * Creates and initializes a {@link BlossomDispatcherServlet} using the provided configLocation. Its internal web
124      * application context is a {@link org.springframework.web.context.support.XmlWebApplicationContext}.
125      */
126     public void initBlossomDispatcherServlet(String servletName, String configLocation) {
127         initDispatcherServlet(new BlossomDispatcherServlet(), servletName, configLocation);
128     }
129 
130     /**
131      * Creates and initializes a {@link BlossomDispatcherServlet} using the provided configuration classes. Its internal web
132      * application context is a {@link AnnotationConfigWebApplicationContext}.
133      */
134     public void initBlossomDispatcherServlet(String servletName, Class<?>... configurationClasses) {
135         BlossomDispatcherServlet dispatcherServlet = new BlossomDispatcherServlet();
136         dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
137         initDispatcherServlet(dispatcherServlet, servletName, classNamesToString(configurationClasses));
138     }
139 
140     /**
141      * Creates and initializes a {@link DispatcherServlet} using the provided configLocation. Its internal web
142      * application context is a {@link org.springframework.web.context.support.XmlWebApplicationContext}.
143      */
144     public void initDispatcherServlet(String servletName, String configLocation) {
145         initDispatcherServlet(new DispatcherServlet(), servletName, configLocation);
146     }
147 
148     /**
149      * Creates and initializes a {@link DispatcherServlet} using the provided configuration classes. Its internal web
150      * application context is a {@link AnnotationConfigWebApplicationContext}.
151      */
152     public void initDispatcherServlet(String servletName, Class<?>... configClasses) {
153         DispatcherServlet dispatcherServlet = new DispatcherServlet();
154         dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
155         initDispatcherServlet(dispatcherServlet, servletName, classNamesToString(configClasses));
156     }
157 
158     /**
159      * Initializes a {@link DispatcherServlet} using the provided configuration classes. Its internal web
160      * application context is by default a {@link org.springframework.web.context.support.XmlWebApplicationContext}.
161      */
162     public void initDispatcherServlet(DispatcherServlet dispatcherServlet, String servletName, String configLocation) {
163         try {
164 
165             CustomServletConfig servletConfig = new CustomServletConfig(servletName, getServletContext());
166             servletConfig.addInitParameter("contextConfigLocation", configLocation);
167 
168             dispatcherServlet.init(servletConfig);
169 
170             dispatcherServlets.add(dispatcherServlet);
171 
172         } catch (ServletException e) {
173             throw new RuntimeException("Servlet initialization failed for servlet [" + servletName + "]", e);
174         }
175     }
176 
177     /**
178      * Destroys all previously initialized dispatcher servlets, including BlossomDispatcherServlets.
179      */
180     public void destroyDispatcherServlets() {
181         for (int i = dispatcherServlets.size() - 1; i >= 0; i--) {
182             dispatcherServlets.get(i).destroy();
183         }
184         dispatcherServlets.clear();
185     }
186 
187     public List<DispatcherServlet> getDispatcherServlets() {
188         return dispatcherServlets;
189     }
190 
191     public ServletContext getServletContext() {
192         return Components.getComponent(ServletContext.class);
193     }
194 
195     protected String classNamesToString(Class<?>... classes) {
196         StringBuilder sb = new StringBuilder();
197         for (Class<?> clazz : classes) {
198             if (sb.length() > 0)
199                 sb.append(",");
200             sb.append(clazz.getName());
201         }
202         return sb.toString();
203     }
204 
205     protected String[] classNamesToArray(Class<?>... classes) {
206         String[] names = new String[classes.length];
207         for (int i = 0; i < classes.length; i++) {
208             names[i] = classes[i].getName();
209         }
210         return names;
211     }
212 }