|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 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; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.springframework.context.ConfigurableApplicationContext; |
| 23 | + |
| 24 | +/** |
| 25 | + * Low-level hooks that can observe a {@link SpringApplication} and modify its behavior. |
| 26 | + * Hooks are managed on a per-thread basis providing isolation when multiple applications |
| 27 | + * are executed in parallel. |
| 28 | + * |
| 29 | + * @author Andy Wilkinson |
| 30 | + */ |
| 31 | +final class SpringApplicationHooks { |
| 32 | + |
| 33 | + private static final ThreadLocal<Hooks> hooks = ThreadLocal.withInitial(Hooks::new); |
| 34 | + |
| 35 | + private SpringApplicationHooks() { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Runs the given {@code action} with the given {@code hook} attached. |
| 41 | + * @param hook the hook to attach |
| 42 | + * @param action the action to run |
| 43 | + * @param <T> the type of the action's result |
| 44 | + * @return the result of the action |
| 45 | + * @throws Exception if a failure occurs while performing the action |
| 46 | + */ |
| 47 | + static <T> T withHook(Hook hook, Action<T> action) throws Exception { |
| 48 | + hooks.get().add(hook); |
| 49 | + try { |
| 50 | + return action.perform(); |
| 51 | + } |
| 52 | + finally { |
| 53 | + hooks.get().remove(hook); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Runs the given {@code action} with the given {@code hook} attached. |
| 59 | + * @param hook the hook to attach |
| 60 | + * @param action the action to run |
| 61 | + */ |
| 62 | + static void withHook(Hook hook, Runnable action) { |
| 63 | + hooks.get().add(hook); |
| 64 | + try { |
| 65 | + action.run(); |
| 66 | + } |
| 67 | + finally { |
| 68 | + hooks.get().remove(hook); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static Hooks hooks() { |
| 73 | + return hooks.get(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * A hook that can observe and modify the behavior of a {@link SpringApplication}. |
| 78 | + */ |
| 79 | + interface Hook { |
| 80 | + |
| 81 | + /** |
| 82 | + * Called at the beginning of {@link SpringApplication#run(String...)}. Provides |
| 83 | + * an opportunity to inspect and customise the application. |
| 84 | + * @param application the application that is being run |
| 85 | + */ |
| 86 | + default void preRun(SpringApplication application) { |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Called at the end of {@link SpringApplication#run(String...)}. Provides access |
| 92 | + * to the {@link ConfigurableApplicationContext context} that has been created for |
| 93 | + * the application. |
| 94 | + * @param application the application that has been run |
| 95 | + * @param context the application's context |
| 96 | + */ |
| 97 | + default void postRun(SpringApplication application, ConfigurableApplicationContext context) { |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Called immediately before the given {@code context} is refreshed. |
| 103 | + * @param application the application for which the context is being refreshed |
| 104 | + * @param context the application's context |
| 105 | + * @return whether to continue with refresh processing |
| 106 | + */ |
| 107 | + default boolean preRefresh(SpringApplication application, ConfigurableApplicationContext context) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * An action that can be performed with a hook attached. |
| 115 | + * <p> |
| 116 | + * <strong>For internal use only.</strong> |
| 117 | + * |
| 118 | + * @param <T> the type of the action's result |
| 119 | + */ |
| 120 | + interface Action<T> { |
| 121 | + |
| 122 | + /** |
| 123 | + * Perform the action. |
| 124 | + * @return the result of the action |
| 125 | + * @throws Exception if a failure occurs |
| 126 | + */ |
| 127 | + T perform() throws Exception; |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + static final class Hooks implements Hook { |
| 132 | + |
| 133 | + private final List<Hook> delegates = new ArrayList<>(); |
| 134 | + |
| 135 | + private void add(Hook hook) { |
| 136 | + this.delegates.add(hook); |
| 137 | + } |
| 138 | + |
| 139 | + private void remove(Hook hook) { |
| 140 | + this.delegates.remove(hook); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void preRun(SpringApplication application) { |
| 145 | + for (Hook delegate : this.delegates) { |
| 146 | + delegate.preRun(application); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void postRun(SpringApplication application, ConfigurableApplicationContext context) { |
| 152 | + for (Hook delegate : this.delegates) { |
| 153 | + delegate.postRun(application, context); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public boolean preRefresh(SpringApplication application, ConfigurableApplicationContext context) { |
| 159 | + for (Hook delegate : this.delegates) { |
| 160 | + if (!delegate.preRefresh(application, context)) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + } |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments