1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity

File XWikiWebappResourceLoader.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
11
3
1
77
38
6
0.55
3.67
3
2

Classes

Class Line # Actions
XWikiWebappResourceLoader 38 11 0% 6 4
0.777777877.8%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.velocity;
21   
22    import org.apache.commons.collections.ExtendedProperties;
23    import org.apache.velocity.tools.view.WebappResourceLoader;
24    import org.xwiki.component.manager.ComponentLookupException;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.environment.Environment;
27    import org.xwiki.environment.internal.ServletEnvironment;
28   
29    /**
30    * Extends the Velocity Tool's {@link WebappResourceLoader} by adding the ServletContext to the Velocity Engine's
31    * Application Attributes since this is a prerequisite for {@link WebappResourceLoader} to work correctly. This resource
32    * loader depends on the fact that the XWiki Component Manager has been set in Velocity Engine's Application Attribute
33    * prior to its initialization.
34    *
35    * @version $Id: ed119c52c8a365da5a3f755b783641431b531a10 $
36    * @since 3.0M3
37    */
 
38    public class XWikiWebappResourceLoader extends WebappResourceLoader
39    {
 
40  49 toggle @Override
41    public void init(ExtendedProperties configuration)
42    {
43  49 Environment environment = getEnvironment();
44  49 if (environment instanceof ServletEnvironment) {
45  49 this.rsvc.setApplicationAttribute("javax.servlet.ServletContext",
46    ((ServletEnvironment) environment).getServletContext());
47    }
48   
49  49 super.init(configuration);
50    }
51   
52    /**
53    * @return the Environment component implementation retrieved from the Component Manager
54    */
 
55  49 toggle private Environment getEnvironment()
56    {
57  49 try {
58  49 return getComponentManager().getInstance(Environment.class);
59    } catch (ComponentLookupException e) {
60  0 throw new RuntimeException(
61    "Cannot initialize Velocity subsystem: missing Environment component implementation");
62    }
63    }
64   
65    /**
66    * @return the Component Manager component implementation retrieved from Velocity Engine's Application Attributes
67    */
 
68  49 toggle private ComponentManager getComponentManager()
69    {
70  49 ComponentManager cm = (ComponentManager) this.rsvc.getApplicationAttribute(ComponentManager.class.getName());
71  49 if (cm == null) {
72  0 throw new RuntimeException(
73    "Cannot initialize Velocity subsystem: missing Component Manager in Velocity Application Attribute");
74    }
75  49 return cm;
76    }
77    }