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

File DefaultVelocityFactory.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

0
13
4
1
108
57
5
0.38
3.25
4
1.25

Classes

Class Line # Actions
DefaultVelocityFactory 47 13 0% 5 6
0.6470588464.7%
 

Contributing tests

This file is covered by 18 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.Map;
23    import java.util.Properties;
24    import java.util.concurrent.ConcurrentHashMap;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.management.JMXBeanRegistration;
33    import org.xwiki.velocity.VelocityEngine;
34    import org.xwiki.velocity.VelocityFactory;
35    import org.xwiki.velocity.XWikiVelocityException;
36    import org.xwiki.velocity.internal.jmx.JMXVelocityEngine;
37    import org.xwiki.velocity.internal.jmx.JMXVelocityEngineMBean;
38   
39    /**
40    * Default implementation for {@link VelocityFactory}.
41    *
42    * @see VelocityFactory
43    * @version $Id: 2cd41bff44faf49b18f86f220764e82620e97cb4 $
44    */
45    @Component
46    @Singleton
 
47    public class DefaultVelocityFactory implements VelocityFactory
48    {
49    private static final String MBEANNAME_PREFIX = "type=Velocity,domain=Engines,name=";
50   
51    /**
52    * The Component manager we use to lookup (and thus create since it's a singleton) the VelocityEngine component.
53    */
54    @Inject
55    private ComponentManager componentManager;
56   
57    /**
58    * In order to register the Velocity MBean for management.
59    */
60    @Inject
61    private JMXBeanRegistration jmxRegistration;
62   
63    /**
64    * A cache of Velocity Engines. See {@link org.xwiki.velocity.VelocityFactory} for more details as to why we need
65    * this cache.
66    */
67    private Map<String, VelocityEngine> velocityEngines = new ConcurrentHashMap<String, VelocityEngine>();
68   
 
69  0 toggle @Override
70    public boolean hasVelocityEngine(String key)
71    {
72  0 return this.velocityEngines.containsKey(key);
73    }
74   
 
75  161328 toggle @Override
76    public VelocityEngine getVelocityEngine(String key)
77    {
78  161323 return this.velocityEngines.get(key);
79    }
80   
 
81  50 toggle @Override
82    public VelocityEngine createVelocityEngine(String key, Properties properties) throws XWikiVelocityException
83    {
84  50 VelocityEngine engine;
85  50 try {
86  50 engine = this.componentManager.getInstance(VelocityEngine.class);
87    } catch (ComponentLookupException e) {
88  0 throw new XWikiVelocityException("Failed to create Velocity Engine", e);
89    }
90  50 engine.initialize(properties);
91  50 this.velocityEngines.put(key, engine);
92   
93    // Register a JMX MBean for providing information about the created Velocity Engine (template namespaces,
94    // macros, etc).
95  50 JMXVelocityEngineMBean mbean = new JMXVelocityEngine(engine);
96  50 this.jmxRegistration.registerMBean(mbean, MBEANNAME_PREFIX + key);
97   
98  50 return engine;
99    }
100   
 
101  0 toggle @Override
102    public VelocityEngine removeVelocityEngine(String key)
103    {
104  0 this.jmxRegistration.unregisterMBean(MBEANNAME_PREFIX + key);
105   
106  0 return this.velocityEngines.remove(key);
107    }
108    }