Skip to content

Commit b10f2b0

Browse files
committed
MLE-25666 Deferring construction of DatabaseClient
This is just scratching the surface - next step will be to modify GenericFileLoader to accept a DatabaseClientSupplier instead of a DatabaseClient. That will allow the commands for loading modules, data, and schemas to only construct a client when necessary instead of eagerly.
1 parent 3fc5055 commit b10f2b0

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/modules/LoadModulesCommand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,13 @@ protected void loadModulesIntoMainServer(CommandContext context) {
6262
}
6363

6464
AppConfig config = context.getAppConfig();
65-
DatabaseClient client = config.newDatabaseClient();
6665

6766
final List<String> pathsList = config.getModulePaths();
6867
final String[] pathsArray = pathsList.toArray(new String[]{});
6968

70-
try {
71-
logger.info("Loading modules from paths: " + pathsList);
69+
try (DatabaseClient client = config.newDatabaseClient()) {
70+
logger.info("Loading modules from paths: {}", pathsList);
7271
modulesLoader.loadModules(client, new DefaultModulesFinder(), pathsArray);
73-
} finally {
74-
client.release();
7572
}
7673
}
7774

ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/plugins/InstallPluginsCommand.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.marklogic.appdeployer.command.AbstractUndoableCommand;
99
import com.marklogic.appdeployer.command.CommandContext;
1010
import com.marklogic.appdeployer.command.SortOrderConstants;
11+
import com.marklogic.client.ext.helper.DatabaseClientSupplier;
1112
import com.marklogic.client.DatabaseClient;
1213
import com.marklogic.client.document.BinaryDocumentManager;
1314
import com.marklogic.client.eval.ServerEvaluationCall;
@@ -39,9 +40,10 @@ public void execute(CommandContext context) {
3940
return;
4041
}
4142

42-
DatabaseClient client = determineDatabaseClient(context.getAppConfig());
43-
for (String path : paths) {
44-
installPluginsInPath(path, context.getAppConfig(), client);
43+
try (DatabaseClientSupplier clientSupplier = new DatabaseClientSupplier(() -> determineDatabaseClient(context.getAppConfig()))) {
44+
for (String path : paths) {
45+
installPluginsInPath(path, context.getAppConfig(), clientSupplier);
46+
}
4547
}
4648
}
4749

@@ -52,9 +54,10 @@ public void undo(CommandContext context) {
5254
return;
5355
}
5456

55-
DatabaseClient client = determineDatabaseClient(context.getAppConfig());
56-
for (String path : paths) {
57-
uninstallPluginsInPath(path, context.getAppConfig(), client);
57+
try (DatabaseClientSupplier clientSupplier = new DatabaseClientSupplier(() -> determineDatabaseClient(context.getAppConfig()))) {
58+
for (String path : paths) {
59+
uninstallPluginsInPath(path, context.getAppConfig(), clientSupplier);
60+
}
5861
}
5962
}
6063

@@ -72,7 +75,7 @@ protected List<String> getPluginPaths(CommandContext context) {
7275
return config.getPluginPaths();
7376
}
7477

75-
protected void installPluginsInPath(String path, AppConfig appConfig, DatabaseClient client) {
78+
protected void installPluginsInPath(String path, AppConfig appConfig, DatabaseClientSupplier clientSupplier) {
7679
File pluginsDir = new File(path);
7780
if (pluginsDir == null || !pluginsDir.exists()) {
7881
return;
@@ -86,9 +89,9 @@ protected void installPluginsInPath(String path, AppConfig appConfig, DatabaseCl
8689
}
8790

8891
makePlugin(dir, appConfig);
89-
final String binaryUri = insertPluginZip(dir, appConfig, client);
92+
final String binaryUri = insertPluginZip(dir, appConfig, clientSupplier.get());
9093
if (binaryUri != null) {
91-
installPlugin(binaryUri, appConfig, client);
94+
installPlugin(binaryUri, appConfig, clientSupplier.get());
9295
}
9396
}
9497
}
@@ -151,7 +154,7 @@ protected void installPlugin(String uri, AppConfig appConfig, DatabaseClient cli
151154
logger.info(format("Installed plugin with scope '%s', result: %s", scope, result));
152155
}
153156

154-
protected void uninstallPluginsInPath(String path, AppConfig appConfig, DatabaseClient client) {
157+
protected void uninstallPluginsInPath(String path, AppConfig appConfig, DatabaseClientSupplier clientSupplier) {
155158
File pluginsDir = new File(path);
156159
if (pluginsDir == null || !pluginsDir.exists()) {
157160
return;
@@ -166,15 +169,15 @@ protected void uninstallPluginsInPath(String path, AppConfig appConfig, Database
166169

167170
final String pluginName = getPluginName(file, appConfig);
168171
if (pluginName != null) {
169-
uninstallPlugin(pluginName, appConfig, client);
172+
uninstallPlugin(pluginName, appConfig, clientSupplier.get());
170173
}
171174
}
172175
}
173176
}
174177

175178
protected String getPluginName(File dir, AppConfig appConfig) {
176179
File manifestFile = new File(dir, "manifest.xml");
177-
if (manifestFile == null || !manifestFile.exists()) {
180+
if (!manifestFile.exists()) {
178181
// Need to make the plugin so the metadata file is guaranteed to exist
179182
makePlugin(dir, appConfig);
180183
manifestFile = new File(dir, "manifest.xml");

ml-app-deployer/src/main/java/com/marklogic/appdeployer/command/security/InsertCertificateHostsTemplateCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public InsertCertificateHostsTemplateCommand() {
3737

3838
@Override
3939
public void execute(CommandContext context) {
40+
// A query has to be made to MarkLogic here to get the list of certificate template names, because otherwise,
41+
// we don't know what template names to look for in the user's project directory.
4042
List<String> templateNames = new CertificateTemplateManager(context.getManageClient()).getAsXml().getListItemNameRefs();
4143
if (templateNames != null && !templateNames.isEmpty()) {
4244
if (logger.isInfoEnabled()) {
43-
logger.info("Looking for host certificates to insert for certificate templates: " + templateNames);
45+
logger.info("Looking for host certificates to insert for certificate templates: {}", templateNames);
4446
}
4547
for (String templateName : templateNames) {
4648
insertHostCertificatesForTemplate(context, templateName);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client.ext.helper;
5+
6+
import com.marklogic.client.DatabaseClient;
7+
8+
import java.io.Closeable;
9+
import java.util.function.Supplier;
10+
11+
/**
12+
* Preferred mechanism for lazy instantiation of a DatabaseClient. Will eventually deprecate DatabaseClientProvider in
13+
* favor of this.
14+
*
15+
* @since 6.2.0
16+
*/
17+
public class DatabaseClientSupplier implements Closeable, Supplier<DatabaseClient> {
18+
19+
private DatabaseClient databaseClient;
20+
private final Supplier<DatabaseClient> databaseClientSupplier;
21+
22+
/**
23+
* @param databaseClientSupplier delegates construction of the client to the given supplier. Will then hold onto
24+
* an instance of the client after it's created so that it's only created once.
25+
*/
26+
public DatabaseClientSupplier(Supplier<DatabaseClient> databaseClientSupplier) {
27+
this.databaseClientSupplier = databaseClientSupplier;
28+
}
29+
30+
public DatabaseClient get() {
31+
if (databaseClient == null) {
32+
databaseClient = databaseClientSupplier.get();
33+
}
34+
return databaseClient;
35+
}
36+
37+
@Override
38+
public void close() {
39+
if (databaseClient != null) {
40+
databaseClient.close();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)