|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2024 Team Fangkehou |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.dapr.spring.boot.cloudconfig.configdata.config; |
| 18 | + |
| 19 | +import io.dapr.spring.boot.autoconfigure.client.DaprClientProperties; |
| 20 | +import io.dapr.spring.boot.cloudconfig.config.DaprCloudConfigClientManager; |
| 21 | +import io.dapr.spring.boot.cloudconfig.config.DaprCloudConfigProperties; |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.springframework.boot.BootstrapRegistry; |
| 24 | +import org.springframework.boot.ConfigurableBootstrapContext; |
| 25 | +import org.springframework.boot.context.config.*; |
| 26 | +import org.springframework.boot.context.properties.bind.BindHandler; |
| 27 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 28 | +import org.springframework.boot.context.properties.bind.Binder; |
| 29 | +import org.springframework.boot.logging.DeferredLogFactory; |
| 30 | +import org.springframework.core.Ordered; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class DaprConfigurationConfigDataLocationResolver |
| 36 | + implements ConfigDataLocationResolver<DaprConfigurationConfigDataResource>, Ordered { |
| 37 | + |
| 38 | + public static final String PREFIX = "dapr:config:"; |
| 39 | + |
| 40 | + private final Log log; |
| 41 | + |
| 42 | + public DaprConfigurationConfigDataLocationResolver(DeferredLogFactory logFactory) { |
| 43 | + this.log = logFactory.getLog(getClass()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns if the specified location address contains dapr prefix. |
| 48 | + * |
| 49 | + * @param context the location resolver context |
| 50 | + * @param location the location to check. |
| 51 | + * @return if the location is supported by this resolver |
| 52 | + */ |
| 53 | + @Override |
| 54 | + public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) { |
| 55 | + log.debug(String.format("checking if %s suits for dapr config", location.toString())); |
| 56 | + return location.hasPrefix(PREFIX); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Resolve a {@link ConfigDataLocation} into one or more {@link ConfigDataResource} instances. |
| 61 | + * |
| 62 | + * @param context the location resolver context |
| 63 | + * @param location the location that should be resolved |
| 64 | + * @return a list of {@link ConfigDataResource resources} in ascending priority order. |
| 65 | + * @throws ConfigDataLocationNotFoundException on a non-optional location that cannot be found |
| 66 | + * @throws ConfigDataResourceNotFoundException if a resolved resource cannot be found |
| 67 | + */ |
| 68 | + @Override |
| 69 | + public List<DaprConfigurationConfigDataResource> resolve(ConfigDataLocationResolverContext context, |
| 70 | + ConfigDataLocation location) |
| 71 | + throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException { |
| 72 | + |
| 73 | + DaprCloudConfigProperties daprSecretStoreConfig = loadProperties(context); |
| 74 | + DaprClientProperties daprClientConfig = loadClientProperties(context); |
| 75 | + |
| 76 | + ConfigurableBootstrapContext bootstrapContext = context |
| 77 | + .getBootstrapContext(); |
| 78 | + |
| 79 | + registerConfigManager(daprSecretStoreConfig, daprClientConfig, bootstrapContext); |
| 80 | + |
| 81 | + List<DaprConfigurationConfigDataResource> result = new ArrayList<>(); |
| 82 | + |
| 83 | + String[] secretConfig = location.getNonPrefixedValue(PREFIX).split("/"); |
| 84 | + |
| 85 | + log.info(String.format("there is %d of values in secretConfig", secretConfig.length)); |
| 86 | + |
| 87 | + switch (secretConfig.length) { |
| 88 | + case 2: |
| 89 | + log.debug("Dapr Secret Store now gains store name: '" + secretConfig[0] + "' and secret name: '" |
| 90 | + + secretConfig[1] + "' secret store for config"); |
| 91 | + result.add( |
| 92 | + new DaprConfigurationConfigDataResource(location.isOptional(), secretConfig[0], secretConfig[1])); |
| 93 | + break; |
| 94 | + case 1: |
| 95 | + log.debug("Dapr Secret Store now gains store name: '" + secretConfig[0] + "' secret store for config"); |
| 96 | + result.add(new DaprConfigurationConfigDataResource(location.isOptional(), secretConfig[0], null)); |
| 97 | + break; |
| 98 | + default: |
| 99 | + throw new ConfigDataLocationNotFoundException(location); |
| 100 | + } |
| 101 | + |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public int getOrder() { |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + private void registerConfigManager(DaprCloudConfigProperties properties, |
| 114 | + DaprClientProperties clientConfig, |
| 115 | + ConfigurableBootstrapContext bootstrapContext) { |
| 116 | + if (!bootstrapContext.isRegistered(DaprCloudConfigClientManager.class)) { |
| 117 | + bootstrapContext.register(DaprCloudConfigClientManager.class, |
| 118 | + BootstrapRegistry.InstanceSupplier |
| 119 | + .of(new DaprCloudConfigClientManager(properties, clientConfig))); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + protected DaprCloudConfigProperties loadProperties( |
| 124 | + ConfigDataLocationResolverContext context) { |
| 125 | + Binder binder = context.getBinder(); |
| 126 | + BindHandler bindHandler = getBindHandler(context); |
| 127 | + |
| 128 | + DaprCloudConfigProperties daprCloudConfigProperties; |
| 129 | + if (context.getBootstrapContext().isRegistered(DaprCloudConfigProperties.class)) { |
| 130 | + daprCloudConfigProperties = context.getBootstrapContext() |
| 131 | + .get(DaprCloudConfigProperties.class); |
| 132 | + } else { |
| 133 | + daprCloudConfigProperties = binder |
| 134 | + .bind(DaprCloudConfigProperties.PROPERTY_PREFIX, Bindable.of(DaprCloudConfigProperties.class), |
| 135 | + bindHandler) |
| 136 | + .orElseGet(DaprCloudConfigProperties::new); |
| 137 | + } |
| 138 | + |
| 139 | + return daprCloudConfigProperties; |
| 140 | + } |
| 141 | + |
| 142 | + protected DaprClientProperties loadClientProperties( |
| 143 | + ConfigDataLocationResolverContext context) { |
| 144 | + Binder binder = context.getBinder(); |
| 145 | + BindHandler bindHandler = getBindHandler(context); |
| 146 | + |
| 147 | + DaprClientProperties daprClientConfig; |
| 148 | + if (context.getBootstrapContext().isRegistered(DaprClientProperties.class)) { |
| 149 | + daprClientConfig = context.getBootstrapContext() |
| 150 | + .get(DaprClientProperties.class); |
| 151 | + } else { |
| 152 | + daprClientConfig = binder |
| 153 | + .bind(DaprClientProperties.PROPERTY_PREFIX, Bindable.of(DaprClientProperties.class), |
| 154 | + bindHandler) |
| 155 | + .orElseGet(DaprClientProperties::new); |
| 156 | + } |
| 157 | + |
| 158 | + return daprClientConfig; |
| 159 | + } |
| 160 | + |
| 161 | + private BindHandler getBindHandler(ConfigDataLocationResolverContext context) { |
| 162 | + return context.getBootstrapContext().getOrElse(BindHandler.class, null); |
| 163 | + } |
| 164 | +} |
0 commit comments