001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
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 */
016package org.talend.sdk.component.server.configuration;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Properties;
023import java.util.Set;
024
025import javax.enterprise.inject.Vetoed;
026
027import org.eclipse.microprofile.config.spi.ConfigSource;
028
029@Vetoed
030public class ScmConfigurationLoader implements ConfigSource {
031
032    private final Map<String, String> map = new HashMap<>();
033
034    private volatile boolean init = false;
035
036    private void ensureInit() {
037        if (init) {
038            return;
039        }
040        synchronized (this) {
041            if (init) {
042                return;
043            }
044            try (final InputStream stream =
045                    Thread.currentThread().getContextClassLoader().getResourceAsStream("TALEND-INF/git.properties")) {
046                final Properties properties = new Properties() {
047
048                    {
049                        try {
050                            load(stream);
051                        } catch (final IOException e) {
052                            throw new IllegalStateException(e);
053                        }
054                    }
055                };
056                properties
057                        .stringPropertyNames()
058                        .stream()
059                        .collect(() -> map, (m, k) -> m.put(k, properties.getProperty(k)), Map::putAll);
060            } catch (final IOException e) {
061                throw new IllegalStateException(e);
062            }
063            init = true;
064        }
065    }
066
067    @Override
068    public Map<String, String> getProperties() {
069        ensureInit();
070        return map;
071    }
072
073    @Override
074    public Set<String> getPropertyNames() {
075        ensureInit();
076        return map.keySet();
077    }
078
079    @Override
080    public int getOrdinal() {
081        return 1000;
082    }
083
084    @Override
085    public String getValue(final String propertyName) {
086        ensureInit();
087        return map.get(propertyName);
088    }
089
090    @Override
091    public String getName() {
092        return "component-scm-configuration";
093    }
094}