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

Commit bb5110e

Browse files
committed
#138 Can now populate custom tokens with Properties and optional prefix and suffix
1 parent 58b96c3 commit bb5110e

File tree

8 files changed

+208
-11
lines changed

8 files changed

+208
-11
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ public class AppConfig {
176176
// Path to use for DeployFlexrepCommand
177177
private String flexrepPath;
178178

179-
// Whether or not to replace tokens in modules
179+
// Whether or not to replace tokens in modules
180180
private boolean replaceTokensInModules = true;
181181
// Whether or not to prefix each module token with "@ml."
182-
private boolean useRoxyTokenPrefix = true;
182+
private boolean useRoxyTokenPrefix = false;
183183
// Additional PropertiesSources instance to use for replacing module tokens
184184
private List<PropertiesSource> moduleTokensPropertiesSources = new ArrayList<>();
185185

@@ -216,6 +216,39 @@ public AppConfig(String defaultModulePath, String defaultSchemasPath) {
216216
schemasPath = defaultSchemasPath;
217217
}
218218

219+
public void populateCustomTokens(PropertiesSource propertiesSource) {
220+
populateCustomTokens(propertiesSource, "%%", "%%");
221+
}
222+
223+
/**
224+
* Populate the customTokens map in this class with the properties from the given properties source.
225+
* @param propertiesSource
226+
* @param prefix optional; if set, then each token key that is added has the prefix prepended to it
227+
* @param suffix optional; if set, then each token key that is added has the suffix appended to it
228+
*/
229+
public void populateCustomTokens(PropertiesSource propertiesSource, String prefix, String suffix) {
230+
Properties props = propertiesSource.getProperties();
231+
if (props != null) {
232+
if (customTokens == null) {
233+
customTokens = new HashMap<>();
234+
}
235+
for (Object key : props.keySet()) {
236+
String skey = (String)key;
237+
String value = props.getProperty(skey);
238+
if (value != null) {
239+
String token = skey;
240+
if (prefix != null) {
241+
token = prefix + token;
242+
}
243+
if (suffix != null) {
244+
token = token + suffix;
245+
}
246+
customTokens.put(token, value);
247+
}
248+
}
249+
}
250+
}
251+
219252
public void setSimpleSslConfig() {
220253
setRestSslContext(SimpleX509TrustManager.newSSLContext());
221254
setRestSslHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY);

src/main/java/com/marklogic/appdeployer/command/modules/DefaultModulesLoaderFactory.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marklogic.appdeployer.command.modules;
22

33
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.util.MapPropertiesSource;
45
import com.marklogic.client.ext.batch.RestBatchWriter;
56
import com.marklogic.client.ext.helper.LoggingObject;
67
import com.marklogic.client.ext.modulesloader.ModulesLoader;
@@ -79,15 +80,8 @@ protected StaticChecker newStaticChecker(AppConfig appConfig) {
7980
protected TokenReplacer buildModuleTokenReplacer(AppConfig appConfig) {
8081
DefaultTokenReplacer r = appConfig.isUseRoxyTokenPrefix() ? new RoxyTokenReplacer() : new DefaultTokenReplacer();
8182
final Map<String, String> customTokens = appConfig.getCustomTokens();
82-
if (customTokens != null && !customTokens.isEmpty()) {
83-
r.addPropertiesSource(new PropertiesSource() {
84-
@Override
85-
public Properties getProperties() {
86-
Properties p = new Properties();
87-
p.putAll(customTokens);
88-
return p;
89-
}
90-
});
83+
if (customTokens != null) {
84+
r.addPropertiesSource(new MapPropertiesSource(customTokens));
9185
}
9286

9387
if (appConfig.getModuleTokensPropertiesSources() != null) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.marklogic.appdeployer.util;
2+
3+
import com.marklogic.client.ext.tokenreplacer.PropertiesSource;
4+
5+
import java.util.Map;
6+
import java.util.Properties;
7+
8+
public class MapPropertiesSource implements PropertiesSource {
9+
10+
private Map<String, String> map;
11+
12+
public MapPropertiesSource(Map<String, String> map) {
13+
this.map = map;
14+
}
15+
16+
/**
17+
* Lazily builds a Properties object based on the Map contained in this class.
18+
*
19+
* @return
20+
*/
21+
@Override
22+
public Properties getProperties() {
23+
Properties props = new Properties();
24+
props.putAll(map);
25+
return props;
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.marklogic.appdeployer.util;
2+
3+
import com.marklogic.client.ext.tokenreplacer.PropertiesSource;
4+
5+
import java.util.Properties;
6+
7+
public class SimplePropertiesSource implements PropertiesSource {
8+
9+
private Properties props;
10+
11+
public SimplePropertiesSource(Properties props) {
12+
this.props = props;
13+
}
14+
15+
@Override
16+
public Properties getProperties() {
17+
return props;
18+
}
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.marklogic.appdeployer;
2+
3+
import com.marklogic.appdeployer.util.SimplePropertiesSource;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.Map;
9+
import java.util.Properties;
10+
11+
public class PopulateCustomTokensTest extends Assert {
12+
13+
private AppConfig appConfig;
14+
private Properties props;
15+
16+
@Before
17+
public void setup() {
18+
appConfig = new AppConfig();
19+
20+
props = new Properties();
21+
props.setProperty("color", "blue");
22+
props.setProperty("size", "M");
23+
}
24+
25+
@Test
26+
public void defaultPrefixAndSuffix() {
27+
appConfig.populateCustomTokens(new SimplePropertiesSource(props));
28+
29+
Map<String, String> tokens = appConfig.getCustomTokens();
30+
assertEquals("blue", tokens.get("%%color%%"));
31+
assertEquals("M", tokens.get("%%size%%"));
32+
}
33+
34+
@Test
35+
public void customPrefixAndSuffix() {
36+
appConfig.populateCustomTokens(new SimplePropertiesSource(props), "!!", "!!");
37+
38+
Map<String, String> tokens = appConfig.getCustomTokens();
39+
assertEquals("blue", tokens.get("!!color!!"));
40+
assertEquals("M", tokens.get("!!size!!"));
41+
}
42+
43+
@Test
44+
public void onlyPrefix() {
45+
appConfig.populateCustomTokens(new SimplePropertiesSource(props), "!!", null);
46+
47+
Map<String, String> tokens = appConfig.getCustomTokens();
48+
assertEquals("blue", tokens.get("!!color"));
49+
assertEquals("M", tokens.get("!!size"));
50+
}
51+
52+
@Test
53+
public void onlySuffix() {
54+
appConfig.populateCustomTokens(new SimplePropertiesSource(props), null, "!!");
55+
56+
Map<String, String> tokens = appConfig.getCustomTokens();
57+
assertEquals("blue", tokens.get("color!!"));
58+
assertEquals("M", tokens.get("size!!"));
59+
}
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.marklogic.appdeployer.command;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
6+
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
7+
import com.marklogic.appdeployer.util.SimplePropertiesSource;
8+
import com.marklogic.client.DatabaseClient;
9+
import com.marklogic.client.ext.modulesloader.impl.DefaultModulesLoader;
10+
import com.marklogic.client.io.BytesHandle;
11+
import com.marklogic.client.io.StringHandle;
12+
import org.junit.Test;
13+
14+
import java.io.File;
15+
import java.util.Arrays;
16+
import java.util.Properties;
17+
18+
public class ReplaceTokensTest extends AbstractAppDeployerTest {
19+
20+
@Test
21+
public void test() {
22+
appConfig.setContentForestsPerHost(1);
23+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/token-test/ml-config")));
24+
appConfig.setModulePaths(Arrays.asList("src/test/resources/token-test/ml-modules"));
25+
26+
Properties props = new Properties();
27+
props.setProperty("xdbcEnabled", "true");
28+
props.setProperty("sample-token", "replaced!");
29+
appConfig.populateCustomTokens(new SimplePropertiesSource(props));
30+
31+
LoadModulesCommand loadModulesCommand = new LoadModulesCommand();
32+
loadModulesCommand.initializeDefaultModulesLoader(new CommandContext(appConfig, manageClient, adminManager));
33+
((DefaultModulesLoader) loadModulesCommand.getModulesLoader()).setModulesManager(null);
34+
35+
initializeAppDeployer(new DeployRestApiServersCommand(), loadModulesCommand);
36+
deploySampleApp();
37+
38+
// We know xdbcEnabled was replaced, otherwise the deployment of the REST API server would have failed
39+
// Gotta verify the text in the module was replaced
40+
41+
DatabaseClient modulesClient = appConfig.newAppServicesDatabaseClient(appConfig.getModulesDatabaseName());
42+
try {
43+
String moduleText = new String(modulesClient.newDocumentManager().read("/hello.xqy", new BytesHandle()).get());
44+
assertTrue("Did not find replaced text in module: " + moduleText, moduleText.contains("replaced!"));
45+
} finally {
46+
modulesClient.release();
47+
undeploySampleApp();
48+
}
49+
}
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"rest-api": {
3+
"name": "%%NAME%%",
4+
"group": "%%GROUP%%",
5+
"database": "%%DATABASE%%",
6+
"modules-database": "%%MODULES_DATABASE%%",
7+
"port": "%%PORT%%",
8+
"xdbc-enabled": %%xdbcEnabled%%,
9+
"error-format": "json"
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<test>
2+
<sample>%%sample-token%%</sample>
3+
</test>

0 commit comments

Comments
 (0)