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

Commit 79e1406

Browse files
committed
#184 Can include/exclude resource filenames based on a regex
1 parent 4758386 commit 79e1406

File tree

8 files changed

+191
-21
lines changed

8 files changed

+191
-21
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.HashMap;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.regex.Pattern;
2122

2223
/**
2324
* Encapsulates common configuration properties for an application deployed to MarkLogic. These properties include not
@@ -172,6 +173,8 @@ public class AppConfig {
172173
private boolean generateSearchOptions = true;
173174

174175
private String[] resourceFilenamesToIgnore;
176+
private Pattern resourceFilenamesExcludePattern;
177+
private Pattern resourceFilenamesIncludePattern;
175178

176179
private Map<String, Object> additionalProperties = new HashMap<>();
177180

@@ -903,4 +906,20 @@ public ConfiguredDatabaseClientFactory getConfiguredDatabaseClientFactory() {
903906
public void setConfiguredDatabaseClientFactory(ConfiguredDatabaseClientFactory configuredDatabaseClientFactory) {
904907
this.configuredDatabaseClientFactory = configuredDatabaseClientFactory;
905908
}
909+
910+
public Pattern getResourceFilenamesExcludePattern() {
911+
return resourceFilenamesExcludePattern;
912+
}
913+
914+
public void setResourceFilenamesExcludePattern(Pattern resourceFilenamesExcludePattern) {
915+
this.resourceFilenamesExcludePattern = resourceFilenamesExcludePattern;
916+
}
917+
918+
public Pattern getResourceFilenamesIncludePattern() {
919+
return resourceFilenamesIncludePattern;
920+
}
921+
922+
public void setResourceFilenamesIncludePattern(Pattern resourceFilenamesIncludePattern) {
923+
this.resourceFilenamesIncludePattern = resourceFilenamesIncludePattern;
924+
}
906925
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.Arrays;
1111
import java.util.List;
12+
import java.util.regex.Pattern;
1213

1314
public class DefaultAppConfigFactory extends PropertySourceFactory implements AppConfigFactory {
1415

@@ -500,6 +501,18 @@ public AppConfig newAppConfig() {
500501
c.setResourceFilenamesToIgnore(values);
501502
}
502503

504+
prop = getProperty("mlResourceFilenamesToExcludeRegex");
505+
if (prop != null) {
506+
logger.info("Excluding resource filenames matching regex: " + prop);
507+
c.setResourceFilenamesExcludePattern(Pattern.compile(prop));
508+
}
509+
510+
prop = getProperty("mlResourceFilenamesToIncludeRegex");
511+
if (prop != null) {
512+
logger.info("Including resource filenames matching regex: " + prop);
513+
c.setResourceFilenamesIncludePattern(Pattern.compile(prop));
514+
}
515+
503516
/**
504517
* Version 2.9.0 of ml-app-deployer, by default, sorts role files by reading each file and looking at the role
505518
* dependencies. You can disable this behavior by setting this property to false.

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.HashSet;
1414
import java.util.Set;
15+
import java.util.regex.Pattern;
1516

1617
/**
1718
* Abstract base class that provides some convenience methods for implementing a command. Subclasses will typically
@@ -43,23 +44,55 @@ public void setFilenamesToIgnore(String... filenames) {
4344
if (filenames == null || filenames.length == 0) {
4445
return;
4546
}
46-
if (resourceFilenameFilter != null && resourceFilenameFilter instanceof ResourceFilenameFilter) {
47-
ResourceFilenameFilter rff = (ResourceFilenameFilter) resourceFilenameFilter;
48-
Set<String> set = null;
49-
if (rff.getFilenamesToIgnore() != null) {
50-
set = rff.getFilenamesToIgnore();
51-
} else {
52-
set = new HashSet<>();
53-
}
54-
for (String f : filenames) {
55-
set.add(f);
56-
}
57-
rff.setFilenamesToIgnore(set);
47+
if (resourceFilenameFilter != null) {
48+
if (resourceFilenameFilter instanceof ResourceFilenameFilter) {
49+
ResourceFilenameFilter rff = (ResourceFilenameFilter) resourceFilenameFilter;
50+
Set<String> set = null;
51+
if (rff.getFilenamesToIgnore() != null) {
52+
set = rff.getFilenamesToIgnore();
53+
} else {
54+
set = new HashSet<>();
55+
}
56+
for (String f : filenames) {
57+
set.add(f);
58+
}
59+
rff.setFilenamesToIgnore(set);
60+
} else {
61+
logger.warn("resourceFilenameFilter is not an instanceof ResourceFilenameFilter, so unable to set resource filenames to ignore");
62+
}
5863
} else {
5964
this.resourceFilenameFilter = new ResourceFilenameFilter(filenames);
6065
}
6166
}
6267

68+
public void setResourceFilenamesExcludePattern(Pattern pattern) {
69+
if (resourceFilenameFilter != null) {
70+
if (resourceFilenameFilter instanceof ResourceFilenameFilter) {
71+
((ResourceFilenameFilter)resourceFilenameFilter).setExcludePattern(pattern);
72+
} else {
73+
logger.warn("resourceFilenameFilter is not an instanceof ResourceFilenameFilter, so unable to set exclude pattern");
74+
}
75+
} else {
76+
ResourceFilenameFilter rff = new ResourceFilenameFilter();
77+
rff.setExcludePattern(pattern);
78+
this.resourceFilenameFilter = rff;
79+
}
80+
}
81+
82+
public void setResourceFilenamesIncludePattern(Pattern pattern) {
83+
if (resourceFilenameFilter != null) {
84+
if (resourceFilenameFilter instanceof ResourceFilenameFilter) {
85+
((ResourceFilenameFilter)resourceFilenameFilter).setIncludePattern(pattern);
86+
} else {
87+
logger.warn("resourceFilenameFilter is not an instanceof ResourceFilenameFilter, so unable to set include pattern");
88+
}
89+
} else {
90+
ResourceFilenameFilter rff = new ResourceFilenameFilter();
91+
rff.setIncludePattern(pattern);
92+
this.resourceFilenameFilter = rff;
93+
}
94+
}
95+
6396
/**
6497
* Simplifies reading the contents of a File into a String.
6598
*

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import java.io.FilenameFilter;
77
import java.util.HashSet;
88
import java.util.Set;
9+
import java.util.regex.Pattern;
910

1011
/**
1112
* Simple filter implementation that returns true for .json and .xml files.
1213
*/
1314
public class ResourceFilenameFilter extends LoggingObject implements FilenameFilter {
1415

1516
private Set<String> filenamesToIgnore;
17+
private Pattern excludePattern;
18+
private Pattern includePattern;
1619

1720
public ResourceFilenameFilter() {
1821
}
@@ -29,14 +32,37 @@ public ResourceFilenameFilter(Set<String> filenamesToIgnore) {
2932
}
3033

3134
@Override
32-
public boolean accept(File dir, String name) {
33-
if (filenamesToIgnore != null && filenamesToIgnore.contains(name)) {
34-
if (logger.isDebugEnabled()) {
35-
logger.debug("Ignoring filename: " + name);
35+
public boolean accept(File dir, String filename) {
36+
if (excludePattern != null && includePattern != null) {
37+
throw new IllegalStateException("Both excludePattern and includePattern cannot be specified");
38+
}
39+
40+
if (excludePattern != null) {
41+
if (excludePattern.matcher(filename).matches()) {
42+
if (logger.isInfoEnabled()) {
43+
logger.info(format("Filename %s matches excludePattern, so ignoring", filename));
44+
}
45+
return false;
46+
}
47+
}
48+
49+
if (includePattern != null) {
50+
if (!includePattern.matcher(filename).matches()) {
51+
if (logger.isInfoEnabled()) {
52+
logger.info(format("Filename %s doesn't match includePattern, so ignoring", filename));
53+
}
54+
return false;
55+
}
56+
}
57+
58+
if (filenamesToIgnore != null && filenamesToIgnore.contains(filename)) {
59+
if (logger.isInfoEnabled()) {
60+
logger.info("Ignoring filename: " + filename);
3661
}
3762
return false;
3863
}
39-
return name.endsWith(".json") || name.endsWith(".xml");
64+
65+
return filename.endsWith(".json") || filename.endsWith(".xml");
4066
}
4167

4268
public void setFilenamesToIgnore(Set<String> ignoreFilenames) {
@@ -47,4 +73,19 @@ public Set<String> getFilenamesToIgnore() {
4773
return filenamesToIgnore;
4874
}
4975

76+
public Pattern getExcludePattern() {
77+
return excludePattern;
78+
}
79+
80+
public void setExcludePattern(Pattern excludePattern) {
81+
this.excludePattern = excludePattern;
82+
}
83+
84+
public Pattern getIncludePattern() {
85+
return includePattern;
86+
}
87+
88+
public void setIncludePattern(Pattern includePattern) {
89+
this.includePattern = includePattern;
90+
}
5091
}

src/main/java/com/marklogic/appdeployer/impl/AbstractAppDeployer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Collections;
1515
import java.util.Comparator;
1616
import java.util.List;
17+
import java.util.regex.Pattern;
1718

1819
/**
1920
* Abstract base class that just needs the subclass to define the list of Command instances to use. Handles executing
@@ -54,12 +55,26 @@ public void deploy(AppConfig appConfig) {
5455
CommandContext context = new CommandContext(appConfig, manageClient, adminManager);
5556

5657
String[] filenamesToIgnore = appConfig.getResourceFilenamesToIgnore();
58+
Pattern excludePattern = appConfig.getResourceFilenamesExcludePattern();
59+
Pattern includePattern = appConfig.getResourceFilenamesIncludePattern();
60+
5761
for (Command command : commands) {
5862
String name = command.getClass().getName();
5963
logger.info(format("Executing command [%s] with sort order [%d]", name, command.getExecuteSortOrder()));
64+
6065
if (command instanceof AbstractCommand) {
61-
((AbstractCommand)command).setFilenamesToIgnore(filenamesToIgnore);
66+
AbstractCommand abstractCommand = (AbstractCommand)command;
67+
if (filenamesToIgnore != null) {
68+
abstractCommand.setFilenamesToIgnore(filenamesToIgnore);
69+
}
70+
if (excludePattern != null) {
71+
abstractCommand.setResourceFilenamesExcludePattern(excludePattern);
72+
}
73+
if (includePattern != null) {
74+
abstractCommand.setResourceFilenamesIncludePattern(includePattern);
75+
}
6276
}
77+
6378
command.execute(context);
6479
logger.info(format("Finished executing command [%s]\n", name));
6580
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ public void allProperties() {
129129
p.setProperty("mlGenerateSchema", "false");
130130
p.setProperty("mlGenerateSearchOptions", "false");
131131
p.setProperty("mlGenerateExtractionTemplate", "false");
132+
132133
p.setProperty("mlResourceFilenamesToIgnore", "role1.json,role2.xml");
134+
p.setProperty("mlResourceFilenamesToExcludeRegex", "dev-.*");
135+
p.setProperty("mlResourceFilenamesToIncludeRegex", "qa-.*");
133136

134137
p.setProperty("mlDatabaseNamesAndReplicaCounts", "Documents,1,Security,2");
135138
p.setProperty("mlReplicaForestDataDirectory", "/var/data");
@@ -208,6 +211,8 @@ public void allProperties() {
208211

209212
assertEquals("role1.json", config.getResourceFilenamesToIgnore()[0]);
210213
assertEquals("role2.xml", config.getResourceFilenamesToIgnore()[1]);
214+
assertEquals("dev-.*", config.getResourceFilenamesExcludePattern().pattern());
215+
assertEquals("qa-.*", config.getResourceFilenamesIncludePattern().pattern());
211216

212217
assertEquals("Documents,1,Security,2", config.getDatabaseNamesAndReplicaCounts());
213218
assertEquals("/var/data", config.getReplicaForestDataDirectory());

src/test/java/com/marklogic/appdeployer/command/security/IgnoreRoleTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.marklogic.mgmt.resource.security.RoleManager;
55
import org.junit.Test;
66

7+
import java.util.regex.Pattern;
8+
79
public class IgnoreRoleTest extends AbstractAppDeployerTest {
810

911
@Test
@@ -20,4 +22,49 @@ public void test() {
2022
undeploySampleApp();
2123
}
2224
}
25+
26+
@Test
27+
public void excludeRoles() {
28+
appConfig.setResourceFilenamesExcludePattern(Pattern.compile(".*role1.*"));
29+
initializeAppDeployer(new DeployRolesCommand());
30+
appDeployer.deploy(appConfig);
31+
32+
try {
33+
RoleManager mgr = new RoleManager(manageClient);
34+
assertTrue(mgr.exists("sample-app-role2"));
35+
assertFalse("Role should not have been created because its filename was excluded via regex", mgr.exists("sample-app-role1"));
36+
} finally {
37+
undeploySampleApp();
38+
}
39+
}
40+
41+
@Test
42+
public void includeRoles() {
43+
appConfig.setResourceFilenamesIncludePattern(Pattern.compile(".*role1.*"));
44+
initializeAppDeployer(new DeployRolesCommand());
45+
appDeployer.deploy(appConfig);
46+
47+
try {
48+
RoleManager mgr = new RoleManager(manageClient);
49+
assertTrue(mgr.exists("sample-app-role1"));
50+
assertFalse("Role should not have been created because its filename was not included via regex", mgr.exists("sample-app-role2"));
51+
} finally {
52+
undeploySampleApp();
53+
}
54+
}
55+
56+
@Test
57+
public void includeAndExcludeRoles() {
58+
appConfig.setResourceFilenamesExcludePattern(Pattern.compile(".*role2.*"));
59+
appConfig.setResourceFilenamesIncludePattern(Pattern.compile(".*role1.*"));
60+
initializeAppDeployer(new DeployRolesCommand());
61+
try {
62+
appDeployer.deploy(appConfig);
63+
fail("Deployment should have failed because exclude and include patterns can't both be set");
64+
} catch (Exception ex) {
65+
assertEquals("Both excludePattern and includePattern cannot be specified", ex.getMessage());
66+
} finally {
67+
undeploySampleApp();
68+
}
69+
}
2370
}

src/test/java/com/marklogic/mgmt/api/ConnectTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ public class ConnectTest extends AbstractApiTest {
99
*/
1010
@Test
1111
public void test() {
12-
manageConfig.setScheme("https");
13-
manageConfig.setConfigureSimpleSsl(true);
14-
1512
api.connect(manageConfig.getHost(), manageConfig);
1613
api.getDb().list();
1714

0 commit comments

Comments
 (0)