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

File DefaultVelocityContextFactory.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
20
2
1
119
66
7
0.35
10
2
3.5

Classes

Class Line # Actions
DefaultVelocityContextFactory 48 20 0% 7 5
0.821428682.1%
 

Contributing tests

This file is covered by 36 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.internal;
21   
22    import java.util.Enumeration;
23    import java.util.Properties;
24   
25    import javax.inject.Inject;
26    import javax.inject.Singleton;
27   
28    import org.apache.velocity.VelocityContext;
29    import org.apache.velocity.context.Context;
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.component.phase.InitializationException;
36    import org.xwiki.velocity.VelocityConfiguration;
37    import org.xwiki.velocity.VelocityContextFactory;
38    import org.xwiki.velocity.VelocityContextInitializer;
39    import org.xwiki.velocity.XWikiVelocityException;
40   
41    /**
42    * Default implementation for {@link VelocityContextFactory}.
43    *
44    * @version $Id: 5fe71addeb61681bf6811988c678a7277e0c31b4 $
45    */
46    @Component
47    @Singleton
 
48    public class DefaultVelocityContextFactory implements VelocityContextFactory, Initializable
49    {
50    /**
51    * The component manager we used to find all components implementing the
52    * {@link org.xwiki.velocity.VelocityContextInitializer} role.
53    */
54    @Inject
55    private ComponentManager componentManager;
56   
57    /**
58    * Velocity configuration to get the list of configured Velocity tools.
59    */
60    @Inject
61    private VelocityConfiguration velocityConfiguration;
62   
63    /**
64    * The logger to use for logging.
65    */
66    @Inject
67    private Logger logger;
68   
69    /**
70    * An internal read-only Velocity Context containing the Tools defined in the component's configuration. We reuse
71    * them across Contexts for better performance.
72    */
73    private Context toolsContext;
74   
 
75  1363 toggle @Override
76    public void initialize() throws InitializationException
77    {
78  1363 this.toolsContext = new VelocityContext();
79   
80    // Instantiate Velocity tools
81  1363 Properties properties = this.velocityConfiguration.getTools();
82  1363 if (properties != null) {
83  19057 for (Enumeration<?> props = properties.propertyNames(); props.hasMoreElements();) {
84  17694 String key = props.nextElement().toString();
85  17694 Object value = properties.get(key);
86  17694 Object toolInstance;
87  17694 if (value instanceof String) {
88  17694 try {
89  17694 toolInstance = Class.forName((String) value).newInstance();
90    } catch (Exception e) {
91  0 throw new InitializationException("Failed to initialize tool [" + value + "]", e);
92    }
93    } else {
94  0 toolInstance = value;
95    }
96  17694 this.toolsContext.put(key, toolInstance);
97  17694 this.logger.debug("Setting tool [{}] = [{}]", key, value);
98    }
99    }
100    }
101   
 
102  19081 toggle @Override
103    public VelocityContext createContext() throws XWikiVelocityException
104    {
105    // Note: This constructor uses the passed context as an internal read-only context.
106  19069 VelocityContext context = new VelocityContext(this.toolsContext);
107   
108    // Call all components implementing the VelocityContextInitializer's role.
109  19060 try {
110  19056 for (Object interceptor : this.componentManager.getInstanceList(VelocityContextInitializer.class)) {
111  37372 ((VelocityContextInitializer) interceptor).initialize(context);
112    }
113    } catch (ComponentLookupException e) {
114  0 throw new XWikiVelocityException("Failed to locate some Velocity Context initializers", e);
115    }
116   
117  19067 return context;
118    }
119    }