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 static java.util.Optional.ofNullable; 019import static java.util.function.Function.identity; 020import static java.util.stream.Collectors.toMap; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.util.HashMap; 027import java.util.Map; 028import java.util.Properties; 029import java.util.Set; 030import java.util.stream.StreamSupport; 031 032import javax.enterprise.event.Observes; 033import javax.enterprise.inject.Vetoed; 034import javax.enterprise.inject.spi.AfterDeploymentValidation; 035import javax.enterprise.inject.spi.Extension; 036 037import org.apache.meecrowave.Meecrowave; 038import org.apache.meecrowave.runner.cli.CliOption; 039import org.eclipse.microprofile.config.Config; 040import org.eclipse.microprofile.config.spi.ConfigSource; 041 042import lombok.Data; 043 044@Vetoed 045public class ComponentConfigurationLoader implements ConfigSource { 046 047 private final Map<String, String> map = new HashMap<>(); 048 049 private void doInit(final Meecrowave.Builder builder) { 050 map.putAll(asMap(builder.getProperties())); 051 ofNullable(builder.getExtension(Cli.class).getConfiguration()).ifPresent(configuration -> { 052 final File file = new File(configuration); 053 if (file.exists()) { 054 try (final InputStream is = new FileInputStream(file)) { 055 map.putAll(load(is)); 056 } catch (final IOException e) { 057 throw new IllegalArgumentException(e); 058 } 059 } else { 060 final ClassLoader loader = Thread.currentThread().getContextClassLoader(); 061 try (final InputStream is = loader.getResourceAsStream(configuration)) { 062 if (is != null) { 063 map.putAll(load(is)); 064 } 065 } catch (final IOException e) { 066 throw new IllegalArgumentException(e); 067 } 068 } 069 }); 070 } 071 072 @Override 073 public Map<String, String> getProperties() { 074 return map; 075 } 076 077 @Override 078 public Set<String> getPropertyNames() { 079 return map.keySet(); 080 } 081 082 @Override 083 public String getValue(final String key) { 084 return getProperties().get(key); 085 } 086 087 @Override 088 public String getName() { 089 return "component-configuration"; 090 } 091 092 @Override 093 public int getOrdinal() { 094 return 1000; 095 } 096 097 private Map<String, String> load(final InputStream is) throws IOException { 098 final Properties properties = new Properties(); 099 properties.load(is); 100 return asMap(properties); 101 } 102 103 private Map<String, String> asMap(final Properties properties) { 104 return properties.stringPropertyNames().stream().collect(toMap(identity(), properties::getProperty)); 105 } 106 107 @Data 108 public static class Cli { 109 110 @CliOption(name = "component-configuration", description = "The file containing application configuration") 111 private String configuration; 112 } 113 114 public static class Init implements Extension { 115 116 void doInit(@Observes final AfterDeploymentValidation afterDeploymentValidation, final Config config, 117 final Meecrowave.Builder builder) { 118 StreamSupport 119 .stream(config.getConfigSources().spliterator(), false) 120 .filter(ComponentConfigurationLoader.class::isInstance) 121 .map(ComponentConfigurationLoader.class::cast) 122 .forEach(it -> it.doInit(builder)); 123 } 124 } 125}