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

Commit 0070e9c

Browse files
committed
#112 Accounting for host when writing module timestamps file
Note that DefaultModulesLoader won't do use this support by default because it doesn't have a constructor that allows for manipulating the ModulesManager. Instead, a client will need to call setModulesManager.
1 parent d803acf commit 0070e9c

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

src/main/java/com/marklogic/client/ext/modulesloader/ModulesLoader.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
/**
99
* Interface for objects that can load a set of modules via the REST API, which is intended to include not just what the
1010
* REST API calls "assets" (regular modules), but also options, services, transforms, and namespaces.
11+
*
12+
* Note that in both of the methods in this interface, the DatabaseClient is used for loading REST extensions. This is
13+
* to account for the fact that how search options are loaded (i.e. the URI they're written to) differs based on what
14+
* REST server they're loaded from. But the implementation is not expected to use this DatabaseClient for loading
15+
* non-REST modules - that is likely done via an instance of AssetFileLoader that uses the App-Services port by
16+
* default for loading non-REST modules.
1117
*/
1218
public interface ModulesLoader {
1319

1420
/**
15-
* Use the given DatabaseClient to load modules found in the given directory. Return a set containing any files that
16-
* were loaded.
21+
* Load modules from the given directory, and return the set of resources containing all modules written.
1722
*
1823
* @param directory
1924
* @param modulesFinder
20-
* @param client
25+
* @param client the DatabaseClient to use for loading REST extensions
2126
* @return
2227
*/
2328
Set<Resource> loadModules(String directory, ModulesFinder modulesFinder, DatabaseClient client);
@@ -28,7 +33,7 @@ public interface ModulesLoader {
2833
*
2934
* @param client
3035
* @param modulesFinder
31-
* @param paths
36+
* @param paths the DatabaseClient to use for loading REST extensions
3237
* @return
3338
*/
3439
Set<Resource> loadModules(DatabaseClient client, ModulesFinder modulesFinder, String... paths);

src/main/java/com/marklogic/client/ext/modulesloader/impl/PropertiesModuleManager.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marklogic.client.ext.modulesloader.impl;
22

3+
import com.marklogic.client.DatabaseClient;
34
import com.marklogic.client.ext.helper.LoggingObject;
45
import com.marklogic.client.ext.modulesloader.ModulesManager;
56

@@ -16,6 +17,7 @@ public class PropertiesModuleManager extends LoggingObject implements ModulesMan
1617
private Properties props;
1718
private String propertiesFilePath;
1819
private long minimumFileTimestampToLoad;
20+
private String host;
1921

2022
public PropertiesModuleManager() {
2123
this(DEFAULT_FILE_PATH);
@@ -26,6 +28,20 @@ public PropertiesModuleManager(String propertiesFilePath) {
2628
this.propertiesFilePath = propertiesFilePath;
2729
}
2830

31+
/**
32+
* Use this constructor so that the keys generated for the properties file account for the host associated with the
33+
* given DatabaseClient.
34+
*
35+
* @param propertiesFilePath
36+
* @param client
37+
*/
38+
public PropertiesModuleManager(String propertiesFilePath, DatabaseClient client) {
39+
this(propertiesFilePath);
40+
if (client != null) {
41+
host = client.getHost();
42+
}
43+
}
44+
2945
@Override
3046
public void initialize() {
3147
this.props = new Properties();
@@ -64,7 +80,13 @@ public void deletePropertiesFile() {
6480
}
6581
}
6682

67-
public boolean hasFileBeenModifiedSinceLastLoaded(File file) {
83+
/**
84+
*
85+
* @param file
86+
* @return
87+
*/
88+
@Override
89+
public boolean hasFileBeenModifiedSinceLastLoaded(File file) {
6890
if (minimumFileTimestampToLoad > 0 && file.lastModified() <= minimumFileTimestampToLoad) {
6991
if (logger.isDebugEnabled()) {
7092
logger.debug(String.format("lastModified for file '%s' is %d, which is before the minimumFileTimestampToLoad of %d",
@@ -83,7 +105,13 @@ public boolean hasFileBeenModifiedSinceLastLoaded(File file) {
83105
return true;
84106
}
85107

86-
public void saveLastLoadedTimestamp(File file, Date date) {
108+
/**
109+
*
110+
* @param file
111+
* @param date
112+
*/
113+
@Override
114+
public void saveLastLoadedTimestamp(File file, Date date) {
87115
String key = buildKey(file);
88116
props.setProperty(key, date.getTime() + "");
89117
FileWriter fw = null;
@@ -110,10 +138,23 @@ public void saveLastLoadedTimestamp(File file, Date date) {
110138
* @return
111139
*/
112140
protected String buildKey(File file) {
113-
return file.getAbsolutePath().toLowerCase();
141+
String path = file.getAbsolutePath().toLowerCase();
142+
final String key = host != null ? host + ":" + path : path;
143+
if (logger.isDebugEnabled()) {
144+
logger.debug("Key for file " + file.getAbsolutePath() + ": " + key);
145+
}
146+
return key;
114147
}
115148

116149
public void setMinimumFileTimestampToLoad(long minimumFileTimestampToLoad) {
117150
this.minimumFileTimestampToLoad = minimumFileTimestampToLoad;
118151
}
152+
153+
public String getHost() {
154+
return host;
155+
}
156+
157+
public void setHost(String host) {
158+
this.host = host;
159+
}
119160
}

src/test/java/com/marklogic/client/ext/modulesloader/impl/LoadModulesTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.core.io.Resource;
1414
import org.springframework.util.StringUtils;
1515

16+
import java.io.File;
1617
import java.nio.file.Paths;
1718
import java.util.List;
1819
import java.util.Properties;
@@ -42,6 +43,11 @@ public void setup() {
4243

4344
modulesLoader = new DefaultModulesLoader(new AssetFileLoader(modulesClient));
4445
modulesLoader.setModulesManager(null);
46+
47+
File file = new File(PropertiesModuleManager.DEFAULT_FILE_PATH);
48+
if (file.exists()) {
49+
file.delete();
50+
}
4551
}
4652

4753
@Test
@@ -195,6 +201,29 @@ private void verifyModuleCountWithPattern(String pattern, String message, int co
195201
assertEquals(message, count, files.size());
196202
}
197203

204+
@Test
205+
public void loadModulesAndAccountForHost() {
206+
PropertiesModuleManager moduleManager = new PropertiesModuleManager(PropertiesModuleManager.DEFAULT_FILE_PATH, modulesClient);
207+
modulesLoader.setAssetFileLoader(new AssetFileLoader(modulesClient, moduleManager));
208+
modulesLoader.setModulesManager(moduleManager);
209+
210+
String dir = Paths.get("src", "test", "resources", "sample-base-dir").toString();
211+
Set<Resource> files = modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
212+
assertEquals(26, files.size());
213+
214+
files = modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
215+
assertEquals("No files should have been loaded since none were new or modified", 0, files.size());
216+
217+
// The host defaults to "localhost", so change it to a host that should still hit localhost on any OS, and
218+
// verify all files were loaded because a different host was used
219+
moduleManager.setHost("127.0.0.1");
220+
files = modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
221+
assertEquals(26, files.size());
222+
223+
files = modulesLoader.loadModules(dir, new DefaultModulesFinder(), client);
224+
assertEquals("No files should have been loaded since none were new or modified", 0, files.size());
225+
}
226+
198227
@Test
199228
public void test() {
200229
String dir = Paths.get("src", "test", "resources", "sample-base-dir").toString();

0 commit comments

Comments
 (0)