001/*
002 * (C) Copyright 2014 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Maxime Hilaire
016 *
017 */
018
019package org.nuxeo.ecm.core.cache;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.runtime.RuntimeServiceEvent;
027import org.nuxeo.runtime.RuntimeServiceListener;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.ComponentName;
031import org.nuxeo.runtime.model.DefaultComponent;
032import org.nuxeo.runtime.model.Extension;
033
034/**
035 * Cache service implementation to manage nuxeo cache
036 *
037 * @since 6.0
038 */
039public class CacheServiceImpl extends DefaultComponent implements CacheService {
040
041    public static final ComponentName NAME = new ComponentName(CacheServiceImpl.class.getName());
042
043    private static final Log log = LogFactory.getLog(CacheServiceImpl.class);
044
045    protected final CacheRegistry cacheRegistry = new CacheRegistry();
046
047    @Override
048    public CacheAttributesChecker getCache(String name) {
049        return cacheRegistry.getCache(name);
050    }
051
052    @Override
053    public void deactivate(ComponentContext context) {
054        if (cacheRegistry.caches.size() > 0) {
055            Map<String, CacheDescriptor> descriptors = new HashMap<String, CacheDescriptor>(cacheRegistry.caches);
056            for (CacheDescriptor desc : descriptors.values()) {
057                log.warn("Unregistery leaked contribution " + desc.name);
058                cacheRegistry.contributionRemoved(desc.name, desc);
059            }
060        }
061    }
062
063    @Override
064    public void applicationStarted(ComponentContext context) {
065        Framework.addListener(new RuntimeServiceListener() {
066
067            @Override
068            public void handleEvent(RuntimeServiceEvent event) {
069                if (RuntimeServiceEvent.RUNTIME_ABOUT_TO_START != event.id) {
070                    return;
071                }
072                Framework.removeListener(this);
073                cacheRegistry.stop();
074            }
075        });
076        cacheRegistry.start();
077    }
078
079    @Override
080    public void registerExtension(Extension extension) {
081        Object[] contribs = extension.getContributions();
082        for (Object contrib : contribs) {
083            CacheDescriptor descriptor = (CacheDescriptor) contrib;
084            registerCache(descriptor);
085        }
086    }
087
088    public void registerCache(CacheDescriptor descriptor) {
089        cacheRegistry.addContribution(descriptor);
090    }
091
092    @Override
093    public void unregisterExtension(Extension extension) throws RuntimeException {
094        Object[] contribs = extension.getContributions();
095        for (Object contrib : contribs) {
096            CacheDescriptor descriptor = (CacheDescriptor) contrib;
097            cacheRegistry.removeContribution(descriptor);
098        }
099    }
100
101    public void unregisterCache(CacheDescriptor descriptor) {
102        cacheRegistry.removeContribution(descriptor);
103    }
104
105}