|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.modulith.core; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | + |
| 25 | +/** |
| 26 | + * SPI to allow build units contribute additional {@link ApplicationModuleSource}s in the form of either declaring them |
| 27 | + * directly via {@link #getModuleBasePackages()} and {@link #getApplicationModuleSources(Function, boolean)} or via |
| 28 | + * provided {@link #getRootPackages()} and subsequent resolution via |
| 29 | + * {@link #getApplicationModuleSources(JavaPackage, ApplicationModuleDetectionStrategy, boolean)} for each of the |
| 30 | + * packages provided. <br> |
| 31 | + * The following snippet would register {@link ApplicationModuleSource}s for {@code com.acme.foo} and |
| 32 | + * {@code com.acme.bar} directly: |
| 33 | + * |
| 34 | + * <pre> |
| 35 | + * {@code |
| 36 | + * class MyCustomFactory implements ApplicationModuleSourceFactory { |
| 37 | + * |
| 38 | + * @Override |
| 39 | + * public List<String> getModuleBasePackages() { |
| 40 | + * return List.of("com.acme.foo", "com.acme.bar"); |
| 41 | + * } |
| 42 | + * } |
| 43 | + * } |
| 44 | + * </pre> |
| 45 | + * |
| 46 | + * The following snippet would register all modules located underneath {@code com.acme} found via the |
| 47 | + * {@link ApplicationModuleDetectionStrategy#explicitlyAnnotated()} strategy: |
| 48 | + * |
| 49 | + * <pre> |
| 50 | + * {@code |
| 51 | + * class MyCustomFactory implements ApplicationModuleSourceFactory { |
| 52 | + * |
| 53 | + * @Override |
| 54 | + * public List<String> getRootPackages() { |
| 55 | + * return List.of("com.acme"); |
| 56 | + * } |
| 57 | + * |
| 58 | + * @Override |
| 59 | + * ApplicationModuleDetectionStrategy getApplicationModuleDetectionStrategy() { |
| 60 | + * return ApplicationModuleDetectionStrategy.explicitlyAnnotated(); |
| 61 | + * } |
| 62 | + * } |
| 63 | + * } |
| 64 | + * </pre> |
| 65 | + * |
| 66 | + * @author Oliver Drotbohm |
| 67 | + * @since 1.3 |
| 68 | + */ |
| 69 | +public interface ApplicationModuleSourceFactory { |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the additional root packages to be considered. The ones returned from this method will be scanned for |
| 73 | + * {@link ApplicationModuleSource}s via |
| 74 | + * {@link #getApplicationModuleSources(JavaPackage, ApplicationModuleDetectionStrategy, boolean)} using the |
| 75 | + * {@link ApplicationModuleDetectionStrategy} returned from {@link #getApplicationModuleDetectionStrategy()}. If the |
| 76 | + * latter is {@literal null}, the default {@link ApplicationModuleDetectionStrategy} is used. |
| 77 | + * |
| 78 | + * @return must not be {@literal null}. |
| 79 | + */ |
| 80 | + default List<String> getRootPackages() { |
| 81 | + return Collections.emptyList(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns additional module base packages to create {@link ApplicationModuleSource}s from. Subsequently handled by |
| 86 | + * {@link #getApplicationModuleSources(Function, boolean)}. |
| 87 | + * |
| 88 | + * @return must not be {@literal null}. |
| 89 | + */ |
| 90 | + default List<String> getModuleBasePackages() { |
| 91 | + return Collections.emptyList(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns the {@link ApplicationModuleDetectionStrategy} to be used to detect {@link ApplicationModuleSource}s from |
| 96 | + * the packages returned by {@link #getRootPackages()}. If {@literal null} is returned, the default |
| 97 | + * {@link ApplicationModuleDetectionStrategy} will be used. |
| 98 | + * |
| 99 | + * @return can be {@literal null}. |
| 100 | + */ |
| 101 | + @Nullable |
| 102 | + default ApplicationModuleDetectionStrategy getApplicationModuleDetectionStrategy() { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Creates all {@link ApplicationModuleSource}s using the given base package and |
| 108 | + * {@link ApplicationModuleDetectionStrategy}. |
| 109 | + * |
| 110 | + * @param rootPackage will never be {@literal null}. |
| 111 | + * @param strategy will never be {@literal null}. |
| 112 | + * @param useFullyQualifiedModuleNames whether to use fully-qualified names for application modules. |
| 113 | + * @return must not be {@literal null}. |
| 114 | + * @see ApplicationModuleSource#from(JavaPackage, ApplicationModuleDetectionStrategy, boolean) |
| 115 | + */ |
| 116 | + default Stream<ApplicationModuleSource> getApplicationModuleSources(JavaPackage rootPackage, |
| 117 | + ApplicationModuleDetectionStrategy strategy, boolean useFullyQualifiedModuleNames) { |
| 118 | + |
| 119 | + return ApplicationModuleSource.from(rootPackage, strategy, useFullyQualifiedModuleNames); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Creates {@link ApplicationModuleSource} for individually, manually described application modules. |
| 124 | + * |
| 125 | + * @param packages will never be {@literal null}. |
| 126 | + * @param useFullyQualifiedModuleNames whether to use fully-qualified names for application modules. |
| 127 | + * @return must not be {@literal null}. |
| 128 | + * @see ApplicationModuleSource#from(JavaPackage, String) |
| 129 | + */ |
| 130 | + default Stream<ApplicationModuleSource> getApplicationModuleSources(Function<String, JavaPackage> packages, |
| 131 | + boolean useFullyQualifiedModuleNames) { |
| 132 | + |
| 133 | + return getModuleBasePackages().stream() |
| 134 | + .map(packages) |
| 135 | + .map(it -> ApplicationModuleSource.from(it, useFullyQualifiedModuleNames ? it.getName() : it.getLocalName())); |
| 136 | + } |
| 137 | +} |
0 commit comments