|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.modulith.docs; |
| 17 | + |
| 18 | +import static org.springframework.modulith.docs.Documenter.CanvasOptions.Grouping.*; |
| 19 | + |
| 20 | +import java.lang.annotation.Annotation; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +import org.jmolecules.architecture.cqrs.Command; |
| 25 | +import org.jmolecules.architecture.cqrs.CommandDispatcher; |
| 26 | +import org.jmolecules.architecture.cqrs.CommandHandler; |
| 27 | +import org.jmolecules.architecture.cqrs.QueryModel; |
| 28 | +import org.jmolecules.architecture.hexagonal.Adapter; |
| 29 | +import org.jmolecules.architecture.hexagonal.Application; |
| 30 | +import org.jmolecules.architecture.hexagonal.Port; |
| 31 | +import org.jmolecules.architecture.hexagonal.PrimaryAdapter; |
| 32 | +import org.jmolecules.architecture.hexagonal.PrimaryPort; |
| 33 | +import org.jmolecules.architecture.hexagonal.SecondaryAdapter; |
| 34 | +import org.jmolecules.architecture.hexagonal.SecondaryPort; |
| 35 | +import org.jmolecules.architecture.layered.ApplicationLayer; |
| 36 | +import org.jmolecules.architecture.layered.DomainLayer; |
| 37 | +import org.jmolecules.architecture.layered.InfrastructureLayer; |
| 38 | +import org.jmolecules.architecture.layered.InterfaceLayer; |
| 39 | +import org.jmolecules.architecture.onion.classical.ApplicationServiceRing; |
| 40 | +import org.jmolecules.architecture.onion.classical.DomainModelRing; |
| 41 | +import org.jmolecules.architecture.onion.classical.DomainServiceRing; |
| 42 | +import org.jmolecules.architecture.onion.simplified.ApplicationRing; |
| 43 | +import org.jmolecules.architecture.onion.simplified.DomainRing; |
| 44 | +import org.jmolecules.architecture.onion.simplified.InfrastructureRing; |
| 45 | +import org.springframework.modulith.core.SpringBean; |
| 46 | +import org.springframework.modulith.docs.Documenter.CanvasOptions.Grouping; |
| 47 | +import org.springframework.util.ClassUtils; |
| 48 | + |
| 49 | +/** |
| 50 | + * A collection of {@link Grouping}s. |
| 51 | + * |
| 52 | + * @author Oliver Drotbohm |
| 53 | + */ |
| 54 | +public class Groupings { |
| 55 | + |
| 56 | + /** |
| 57 | + * Spring Framework-related {@link Grouping}s. |
| 58 | + * |
| 59 | + * @author Oliver Drotbohm |
| 60 | + */ |
| 61 | + public static class SpringGroupings { |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns Spring Framework-related {@link Grouping}s. |
| 65 | + * |
| 66 | + * @return will never be {@literal null}. |
| 67 | + */ |
| 68 | + public static Grouping[] getGroupings() { |
| 69 | + |
| 70 | + return new Grouping[] { |
| 71 | + of("Controllers", bean -> bean.toArchitecturallyEvidentType().isController()), |
| 72 | + of("Services", bean -> bean.toArchitecturallyEvidentType().isService()), |
| 73 | + of("Repositories", bean -> bean.toArchitecturallyEvidentType().isRepository()), |
| 74 | + of("Event listeners", bean -> bean.toArchitecturallyEvidentType().isEventListener()), |
| 75 | + of("Configuration properties", |
| 76 | + bean -> bean.toArchitecturallyEvidentType().isConfigurationProperties()) |
| 77 | + }; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * jMolecules-related {@link Grouping}s. |
| 83 | + * |
| 84 | + * @author Oliver Drotbohm |
| 85 | + */ |
| 86 | + public static class JMoleculesGroupings { |
| 87 | + |
| 88 | + private static final boolean JMOLECULES_CQRS_PRESENT = ClassUtils |
| 89 | + .isPresent("org.jmolecules.architecture.cqrs.Command", JMoleculesGroupings.class.getClassLoader()); |
| 90 | + |
| 91 | + private static final boolean JMOLECULES_HEXAGONAL_PRESENT = ClassUtils |
| 92 | + .isPresent("org.jmolecules.architecture.hexagonal.Port", JMoleculesGroupings.class.getClassLoader()); |
| 93 | + |
| 94 | + private static final boolean JMOLECULES_LAYERS_PRESENT = ClassUtils |
| 95 | + .isPresent("org.jmolecules.architecture.layered", JMoleculesGroupings.class.getClassLoader()); |
| 96 | + |
| 97 | + private static final boolean JMOLECULES_ONION_PRESENT = ClassUtils |
| 98 | + .isPresent("org.jmolecules.architecture.onion.classical.ApplicationRing", |
| 99 | + JMoleculesGroupings.class.getClassLoader()); |
| 100 | + |
| 101 | + public static Grouping[] getGroupings() { |
| 102 | + |
| 103 | + var groupings = new ArrayList<Grouping>(); |
| 104 | + |
| 105 | + if (JMOLECULES_CQRS_PRESENT) { |
| 106 | + |
| 107 | + groupings.add(of("Commands", packageOrTypeAnnotatedWith(Command.class))); |
| 108 | + groupings.add(of("Command dispatchers", packageOrTypeAnnotatedWith(CommandDispatcher.class))); |
| 109 | + groupings.add(of("Command handlers", packageOrTypeAnnotatedWith(CommandHandler.class))); |
| 110 | + groupings.add(of("Query models", packageOrTypeAnnotatedWith(QueryModel.class))); |
| 111 | + } |
| 112 | + |
| 113 | + if (JMOLECULES_HEXAGONAL_PRESENT) { |
| 114 | + |
| 115 | + groupings.add(of("Primary ports", packageOrTypeAnnotatedWith(PrimaryPort.class))); |
| 116 | + groupings.add(of("Secondary ports", packageOrTypeAnnotatedWith(SecondaryPort.class))); |
| 117 | + groupings.add(of("Ports", packageOrTypeAnnotatedWith(Port.class))); |
| 118 | + |
| 119 | + groupings.add(of("Application", packageOrTypeAnnotatedWith(Application.class))); |
| 120 | + |
| 121 | + groupings.add(of("Primary adapters", packageOrTypeAnnotatedWith(PrimaryAdapter.class))); |
| 122 | + groupings.add(of("Secondary adapters", packageOrTypeAnnotatedWith(SecondaryAdapter.class))); |
| 123 | + groupings.add(of("Adapters", packageOrTypeAnnotatedWith(Adapter.class))); |
| 124 | + } |
| 125 | + |
| 126 | + if (JMOLECULES_LAYERS_PRESENT) { |
| 127 | + |
| 128 | + groupings.add(of("Application layer", packageOrTypeAnnotatedWith(ApplicationLayer.class))); |
| 129 | + groupings.add(of("Domain layer", packageOrTypeAnnotatedWith(DomainLayer.class))); |
| 130 | + groupings.add(of("Infrastructure layer", packageOrTypeAnnotatedWith(InfrastructureLayer.class))); |
| 131 | + groupings.add(of("Interface layer", packageOrTypeAnnotatedWith(InterfaceLayer.class))); |
| 132 | + } |
| 133 | + |
| 134 | + if (JMOLECULES_ONION_PRESENT) { |
| 135 | + |
| 136 | + groupings.add(of("Application ring", packageOrTypeAnnotatedWith(ApplicationRing.class))); |
| 137 | + groupings.add(of("Domain ring", packageOrTypeAnnotatedWith(DomainRing.class))); |
| 138 | + groupings.add(of("Infrastructure ring", packageOrTypeAnnotatedWith(InfrastructureRing.class))); |
| 139 | + |
| 140 | + groupings.add(of("Application service ring", packageOrTypeAnnotatedWith(ApplicationServiceRing.class))); |
| 141 | + groupings.add(of("Domain service ring", packageOrTypeAnnotatedWith(DomainServiceRing.class))); |
| 142 | + groupings.add(of("Domain model ring", packageOrTypeAnnotatedWith(DomainModelRing.class))); |
| 143 | + groupings.add(of("Infrastructure ring", |
| 144 | + packageOrTypeAnnotatedWith( |
| 145 | + org.jmolecules.architecture.onion.classical.InfrastructureRing.class))); |
| 146 | + } |
| 147 | + |
| 148 | + return groupings.toArray(Grouping[]::new); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static Predicate<SpringBean> packageOrTypeAnnotatedWith(Class<? extends Annotation> annotation) { |
| 153 | + |
| 154 | + return bean -> { |
| 155 | + |
| 156 | + var type = bean.getType(); |
| 157 | + var pkg = type.getPackage(); |
| 158 | + |
| 159 | + return type.isAnnotatedWith(annotation) || type.isMetaAnnotatedWith(annotation) // |
| 160 | + || pkg.isAnnotatedWith(annotation) || pkg.isMetaAnnotatedWith(annotation); |
| 161 | + }; |
| 162 | + } |
| 163 | +} |
0 commit comments