/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.velocity.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.management.JMXBeanRegistration;
import org.xwiki.velocity.VelocityEngine;
import org.xwiki.velocity.VelocityFactory;
import org.xwiki.velocity.XWikiVelocityException;
import org.xwiki.velocity.internal.jmx.JMXVelocityEngine;
import org.xwiki.velocity.internal.jmx.JMXVelocityEngineMBean;

/**
 * Default implementation for {@link VelocityFactory}.
 * 
 * @see VelocityFactory
 * @version $Id$
 */
@Component
@Singleton
public class DefaultVelocityFactory implements VelocityFactory
{
    /**
     * The Component manager we use to lookup (and thus create since it's a singleton) the VelocityEngine component.
     */
    @Inject
    private ComponentManager componentManager;

    /**
     * In order to register the Velocity MBean for management.
     */
    @Inject
    private JMXBeanRegistration jmxRegistration;

    /**
     * A cache of Velocity Engines. See {@link org.xwiki.velocity.VelocityFactory} for more details as to why we need
     * this cache.
     */
    private Map<String, VelocityEngine> velocityEngines = new HashMap<String, VelocityEngine>();

    @Override
    public synchronized boolean hasVelocityEngine(String key)
    {
        return this.velocityEngines.containsKey(key);
    }

    @Override
    public synchronized VelocityEngine getVelocityEngine(String key)
    {
        return this.velocityEngines.get(key);
    }

    @Override
    public synchronized VelocityEngine createVelocityEngine(String key, Properties properties)
        throws XWikiVelocityException
    {
        VelocityEngine engine;
        try {
            engine = this.componentManager.getInstance(VelocityEngine.class);
        } catch (ComponentLookupException e) {
            throw new XWikiVelocityException("Failed to create Velocity Engine", e);
        }
        engine.initialize(properties);
        this.velocityEngines.put(key, engine);

        // Register a JMX MBean for providing information about the created Velocity Engine (template namespaces,
        // macros, etc).
        JMXVelocityEngineMBean mbean = new JMXVelocityEngine(engine);
        this.jmxRegistration.registerMBean(mbean, "type=Velocity,domain=Engines,name=" + key);

        return engine;
    }
}

@@ -942,20 +942,16 @@
 va.util.
-Hash
 Map;%0Aimp
@@ -956,35 +956,42 @@
 mport java.util.
-Map
+Properties
 ;%0Aimport java.ut
@@ -993,26 +993,44 @@
 va.util.
-Properties
+concurrent.ConcurrentHashMap
 ;%0A%0Aimpor
@@ -2297,16 +2297,26 @@
 s = new 
+Concurrent
 HashMap%3C
@@ -2368,29 +2368,16 @@
  public 
-synchronized 
 boolean 
@@ -2494,37 +2494,24 @@
 %0A    public 
-synchronized 
 VelocityEngi
@@ -2631,21 +2631,8 @@
 lic 
-synchronized 
 Velo

Replace synchronized methods by the use of a Concurrent Hash Map for improved performances (see also XWIKI-7860: "Failed to register resource with name [type=Velocity,domain=Engines,name=default]" error message).