diff --git a/src/main/java/com/marklogic/client/file/CollectionsDocumentFileProcessor.java b/src/main/java/com/marklogic/client/file/CollectionsDocumentFileProcessor.java index 02fc157..aa971f8 100644 --- a/src/main/java/com/marklogic/client/file/CollectionsDocumentFileProcessor.java +++ b/src/main/java/com/marklogic/client/file/CollectionsDocumentFileProcessor.java @@ -1,83 +1,28 @@ 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. + * into (which means you can't use a comma in any collection name). */ -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 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); - } - } +public class CollectionsDocumentFileProcessor extends PropertiesDrivenDocumentFileProcessor { - return documentFile; + public CollectionsDocumentFileProcessor() { + this("collections.properties"); } - 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 CollectionsDocumentFileProcessor(String propertiesFilename) { + super(propertiesFilename); } - public Map getPropertiesCache() { - return propertiesCache; - } - - public void setCollectionsFilename(String collectionsFilename) { - this.collectionsFilename = collectionsFilename; + @Override + protected void processProperties(DocumentFile documentFile, Properties properties) { + String name = documentFile.getFile().getName(); + if (properties.containsKey(name)) { + String value = properties.getProperty(name); + documentFile.getDocumentMetadata().withCollections(value.split(",")); + } } } diff --git a/src/main/java/com/marklogic/client/file/DefaultDocumentFileFinder.java b/src/main/java/com/marklogic/client/file/DefaultDocumentFileFinder.java deleted file mode 100644 index 88bf021..0000000 --- a/src/main/java/com/marklogic/client/file/DefaultDocumentFileFinder.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.marklogic.client.file; - -import com.marklogic.client.helper.LoggingObject; - -import java.io.File; -import java.io.FileFilter; -import java.io.IOException; -import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.List; - -public class DefaultDocumentFileFinder extends LoggingObject implements FileVisitor, DocumentFileFinder { - - private Path currentAssetPath; - private FileFilter fileFilter; - private List documentFiles; - - public List findDocumentFiles(String... paths) { - documentFiles = new ArrayList<>(); - for (String path : paths) { - if (logger.isDebugEnabled()) { - logger.debug(format("Finding documents at path: %s", path)); - } - this.currentAssetPath = Paths.get(path); - try { - Files.walkFileTree(this.currentAssetPath, this); - } catch (IOException ie) { - throw new RuntimeException(format("IO error while walking file tree at path: %s", path), ie); - } - } - return documentFiles; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - boolean accept = fileFilter == null || fileFilter.accept(dir.toFile()); - if (accept) { - if (logger.isTraceEnabled()) { - logger.trace("Visiting directory: " + dir); - } - return FileVisitResult.CONTINUE; - } else { - if (logger.isTraceEnabled()) { - logger.trace("Skipping directory: " + dir); - } - return FileVisitResult.SKIP_SUBTREE; - } - } - - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { - if (fileFilter == null || fileFilter.accept(path.toFile())) { - Path relPath = currentAssetPath.relativize(path); - String uri = "/" + relPath.toString().replace("\\", "/"); - File f = path.toFile(); - this.documentFiles.add(new DocumentFile(uri, f)); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - - public void setFileFilter(FileFilter fileFilter) { - this.fileFilter = fileFilter; - } -} diff --git a/src/main/java/com/marklogic/client/file/DefaultDocumentFileReader.java b/src/main/java/com/marklogic/client/file/DefaultDocumentFileReader.java new file mode 100644 index 0000000..4e71525 --- /dev/null +++ b/src/main/java/com/marklogic/client/file/DefaultDocumentFileReader.java @@ -0,0 +1,175 @@ +package com.marklogic.client.file; + +import com.marklogic.client.helper.LoggingObject; +import org.springframework.util.ClassUtils; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; + +/** + * Non-threadsafe implementation that implements FileVisitor as a way of descending one or more file paths. + */ +public class DefaultDocumentFileReader extends LoggingObject implements FileVisitor, DocumentFileReader { + + private Path currentRootPath; + private List fileFilters; + private List documentFiles; + private List documentFileProcessors; + + public DefaultDocumentFileReader() { + initialize(); + } + + /** + * Walk the file tree at each of the given paths, applying any configured DocumentFileProcessor instances on each + * DocumentFile that is constructed by a File. + * + * @param paths + * @return + */ + public List readDocumentFiles(String... paths) { + documentFiles = new ArrayList<>(); + for (String path : paths) { + if (logger.isDebugEnabled()) { + logger.debug(format("Finding documents at path: %s", path)); + } + this.currentRootPath = Paths.get(path); + try { + Files.walkFileTree(this.currentRootPath, this); + } catch (IOException ie) { + throw new RuntimeException(format("IO error while walking file tree at path: %s", path), ie); + } + } + return documentFiles; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + boolean accept = acceptPath(dir, attrs); + if (accept) { + if (logger.isTraceEnabled()) { + logger.trace("Visiting directory: " + dir); + } + return FileVisitResult.CONTINUE; + } else { + if (logger.isTraceEnabled()) { + logger.trace("Skipping directory: " + dir); + } + return FileVisitResult.SKIP_SUBTREE; + } + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + if (acceptPath(path, attrs)) { + DocumentFile documentFile = buildDocumentFile(path, currentRootPath); + documentFile = processDocumentFile(documentFile); + if (documentFile != null) { + this.documentFiles.add(documentFile); + } + } + return FileVisitResult.CONTINUE; + } + + /** + * If any of the configured FileFilter objects do not accept the Path, then it is not accepted. + * + * @param path + * @param attrs + * @return + */ + protected boolean acceptPath(Path path, BasicFileAttributes attrs) { + if (fileFilters != null) { + File file = path.toFile(); + for (FileFilter filter : fileFilters) { + if (!filter.accept(file)) { + return false; + } + } + } + return true; + } + + protected DocumentFile buildDocumentFile(Path path, Path currentRootPath) { + Path relPath = currentRootPath.relativize(path); + String uri = "/" + relPath.toString().replace("\\", "/"); + File f = path.toFile(); + return new DocumentFile(uri, f); + } + + protected DocumentFile processDocumentFile(DocumentFile documentFile) { + for (DocumentFileProcessor processor : documentFileProcessors) { + documentFile = processor.processDocumentFile(documentFile); + if (documentFile == null) { + break; + } + } + return documentFile; + } + + protected void initialize() { + CollectionsDocumentFileProcessor cdfp = new CollectionsDocumentFileProcessor(); + PermissionsDocumentFileProcessor pdfp = new PermissionsDocumentFileProcessor(); + + addFileFilter(cdfp); + addFileFilter(pdfp); + + addDocumentFileProcessor(cdfp); + addDocumentFileProcessor(pdfp); + addDocumentFileProcessor(new FormatDocumentFileProcessor()); + } + + public DocumentFileProcessor getDocumentFileProcessor(String classShortName) { + for (DocumentFileProcessor processor : documentFileProcessors) { + if (ClassUtils.getShortName(processor.getClass()).equals(classShortName)) { + return processor; + } + } + return null; + } + + public void addDocumentFileProcessor(DocumentFileProcessor processor) { + if (documentFileProcessors == null) { + documentFileProcessors = new ArrayList<>(); + } + documentFileProcessors.add(processor); + } + + public void addFileFilter(FileFilter fileFilter) { + if (fileFilters == null) { + fileFilters = new ArrayList<>(); + } + fileFilters.add(fileFilter); + } + + public List getDocumentFileProcessors() { + return documentFileProcessors; + } + + public void setDocumentFileProcessors(List documentFileProcessors) { + this.documentFileProcessors = documentFileProcessors; + } + + public List getFileFilters() { + return fileFilters; + } + + public void setFileFilters(List fileFilters) { + this.fileFilters = fileFilters; + } +} diff --git a/src/main/java/com/marklogic/client/file/DefaultFileLoader.java b/src/main/java/com/marklogic/client/file/DefaultFileLoader.java deleted file mode 100644 index 1b1fff9..0000000 --- a/src/main/java/com/marklogic/client/file/DefaultFileLoader.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.marklogic.client.file; - -import com.marklogic.client.batch.BatchWriter; -import com.marklogic.client.helper.LoggingObject; -import org.springframework.util.ClassUtils; - -import java.util.ArrayList; -import java.util.List; - -public class DefaultFileLoader extends LoggingObject implements FileLoader { - - private DocumentFileFinder documentFileFinder; - private BatchWriter batchWriter; - private List documentFileProcessors; - - public DefaultFileLoader(BatchWriter batchWriter) { - this(batchWriter, new DefaultDocumentFileFinder()); - } - - /** - * @param batchWriter This is assumed to have already been initialized, as this class is not interested in managing - * the lifecycle of a BatchWriter - * @param documentFileFinder - */ - public DefaultFileLoader(BatchWriter batchWriter, DocumentFileFinder documentFileFinder) { - this.batchWriter = batchWriter; - this.documentFileFinder = documentFileFinder; - initializeDocumentFileProcessors(); - } - - protected void initializeDocumentFileProcessors() { - documentFileProcessors = new ArrayList<>(); - documentFileProcessors.add(new CollectionsDocumentFileProcessor()); - documentFileProcessors.add(new FormatDocumentFileProcessor()); - } - - @Override - public List loadFiles(String... paths) { - List documentFiles = documentFileFinder.findDocumentFiles(paths); - documentFiles = processDocumentFiles(documentFiles); - batchWriter.write(documentFiles); - return documentFiles; - } - - protected List processDocumentFiles(List documentFiles) { - List newFiles = new ArrayList<>(); - for (DocumentFile file : documentFiles) { - for (DocumentFileProcessor processor : documentFileProcessors) { - file = processor.processDocumentFile(file); - if (file == null) { - break; - } - } - if (file != null) { - newFiles.add(file); - } - } - return newFiles; - } - public DocumentFileProcessor getDocumentFileProcessor(String classShortName) { - for (DocumentFileProcessor processor : documentFileProcessors) { - if (ClassUtils.getShortName(processor.getClass()).equals(classShortName)) { - return processor; - } - } - return null; - } - - public void addDocumentFileProcessor(DocumentFileProcessor processor) { - if (documentFileProcessors == null) { - documentFileProcessors = new ArrayList<>(); - } - documentFileProcessors.add(processor); - } - - public List getDocumentFileProcessors() { - return documentFileProcessors; - } - - public void setDocumentFileProcessors(List documentFileProcessors) { - this.documentFileProcessors = documentFileProcessors; - } -} diff --git a/src/main/java/com/marklogic/client/file/DocumentFileFinder.java b/src/main/java/com/marklogic/client/file/DocumentFileReader.java similarity index 69% rename from src/main/java/com/marklogic/client/file/DocumentFileFinder.java rename to src/main/java/com/marklogic/client/file/DocumentFileReader.java index cfac5c4..c2d5f45 100644 --- a/src/main/java/com/marklogic/client/file/DocumentFileFinder.java +++ b/src/main/java/com/marklogic/client/file/DocumentFileReader.java @@ -6,7 +6,7 @@ * Strategy interface for determining which files to load into MarkLogic, with those files being captured as a List of * DocumentFile objects. */ -public interface DocumentFileFinder { +public interface DocumentFileReader { - List findDocumentFiles(String... paths); + List readDocumentFiles(String... paths); } diff --git a/src/main/java/com/marklogic/client/file/DocumentMetadataSetter.java b/src/main/java/com/marklogic/client/file/DocumentMetadataSetter.java deleted file mode 100644 index b269d3a..0000000 --- a/src/main/java/com/marklogic/client/file/DocumentMetadataSetter.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.marklogic.client.file; - -public interface DocumentMetadataSetter { - - void setMetadata(DocumentFile documentFile); -} diff --git a/src/main/java/com/marklogic/client/file/FileLoader.java b/src/main/java/com/marklogic/client/file/FileLoader.java deleted file mode 100644 index cee85ae..0000000 --- a/src/main/java/com/marklogic/client/file/FileLoader.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.marklogic.client.file; - -import java.util.List; - -/** - * Interface for loading files from a set of paths, returning a list of the files that were loaded as DocumentFile - * objects. - */ -public interface FileLoader { - - List loadFiles(String... paths); -} diff --git a/src/main/java/com/marklogic/client/file/FilterDocumentFileProcessor.java b/src/main/java/com/marklogic/client/file/FilterDocumentFileProcessor.java deleted file mode 100644 index 4f528d3..0000000 --- a/src/main/java/com/marklogic/client/file/FilterDocumentFileProcessor.java +++ /dev/null @@ -1,20 +0,0 @@ -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; - } -} diff --git a/src/main/java/com/marklogic/client/file/PermissionsDocumentFileProcessor.java b/src/main/java/com/marklogic/client/file/PermissionsDocumentFileProcessor.java new file mode 100644 index 0000000..5f6d270 --- /dev/null +++ b/src/main/java/com/marklogic/client/file/PermissionsDocumentFileProcessor.java @@ -0,0 +1,41 @@ +package com.marklogic.client.file; + +import com.marklogic.client.modulesloader.impl.DefaultDocumentPermissionsParser; +import com.marklogic.client.modulesloader.impl.DocumentPermissionsParser; + +import java.util.Properties; + +/** + * Looks for a special file in each directory - defaults to permissions.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 role,capability,role,capability,etc. + */ +public class PermissionsDocumentFileProcessor extends PropertiesDrivenDocumentFileProcessor { + + private DocumentPermissionsParser documentPermissionsParser; + + public PermissionsDocumentFileProcessor() { + this("permissions.properties"); + } + + public PermissionsDocumentFileProcessor(String propertiesFilename) { + this(propertiesFilename, new DefaultDocumentPermissionsParser()); + } + + public PermissionsDocumentFileProcessor(String propertiesFilename, DocumentPermissionsParser documentPermissionsParser) { + super(propertiesFilename); + this.documentPermissionsParser = documentPermissionsParser; + } + + @Override + protected void processProperties(DocumentFile documentFile, Properties properties) { + String name = documentFile.getFile().getName(); + if (properties.containsKey(name)) { + String value = properties.getProperty(name); + documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions()); + } + } + + public void setDocumentPermissionsParser(DocumentPermissionsParser documentPermissionsParser) { + this.documentPermissionsParser = documentPermissionsParser; + } +} \ No newline at end of file diff --git a/src/main/java/com/marklogic/client/file/PropertiesDrivenDocumentFileProcessor.java b/src/main/java/com/marklogic/client/file/PropertiesDrivenDocumentFileProcessor.java new file mode 100644 index 0000000..0651748 --- /dev/null +++ b/src/main/java/com/marklogic/client/file/PropertiesDrivenDocumentFileProcessor.java @@ -0,0 +1,89 @@ +package com.marklogic.client.file; + +import com.marklogic.client.helper.LoggingObject; + +import java.io.File; +import java.io.FileFilter; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * Base class for processors that look for a special file in each directory and intend to perform some processing based + * on the contents of that file. By default, that special file is NOT loaded into MarkLogic. + */ +public abstract class PropertiesDrivenDocumentFileProcessor extends LoggingObject implements DocumentFileProcessor, FileFilter { + + private String propertiesFilename; + + // Used to avoid checking for and loading the properties for every file in a directory + private Map propertiesCache = new HashMap<>(); + + protected PropertiesDrivenDocumentFileProcessor(String propertiesFilename) { + this.propertiesFilename = propertiesFilename; + } + + @Override + public boolean accept(File file) { + return !file.getName().equals(propertiesFilename); + } + + /** + * @param documentFile + * @return + */ + @Override + public DocumentFile processDocumentFile(DocumentFile documentFile) { + File file = documentFile.getFile(); + if (!accept(file)) { + return null; + } + + File propertiesFile = new File(file.getParentFile(), propertiesFilename); + if (propertiesFile.exists()) { + try { + Properties props = loadProperties(propertiesFile); + processProperties(documentFile, props); + } catch (IOException e) { + logger.warn("Unable to load properties from file: " + propertiesFile.getAbsolutePath(), e); + } + } + + return documentFile; + } + + protected abstract void processProperties(DocumentFile documentFile, Properties properties); + + protected Properties loadProperties(File propertiesFile) throws IOException { + Properties props = null; + if (propertiesCache.containsKey(propertiesFile)) { + props = propertiesCache.get(propertiesFile); + } + if (props != null) { + return props; + } + + props = new Properties(); + FileReader reader = null; + try { + reader = new FileReader(propertiesFile); + props.load(reader); + propertiesCache.put(propertiesFile, props); + return props; + } finally { + if (reader != null) { + reader.close(); + } + } + } + + public Map getPropertiesCache() { + return propertiesCache; + } + + public String getPropertiesFilename() { + return propertiesFilename; + } +} diff --git a/src/main/java/com/marklogic/client/helper/ClientHelper.java b/src/main/java/com/marklogic/client/helper/ClientHelper.java index 06b2485..9dcadfb 100644 --- a/src/main/java/com/marklogic/client/helper/ClientHelper.java +++ b/src/main/java/com/marklogic/client/helper/ClientHelper.java @@ -5,6 +5,7 @@ import java.util.List; import com.marklogic.client.DatabaseClient; +import com.marklogic.client.document.GenericDocumentManager; import com.marklogic.client.document.XMLDocumentManager; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; @@ -29,11 +30,12 @@ public DatabaseClient getClient() { return this.client; } + public DocumentMetadataHandle getMetadata(String uri) { + return client.newDocumentManager().readMetadata(uri, new DocumentMetadataHandle()); + } + public List getCollections(String uri) { - XMLDocumentManager mgr = client.newXMLDocumentManager(); - DocumentMetadataHandle h = new DocumentMetadataHandle(); - mgr.readMetadata(uri, h); - DocumentCollections colls = h.getCollections(); + DocumentCollections colls = getMetadata(uri).getCollections(); return Arrays.asList(colls.toArray(new String[] {})); } diff --git a/src/main/java/com/marklogic/client/schemasloader/SchemasLoader.java b/src/main/java/com/marklogic/client/schemasloader/SchemasLoader.java index 5f700e7..6b70c19 100644 --- a/src/main/java/com/marklogic/client/schemasloader/SchemasLoader.java +++ b/src/main/java/com/marklogic/client/schemasloader/SchemasLoader.java @@ -16,7 +16,7 @@ public interface SchemasLoader { * @param schemasDataFinder * @param client * @return a set of files that were loaded. - * @deprecated Prefer loadSchemas instead, which is assumed to use the new FileLoader library + * @deprecated Prefer loadSchemas instead, which is assumed to use DocumentFileReader and BatchWriter */ @Deprecated Set loadSchemas(File directory, SchemasFinder schemasDataFinder, DatabaseClient client); diff --git a/src/main/java/com/marklogic/client/schemasloader/impl/DefaultSchemasLoader.java b/src/main/java/com/marklogic/client/schemasloader/impl/DefaultSchemasLoader.java index 36cf766..510e4ce 100644 --- a/src/main/java/com/marklogic/client/schemasloader/impl/DefaultSchemasLoader.java +++ b/src/main/java/com/marklogic/client/schemasloader/impl/DefaultSchemasLoader.java @@ -1,60 +1,94 @@ package com.marklogic.client.schemasloader.impl; -import java.io.File; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - import com.marklogic.client.DatabaseClient; import com.marklogic.client.batch.BatchWriter; import com.marklogic.client.batch.RestBatchWriter; import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.file.DefaultFileLoader; +import com.marklogic.client.file.DefaultDocumentFileReader; +import com.marklogic.client.file.DocumentFile; +import com.marklogic.client.file.DocumentFileReader; import com.marklogic.client.helper.LoggingObject; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.FileHandle; import com.marklogic.client.io.Format; import com.marklogic.client.schemasloader.SchemasFinder; import com.marklogic.client.schemasloader.SchemasLoader; -import com.marklogic.client.file.DefaultDocumentFileFinder; -import com.marklogic.client.file.DocumentFile; -import com.marklogic.client.file.DocumentFileFinder; + +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Set; public class DefaultSchemasLoader extends LoggingObject implements SchemasLoader { - private DocumentFileFinder documentFileFinder = new DefaultDocumentFileFinder(); - private DatabaseClient databaseClient; + private DocumentFileReader documentFileReader; + private BatchWriter batchWriter; + private boolean waitForCompletion = true; + /** + * @deprecated This constructor is only for the deprecated way of loading schemas + */ public DefaultSchemasLoader() { } + /** + * Simplest constructor for using this class. Just provide a DatabaseClient, and this will use sensible defaults + * for how documents are read and written. Note that the DatabaseClient will not be released after this class is + * done with it, as this class wasn't the one that created it. + * + * @param databaseClient + */ public DefaultSchemasLoader(DatabaseClient databaseClient) { - this.databaseClient = databaseClient; + RestBatchWriter restBatchWriter = new RestBatchWriter(databaseClient); + restBatchWriter.setReleaseDatabaseClients(false); + restBatchWriter.initialize(); + this.batchWriter = restBatchWriter; + + DefaultDocumentFileReader reader = new DefaultDocumentFileReader(); + reader.addDocumentFileProcessor(new TdeDocumentFileProcessor()); + this.documentFileReader = reader; + } + + /** + * Assumes that the BatchWriter has already been initialized. + * + * @param documentFileReader + * @param batchWriter + */ + public DefaultSchemasLoader(DocumentFileReader documentFileReader, BatchWriter batchWriter) { + this.documentFileReader = documentFileReader; + this.batchWriter = batchWriter; } /** - * A RestBatchWriter is used for loading documents. The DatabaseClient is not released by this BatchWriter, as it's - * expected that it will be reused by the client that constructed this object. + * Run the given paths through the DocumentFileReader, and then send the result to the BatchWriter, and then + * return the result. * * @param paths * @return */ @Override public List loadSchemas(String... paths) { - RestBatchWriter batchWriter = new RestBatchWriter(databaseClient); - batchWriter.setReleaseDatabaseClients(false); - batchWriter.initialize(); try { - DefaultFileLoader fileLoader = new DefaultFileLoader(batchWriter); - fileLoader.addDocumentFileProcessor(new TdeDocumentFileProcessor()); - return fileLoader.loadFiles(paths); + List documentFiles = documentFileReader.readDocumentFiles(paths); + batchWriter.write(documentFiles); + return documentFiles; } finally { - batchWriter.waitForCompletion(); + if (waitForCompletion) { + batchWriter.waitForCompletion(); + } } } + /** + * @deprecated + * @param baseDir + * @param schemasDataFinder + * @param client + * @return + */ @Override public Set loadSchemas(File baseDir, SchemasFinder schemasDataFinder, DatabaseClient client) { XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); @@ -90,7 +124,15 @@ protected String getExtensionNameFromFile(File file) { return pos < 0 ? name : name.substring(pos + 1); } - public void setDocumentFileFinder(DocumentFileFinder documentFileFinder) { - this.documentFileFinder = documentFileFinder; + public void setWaitForCompletion(boolean waitForCompletion) { + this.waitForCompletion = waitForCompletion; + } + + public DocumentFileReader getDocumentFileReader() { + return documentFileReader; + } + + public BatchWriter getBatchWriter() { + return batchWriter; } } diff --git a/src/test/java/com/marklogic/client/schemasloader/impl/LoadRulesetsTest.java b/src/test/java/com/marklogic/client/schemasloader/impl/LoadRulesetsTest.java index 7f6fcee..99dad16 100644 --- a/src/test/java/com/marklogic/client/schemasloader/impl/LoadRulesetsTest.java +++ b/src/test/java/com/marklogic/client/schemasloader/impl/LoadRulesetsTest.java @@ -3,6 +3,7 @@ import com.marklogic.client.AbstractIntegrationTest; import com.marklogic.client.file.DocumentFile; import com.marklogic.client.helper.ClientHelper; +import com.marklogic.client.io.DocumentMetadataHandle; import org.junit.Before; import org.junit.Test; @@ -40,5 +41,11 @@ public void test() { assertTrue(collections.contains("ruleset-abc")); assertTrue(collections.contains("ruleset-xyz")); + DocumentMetadataHandle.DocumentPermissions perms = helper.getMetadata("/ruleset1.xml").getPermissions(); + assertEquals("Should have the two default perms plus the two custom ones", 4, perms.size()); + assertEquals(DocumentMetadataHandle.Capability.READ, perms.get("rest-reader").iterator().next()); + assertEquals(DocumentMetadataHandle.Capability.UPDATE, perms.get("rest-writer").iterator().next()); + assertEquals(DocumentMetadataHandle.Capability.READ, perms.get("rest-admin").iterator().next()); + assertEquals(DocumentMetadataHandle.Capability.UPDATE, perms.get("manage-admin").iterator().next()); } } diff --git a/src/test/java/com/marklogic/client/util/DocumentFinderTest.java b/src/test/java/com/marklogic/client/util/DocumentFinderTest.java index c132f9e..343dfe4 100644 --- a/src/test/java/com/marklogic/client/util/DocumentFinderTest.java +++ b/src/test/java/com/marklogic/client/util/DocumentFinderTest.java @@ -1,8 +1,8 @@ package com.marklogic.client.util; -import com.marklogic.client.file.DefaultDocumentFileFinder; +import com.marklogic.client.file.DefaultDocumentFileReader; import com.marklogic.client.file.DocumentFile; -import com.marklogic.client.file.DocumentFileFinder; +import com.marklogic.client.file.DocumentFileReader; import org.junit.Assert; import org.junit.Test; @@ -11,12 +11,12 @@ public class DocumentFinderTest extends Assert { - private DocumentFileFinder sut = new DefaultDocumentFileFinder(); + private DocumentFileReader sut = new DefaultDocumentFileReader(); @Test public void noFileFilter() { String path = "src/test/resources/schemas"; - List list = sut.findDocumentFiles(path); + List list = sut.readDocumentFiles(path); assertEquals(3, list.size()); List uris = new ArrayList<>(); diff --git a/src/test/resources/rulesets/collection-test/permissions.properties b/src/test/resources/rulesets/collection-test/permissions.properties new file mode 100644 index 0000000..38689c0 --- /dev/null +++ b/src/test/resources/rulesets/collection-test/permissions.properties @@ -0,0 +1 @@ +ruleset1.xml=rest-admin,read,manage-admin,update