/*
 * 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.wiki.internal.manager;

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

import org.xwiki.cache.Cache;
import org.xwiki.cache.CacheException;
import org.xwiki.cache.CacheFactory;
import org.xwiki.cache.CacheManager;
import org.xwiki.cache.config.CacheConfiguration;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;

/**
 * Default implementation of {@link WikiDescriptorCache}.
 * @version $Id$
 * @since 5.3M2
 */
@Component
@Singleton
public class DefaultWikiDescriptorCache implements WikiDescriptorCache, Initializable
{
    @Inject
    private CacheManager cacheManager;

    private Cache<DefaultWikiDescriptor> wikiAliasCache;

    private Cache<DefaultWikiDescriptor> wikiIdCache;

    @Override
    public void initialize() throws InitializationException
    {
        this.wikiAliasCache = createCache("wiki.descriptor.cache.wikiAlias");
        this.wikiIdCache = createCache("wiki.descriptor.cache.wikiId");
    }

    private Cache<DefaultWikiDescriptor> createCache(String cacheId) throws InitializationException
    {
        Cache<DefaultWikiDescriptor> cache;

        CacheConfiguration configuration = new CacheConfiguration(cacheId);

        final String errorMessage = "Failed to initialize wiki descriptor caches [%s]";

        try {
            CacheFactory cacheFactory = cacheManager.getCacheFactory();
            cache = cacheFactory.newCache(configuration);
        } catch (CacheException e) {
            throw new InitializationException(String.format(errorMessage, configuration.getConfigurationId()), e);
        } catch (ComponentLookupException e) {
            throw new InitializationException(String.format(errorMessage, configuration.getConfigurationId()), e);
        }

        return cache;
    }

    @Override
    public void add(DefaultWikiDescriptor descriptor)
    {
        // Update the wiki name cache
        this.wikiIdCache.set(descriptor.getId(), descriptor);

        // Update the wiki alias cache
        for (String alias : descriptor.getAliases()) {
            this.wikiAliasCache.set(alias, descriptor);
        }
    }

    @Override
    public void remove(DefaultWikiDescriptor descriptor)
    {
        // Remove from the wiki name cache
        this.wikiIdCache.remove(descriptor.getId());

        // Remove from the wiki alias cache
        for (String alias : descriptor.getAliases()) {
            this.wikiAliasCache.remove(alias);
        }
    }

    @Override
    public DefaultWikiDescriptor getFromId(String wikiId)
    {
        return wikiIdCache.get(wikiId);
    }

    @Override
    public DefaultWikiDescriptor getFromAlias(String wikiAlias)
    {
        return wikiAliasCache.get(wikiAlias);
    }
}

@@ -1062,45 +1062,8 @@
 on;%0A
-import org.xwiki.cache.CacheFactory;%0A
 impo
@@ -1198,69 +1198,8 @@
 nt;%0A
-import org.xwiki.component.manager.ComponentLookupException;%0A
 impo
@@ -1428,16 +1428,20 @@
 Cache%7D.%0A
+ * %0A
  * @vers
@@ -2091,53 +2091,8 @@
   %7B%0A
-        Cache%3CDefaultWikiDescriptor%3E cache;%0A%0A
     
@@ -2176,505 +2176,246 @@
     
-final String errorMessage = %22Failed to initialize wiki descriptor caches %5B%25s%5D%22;%0A%0A        try %7B%0A            CacheFactory cacheFactory = cacheManager.getCacheFactory();%0A            cache = cacheFactory.newCache(configuration);%0A        %7D catch (CacheException e) %7B%0A            throw new InitializationException(String.format(errorMessage, configuration.getConfigurationId()), e);%0A        %7D catch (ComponentLookupException e) %7B%0A            throw new InitializationException(String.format(errorMessage,
+try %7B%0A            return this.cacheManager.createNewCache(configuration);%0A        %7D catch (CacheException e) %7B%0A            throw new InitializationException(String.format(%22Failed to initialize wiki descriptor caches %5B%25s%5D%22,%0A               
  con
@@ -2465,31 +2465,8 @@
    %7D
-%0A%0A        return cache;
 %0A   

Improve cache initialization