001/* 002 * (C) Copyright 2015 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.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 * Nuxeo - initial API and implementation 016 */ 017 018package org.nuxeo.ecm.core.transientstore; 019 020import java.util.HashMap; 021import java.util.Map; 022 023import org.nuxeo.ecm.core.transientstore.api.TransientStore; 024import org.nuxeo.ecm.core.transientstore.api.TransientStoreConfig; 025import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 026import org.nuxeo.runtime.model.ComponentContext; 027import org.nuxeo.runtime.model.ComponentInstance; 028import org.nuxeo.runtime.model.DefaultComponent; 029 030/** 031 * Component exposing the {@link TransientStoreService} and managing the unerlying extension point 032 * 033 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 034 * @since 7.2 035 */ 036public class TransientStorageComponent extends DefaultComponent implements TransientStoreService { 037 038 protected Map<String, TransientStoreConfig> configs = new HashMap<>(); 039 040 protected Map<String, TransientStore> stores = new HashMap<>(); 041 042 public static final String EP_STORE = "store"; 043 044 public static final String DEFAULT_STORE_NAME = "default"; 045 046 @Override 047 public TransientStore getStore(String name) { 048 TransientStore store = stores.get(name); 049 if (store == null) { 050 store = stores.get(DEFAULT_STORE_NAME); 051 if (store == null) { 052 store = registerDefaultStore(); 053 } 054 } 055 return store; 056 } 057 058 protected TransientStore registerDefaultStore() { 059 TransientStoreConfig defaultConfig = new TransientStoreConfig(DEFAULT_STORE_NAME); 060 TransientStore store = defaultConfig.getStore(); 061 stores.put(defaultConfig.getName(), store); 062 return store; 063 } 064 065 public void doGC() { 066 stores.values().forEach(TransientStore::doGC); 067 } 068 069 @Override 070 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 071 if (EP_STORE.equals(extensionPoint)) { 072 TransientStoreConfig config = (TransientStoreConfig) contribution; 073 // XXX merge 074 configs.put(config.getName(), config); 075 } 076 } 077 078 @Override 079 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 080 if (EP_STORE.equals(extensionPoint)) { 081 TransientStoreConfig config = (TransientStoreConfig) contribution; 082 TransientStore store = stores.get(config.getName()); 083 store.shutdown(); 084 } 085 } 086 087 @Override 088 public void applicationStarted(ComponentContext context) { 089 for (TransientStoreConfig config : configs.values()) { 090 registerStore(config); 091 } 092 } 093 094 protected TransientStore registerStore(TransientStoreConfig config) { 095 TransientStore store = config.getStore(); 096 stores.put(config.getName(), store); 097 return store; 098 } 099 100 @Override 101 public void deactivate(ComponentContext context) { 102 stores.values().forEach(TransientStore::shutdown); 103 super.deactivate(context); 104 } 105 106}