Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit f439019

Browse files
committed
#354 Including custom forests in CMA configurations
1 parent 6d639ee commit f439019

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ protected String getContextKeyForResourcesToSave() {
218218
return getClass().getName() + "-resources-to-save";
219219
}
220220

221+
/**
222+
* If the payload is XML, this will first convert it to JSON.
223+
*
224+
* @param context
225+
* @param payload
226+
* @return
227+
*/
221228
protected ObjectNode convertPayloadToObjectNode(CommandContext context, String payload) {
222229
payload = convertXmlPayloadToJsonIfNecessary(context, payload);
223230
try {

src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
import com.marklogic.appdeployer.AppConfig;
55
import com.marklogic.appdeployer.ConfigDir;
66
import com.marklogic.mgmt.SaveReceipt;
7-
import com.marklogic.mgmt.api.API;
87
import com.marklogic.mgmt.api.configuration.Configuration;
98
import com.marklogic.mgmt.api.configuration.Configurations;
10-
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
11-
import com.marklogic.mgmt.mapper.ResourceMapper;
129
import com.marklogic.mgmt.resource.ResourceManager;
13-
import com.marklogic.mgmt.util.ObjectMapperFactory;
1410

1511
import java.io.File;
16-
import java.io.IOException;
1712
import java.util.ArrayList;
1813
import java.util.List;
1914

@@ -161,28 +156,21 @@ protected void deployResourcesViaCma(CommandContext context, File resourceDir) {
161156
}
162157
String payload = readResourceFromFile(context, f);
163158
if (payload != null && payload.trim().length() > 0) {
164-
payload = convertXmlPayloadToJsonIfNecessary(context, payload);
165-
ObjectNode objectNode;
166-
try {
167-
objectNode = (ObjectNode)ObjectMapperFactory.getObjectMapper().readTree(payload);
168-
} catch (IOException e) {
169-
throw new RuntimeException("Unable to read JSON, cause: " + e.getMessage(), e);
170-
}
159+
ObjectNode objectNode = convertPayloadToObjectNode(context, payload);
171160
((SupportsCmaCommand) this).addResourceToConfiguration(objectNode, config);
172161
}
173162
}
174163
deployConfiguration(context, config);
175164
}
176165

177166
/**
178-
*
179167
* @param context
180168
* @param mergedReferences
181169
*/
182170
protected void saveMergedResourcesViaCma(CommandContext context, List<ResourceReference> mergedReferences) {
183171
Configuration config = new Configuration();
184172
for (ResourceReference reference : mergedReferences) {
185-
((SupportsCmaCommand)this).addResourceToConfiguration(reference.getObjectNode(), config);
173+
((SupportsCmaCommand) this).addResourceToConfiguration(reference.getObjectNode(), config);
186174
}
187175
deployConfiguration(context, config);
188176
}

src/main/java/com/marklogic/appdeployer/command/databases/DeployOtherDatabasesCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ protected void deployDatabasesAndForestsViaCma(CommandContext context, List<Data
363363
databasePlans.forEach(plan -> {
364364
final DeployDatabaseCommand deployDatabaseCommand = plan.getDeployDatabaseCommand();
365365
String payload = deployDatabaseCommand.buildPayloadForSaving(context);
366-
payload = convertXmlPayloadToJsonIfNecessary(context, payload);
367-
dbConfig.addDatabase(payload);
366+
dbConfig.addDatabase(convertPayloadToObjectNode(context, payload));
368367

369368
DeployForestsCommand deployForestsCommand = deployDatabaseCommand.buildDeployForestsCommand(plan.getDatabaseName(), context);
370369
if (deployForestsCommand != null) {

src/main/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsCommand.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package com.marklogic.appdeployer.command.forests;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
35
import com.marklogic.appdeployer.ConfigDir;
46
import com.marklogic.appdeployer.command.AbstractCommand;
57
import com.marklogic.appdeployer.command.CommandContext;
68
import com.marklogic.appdeployer.command.SortOrderConstants;
79
import com.marklogic.mgmt.PayloadParser;
10+
import com.marklogic.mgmt.api.configuration.Configuration;
811
import com.marklogic.mgmt.resource.forests.ForestManager;
912

1013
import java.io.File;
14+
import java.util.Iterator;
1115

1216
/**
1317
* Use this command when you want precise control over the forests that are created for a database. It processes
1418
* each directory under ml-config/forests (the name of the directory does not matter, but it makes sense to name
1519
* it after the database that the forests belong to), and each file in a directory can have a single forest object
1620
* or an array of forest objects.
17-
*
21+
* <p>
1822
* You can also set customForestsPath to specify a directory other than "forest" as the path that contains
1923
* directories of custom forests. This allows you to easily support different custom forests in different
2024
* environments.
@@ -30,19 +34,28 @@ public DeployCustomForestsCommand() {
3034

3135
@Override
3236
public void execute(CommandContext context) {
37+
Configuration configuration = null;
38+
if (context.getAppConfig().getCmaConfig().isDeployForests()) {
39+
configuration = new Configuration();
40+
}
41+
3342
for (ConfigDir configDir : context.getAppConfig().getConfigDirs()) {
3443
File dir = new File(configDir.getBaseDir(), customForestsPath);
3544
if (dir != null && dir.exists()) {
3645
payloadParser = new PayloadParser();
3746
for (File f : dir.listFiles()) {
3847
if (f.isDirectory()) {
39-
processDirectory(f, context);
48+
processDirectory(f, context, configuration);
4049
}
4150
}
4251
} else {
4352
logResourceDirectoryNotFound(dir);
4453
}
4554
}
55+
56+
if (configuration != null) {
57+
context.addCmaConfigurationToCombinedRequest(configuration);
58+
}
4659
}
4760

4861
/**
@@ -51,21 +64,43 @@ public void execute(CommandContext context) {
5164
* @param dir
5265
* @param context
5366
*/
54-
protected void processDirectory(File dir, CommandContext context) {
67+
protected void processDirectory(File dir, CommandContext context, Configuration configuration) {
5568
if (logger.isInfoEnabled()) {
5669
logger.info("Processing custom forest files in directory: " + dir.getAbsolutePath());
5770
}
5871
ForestManager mgr = new ForestManager(context.getManageClient());
72+
5973
for (File f : listFilesInDirectory(dir)) {
6074
if (logger.isInfoEnabled()) {
6175
logger.info("Processing forests in file: " + f.getAbsolutePath());
6276
}
6377
String payload = readResourceFromFile(context, f);
78+
6479
if (payloadParser.isJsonPayload(payload)) {
65-
mgr.saveJsonForests(payload);
80+
if (configuration != null) {
81+
addForestsToCmaConfiguration(context, payload, configuration);
82+
} else {
83+
mgr.saveJsonForests(payload);
84+
}
6685
} else {
67-
mgr.save(payload);
86+
if (configuration != null) {
87+
configuration.addForest(convertPayloadToObjectNode(context, payload));
88+
} else {
89+
mgr.save(payload);
90+
}
91+
}
92+
}
93+
}
94+
95+
protected void addForestsToCmaConfiguration(CommandContext context, String payload, Configuration configuration) {
96+
JsonNode node = payloadParser.parseJson(payload);
97+
if (node.isArray()) {
98+
Iterator<JsonNode> iter = node.iterator();
99+
while (iter.hasNext()) {
100+
configuration.addForest((ObjectNode) iter.next());
68101
}
102+
} else {
103+
configuration.addForest((ObjectNode) node);
69104
}
70105
}
71106

src/main/java/com/marklogic/mgmt/api/configuration/Configuration.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ protected ObjectNode readJson(String json) {
6666
}
6767
}
6868

69-
public ObjectNode addDatabase(String json) {
70-
ObjectNode node = readJson(json);
71-
addDatabase(node);
72-
return node;
73-
}
74-
7569
public void addDatabase(ObjectNode d) {
7670
if (databases == null) databases = new ArrayList<>();
7771
databases.add(d);

src/test/java/com/marklogic/appdeployer/command/forests/DeployCustomForestsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ public void test() {
3232
assertTrue(mgr.exists("sample-app-content-custom-2"));
3333
assertTrue(mgr.exists("sample-app-content-custom-3"));
3434
}
35+
36+
@Test
37+
public void deployWithCma() {
38+
appConfig.getCmaConfig().enableAll();
39+
test();
40+
}
3541
}

0 commit comments

Comments
 (0)