001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Maxime Hilaire 018 * 019 */ 020 021package org.nuxeo.ecm.core.cache; 022 023import java.util.HashMap; 024import java.util.Map; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.runtime.RuntimeServiceEvent; 029import org.nuxeo.runtime.RuntimeServiceListener; 030import org.nuxeo.runtime.api.Framework; 031import org.nuxeo.runtime.model.ComponentContext; 032import org.nuxeo.runtime.model.ComponentName; 033import org.nuxeo.runtime.model.DefaultComponent; 034import org.nuxeo.runtime.model.Extension; 035 036/** 037 * Cache service implementation to manage nuxeo cache 038 * 039 * @since 6.0 040 */ 041public class CacheServiceImpl extends DefaultComponent implements CacheService { 042 043 public static final ComponentName NAME = new ComponentName(CacheServiceImpl.class.getName()); 044 045 private static final Log log = LogFactory.getLog(CacheServiceImpl.class); 046 047 protected final CacheRegistry cacheRegistry = new CacheRegistry(); 048 049 @Override 050 public CacheAttributesChecker getCache(String name) { 051 return cacheRegistry.getCache(name); 052 } 053 054 @Override 055 public void deactivate(ComponentContext context) { 056 if (cacheRegistry.caches.size() > 0) { 057 Map<String, CacheDescriptor> descriptors = new HashMap<String, CacheDescriptor>(cacheRegistry.caches); 058 for (CacheDescriptor desc : descriptors.values()) { 059 log.warn("Unregistery leaked contribution " + desc.name); 060 cacheRegistry.contributionRemoved(desc.name, desc); 061 } 062 } 063 } 064 065 @Override 066 public void applicationStarted(ComponentContext context) { 067 Framework.addListener(new RuntimeServiceListener() { 068 069 @Override 070 public void handleEvent(RuntimeServiceEvent event) { 071 if (RuntimeServiceEvent.RUNTIME_ABOUT_TO_START != event.id) { 072 return; 073 } 074 Framework.removeListener(this); 075 cacheRegistry.stop(); 076 } 077 }); 078 cacheRegistry.start(); 079 } 080 081 @Override 082 public void registerExtension(Extension extension) { 083 Object[] contribs = extension.getContributions(); 084 for (Object contrib : contribs) { 085 CacheDescriptor descriptor = (CacheDescriptor) contrib; 086 registerCache(descriptor); 087 } 088 } 089 090 public void registerCache(CacheDescriptor descriptor) { 091 cacheRegistry.addContribution(descriptor); 092 } 093 094 @Override 095 public void unregisterExtension(Extension extension) throws RuntimeException { 096 Object[] contribs = extension.getContributions(); 097 for (Object contrib : contribs) { 098 CacheDescriptor descriptor = (CacheDescriptor) contrib; 099 cacheRegistry.removeContribution(descriptor); 100 } 101 } 102 103 public void unregisterCache(CacheDescriptor descriptor) { 104 cacheRegistry.removeContribution(descriptor); 105 } 106 107}