|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package group.rxcloud.capa.component.log.configuration; |
| 18 | + |
| 19 | +import com.google.common.collect.Lists; |
| 20 | +import com.google.common.collect.Maps; |
| 21 | +import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; |
| 22 | +import group.rxcloud.capa.infrastructure.hook.Mixer; |
| 23 | +import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; |
| 24 | +import group.rxcloud.cloudruntimes.utils.TypeRef; |
| 25 | +import org.apache.commons.collections.CollectionUtils; |
| 26 | +import org.apache.commons.lang3.StringUtils; |
| 27 | +import reactor.core.publisher.Flux; |
| 28 | + |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +/** |
| 33 | + * Log switch configuration |
| 34 | + */ |
| 35 | +public class LogSwitchConfiguration { |
| 36 | + |
| 37 | + /** |
| 38 | + * Capa configuration hooks. |
| 39 | + */ |
| 40 | + private static final Optional<ConfigurationHooks> configurationHooks; |
| 41 | + /** |
| 42 | + * Log switch config file name. |
| 43 | + */ |
| 44 | + private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "log-level-switch.properties"; |
| 45 | + /** |
| 46 | + * Log switch config's value. |
| 47 | + */ |
| 48 | + private static Map<String, Boolean> logsSwitchConfig = Maps.newHashMap(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Init configuration hooks and subscribe configuration. |
| 52 | + */ |
| 53 | + static { |
| 54 | + configurationHooks = Mixer.configurationHooksNullable(); |
| 55 | + configurationHooks.ifPresent(configurationHooks -> { |
| 56 | + subscribeConfiguration(configurationHooks); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Subscribe configuration. |
| 62 | + * |
| 63 | + * @param configurationHooks |
| 64 | + */ |
| 65 | + private static void subscribeConfiguration(ConfigurationHooks configurationHooks) { |
| 66 | + String storeName = configurationHooks.registryStoreNames().get(0); |
| 67 | + String appId = configurationHooks.defaultConfigurationAppId(); |
| 68 | + Flux<SubConfigurationResp<Map>> configFlux = configurationHooks.subscribeConfiguration( |
| 69 | + storeName, |
| 70 | + appId, |
| 71 | + Lists.newArrayList(LOGS_SWITCH_CONFIG_FILE_NAME), |
| 72 | + null, |
| 73 | + StringUtils.EMPTY, |
| 74 | + StringUtils.EMPTY, |
| 75 | + TypeRef.get(Map.class)); |
| 76 | + configFlux.subscribe(resp -> { |
| 77 | + if (CollectionUtils.isNotEmpty(resp.getItems())) { |
| 78 | + logsSwitchConfig = resp.getItems().get(0).getContent(); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the log switch config's values. |
| 85 | + * |
| 86 | + * @return the log switch config's values. |
| 87 | + */ |
| 88 | + public static Map<String, Boolean> getLogsSwitchConfig() { |
| 89 | + return logsSwitchConfig; |
| 90 | + } |
| 91 | +} |
0 commit comments