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