From 8eccba76dd3ba4bbb0f9795648fdf660a63ae73c Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Wed, 17 Jan 2024 14:54:00 +0530 Subject: [PATCH 1/2] Remove generating observability-symbols.jar --- .../ballerina/projects/JBallerinaBackend.java | 8 - .../io/ballerina/projects/JarResolver.java | 38 --- .../internal/CompilerPhaseRunner.java | 4 - .../src/main/java/module-info.java | 1 - .../NoOpObservabilitySymbolCollector.java | 41 ---- .../ObservabilitySymbolCollectorRunner.java | 54 ----- .../spi/ObservabilitySymbolCollector.java | 36 --- .../zip/jballerina-tools/build.gradle | 1 - .../build.gradle | 43 ---- .../spotbugs-exclude.xml | 5 - .../src/main/java/module-info.java | 12 - .../DefaultObservabilitySymbolCollector.java | 220 ------------------ .../anaylze/model/PackageHolder.java | 64 ----- ....compiler.spi.ObservabilitySymbolCollector | 19 -- settings.gradle | 2 - tests/jballerina-unit-test/build.gradle | 1 - .../syntaxtree/SyntaxTreeEqualityTest.java | 58 ----- 17 files changed, 607 deletions(-) delete mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/NoOpObservabilitySymbolCollector.java delete mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ObservabilitySymbolCollectorRunner.java delete mode 100644 compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/spi/ObservabilitySymbolCollector.java delete mode 100644 misc/observability-symbol-collector/build.gradle delete mode 100644 misc/observability-symbol-collector/spotbugs-exclude.xml delete mode 100644 misc/observability-symbol-collector/src/main/java/module-info.java delete mode 100644 misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java delete mode 100644 misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java delete mode 100644 misc/observability-symbol-collector/src/main/resources/META-INF/services/org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector delete mode 100644 tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/observe/syntaxtree/SyntaxTreeEqualityTest.java diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java index 2bba26323976..78083355e948 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java @@ -39,8 +39,6 @@ import org.wso2.ballerinalang.compiler.CompiledJarFile; import org.wso2.ballerinalang.compiler.bir.codegen.CodeGenerator; import org.wso2.ballerinalang.compiler.bir.codegen.interop.InteropValidator; -import org.wso2.ballerinalang.compiler.semantics.analyzer.ObservabilitySymbolCollectorRunner; -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; import org.wso2.ballerinalang.compiler.tree.BLangPackage; import org.wso2.ballerinalang.compiler.util.CompilerContext; import org.wso2.ballerinalang.util.Lists; @@ -133,12 +131,6 @@ private JBallerinaBackend(PackageCompilation packageCompilation, JvmTarget jdkVe this.compilerContext = projectEnvContext.getService(CompilerContext.class); this.interopValidator = InteropValidator.getInstance(compilerContext); this.jvmCodeGenerator = CodeGenerator.getInstance(compilerContext); - // TODO: Move to a compiler extension once Compiler revamp is complete - if (packageContext.compilationOptions().observabilityIncluded()) { - ObservabilitySymbolCollector observabilitySymbolCollector - = ObservabilitySymbolCollectorRunner.getInstance(compilerContext); - observabilitySymbolCollector.process(packageContext.project()); - } this.conflictedJars = new ArrayList<>(); performCodeGen(); } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java index 5accc547a6f0..c8c9b1413e15 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java @@ -26,8 +26,6 @@ import io.ballerina.tools.diagnostics.DiagnosticInfo; import io.ballerina.tools.diagnostics.DiagnosticSeverity; import org.apache.maven.artifact.versioning.ComparableVersion; -import org.wso2.ballerinalang.compiler.semantics.analyzer.ObservabilitySymbolCollectorRunner; -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; import org.wso2.ballerinalang.compiler.util.CompilerContext; import org.wso2.ballerinalang.compiler.util.CompilerUtils; @@ -112,42 +110,6 @@ public Collection getJarFilePathsRequiredForExecution() { PlatformLibraryScope.DEFAULT, getPackageName(rootPackageContext))); - // TODO: Move to a compiler extension once Compiler revamp is complete - // 4) Add the Observability Symbols Jar - if (rootPackageContext.compilationOptions().observabilityIncluded()) { - try { - // Generating an empty Jar which can be used by the Observability Symbol Collector - String packageName = rootPackageContext.packageOrg().value() + "-" - + rootPackageContext.packageName().value(); - Path observabilityJarPath = ProjectUtils.generateObservabilitySymbolsJar(packageName); - - // Writing the Syntax Tree to the Jar - CompilerContext compilerContext = rootPackageContext.project().projectEnvironmentContext() - .getService(CompilerContext.class); - ObservabilitySymbolCollector observabilitySymbolCollector - = ObservabilitySymbolCollectorRunner.getInstance(compilerContext); - observabilitySymbolCollector.writeToExecutable(observabilityJarPath, rootPackageContext.project()); - // Cache observability jar in target - Path observeJarCachePath = rootPackageContext.project().targetDir() - .resolve(CACHES_DIR_NAME) - .resolve(rootPackageContext.packageOrg().value()) - .resolve(rootPackageContext.packageName().value()) - .resolve(rootPackageContext.packageVersion().value().toString()) - .resolve("observe") - .resolve(rootPackageContext.packageOrg().value() + "-" - + rootPackageContext.packageName().value() - + "-observability-symbols.jar"); - Path observeCachePath = Optional.of(observeJarCachePath.getParent()).orElseThrow(); - Files.createDirectories(observeCachePath); - Files.copy(observabilityJarPath, observeJarCachePath, StandardCopyOption.REPLACE_EXISTING); - - jarFiles.add(new JarLibrary(observabilityJarPath, PlatformLibraryScope.DEFAULT, - getPackageName(rootPackageContext))); - } catch (IOException e) { - err.println("\twarning: Failed to add Observability information to Jar due to: " + e.getMessage()); - } - } - // TODO Filter out duplicate jar entries return jarFiles; } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/CompilerPhaseRunner.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/CompilerPhaseRunner.java index 41ff49afbb98..3163dfd71121 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/CompilerPhaseRunner.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/CompilerPhaseRunner.java @@ -30,12 +30,10 @@ import org.wso2.ballerinalang.compiler.semantics.analyzer.DataflowAnalyzer; import org.wso2.ballerinalang.compiler.semantics.analyzer.DocumentationAnalyzer; import org.wso2.ballerinalang.compiler.semantics.analyzer.IsolationAnalyzer; -import org.wso2.ballerinalang.compiler.semantics.analyzer.ObservabilitySymbolCollectorRunner; import org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer; import org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter; import org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver; import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable; -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; import org.wso2.ballerinalang.compiler.tree.BLangPackage; import org.wso2.ballerinalang.compiler.util.CompilerContext; import org.wso2.ballerinalang.compiler.util.CompilerOptions; @@ -64,7 +62,6 @@ public class CompilerPhaseRunner { private final ConstantPropagation constantPropagation; private final DocumentationAnalyzer documentationAnalyzer; private final CompilerPluginRunner compilerPluginRunner; - private final ObservabilitySymbolCollector observabilitySymbolCollector; private final Desugar desugar; private final BIRGen birGenerator; private final BIREmitter birEmitter; @@ -95,7 +92,6 @@ private CompilerPhaseRunner(CompilerContext context) { this.documentationAnalyzer = DocumentationAnalyzer.getInstance(context); this.constantPropagation = ConstantPropagation.getInstance(context); this.compilerPluginRunner = CompilerPluginRunner.getInstance(context); - this.observabilitySymbolCollector = ObservabilitySymbolCollectorRunner.getInstance(context); this.desugar = Desugar.getInstance(context); this.birGenerator = BIRGen.getInstance(context); this.birEmitter = BIREmitter.getInstance(context); diff --git a/compiler/ballerina-lang/src/main/java/module-info.java b/compiler/ballerina-lang/src/main/java/module-info.java index 89c5534e5d54..59933755b8f7 100644 --- a/compiler/ballerina-lang/src/main/java/module-info.java +++ b/compiler/ballerina-lang/src/main/java/module-info.java @@ -58,7 +58,6 @@ exports org.wso2.ballerinalang.compiler.parser; exports org.ballerinalang.model.symbols; exports org.ballerinalang.repository.fs; - exports org.wso2.ballerinalang.compiler.spi; exports org.ballerinalang.util; exports org.wso2.ballerinalang.compiler.tree.clauses; exports org.ballerinalang.model.clauses; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/NoOpObservabilitySymbolCollector.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/NoOpObservabilitySymbolCollector.java deleted file mode 100644 index 7fa1ef51570d..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/NoOpObservabilitySymbolCollector.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.ballerinalang.compiler.semantics.analyzer; - -import io.ballerina.projects.Project; -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; - -import java.nio.file.Path; - -/** - * Null Object for {@link ObservabilitySymbolCollector} interface. - * - * @since 2.0.0 - */ -public class NoOpObservabilitySymbolCollector implements ObservabilitySymbolCollector { - @Override - public void process(Project project) { - // Do nothing - } - - @Override - public void writeToExecutable(Path executableFile, Project project) { - // Do nothing - } -} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ObservabilitySymbolCollectorRunner.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ObservabilitySymbolCollectorRunner.java deleted file mode 100644 index 2e9f5af6d689..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ObservabilitySymbolCollectorRunner.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.ballerinalang.compiler.semantics.analyzer; - -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; -import org.wso2.ballerinalang.compiler.util.CompilerContext; - -import java.util.Iterator; -import java.util.ServiceLoader; - -/** - * Used to analyze ballerina code and store observability symbols in the Ballerina program artifact. - * - * @since 2.0.0 - */ -public class ObservabilitySymbolCollectorRunner { - private static final CompilerContext.Key OBSERVABILITY_SYMBOL_COLLECTOR_KEY = - new CompilerContext.Key<>(); - - public static synchronized ObservabilitySymbolCollector getInstance(CompilerContext context) { - ObservabilitySymbolCollector observabilitySymbolCollector = context.get(OBSERVABILITY_SYMBOL_COLLECTOR_KEY); - if (observabilitySymbolCollector == null) { - ServiceLoader observabilitySymbolCollectors - = ServiceLoader.load(ObservabilitySymbolCollector.class); - - Iterator collectorIterator = observabilitySymbolCollectors.iterator(); - if (collectorIterator.hasNext()) { - observabilitySymbolCollector = collectorIterator.next(); - } else { - observabilitySymbolCollector = new NoOpObservabilitySymbolCollector(); - } - context.put(OBSERVABILITY_SYMBOL_COLLECTOR_KEY, observabilitySymbolCollector); - } - - return observabilitySymbolCollector; - } - -} diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/spi/ObservabilitySymbolCollector.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/spi/ObservabilitySymbolCollector.java deleted file mode 100644 index ea265beef7b3..000000000000 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/spi/ObservabilitySymbolCollector.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.ballerinalang.compiler.spi; - -import io.ballerina.projects.Project; - -import java.io.IOException; -import java.nio.file.Path; - -/** - * Implementation of this interface analyze the AST of the Ballerina program - * and generate symbol data to be used for observability purposes. - * - * @since 2.0.0 - */ -public interface ObservabilitySymbolCollector { - void process(Project project); - - void writeToExecutable(Path executableFile, Project project) throws IOException; -} diff --git a/distribution/zip/jballerina-tools/build.gradle b/distribution/zip/jballerina-tools/build.gradle index f069c78ee4db..d87a6e0dedb6 100644 --- a/distribution/zip/jballerina-tools/build.gradle +++ b/distribution/zip/jballerina-tools/build.gradle @@ -123,7 +123,6 @@ dependencies { dist project(':syntax-api-calls-gen') dist project(':language-server:language-server-commons') dist project(':docerina') - dist project(':observability-symbol-collector') dist project(':testerina:testerina-runtime') dist project(':toml-parser') docerina project(':docerina-gradle-plugin') diff --git a/misc/observability-symbol-collector/build.gradle b/misc/observability-symbol-collector/build.gradle deleted file mode 100644 index ca5015d26174..000000000000 --- a/misc/observability-symbol-collector/build.gradle +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -apply from: "$rootDir/gradle/javaProject.gradle" - -dependencies { - implementation project(':ballerina-lang') - implementation project(':ballerina-parser') - implementation project(':ballerina-tools-api') - implementation project(':language-server:language-server-core') - implementation project(':diagram-util') - - implementation 'com.google.code.gson:gson' -} - -description = 'Ballerina - Observability Symbol Collector' - -ext.moduleName = 'observability-symbol-collector' - -compileJava { - inputs.property("moduleName", moduleName) - doFirst { - options.compilerArgs = [ - '--module-path', classpath.asPath, - ] - classpath = files() - } -} diff --git a/misc/observability-symbol-collector/spotbugs-exclude.xml b/misc/observability-symbol-collector/spotbugs-exclude.xml deleted file mode 100644 index 7f22dc25bf6c..000000000000 --- a/misc/observability-symbol-collector/spotbugs-exclude.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/misc/observability-symbol-collector/src/main/java/module-info.java b/misc/observability-symbol-collector/src/main/java/module-info.java deleted file mode 100644 index 1abe9d8775a9..000000000000 --- a/misc/observability-symbol-collector/src/main/java/module-info.java +++ /dev/null @@ -1,12 +0,0 @@ -module org.ballerinalang.observability.anaylze { - requires io.ballerina.lang; - requires io.ballerina.parser; - requires io.ballerina.tools.api; - requires io.ballerina.language.server.core; - requires io.ballerina.diagram.util; - - requires com.google.gson; - - provides org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector - with org.ballerinalang.observability.anaylze.DefaultObservabilitySymbolCollector; -} diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java deleted file mode 100644 index 19fa5faac9e7..000000000000 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/DefaultObservabilitySymbolCollector.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.observability.anaylze; - -import com.google.gson.JsonElement; -import io.ballerina.compiler.api.SemanticModel; -import io.ballerina.projects.Document; -import io.ballerina.projects.DocumentId; -import io.ballerina.projects.Module; -import io.ballerina.projects.ModuleId; -import io.ballerina.projects.Package; -import io.ballerina.projects.PackageCompilation; -import io.ballerina.projects.Project; -import io.ballerina.projects.util.ProjectUtils; -import org.ballerinalang.diagramutil.DiagramUtil; -import org.ballerinalang.observability.anaylze.model.PackageHolder; -import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector; -import org.wso2.ballerinalang.util.RepoUtils; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Path; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.util.Properties; -import java.util.jar.JarFile; -import java.util.zip.ZipEntry; - -import static io.ballerina.projects.util.ProjectConstants.CACHES_DIR_NAME; - -/** - * Default implementation of {@link ObservabilitySymbolCollector}. - * - * @since 2.0.0 - */ -public class DefaultObservabilitySymbolCollector implements ObservabilitySymbolCollector { - - private static final String SYNTAX_TREE_DIR = "syntax-tree"; - private static final String SYNTAX_TREE_FILE_NAME = "syntax-tree.json"; - private static final String SYNTAX_TREE_META_FILENAME = "meta.properties"; - - // JSON Keys - private static final String BALLERINA_VERSION_KEY = "ballerinaVersion"; - private static final String PACKAGE_ORG_KEY = "packageOrg"; - private static final String PACKAGE_NAME_KEY = "packageName"; - private static final String PACKAGE_VERSION_KEY = "packageVersion"; - private static final String PACKAGE_MODULES_KEY = "modules"; - private static final String MODULE_NAME_KEY = "moduleName"; - private static final String MODULE_DOCUMENTS_KEY = "documents"; - private static final String DOCUMENT_NAME_KEY = "documentName"; - private static final String DOCUMENT_SYNTAX_TREE_KEY = "syntaxTree"; - - // Metadata Keys - private static final String PROGRAM_HASH_KEY = "PROGRAM_HASH"; - - private static final PrintStream out = System.out; - - private boolean isObservabilityIncluded = false; - private final PackageHolder packageHolder = new PackageHolder(); - - @Override - public void process(Project project) { - isObservabilityIncluded = project.buildOptions().observabilityIncluded(); - if (!isObservabilityIncluded) { - return; - } - - // Skip this part if project has not updated - if (ProjectUtils.isProjectUpdated(project)) { - packageHolder.setSyntaxTreeDataString(generateSyntaxTreeJsonString(project)); - } - } - - @Override - public void writeToExecutable(Path executableFile, Project project) throws IOException { - if (!isObservabilityIncluded) { - return; - } - try (FileSystem fs = FileSystems.newFileSystem(executableFile, - DefaultObservabilitySymbolCollector.class.getClassLoader())) { - Path syntaxTreeDirPath = fs.getPath(SYNTAX_TREE_DIR); - Files.createDirectories(syntaxTreeDirPath); - - // Writing Syntax Tree Json - String syntaxTreeDataString; - if (ProjectUtils.isProjectUpdated(project)) { - syntaxTreeDataString = packageHolder.getSyntaxTreeDataString(); - } else { - // When project is not updated read Syntax Tree Json from Observability Symbols Jar - Path observeJarCachePath = project.targetDir() - .resolve(CACHES_DIR_NAME) - .resolve(project.currentPackage().packageOrg().value()) - .resolve(project.currentPackage().packageName().value()) - .resolve(project.currentPackage().packageVersion().value().toString()) - .resolve("observe") - .resolve(project.currentPackage().packageOrg().value() + "-" - + project.currentPackage().packageName().value() - + "-observability-symbols.jar"); - JarFile observeSymbolsJar = new JarFile(String.valueOf(observeJarCachePath)); - ZipEntry syntaxTreeJsonEntry = observeSymbolsJar.getEntry("syntax-tree/syntax-tree.json"); - InputStream syntaxTreeJsonStream = observeSymbolsJar.getInputStream(syntaxTreeJsonEntry); - syntaxTreeDataString = new String(syntaxTreeJsonStream.readAllBytes(), StandardCharsets.UTF_8); - } - Files.write(syntaxTreeDirPath.resolve(SYNTAX_TREE_FILE_NAME), - syntaxTreeDataString.getBytes(StandardCharsets.UTF_8)); - - // Writing Syntax Tree Metadata - Properties props = new Properties(); - try (OutputStream outputStream = - Files.newOutputStream(syntaxTreeDirPath.resolve(SYNTAX_TREE_META_FILENAME))) { - props.setProperty(PROGRAM_HASH_KEY, getSyntaxTreeHash(syntaxTreeDataString)); - props.store(outputStream, null); - } catch (NoSuchAlgorithmException e) { - out.println("error: failed to store Enriched Syntax Tree Json into the Jar due to " + e.getMessage()); - } - } - } - - private String getSyntaxTreeHash(String syntaxTreeDataString) throws NoSuchAlgorithmException { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] encodedHash = digest.digest(syntaxTreeDataString.getBytes(StandardCharsets.UTF_8)); - return bytesToHex(encodedHash); - } - - private static String bytesToHex(byte[] hash) { - StringBuilder hexString = new StringBuilder(); - for (byte b : hash) { - String hex = Integer.toHexString(0xff & b); - if (hex.length() == 1) { - hexString.append('0'); - } - hexString.append(hex); - } - return hexString.toString(); - } - - private String generateSyntaxTreeJsonString(Project project) { - Package currentPackage = project.currentPackage(); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - packageCompilation.diagnosticResult(); // Trigger Compilation - - packageHolder.setOrg(currentPackage.packageOrg().toString()); - packageHolder.setName(currentPackage.packageName().toString()); - packageHolder.setVersion(currentPackage.packageVersion().toString()); - - final String ballerinaVersion = RepoUtils.getBallerinaVersion(); - StringBuilder jsonStringBuilder = new StringBuilder().append("{\"") - .append(BALLERINA_VERSION_KEY).append("\":\"").append(ballerinaVersion).append("\",\"") - .append(PACKAGE_ORG_KEY).append("\":\"").append(packageHolder.getOrg()).append("\",\"") - .append(PACKAGE_NAME_KEY).append("\":\"").append(packageHolder.getName()).append("\",\"") - .append(PACKAGE_VERSION_KEY).append("\":\"").append(packageHolder.getVersion()).append("\",\"") - .append(PACKAGE_MODULES_KEY).append("\":{"); - - - ModuleId[] moduleIds = currentPackage.moduleIds().toArray(new ModuleId[0]); - Arrays.sort(moduleIds, (moduleId1, moduleId2) -> { - String moduleName1 = moduleId1.moduleName(); - String moduleName2 = moduleId2.moduleName(); - return moduleName1.compareTo(moduleName2); - }); - - for (ModuleId moduleId : moduleIds) { - SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId); - Module module = currentPackage.module(moduleId); - String moduleName = module.descriptor().name().toString(); - - jsonStringBuilder.append("\"").append(moduleName).append("\":{\"") - .append(MODULE_NAME_KEY).append("\":\"").append(moduleName).append("\",\"") - .append(MODULE_DOCUMENTS_KEY).append("\":{"); - - DocumentId[] documentIds = module.documentIds().toArray(new DocumentId[0]); - Arrays.sort(documentIds, (documentId1, documentId2) -> { - String documentName1 = module.document(documentId1).name(); - String documentName2 = module.document(documentId2).name(); - return documentName1.compareTo(documentName2); - }); - - for (DocumentId documentId : documentIds) { - Document document = module.document(documentId); - JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(document, semanticModel); - - jsonStringBuilder.append("\"").append(document.name()).append("\":{\"") - .append(DOCUMENT_NAME_KEY).append("\":\"") - .append(document.name()).append("\",\"") - .append(DOCUMENT_SYNTAX_TREE_KEY).append("\":") - .append(syntaxTreeJSON.toString()) - .append("}") - .append(","); - } - jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1); - jsonStringBuilder.append("}}") - .append(","); - } - jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1); - - return jsonStringBuilder.append("}}").toString(); - } -} diff --git a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java b/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java deleted file mode 100644 index 6b8e5568b5ff..000000000000 --- a/misc/observability-symbol-collector/src/main/java/org/ballerinalang/observability/anaylze/model/PackageHolder.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.observability.anaylze.model; - - -/** - * Hold data related to a package. - * - * @since 2.0.0 - */ -public class PackageHolder { - - private String org; - private String name; - private String version; - private String syntaxTreeDataString; - - public void setSyntaxTreeDataString(String syntaxTreeDataString) { - this.syntaxTreeDataString = syntaxTreeDataString; - } - - public String getOrg() { - return org; - } - - public void setOrg(String org) { - this.org = org; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getSyntaxTreeDataString() { - return syntaxTreeDataString; - } -} diff --git a/misc/observability-symbol-collector/src/main/resources/META-INF/services/org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector b/misc/observability-symbol-collector/src/main/resources/META-INF/services/org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector deleted file mode 100644 index 0e094634d99c..000000000000 --- a/misc/observability-symbol-collector/src/main/resources/META-INF/services/org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector +++ /dev/null @@ -1,19 +0,0 @@ -# -# Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -# -# WSO2 Inc. licenses this file to you under the Apache License, -# Version 2.0 (the "License"); you may not use this file except -# in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -org.ballerinalang.observability.anaylze.DefaultObservabilitySymbolCollector \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 5db1ff099167..66b3be5bfe77 100644 --- a/settings.gradle +++ b/settings.gradle @@ -75,7 +75,6 @@ include(':testerina:report-tools') include(':testerina:testerina-compiler-plugin') include(':testerina:testerina-runtime') include(':ballerina-spec-conformance-tests') -include(':observability-symbol-collector') include(':metrics-extensions:ballerina-metrics-extension') include(':metrics-extensions') include(':toml-parser') @@ -232,7 +231,6 @@ project(':testerina:testerina-compiler-plugin').projectDir = file('misc/testerin project(':testerina:testerina-core').projectDir = file('misc/testerina/modules/testerina-core') project(':testerina').projectDir = file('misc/testerina') project(':ballerina-spec-conformance-tests').projectDir = file('tests/ballerina-spec-conformance-tests') -project(':observability-symbol-collector').projectDir = file('misc/observability-symbol-collector') project(':metrics-extensions:ballerina-metrics-extension').projectDir = file('misc/metrics-extensions/modules/ballerina-metrics-extension') project(':metrics-extensions').projectDir = file('misc/metrics-extensions') project(':toml-parser').projectDir = file('misc/toml-parser') diff --git a/tests/jballerina-unit-test/build.gradle b/tests/jballerina-unit-test/build.gradle index 625bc88b3508..2dbdd1aeebe9 100644 --- a/tests/jballerina-unit-test/build.gradle +++ b/tests/jballerina-unit-test/build.gradle @@ -28,7 +28,6 @@ dependencies { implementation project(':ballerina-runtime') implementation project(':ballerina-lang:value'); implementation project(':ballerina-lang:regexp'); - implementation project(':observability-symbol-collector'); testImplementation "org.mockito:mockito-core:${project.mockitoCoreVersion}" testImplementation "org.mockito:mockito-testng:${project.mockitoTestNGVersion}" diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/observe/syntaxtree/SyntaxTreeEqualityTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/observe/syntaxtree/SyntaxTreeEqualityTest.java deleted file mode 100644 index 6a337be230ef..000000000000 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/observe/syntaxtree/SyntaxTreeEqualityTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.ballerinalang.test.observe.syntaxtree; - -import org.ballerinalang.test.BCompileUtil; -import org.ballerinalang.test.CompileResult; -import org.testng.Assert; -import org.testng.annotations.Test; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URLClassLoader; -import java.nio.file.Paths; - -/** - * Test class to test the equality of the syntax tree generated by the ObservabilitySymbolCollector. - * - * @since 2201.6.0 - */ -public class SyntaxTreeEqualityTest { - - private static final String testPkgLocation = Paths.get("test-src", "syntaxtree").toFile().getPath(); - - @Test - public void testSyntaxTreeEquality() throws IOException { - String jsonFilePath = "syntax-tree/syntax-tree.json"; - - // Build the package - CompileResult firstBuildResult = BCompileUtil.compile(testPkgLocation); - URLClassLoader firstBuildClassLoader = (URLClassLoader) firstBuildResult.getClassLoader(); - InputStream jsonFile = firstBuildClassLoader.getResourceAsStream(jsonFilePath); - String firstBuildJsonFileContent = new String(jsonFile.readAllBytes()); - - // Build the package again - CompileResult secondBuildResult = BCompileUtil.compile(testPkgLocation); - URLClassLoader secondBuildClassLoader = (URLClassLoader) secondBuildResult.getClassLoader(); - InputStream secondJsonFile = secondBuildClassLoader.getResourceAsStream(jsonFilePath); - String secondBuildJsonFileContent = new String(secondJsonFile.readAllBytes()); - - // Compare the syntax trees - Assert.assertEquals(secondBuildJsonFileContent, firstBuildJsonFileContent); - } -} From 16c19671ff1be7b5429a356a0b0b00c3b3539ca7 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Thu, 18 Jan 2024 23:13:12 +0530 Subject: [PATCH 2/2] Fix checkstyle errors --- .../src/main/java/io/ballerina/projects/JarResolver.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java index c8c9b1413e15..6fd4e3480130 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java @@ -26,29 +26,21 @@ import io.ballerina.tools.diagnostics.DiagnosticInfo; import io.ballerina.tools.diagnostics.DiagnosticSeverity; import org.apache.maven.artifact.versioning.ComparableVersion; -import org.wso2.ballerinalang.compiler.util.CompilerContext; import org.wso2.ballerinalang.compiler.util.CompilerUtils; -import java.io.IOException; -import java.io.PrintStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; import static io.ballerina.identifier.Utils.encodeNonFunctionIdentifier; import static io.ballerina.projects.util.ProjectConstants.ANON_ORG; -import static io.ballerina.projects.util.ProjectConstants.CACHES_DIR_NAME; import static io.ballerina.projects.util.ProjectConstants.DOT; // TODO move this class to a separate Java package. e.g. io.ballerina.projects.platform.jballerina @@ -64,7 +56,6 @@ public class JarResolver { private final JBallerinaBackend jBalBackend; private final PackageResolution pkgResolution; private final PackageContext rootPackageContext; - private final PrintStream err = System.err; private final List diagnosticList; private DiagnosticResult diagnosticResult;