From 3eb14f9fa357d47dee634f4d8c9348f6f3367186 Mon Sep 17 00:00:00 2001 From: Nipuna Fernando Date: Fri, 1 Nov 2024 19:52:03 +0530 Subject: [PATCH 1/2] Build the fat jar of the extension --- .../core/db/DatabaseManager.java | 25 ++++++++++++++++++- .../src/main/java/module-info.java | 1 + .../build.gradle | 19 +++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java index d55e376f6..58549a936 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java @@ -22,7 +22,10 @@ import io.ballerina.flowmodelgenerator.core.db.model.FunctionResult; import io.ballerina.flowmodelgenerator.core.db.model.ParameterResult; +import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -46,11 +49,31 @@ public class DatabaseManager { private final String dbPath; public DatabaseManager() { + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Failed to load SQLite JDBC driver", e); + } + + Path tempDir; + try { + tempDir = Files.createTempDirectory("central-index"); + } catch (IOException e) { + throw new RuntimeException("Failed to create a temporary directory", e); + } + URL dbUrl = getClass().getClassLoader().getResource(INDEX_FILE_NAME); if (dbUrl == null) { throw new RuntimeException("Database resource not found: " + INDEX_FILE_NAME); } - dbPath = "jdbc:sqlite:" + dbUrl.getPath(); + Path tempFile = tempDir.resolve(INDEX_FILE_NAME); + try { + Files.copy(dbUrl.openStream(), tempFile); + } catch (IOException e) { + throw new RuntimeException("Failed to copy the database file to the temporary directory", e); + } + + dbPath = "jdbc:sqlite:" + tempFile.toString(); } public enum FunctionKind { diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/module-info.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/module-info.java index 3228af6d5..2123708c4 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/module-info.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/module-info.java @@ -31,6 +31,7 @@ requires io.ballerina.openapi.core; requires io.swagger.v3.oas.models; requires jakarta.persistence; + requires org.xerial.sqlitejdbc; exports io.ballerina.flowmodelgenerator.core; } diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/build.gradle b/flow-model-generator/modules/flow-model-generator-ls-extension/build.gradle index 64ebbb3e6..5f8a2bb5e 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/build.gradle +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/build.gradle @@ -115,6 +115,20 @@ def pullBallerinaModule(String packageName) { } } +// TODO: This should be removed once https://github.com/ballerina-platform/ballerina-distribution/issues/5811 is completed +shadowJar { + archiveBaseName.set(project.name) + archiveClassifier.set('') + archiveVersion.set(project.version.toString()) + configurations = [project.configurations.runtimeClasspath] + dependencies { + include(dependency("org.xerial:sqlite-jdbc:${sqliteJdbcVersion}")) + exclude('META-INF/*.SF') + exclude('META-INF/*.DSA') + exclude('META-INF/*.RSA') + } +} + test.dependsOn pullBallerinaModule('ballerinax/redis') test.dependsOn pullBallerinaModule('ballerinax/trigger.salesforce') @@ -130,7 +144,6 @@ test { } ext.moduleName = 'io.ballerina.flowmodelgenerator.extension' - jar { duplicatesStrategy = DuplicatesStrategy.EXCLUDE dependsOn configurations.dist @@ -147,3 +160,7 @@ compileJava { classpath = files() } } + +artifacts { + archives shadowJar +} From 29b33059d392b878f1233d60ee68658e3a62df49 Mon Sep 17 00:00:00 2001 From: Nipuna Fernando Date: Fri, 1 Nov 2024 20:00:25 +0530 Subject: [PATCH 2/2] Make the DatabaseManager a singleton --- .../flowmodelgenerator/core/AvailableNodesGenerator.java | 2 +- .../flowmodelgenerator/core/ConnectorGenerator.java | 2 +- .../flowmodelgenerator/core/FunctionGenerator.java | 2 +- .../flowmodelgenerator/core/db/DatabaseManager.java | 9 +++++++++ .../flowmodelgenerator/core/model/node/ActionCall.java | 2 +- .../flowmodelgenerator/core/model/node/FunctionCall.java | 4 ++-- .../core/model/node/NewConnection.java | 2 +- 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/AvailableNodesGenerator.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/AvailableNodesGenerator.java index 786e88262..230598643 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/AvailableNodesGenerator.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/AvailableNodesGenerator.java @@ -217,7 +217,7 @@ private Optional getConnection(Symbol symbol) { } private static List fetchConnections(ModuleID moduleId, String parentSymbol) { - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); Optional connectorResult = dbManager.getFunction(moduleId.orgName(), moduleId.packageName(), NewConnection.INIT_SYMBOL, DatabaseManager.FunctionKind.CONNECTOR); diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ConnectorGenerator.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ConnectorGenerator.java index d878e61b7..18085caf0 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ConnectorGenerator.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ConnectorGenerator.java @@ -54,7 +54,7 @@ public JsonArray getConnectors(Map queryMap) { if (CommonUtils.hasNoKeyword(queryMap, "offset")) { modifiedQueryMap.put("offset", "0"); } - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); List connectorResults = CommonUtils.hasNoKeyword(queryMap, "q") ? dbManager.getAllFunctions(DatabaseManager.FunctionKind.CONNECTOR) : diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/FunctionGenerator.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/FunctionGenerator.java index 325fae2f6..7428cffb9 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/FunctionGenerator.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/FunctionGenerator.java @@ -126,7 +126,7 @@ private void buildProjectNodes(Map queryMap, LineRange position) private void buildUtilityNodes(Map queryMap) { Category.Builder utilityBuilder = rootBuilder.stepIn(Category.Name.UTILITIES); - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); List functionResults = CommonUtils.hasNoKeyword(queryMap, "q") ? dbManager.getAllFunctions(DatabaseManager.FunctionKind.FUNCTION) : diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java index 58549a936..749778594 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/db/DatabaseManager.java @@ -48,6 +48,15 @@ public class DatabaseManager { private static final Logger LOGGER = Logger.getLogger(DatabaseManager.class.getName()); private final String dbPath; + private static class Holder { + + private static final DatabaseManager INSTANCE = new DatabaseManager(); + } + + public static DatabaseManager getInstance() { + return Holder.INSTANCE; + } + public DatabaseManager() { try { Class.forName("org.sqlite.JDBC"); diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/ActionCall.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/ActionCall.java index db8109067..7a7a06e06 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/ActionCall.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/ActionCall.java @@ -94,7 +94,7 @@ private static FlowNode fetchNodeTemplate(NodeBuilder nodeBuilder, Codedata code return null; } - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); Optional functionResult = codedata.id() != null ? dbManager.getFunction(codedata.id()) : dbManager.getAction(codedata.org(), codedata.module(), codedata.symbol()); if (functionResult.isEmpty()) { diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/FunctionCall.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/FunctionCall.java index db3481ad3..750a5f591 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/FunctionCall.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/FunctionCall.java @@ -110,7 +110,7 @@ public void setConcreteTemplateData(TemplateContext context) { return; } - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); Optional functionResult = dbManager.getFunction(codedata.org(), codedata.module(), codedata.symbol(), DatabaseManager.FunctionKind.FUNCTION); @@ -184,7 +184,7 @@ public Map> toSource(SourceBuilder sourceBuilder) { } private static FlowNode fetchNodeTemplate(NodeBuilder nodeBuilder, Codedata codedata) { - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); Optional functionResult = dbManager.getFunction(codedata.org(), codedata.module(), codedata.symbol(), DatabaseManager.FunctionKind.FUNCTION); diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/NewConnection.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/NewConnection.java index a498985ad..d60f5dd25 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/NewConnection.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/model/node/NewConnection.java @@ -92,7 +92,7 @@ public Map> toSource(SourceBuilder sourceBuilder) { } private static FlowNode fetchNodeTemplate(NodeBuilder nodeBuilder, Codedata codedata) { - DatabaseManager dbManager = new DatabaseManager(); + DatabaseManager dbManager = DatabaseManager.getInstance(); Optional functionResult = codedata.id() != null ? dbManager.getFunction(codedata.id()) : dbManager.getFunction(codedata.org(), codedata.module(), codedata.symbol(), DatabaseManager.FunctionKind.CONNECTOR);