|
| 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 org.apache.dubbo.config.spring.beans.factory.annotation; |
| 18 | + |
| 19 | +import org.apache.dubbo.common.Constants; |
| 20 | +import org.apache.dubbo.config.annotation.Reference; |
| 21 | +import org.apache.dubbo.config.annotation.Service; |
| 22 | +import org.apache.dubbo.registry.Registry; |
| 23 | + |
| 24 | +import org.springframework.core.env.Environment; |
| 25 | + |
| 26 | +import static org.apache.dubbo.common.Constants.CONSUMERS_CATEGORY; |
| 27 | +import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL; |
| 28 | +import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; |
| 29 | +import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; |
| 30 | +import static org.springframework.util.StringUtils.arrayToCommaDelimitedString; |
| 31 | +import static org.springframework.util.StringUtils.hasText; |
| 32 | + |
| 33 | +/** |
| 34 | + * The Bean Name Builder for the annotations {@link Service} and {@link Reference} |
| 35 | + * <p> |
| 36 | + * The naming rule is consistent with the the implementation {@link Registry} that is based on the service-name aware |
| 37 | + * infrastructure, e.g Spring Cloud, Cloud Native and so on. |
| 38 | + * <p> |
| 39 | + * The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}. |
| 40 | + * <p> |
| 41 | + * ${version} and ${group} are optional. |
| 42 | + * |
| 43 | + * @since 2.6.6 |
| 44 | + */ |
| 45 | +class AnnotationBeanNameBuilder { |
| 46 | + |
| 47 | + private static final String SEPARATOR = ":"; |
| 48 | + |
| 49 | + // Required properties |
| 50 | + |
| 51 | + private final String category; |
| 52 | + |
| 53 | + private final String protocol; |
| 54 | + |
| 55 | + private final String interfaceClassName; |
| 56 | + |
| 57 | + // Optional properties |
| 58 | + |
| 59 | + private String version; |
| 60 | + |
| 61 | + private String group; |
| 62 | + |
| 63 | + private Environment environment; |
| 64 | + |
| 65 | + private AnnotationBeanNameBuilder(String category, String protocol, String interfaceClassName) { |
| 66 | + this.category = category; |
| 67 | + this.protocol = protocol; |
| 68 | + this.interfaceClassName = interfaceClassName; |
| 69 | + } |
| 70 | + |
| 71 | + private AnnotationBeanNameBuilder(Service service, Class<?> interfaceClass) { |
| 72 | + this(PROVIDERS_CATEGORY, resolveProtocol(service.protocol()), resolveInterfaceName(service, interfaceClass)); |
| 73 | + this.group(service.group()); |
| 74 | + this.version(service.version()); |
| 75 | + } |
| 76 | + |
| 77 | + private AnnotationBeanNameBuilder(Reference reference, Class<?> interfaceClass) { |
| 78 | + this(CONSUMERS_CATEGORY, resolveProtocol(reference.protocol()), resolveInterfaceName(reference, interfaceClass)); |
| 79 | + this.group(reference.group()); |
| 80 | + this.version(reference.version()); |
| 81 | + } |
| 82 | + |
| 83 | + public static AnnotationBeanNameBuilder create(Service service, Class<?> interfaceClass) { |
| 84 | + return new AnnotationBeanNameBuilder(service, interfaceClass); |
| 85 | + } |
| 86 | + |
| 87 | + public static AnnotationBeanNameBuilder create(Reference reference, Class<?> interfaceClass) { |
| 88 | + return new AnnotationBeanNameBuilder(reference, interfaceClass); |
| 89 | + } |
| 90 | + |
| 91 | + private static void append(StringBuilder builder, String value) { |
| 92 | + if (hasText(value)) { |
| 93 | + builder.append(SEPARATOR).append(value); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public AnnotationBeanNameBuilder group(String group) { |
| 98 | + this.group = group; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public AnnotationBeanNameBuilder version(String version) { |
| 103 | + this.version = version; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public AnnotationBeanNameBuilder environment(Environment environment) { |
| 108 | + this.environment = environment; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Resolve the protocol |
| 114 | + * |
| 115 | + * @param protocols one or more protocols |
| 116 | + * @return if <code>protocols</code> == <code>null</code>, it will return |
| 117 | + * {@link Constants#DEFAULT_PROTOCOL "dubbo"} as the default protocol |
| 118 | + * @see Constants#DEFAULT_PROTOCOL |
| 119 | + */ |
| 120 | + private static String resolveProtocol(String... protocols) { |
| 121 | + String protocol = arrayToCommaDelimitedString(protocols); |
| 122 | + return hasText(protocol) ? protocol : DEFAULT_PROTOCOL; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Build bean name while resolve the placeholders if possible. |
| 127 | + * |
| 128 | + * @return pattern : ${category}:${protocol}:${serviceInterface}:${version}:${group} |
| 129 | + */ |
| 130 | + public String build() { |
| 131 | + // Append the required properties |
| 132 | + StringBuilder beanNameBuilder = new StringBuilder(category); |
| 133 | + append(beanNameBuilder, protocol); |
| 134 | + append(beanNameBuilder, interfaceClassName); |
| 135 | + // Append the optional properties |
| 136 | + append(beanNameBuilder, version); |
| 137 | + append(beanNameBuilder, group); |
| 138 | + String beanName = beanNameBuilder.toString(); |
| 139 | + // Resolve placeholders |
| 140 | + return environment != null ? environment.resolvePlaceholders(beanName) : beanName; |
| 141 | + } |
| 142 | +} |
0 commit comments