|
| 1 | +package com.baeldung.structurizr; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.StringWriter; |
| 5 | + |
| 6 | +import com.structurizr.Workspace; |
| 7 | +import com.structurizr.api.StructurizrClient; |
| 8 | +import com.structurizr.api.StructurizrClientException; |
| 9 | +import com.structurizr.componentfinder.ComponentFinder; |
| 10 | +import com.structurizr.componentfinder.ReferencedTypesSupportingTypesStrategy; |
| 11 | +import com.structurizr.componentfinder.SourceCodeComponentFinderStrategy; |
| 12 | +import com.structurizr.componentfinder.SpringComponentFinderStrategy; |
| 13 | +import com.structurizr.io.WorkspaceWriterException; |
| 14 | +import com.structurizr.io.plantuml.PlantUMLWriter; |
| 15 | +import com.structurizr.model.Component; |
| 16 | +import com.structurizr.model.Container; |
| 17 | +import com.structurizr.model.Model; |
| 18 | +import com.structurizr.model.Person; |
| 19 | +import com.structurizr.model.SoftwareSystem; |
| 20 | +import com.structurizr.model.Tags; |
| 21 | +import com.structurizr.view.ComponentView; |
| 22 | +import com.structurizr.view.ContainerView; |
| 23 | +import com.structurizr.view.Routing; |
| 24 | +import com.structurizr.view.Shape; |
| 25 | +import com.structurizr.view.Styles; |
| 26 | +import com.structurizr.view.SystemContextView; |
| 27 | +import com.structurizr.view.View; |
| 28 | +import com.structurizr.view.ViewSet; |
| 29 | + |
| 30 | +public class StructurizrSimple { |
| 31 | + |
| 32 | + public static final String PAYMENT_TERMINAL = "Payment Terminal"; |
| 33 | + public static final String FRAUD_DETECTOR = "Fraud Detector"; |
| 34 | + public static final String SOFTWARE_SYSTEM_VIEW = "SoftwareSystemView"; |
| 35 | + public static final String CONTAINER_VIEW = "ContainerView"; |
| 36 | + public static final String COMPONENT_VIEW = "ComponentView"; |
| 37 | + public static final String JVM2_COMPONENT_VIEW = "JVM2ComponentView"; |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + Workspace workspace = getSoftwareSystem(); |
| 41 | + |
| 42 | + addContainers(workspace); |
| 43 | + addComponents(workspace); |
| 44 | + addSpringComponents(workspace); |
| 45 | + exportToPlantUml(workspace.getViews().getViewWithKey(SOFTWARE_SYSTEM_VIEW)); |
| 46 | + exportToPlantUml(workspace.getViews().getViewWithKey(CONTAINER_VIEW)); |
| 47 | + exportToPlantUml(workspace.getViews().getViewWithKey(COMPONENT_VIEW)); |
| 48 | + |
| 49 | + exportToPlantUml(workspace.getViews().getViewWithKey(JVM2_COMPONENT_VIEW)); |
| 50 | + |
| 51 | + addStyles(workspace.getViews()); |
| 52 | + //uploadToExternal(workspace); |
| 53 | + } |
| 54 | + |
| 55 | + private static void addSpringComponents(Workspace workspace) throws Exception { |
| 56 | + Container jvm2 = workspace.getModel().getSoftwareSystemWithName(PAYMENT_TERMINAL).getContainerWithName("JVM-2"); |
| 57 | + findComponents(jvm2); |
| 58 | + |
| 59 | + ComponentView view = workspace.getViews().createComponentView(jvm2, JVM2_COMPONENT_VIEW, "JVM2ComponentView"); |
| 60 | + view.addAllComponents(); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + private static void findComponents(Container jvm) throws Exception { |
| 65 | + ComponentFinder componentFinder = new ComponentFinder( |
| 66 | + jvm, |
| 67 | + "com.baeldung.structurizr", |
| 68 | + new SpringComponentFinderStrategy( |
| 69 | + new ReferencedTypesSupportingTypesStrategy() |
| 70 | + ), |
| 71 | + new SourceCodeComponentFinderStrategy(new File("."), 150)); |
| 72 | + |
| 73 | + componentFinder.findComponents(); |
| 74 | + } |
| 75 | + |
| 76 | + private static void addComponents(Workspace workspace) { |
| 77 | + Model model = workspace.getModel(); |
| 78 | + |
| 79 | + SoftwareSystem paymentTerminal = model.getSoftwareSystemWithName(PAYMENT_TERMINAL); |
| 80 | + Container jvm1 = paymentTerminal.getContainerWithName("JVM-1"); |
| 81 | + |
| 82 | + Component jaxrs = jvm1.addComponent("jaxrs-jersey", "restful webservice implementation", "rest"); |
| 83 | + Component gemfire = jvm1.addComponent("gemfire", "Clustered Cache Gemfire", "cache"); |
| 84 | + Component hibernate = jvm1.addComponent("hibernate", "Data Access Layer", "jpa"); |
| 85 | + |
| 86 | + jaxrs.uses(gemfire, ""); |
| 87 | + gemfire.uses(hibernate, ""); |
| 88 | + |
| 89 | + ComponentView componentView = workspace.getViews().createComponentView(jvm1, COMPONENT_VIEW, "JVM Components"); |
| 90 | + componentView.addAllComponents(); |
| 91 | + } |
| 92 | + |
| 93 | + private static void addContainers(Workspace workspace) { |
| 94 | + Model model = workspace.getModel(); |
| 95 | + |
| 96 | + SoftwareSystem paymentTerminal = model.getSoftwareSystemWithName(PAYMENT_TERMINAL); |
| 97 | + Container f5 = paymentTerminal.addContainer("Payment Load Balancer", "Payment Load Balancer", "F5"); |
| 98 | + Container jvm1 = paymentTerminal.addContainer("JVM-1", "JVM-1", "Java Virtual Machine"); |
| 99 | + Container jvm2 = paymentTerminal.addContainer("JVM-2", "JVM-2", "Java Virtual Machine"); |
| 100 | + Container jvm3 = paymentTerminal.addContainer("JVM-3", "JVM-3", "Java Virtual Machine"); |
| 101 | + Container oracle = paymentTerminal.addContainer("oracleDB", "Oracle Database", "RDBMS"); |
| 102 | + |
| 103 | + f5.uses(jvm1, "route"); |
| 104 | + f5.uses(jvm2, "route"); |
| 105 | + f5.uses(jvm3, "route"); |
| 106 | + |
| 107 | + jvm1.uses(oracle, "storage"); |
| 108 | + jvm2.uses(oracle, "storage"); |
| 109 | + jvm3.uses(oracle, "storage"); |
| 110 | + |
| 111 | + ContainerView view = workspace.getViews().createContainerView(paymentTerminal, CONTAINER_VIEW, "Container View"); |
| 112 | + view.addAllContainers(); |
| 113 | + } |
| 114 | + |
| 115 | + private static void uploadToExternal(Workspace workspace) throws StructurizrClientException { |
| 116 | + StructurizrClient client = new StructurizrClient("e94bc0c9-76ef-41b0-8de7-82afc1010d04", "78d555dd-2a31-487c-952c-50508f1da495"); |
| 117 | + client.putWorkspace(32961L, workspace); |
| 118 | + } |
| 119 | + |
| 120 | + private static void exportToPlantUml(View view) throws WorkspaceWriterException { |
| 121 | + StringWriter stringWriter = new StringWriter(); |
| 122 | + PlantUMLWriter plantUMLWriter = new PlantUMLWriter(); |
| 123 | + plantUMLWriter.write(view, stringWriter); |
| 124 | + System.out.println(stringWriter.toString()); |
| 125 | + } |
| 126 | + |
| 127 | + private static Workspace getSoftwareSystem() { |
| 128 | + Workspace workspace = new Workspace("Payment Gateway", "Payment Gateway"); |
| 129 | + Model model = workspace.getModel(); |
| 130 | + |
| 131 | + Person user = model.addPerson("Merchant", "Merchant"); |
| 132 | + SoftwareSystem paymentTerminal = model.addSoftwareSystem(PAYMENT_TERMINAL, "Payment Terminal"); |
| 133 | + user.uses(paymentTerminal, "Makes payment"); |
| 134 | + SoftwareSystem fraudDetector = model.addSoftwareSystem(FRAUD_DETECTOR, "Fraud Detector"); |
| 135 | + paymentTerminal.uses(fraudDetector, "Obtains fraud score"); |
| 136 | + |
| 137 | + ViewSet viewSet = workspace.getViews(); |
| 138 | + |
| 139 | + SystemContextView contextView = viewSet.createSystemContextView(workspace.getModel().getSoftwareSystemWithName(PAYMENT_TERMINAL), SOFTWARE_SYSTEM_VIEW, "Payment Gateway Diagram"); |
| 140 | + contextView.addAllElements(); |
| 141 | + |
| 142 | + return workspace; |
| 143 | + } |
| 144 | + |
| 145 | + private static void addStyles(ViewSet viewSet) { |
| 146 | + Styles styles = viewSet.getConfiguration().getStyles(); |
| 147 | + styles.addElementStyle(Tags.ELEMENT).color("#000000"); |
| 148 | + styles.addElementStyle(Tags.PERSON).background("#ffbf00").shape(Shape.Person); |
| 149 | + styles.addElementStyle(Tags.CONTAINER).background("#facc2E"); |
| 150 | + styles.addRelationshipStyle(Tags.RELATIONSHIP).routing(Routing.Orthogonal); |
| 151 | + |
| 152 | + styles.addRelationshipStyle(Tags.ASYNCHRONOUS).dashed(true); |
| 153 | + styles.addRelationshipStyle(Tags.SYNCHRONOUS).dashed(false); |
| 154 | + } |
| 155 | +} |
0 commit comments