|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 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 | + * https://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 org.springframework.boot.autoconfigure.service.connection; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import org.springframework.core.ResolvableType; |
| 24 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
| 25 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
| 26 | +import org.springframework.core.style.ToStringCreator; |
| 27 | + |
| 28 | +/** |
| 29 | + * A registry of {@link ConnectionDetailsFactory} instances. |
| 30 | + * |
| 31 | + * @author Moritz Halbritter |
| 32 | + * @author Andy Wilkinson |
| 33 | + * @author Phillip Webb |
| 34 | + * @since 3.1.0 |
| 35 | + */ |
| 36 | +public class ConnectionDetailsFactories { |
| 37 | + |
| 38 | + private List<FactoryDetails> registeredFactories = new ArrayList<>(); |
| 39 | + |
| 40 | + public ConnectionDetailsFactories() { |
| 41 | + this(SpringFactoriesLoader.forDefaultResourceLocation(ConnectionDetailsFactory.class.getClassLoader())); |
| 42 | + } |
| 43 | + |
| 44 | + @SuppressWarnings("rawtypes") |
| 45 | + ConnectionDetailsFactories(SpringFactoriesLoader loader) { |
| 46 | + List<ConnectionDetailsFactory> factories = loader.load(ConnectionDetailsFactory.class); |
| 47 | + factories.stream().map(this::factoryDetails).filter(Objects::nonNull).forEach(this::register); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + private FactoryDetails factoryDetails(ConnectionDetailsFactory<?, ?> factory) { |
| 52 | + ResolvableType connectionDetailsFactory = findConnectionDetailsFactory( |
| 53 | + ResolvableType.forClass(factory.getClass())); |
| 54 | + if (connectionDetailsFactory != null) { |
| 55 | + ResolvableType input = connectionDetailsFactory.getGeneric(0); |
| 56 | + ResolvableType output = connectionDetailsFactory.getGeneric(1); |
| 57 | + return new FactoryDetails(input.getRawClass(), (Class<? extends ConnectionDetails>) output.getRawClass(), |
| 58 | + factory); |
| 59 | + } |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + private ResolvableType findConnectionDetailsFactory(ResolvableType type) { |
| 64 | + try { |
| 65 | + ResolvableType[] interfaces = type.getInterfaces(); |
| 66 | + for (ResolvableType iface : interfaces) { |
| 67 | + if (iface.getRawClass().equals(ConnectionDetailsFactory.class)) { |
| 68 | + return iface; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + catch (TypeNotPresentException ex) { |
| 73 | + // A type referenced by the factory is not present. Skip it. |
| 74 | + } |
| 75 | + ResolvableType superType = type.getSuperType(); |
| 76 | + return ResolvableType.NONE.equals(superType) ? null : findConnectionDetailsFactory(superType); |
| 77 | + } |
| 78 | + |
| 79 | + private void register(FactoryDetails details) { |
| 80 | + this.registeredFactories.add(details); |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings("unchecked") |
| 84 | + public <S> ConnectionDetailsFactory<S, ConnectionDetails> getConnectionDetailsFactory(S source) { |
| 85 | + Class<S> input = (Class<S>) source.getClass(); |
| 86 | + List<ConnectionDetailsFactory<S, ConnectionDetails>> matchingFactories = new ArrayList<>(); |
| 87 | + for (FactoryDetails factoryDetails : this.registeredFactories) { |
| 88 | + if (factoryDetails.input.isAssignableFrom(input)) { |
| 89 | + matchingFactories.add((ConnectionDetailsFactory<S, ConnectionDetails>) factoryDetails.factory); |
| 90 | + } |
| 91 | + } |
| 92 | + if (matchingFactories.isEmpty()) { |
| 93 | + throw new ConnectionDetailsFactoryNotFoundException(source); |
| 94 | + } |
| 95 | + else { |
| 96 | + if (matchingFactories.size() == 1) { |
| 97 | + return matchingFactories.get(0); |
| 98 | + } |
| 99 | + AnnotationAwareOrderComparator.sort(matchingFactories); |
| 100 | + return new CompositeConnectionDetailsFactory<>(matchingFactories); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private record FactoryDetails(Class<?> input, Class<? extends ConnectionDetails> output, |
| 105 | + ConnectionDetailsFactory<?, ?> factory) { |
| 106 | + } |
| 107 | + |
| 108 | + static class CompositeConnectionDetailsFactory<S> implements ConnectionDetailsFactory<S, ConnectionDetails> { |
| 109 | + |
| 110 | + private final List<ConnectionDetailsFactory<S, ConnectionDetails>> delegates; |
| 111 | + |
| 112 | + CompositeConnectionDetailsFactory(List<ConnectionDetailsFactory<S, ConnectionDetails>> delegates) { |
| 113 | + this.delegates = delegates; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + @SuppressWarnings("unchecked") |
| 118 | + public ConnectionDetails getConnectionDetails(Object source) { |
| 119 | + for (ConnectionDetailsFactory<S, ConnectionDetails> delegate : this.delegates) { |
| 120 | + ConnectionDetails connectionDetails = delegate.getConnectionDetails((S) source); |
| 121 | + if (connectionDetails != null) { |
| 122 | + return connectionDetails; |
| 123 | + } |
| 124 | + } |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String toString() { |
| 130 | + return new ToStringCreator(this).append("delegates", this.delegates).toString(); |
| 131 | + } |
| 132 | + |
| 133 | + List<ConnectionDetailsFactory<S, ConnectionDetails>> getDelegates() { |
| 134 | + return this.delegates; |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments