|
| 1 | +package org.palladiosimulator.retriever.extraction.rules |
| 2 | + |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.util.HashSet |
| 5 | +import org.eclipse.jdt.core.dom.CompilationUnit |
| 6 | +import java.util.Set |
| 7 | +import org.palladiosimulator.retriever.services.blackboard.RetrieverBlackboard |
| 8 | +import org.palladiosimulator.retriever.services.Rule |
| 9 | +import org.palladiosimulator.retriever.extraction.engine.PCMDetector |
| 10 | +import org.palladiosimulator.retriever.extraction.commonalities.CompUnitOrName |
| 11 | +import java.util.Map |
| 12 | +import static org.palladiosimulator.retriever.extraction.engine.RuleHelper.* |
| 13 | +import org.eclipse.jdt.core.dom.ASTVisitor |
| 14 | +import org.eclipse.jdt.core.dom.ExpressionStatement |
| 15 | +import org.eclipse.jdt.core.dom.MethodInvocation |
| 16 | +import org.palladiosimulator.retriever.extraction.commonalities.RESTOperationName |
| 17 | +import org.palladiosimulator.retriever.extraction.commonalities.RESTName |
| 18 | + |
| 19 | +class ProjectSpecificRules implements Rule { |
| 20 | + |
| 21 | + static final String RULE_ID = "org.palladiosimulator.retriever.extraction.rules.teastore" |
| 22 | + static final String JAVA_DISCOVERER_ID = "org.palladiosimulator.retriever.extraction.discoverers.java"; |
| 23 | + static final String DOCKER_DISCOVERER_ID = "org.palladiosimulator.retriever.extraction.discoverers.docker"; |
| 24 | + static final String JAX_RS_RULE_ID = "org.palladiosimulator.retriever.extraction.rules.jax_rs"; |
| 25 | + static final String JAX_RS_DEPLOYMENT_RULE_ID = "org.palladiosimulator.retriever.extraction.rules.jax_rs.deployment"; |
| 26 | + static final String DOCKER_FILE_NAME = "Dockerfile"; |
| 27 | + |
| 28 | + override processRules(RetrieverBlackboard blackboard, Path path) { |
| 29 | + if (path === null) { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + val compilationUnits = blackboard.getDiscoveredFiles(JAVA_DISCOVERER_ID, typeof(CompilationUnit)); |
| 34 | + val pcmDetector = blackboard.PCMDetector as PCMDetector; |
| 35 | + |
| 36 | + if (path.fileName.toString().equals(DOCKER_FILE_NAME)) { |
| 37 | + val parentName = path.parent.fileName.toString() |
| 38 | + |
| 39 | + // Add all file system children as associated compilation units |
| 40 | + var children = new HashSet<CompilationUnit>(); |
| 41 | + var parentPath = path.parent; |
| 42 | + for (entry : compilationUnits.entrySet) { |
| 43 | + if (entry.key.startsWith(parentPath)) { |
| 44 | + children.add(entry.value); |
| 45 | + } |
| 46 | + } |
| 47 | + for (unit : children) { |
| 48 | + pcmDetector.detectSeparatingIdentifier(new CompUnitOrName(unit), parentName) |
| 49 | + pcmDetector.detectPartOfWeakComposite(new CompUnitOrName(unit), parentName) |
| 50 | + } |
| 51 | + |
| 52 | + val hostnameMap = blackboard.getPartition(JAX_RS_DEPLOYMENT_RULE_ID) as Map<Path, String> |
| 53 | + var hasHostnamePath = false |
| 54 | + for (hostnamePath : hostnameMap.keySet) { |
| 55 | + if (hostnamePath !== null && path.startsWith(hostnamePath)) { |
| 56 | + hasHostnamePath = true |
| 57 | + } |
| 58 | + } |
| 59 | + if (!hasHostnamePath) { |
| 60 | + hostnameMap.put(path.parent, parentName) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (compilationUnits.containsKey(path)) { |
| 65 | + val unit = compilationUnits.get(path) |
| 66 | + val identifier = new CompUnitOrName(unit) |
| 67 | + if (identifier.name().endsWith("Startup") && !identifier.name.endsWith("LogReaderStartup")) { |
| 68 | + // Make Startup components part of a composite so that they become part of the weak Dockerfile composite |
| 69 | + pcmDetector.detectPartOfComposite(identifier, identifier.name() + "_tempComposite") |
| 70 | + val contextInitializedMethod = getMethods(unit).stream().filter[m|m.name.fullyQualifiedName.endsWith("contextInitialized")].findFirst() |
| 71 | + if (contextInitializedMethod.present) { |
| 72 | + contextInitializedMethod.get.accept(new TeaStoreASTVisitor(identifier, pcmDetector)); |
| 73 | + } |
| 74 | + } else if (identifier.name().endsWith("ServiceLoadBalancer")) { |
| 75 | + pcmDetector.detectComponent(identifier) |
| 76 | + } else if (identifier.name().endsWith("RegistryClient")) { |
| 77 | + pcmDetector.detectComponent(identifier) |
| 78 | + pcmDetector.detectRequiredInterface(identifier, new RESTName("tools.descartes.teastore.registry", "/services")) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static class TeaStoreASTVisitor extends ASTVisitor { |
| 84 | + CompUnitOrName identifier; |
| 85 | + PCMDetector pcmDetector; |
| 86 | + |
| 87 | + new(CompUnitOrName identifier, PCMDetector pcmDetector) { |
| 88 | + this.identifier = identifier; |
| 89 | + this.pcmDetector = pcmDetector |
| 90 | + } |
| 91 | + |
| 92 | + override visit(ExpressionStatement statement) { |
| 93 | + if (!(statement.expression instanceof MethodInvocation)) { |
| 94 | + return true |
| 95 | + } |
| 96 | + |
| 97 | + val invocation = statement.expression as MethodInvocation |
| 98 | + // TODO: avoid self-references |
| 99 | + pcmDetector.detectCompositeRequiredInterfaceWeakly(identifier, invocation) |
| 100 | + |
| 101 | + return true |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + override isBuildRule() { |
| 106 | + return false |
| 107 | + } |
| 108 | + |
| 109 | + override getConfigurationKeys() { |
| 110 | + return Set.of |
| 111 | + } |
| 112 | + |
| 113 | + override getID() { |
| 114 | + return RULE_ID |
| 115 | + } |
| 116 | + |
| 117 | + override getName() { |
| 118 | + return "TeaStore Rules" |
| 119 | + } |
| 120 | + |
| 121 | + override getRequiredServices() { |
| 122 | + return Set.of(JAVA_DISCOVERER_ID, DOCKER_DISCOVERER_ID, JAX_RS_DEPLOYMENT_RULE_ID) |
| 123 | + } |
| 124 | + |
| 125 | + override getDependentServices() { |
| 126 | + Set.of(JAX_RS_RULE_ID) |
| 127 | + } |
| 128 | +} |
0 commit comments