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