001/*
002 * (C) Copyright 2017-2018 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 *     Florent Guillaume
018 */
019package org.nuxeo.runtime.kv;
020
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023
024import org.nuxeo.runtime.model.ComponentContext;
025import org.nuxeo.runtime.model.DefaultComponent;
026
027/**
028 * Implementation for the Key/Value Service.
029 *
030 * @since 9.1
031 */
032public class KeyValueServiceImpl extends DefaultComponent implements KeyValueService {
033
034    public static final String XP_CONFIG = "configuration";
035
036    public static final String DEFAULT_STORE_ID = "default";
037
038    public static final int APPLICATION_STARTED_ORDER = -500;
039
040    protected Map<String, KeyValueStoreProvider> providers = new ConcurrentHashMap<>();
041
042    @Override
043    public int getApplicationStartedOrder() {
044        return APPLICATION_STARTED_ORDER;
045    }
046
047    @Override
048    public void stop(ComponentContext context) throws InterruptedException {
049        providers.values().forEach(KeyValueStoreProvider::close);
050        providers.clear();
051        super.stop(context);
052    }
053
054    // ===== KeyValueService =====
055
056    @Override
057    public synchronized KeyValueStore getKeyValueStore(String name) {
058        KeyValueStoreProvider provider = providers.get(name);
059        if (provider == null) {
060            KeyValueStoreDescriptor descriptor = getDescriptor(XP_CONFIG, name);
061            if (descriptor == null) {
062                // instantiate a copy of the default descriptor
063                descriptor = getDescriptor(XP_CONFIG, DEFAULT_STORE_ID);
064                if (descriptor == null) {
065                    throw new RuntimeException("Missing configuration for default key/value store");
066                }
067                descriptor = new KeyValueStoreDescriptor(descriptor); // copy
068                descriptor.name = name;
069                descriptor.namespace = name; // set new namespace in copy
070            }
071            try {
072                provider = descriptor.klass.getDeclaredConstructor().newInstance();
073                provider.initialize(descriptor);
074            } catch (ReflectiveOperationException e) {
075                throw new RuntimeException(e);
076            }
077            providers.put(name, provider);
078        }
079        return provider;
080    }
081
082}