001/*
002 * (C) Copyright 2015 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 * Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.core.transientstore;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.transientstore.api.TransientStore;
026import org.nuxeo.ecm.core.transientstore.api.TransientStoreConfig;
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, TransientStore> 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            TransientStore 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    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 applicationStarted(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        super.deactivate(context);
111    }
112
113    public void cleanUpStores() {
114        stores.values().forEach(TransientStore::removeAll);
115    }
116
117}