1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.web

File InstallationAwareServletProxy.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
65% of files have more coverage

Code metrics

8
28
5
1
127
70
12
0.43
5.6
5
2.4

Classes

Class Line # Actions
InstallationAwareServletProxy 59 28 0% 12 41
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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.web;
35   
36    import java.io.IOException;
37    import javax.servlet.Servlet;
38    import javax.servlet.ServletConfig;
39    import javax.servlet.ServletException;
40    import javax.servlet.ServletRequest;
41    import javax.servlet.ServletResponse;
42    import javax.servlet.http.HttpServlet;
43   
44    import info.magnolia.module.ModuleManager;
45    import info.magnolia.module.blossom.support.CustomServletConfig;
46    import info.magnolia.objectfactory.Components;
47   
48    /**
49    * Proxy that postpones initialization of its target servlet while Magnolia is in update/install-mode. The servlet is
50    * initialized when it sees the first request after the update/installation phase is finished. It looks for an init
51    * parameter named 'servletClass' to find the class of the target servlet. Subclasses can override this behaviour. This
52    * is an alternative to running the servlet in the Magnolia filter chain. Any servlets configured in web.xml need to
53    * have a bypass configured on the Magnolia filter chain that matches its servlet mapping.
54    * <p/>
55    * Initialization of the target servlet will happen after all modules have started.
56    *
57    * @since 1.2
58    */
 
59    public class InstallationAwareServletProxy extends HttpServlet {
60   
61    private static final String SERVLET_CLASS_PARAMETER = "servletClass";
62   
63    private Servlet targetServlet;
64    private boolean initialized = false;
65    private final Object monitor = new Object();
66   
 
67  0 toggle @Override
68    public void init(ServletConfig config) throws ServletException {
69  0 super.init(config);
70  0 synchronized (monitor) {
71  0 if (canInitialize()) {
72  0 targetServlet = initTargetServlet();
73  0 initialized = true;
74    }
75    }
76    }
77   
 
78  0 toggle @Override
79    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
80  0 synchronized (monitor) {
81  0 if (!initialized) {
82    // Since the InstallFilter blocks all requests until the modules have started up we can safely init
83    // here. In fact this check is probably not necessary.
84  0 if (canInitialize()) {
85  0 targetServlet = initTargetServlet();
86  0 initialized = true;
87    } else {
88  0 return;
89    }
90    }
91    }
92  0 targetServlet.service(req, res);
93    }
94   
 
95  0 toggle @Override
96    public void destroy() {
97  0 synchronized (monitor) {
98  0 if (!initialized) {
99  0 return;
100    }
101    }
102  0 targetServlet.destroy();
103    }
104   
 
105  0 toggle protected Servlet initTargetServlet() throws ServletException {
106  0 ServletConfig config = getServletConfig();
107  0 Servlet servlet;
108  0 try {
109  0 servlet = (Servlet) Class.forName(config.getInitParameter(SERVLET_CLASS_PARAMETER)).newInstance();
110    } catch (ClassNotFoundException e) {
111  0 throw new ServletException(e);
112    } catch (InstantiationException e) {
113  0 throw new ServletException(e);
114    } catch (IllegalAccessException e) {
115  0 throw new ServletException(e);
116    }
117  0 CustomServletConfig customServletConfig = new CustomServletConfig(config);
118  0 customServletConfig.removeInitParameter(SERVLET_CLASS_PARAMETER);
119  0 servlet.init(customServletConfig);
120  0 return servlet;
121    }
122   
 
123  0 toggle protected boolean canInitialize() {
124    // This is the same criteria used by MgnlMainFilter when it decides if it should use the update/install -UI.
125  0 return !Components.getComponent(ModuleManager.class).getStatus().needsUpdateOrInstall();
126    }
127    }