|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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.io; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.BeanFactory; |
| 22 | +import org.springframework.boot.context.event.ApplicationPreparedEvent; |
| 23 | +import org.springframework.context.ApplicationListener; |
| 24 | +import org.springframework.context.ConfigurableApplicationContext; |
| 25 | +import org.springframework.core.env.Environment; |
| 26 | +import org.springframework.core.io.DefaultResourceLoader; |
| 27 | +import org.springframework.core.io.ProtocolResolver; |
| 28 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
| 29 | +import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link ProtocolResolver} implementations that are loaded from a |
| 34 | + * {@code spring.factories} file. |
| 35 | + * |
| 36 | + * @author Scott Frederick |
| 37 | + */ |
| 38 | +final class ProtocolResolvers { |
| 39 | + |
| 40 | + private static List<ProtocolResolver> protocolResolvers; |
| 41 | + |
| 42 | + private ProtocolResolvers() { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Apply all discovered {@link ProtocolResolver}s to the provided |
| 47 | + * {@link DefaultResourceLoader}. |
| 48 | + * @param resourceLoader the resource loader to apply protocol resolvers to |
| 49 | + * @param <T> a resource loader |
| 50 | + */ |
| 51 | + static <T extends DefaultResourceLoader> void applyTo(T resourceLoader) { |
| 52 | + Assert.notNull(resourceLoader, "ResourceLoader must not be null"); |
| 53 | + if (protocolResolvers != null) { |
| 54 | + resourceLoader.getProtocolResolvers().addAll(protocolResolvers); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static void initialize(ConfigurableApplicationContext context) { |
| 59 | + ArgumentResolver argumentResolver = getArgumentResolver(context); |
| 60 | + SpringFactoriesLoader loader = SpringFactoriesLoader |
| 61 | + .forDefaultResourceLocation((context != null) ? context.getClassLoader() : null); |
| 62 | + protocolResolvers = loader.load(ProtocolResolver.class, argumentResolver); |
| 63 | + } |
| 64 | + |
| 65 | + private static ArgumentResolver getArgumentResolver(ConfigurableApplicationContext context) { |
| 66 | + if (context == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + return ArgumentResolver.of(BeanFactory.class, context.getBeanFactory()) |
| 70 | + .and(Environment.class, context.getEnvironment()); |
| 71 | + } |
| 72 | + |
| 73 | + static class ProtocolResolversInitializer implements ApplicationListener<ApplicationPreparedEvent> { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onApplicationEvent(ApplicationPreparedEvent event) { |
| 77 | + ConfigurableApplicationContext context = event.getApplicationContext(); |
| 78 | + ProtocolResolvers.initialize(context); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments