001/** 002 * Copyright (C) 2006-2025 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.runtime.manager.service; 017 018import static java.util.Optional.ofNullable; 019import static java.util.stream.Collectors.toSet; 020 021import java.io.ObjectStreamException; 022import java.io.Serializable; 023import java.util.Collection; 024import java.util.Objects; 025import java.util.Set; 026import java.util.stream.Stream; 027 028import org.talend.sdk.component.api.service.configuration.LocalConfiguration; 029import org.talend.sdk.component.runtime.serialization.SerializableService; 030 031import lombok.AllArgsConstructor; 032 033@AllArgsConstructor 034public class LocalConfigurationService implements LocalConfiguration, Serializable { 035 036 private final Collection<LocalConfiguration> rawDelegates; 037 038 private final String plugin; 039 040 @Override 041 public String get(final String key) { 042 return rawDelegates 043 .stream() 044 .map(d -> read(d, plugin + "." + key)) 045 .filter(Objects::nonNull) 046 .findFirst() 047 .orElseGet(() -> rawDelegates // fallback on direct keys 048 .stream() 049 .map(d -> read(d, key)) 050 .filter(Objects::nonNull) 051 .findFirst() 052 .orElse(null)); 053 } 054 055 @Override 056 public Set<String> keys() { 057 return rawDelegates 058 .stream() 059 .flatMap(d -> d.keys().stream()) 060 .flatMap(k -> !k.startsWith(plugin + '.') ? Stream.of(k) 061 : Stream.of(k, k.substring(plugin.length() + 1))) 062 .collect(toSet()); 063 } 064 065 private String read(final LocalConfiguration d, final String entryKey) { 066 return ofNullable(d.get(entryKey)).orElseGet(() -> d.get(entryKey.replace('.', '_'))); 067 } 068 069 Object writeReplace() throws ObjectStreamException { 070 return new SerializableService(plugin, LocalConfiguration.class.getName()); 071 } 072}