This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#53 Can now specify a collections.properties file to configure collec…
…tions for a ruleset
- Loading branch information
Rob Rudin
committed
Feb 22, 2017
1 parent
f4a0b98
commit b60bd1f
Showing
11 changed files
with
200 additions
and
26 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/com/marklogic/client/file/CollectionsDocumentFileProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.marklogic.client.file; | ||
|
||
import com.marklogic.client.helper.LoggingObject; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Looks for a special file in each directory - defaults to collections.properties - that contains properties where the | ||
* key is the name of a file in the directory, and the value is a comma-delimited list of collections to load the file | ||
* into (which means you can't use a comma in any collection name). This will by default NOT load the configured | ||
* properties file, as that's expected to just be configuration data and not something that's intended to be loaded | ||
* into MarkLogic. | ||
*/ | ||
public class CollectionsDocumentFileProcessor extends LoggingObject implements DocumentFileProcessor { | ||
|
||
private String collectionsFilename = "collections.properties"; | ||
|
||
// Used to avoid checking for and loading the properties for every file in a directory | ||
private Map<File, Properties> propertiesCache = new HashMap<>(); | ||
|
||
/** | ||
* @param documentFile | ||
* @return | ||
*/ | ||
@Override | ||
public DocumentFile processDocumentFile(DocumentFile documentFile) { | ||
File file = documentFile.getFile(); | ||
String name = file.getName(); | ||
if (collectionsFilename.equals(name)) { | ||
return null; | ||
} | ||
|
||
File collectionsFile = new File(file.getParentFile(), collectionsFilename); | ||
if (collectionsFile.exists()) { | ||
try { | ||
Properties props = loadProperties(collectionsFile); | ||
if (props.containsKey(name)) { | ||
String value = props.getProperty(name); | ||
documentFile.getDocumentMetadata().withCollections(value.split(",")); | ||
} | ||
} catch (IOException e) { | ||
logger.warn("Unable to load properties from collections file: " + collectionsFile.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
return documentFile; | ||
} | ||
|
||
protected Properties loadProperties(File collectionsFile) throws IOException { | ||
Properties props = null; | ||
if (propertiesCache.containsKey(collectionsFile)) { | ||
props = propertiesCache.get(collectionsFile); | ||
} | ||
if (props != null) { | ||
return props; | ||
} | ||
props = new Properties(); | ||
FileReader reader = null; | ||
try { | ||
reader = new FileReader(collectionsFile); | ||
props.load(reader); | ||
propertiesCache.put(collectionsFile, props); | ||
return props; | ||
} finally { | ||
if (reader != null) { | ||
reader.close(); | ||
} | ||
} | ||
} | ||
|
||
public Map<File, Properties> getPropertiesCache() { | ||
return propertiesCache; | ||
} | ||
|
||
public void setCollectionsFilename(String collectionsFilename) { | ||
this.collectionsFilename = collectionsFilename; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
src/main/java/com/marklogic/client/file/DocumentFileProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package com.marklogic.client.file; | ||
|
||
/** | ||
* Callback-style interface for processing - i.e. do whatever you want with - a DocumentFile instance before it's | ||
* written to MarkLogic. | ||
* Interface for processing - i.e. do whatever you want with - a DocumentFile instance before it's written to MarkLogic. | ||
*/ | ||
public interface DocumentFileProcessor { | ||
|
||
boolean supportsDocumentFile(DocumentFile documentFile); | ||
/** | ||
* @param documentFile | ||
* @return the same or a new DocumentFile instance | ||
*/ | ||
DocumentFile processDocumentFile(DocumentFile documentFile); | ||
|
||
void processDocumentFile(DocumentFile documentFile); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/marklogic/client/file/FilterDocumentFileProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.marklogic.client.file; | ||
|
||
import java.io.FileFilter; | ||
|
||
/** | ||
* Simple filter implementation for returning null if the DocumentFile doesn't match the given FileFilter. | ||
*/ | ||
public class FilterDocumentFileProcessor implements DocumentFileProcessor { | ||
|
||
private FileFilter fileFilter; | ||
|
||
public FilterDocumentFileProcessor(FileFilter fileFilter) { | ||
this.fileFilter = fileFilter; | ||
} | ||
|
||
@Override | ||
public DocumentFile processDocumentFile(DocumentFile documentFile) { | ||
return fileFilter.accept(documentFile.getFile()) ? documentFile : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/com/marklogic/client/schemasloader/impl/LoadRulesetsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.marklogic.client.schemasloader.impl; | ||
|
||
import com.marklogic.client.AbstractIntegrationTest; | ||
import com.marklogic.client.file.DocumentFile; | ||
import com.marklogic.client.helper.ClientHelper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by rrudin on 2/21/2017. | ||
*/ | ||
public class LoadRulesetsTest extends AbstractIntegrationTest { | ||
|
||
/** | ||
* Wipes out documents matching the ones we intend to load - it's assumed you're not using the Schemas database for | ||
* anything besides ad hoc testing like this. | ||
*/ | ||
@Before | ||
public void setup() { | ||
client = newClient("Schemas"); | ||
client.newServerEval().xquery("cts:uri-match('/ruleset*.*') ! xdmp:document-delete(.)").eval(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
DefaultSchemasLoader loader = new DefaultSchemasLoader(client); | ||
List<DocumentFile> files = loader.loadSchemas("src/test/resources/rulesets/collection-test"); | ||
assertEquals(2, files.size()); | ||
|
||
ClientHelper helper = new ClientHelper(client); | ||
|
||
List<String> collections = helper.getCollections("/ruleset1.xml"); | ||
assertEquals(1, collections.size()); | ||
assertTrue(collections.contains("ruleset-abc")); | ||
|
||
collections = helper.getCollections("/ruleset2.json"); | ||
assertEquals(2, collections.size()); | ||
assertTrue(collections.contains("ruleset-abc")); | ||
assertTrue(collections.contains("ruleset-xyz")); | ||
|
||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/test/resources/rulesets/collection-test/collections.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ruleset1.xml=ruleset-abc | ||
ruleset2.json=ruleset-abc,ruleset-xyz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<rdt:rule xml:lang="zxx" xmlns:rdt="http://marklogic.com/xdmp/redaction"> | ||
<rdt:path>//name</rdt:path> | ||
<rdt:method> | ||
<rdt:function>mask-deterministic</rdt:function> | ||
</rdt:method> | ||
<rdt:options> | ||
<length>10</length> | ||
</rdt:options> | ||
</rdt:rule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"description": "hide SSNs", | ||
"path": "//ssn", | ||
"method": { | ||
"function": "redact-us-ssn" | ||
}, | ||
"options": { | ||
"pattern": "partial" | ||
} | ||
} |