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

Commit 3756cde

Browse files
committed
#127 Tokens can now be replaced in values in property files
1 parent 7ac7415 commit 3756cde

File tree

10 files changed

+89
-6
lines changed

10 files changed

+89
-6
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
3-
version=3.13.4
3+
version=3.14.0-develop

src/main/java/com/marklogic/client/ext/file/CollectionsFileDocumentFileProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public CollectionsFileDocumentFileProcessor(String propertiesFilename) {
2323
protected void processProperties(DocumentFile documentFile, Properties properties) {
2424
String name = documentFile.getFile().getName();
2525
if (properties.containsKey(name)) {
26-
String value = properties.getProperty(name);
26+
String value = getPropertyValue(properties, name);
2727
documentFile.getDocumentMetadata().withCollections(value.split(delimiter));
2828
}
2929

3030
if (properties.containsKey(WILDCARD_KEY)) {
31-
String value = properties.getProperty(WILDCARD_KEY);
31+
String value = getPropertyValue(properties, WILDCARD_KEY);
3232
documentFile.getDocumentMetadata().withCollections(value.split(delimiter));
3333
}
3434
}

src/main/java/com/marklogic/client/ext/file/GenericFileLoader.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public List<DocumentFile> loadFiles(String... paths) {
8686
* If batchSize is not set, then this method will load all the documents in one call to the BatchWriter. Otherwise,
8787
* this will divide up the list of documentFiles into batches matching the value of batchSize, with the last batch
8888
* possibly being less than batchSize.
89-
*
89+
*
9090
* @param documentFiles
9191
* @param startPosition
9292
*/
@@ -155,6 +155,8 @@ public void prepareAbstractDocumentFileReader(AbstractDocumentFileReader reader)
155155
reader.addDocumentFileProcessor(processor);
156156
}
157157

158+
applyTokenReplacerOnKnownDocumentProcessors(reader);
159+
158160
if (additionalBinaryExtensions != null) {
159161
FormatDocumentFileProcessor processor = reader.getFormatDocumentFileProcessor();
160162
FormatGetter formatGetter = processor.getFormatGetter();
@@ -170,6 +172,27 @@ public void prepareAbstractDocumentFileReader(AbstractDocumentFileReader reader)
170172
}
171173
}
172174

175+
/**
176+
* If this is an instance of DefaultDocumentFileReader and a TokenReplacer has been set on the instance of this
177+
* class, then pass the TokenReplacer along to the known processors in the reader so that those processors
178+
* can replace token occurrences in property values.
179+
*
180+
* @param reader
181+
*/
182+
protected void applyTokenReplacerOnKnownDocumentProcessors(AbstractDocumentFileReader reader) {
183+
if (reader instanceof DefaultDocumentFileReader && tokenReplacer != null) {
184+
DefaultDocumentFileReader defaultReader = (DefaultDocumentFileReader)reader;
185+
CollectionsFileDocumentFileProcessor cp = defaultReader.getCollectionsFileDocumentFileProcessor();
186+
if (cp != null) {
187+
cp.setTokenReplacer(tokenReplacer);
188+
}
189+
PermissionsFileDocumentFileProcessor pp = defaultReader.getPermissionsFileDocumentFileProcessor();
190+
if (pp != null) {
191+
pp.setTokenReplacer(tokenReplacer);
192+
}
193+
}
194+
}
195+
173196
/**
174197
* Builds a set of DocumentFileProcessor objects based on how this class has been configured.
175198
*

src/main/java/com/marklogic/client/ext/file/PermissionsFileDocumentFileProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public PermissionsFileDocumentFileProcessor(String propertiesFilename, DocumentP
3030
protected void processProperties(DocumentFile documentFile, Properties properties) {
3131
String name = documentFile.getFile().getName();
3232
if (properties.containsKey(name)) {
33-
String value = properties.getProperty(name);
33+
String value = getPropertyValue(properties, name);
3434
documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions());
3535
}
3636

3737
if (properties.containsKey(WILDCARD_KEY)) {
38-
String value = properties.getProperty(WILDCARD_KEY);
38+
String value = getPropertyValue(properties, WILDCARD_KEY);
3939
documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions());
4040
}
4141
}

src/main/java/com/marklogic/client/ext/file/PropertiesDrivenDocumentFileProcessor.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marklogic.client.ext.file;
22

33
import com.marklogic.client.ext.helper.LoggingObject;
4+
import com.marklogic.client.ext.tokenreplacer.TokenReplacer;
45

56
import java.io.File;
67
import java.io.FileFilter;
@@ -23,6 +24,8 @@ public abstract class PropertiesDrivenDocumentFileProcessor extends LoggingObjec
2324
// Used to avoid checking for and loading the properties for every file in a directory
2425
private Map<File, Properties> propertiesCache = new HashMap<>();
2526

27+
private TokenReplacer tokenReplacer;
28+
2629
protected PropertiesDrivenDocumentFileProcessor(String propertiesFilename) {
2730
this.propertiesFilename = propertiesFilename;
2831
}
@@ -81,11 +84,27 @@ protected Properties loadProperties(File propertiesFile) throws IOException {
8184
}
8285
}
8386

87+
protected String getPropertyValue(Properties properties, String propertyName) {
88+
if (properties == null || propertyName == null) {
89+
return null;
90+
}
91+
String value = properties.getProperty(propertyName);
92+
return tokenReplacer != null && value != null ? tokenReplacer.replaceTokens(value) : value;
93+
}
94+
8495
public Map<File, Properties> getPropertiesCache() {
8596
return propertiesCache;
8697
}
8798

8899
public String getPropertiesFilename() {
89100
return propertiesFilename;
90101
}
102+
103+
public void setTokenReplacer(TokenReplacer tokenReplacer) {
104+
this.tokenReplacer = tokenReplacer;
105+
}
106+
107+
protected TokenReplacer getTokenReplacer() {
108+
return tokenReplacer;
109+
}
91110
}

src/test/java/com/marklogic/client/ext/file/CollectionsFileDocumentFileProcessorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.marklogic.client.ext.file;
22

3+
import com.marklogic.client.ext.tokenreplacer.DefaultTokenReplacer;
34
import org.junit.Assert;
45
import org.junit.Test;
56

67
import java.io.File;
8+
import java.util.Properties;
79

810
public class CollectionsFileDocumentFileProcessorTest extends Assert {
911

@@ -25,4 +27,20 @@ public void wildcard() {
2527
assertTrue(file.getDocumentMetadata().getCollections().contains("xml-data"));
2628
assertTrue(file.getDocumentMetadata().getCollections().contains("global"));
2729
}
30+
31+
@Test
32+
public void replaceTokens() {
33+
File testDir = new File("src/test/resources/process-files/token-test");
34+
35+
DefaultTokenReplacer tokenReplacer = new DefaultTokenReplacer();
36+
Properties props = new Properties();
37+
props.setProperty("%%someCollection%%", "this-was-replaced");
38+
tokenReplacer.setProperties(props);
39+
processor.setTokenReplacer(tokenReplacer);
40+
41+
DocumentFile file = new DocumentFile("/test.json", new File(testDir, "test.json"));
42+
processor.processDocumentFile(file);
43+
assertTrue(file.getDocumentMetadata().getCollections().contains("this-was-replaced"));
44+
assertFalse(file.getDocumentMetadata().getCollections().contains("%%someCollection%%"));
45+
}
2846
}

src/test/java/com/marklogic/client/ext/file/PermissionsFileDocumentFileProcessorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.marklogic.client.ext.file;
22

3+
import com.marklogic.client.ext.tokenreplacer.DefaultTokenReplacer;
34
import com.marklogic.client.io.DocumentMetadataHandle;
45
import org.junit.Assert;
56
import org.junit.Test;
67

78
import java.io.File;
9+
import java.util.Properties;
810

911
public class PermissionsFileDocumentFileProcessorTest extends Assert {
1012

@@ -35,4 +37,20 @@ public void wildcard() {
3537
assertNull(permissions.get("manage-admin"));
3638
assertTrue(permissions.get("qconsole-user").contains(DocumentMetadataHandle.Capability.UPDATE));
3739
}
40+
41+
@Test
42+
public void replaceTokens() {
43+
File testDir = new File("src/test/resources/process-files/token-test");
44+
45+
DefaultTokenReplacer tokenReplacer = new DefaultTokenReplacer();
46+
Properties props = new Properties();
47+
props.setProperty("%%roleName%%", "rest-admin");
48+
tokenReplacer.setProperties(props);
49+
processor.setTokenReplacer(tokenReplacer);
50+
51+
DocumentFile file = new DocumentFile("/test.json", new File(testDir, "test.json"));
52+
processor.processDocumentFile(file);
53+
DocumentMetadataHandle.DocumentPermissions permissions = file.getDocumentMetadata().getPermissions();
54+
assertTrue(permissions.get("rest-admin").contains(DocumentMetadataHandle.Capability.UPDATE));
55+
}
3856
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.json=%%someCollection%%
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.json=%%roleName%%,update
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"test": "test"
3+
}

0 commit comments

Comments
 (0)