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

Commit 7420550

Browse files
committed
Merging in 2.9.0 features from dev
1 parent 3805c23 commit 7420550

File tree

9 files changed

+205
-107
lines changed

9 files changed

+205
-107
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ repositories {
1414
}
1515

1616
dependencies {
17-
compile 'com.marklogic:java-client-api:3.0.4'
18-
compile 'com.marklogic:marklogic-xcc:8.0.4'
17+
compile 'com.marklogic:java-client-api:3.0.5'
18+
compile 'com.marklogic:marklogic-xcc:8.0.5'
1919
compile 'org.jdom:jdom2:2.0.5'
2020
compile 'org.springframework:spring-context:4.1.5.RELEASE'
2121

@@ -55,7 +55,7 @@ if (project.hasProperty("myBintrayUser")) {
5555
licenses = ['Apache-2.0']
5656
vcsUrl = 'https://github.com/rjrudin/' + project.name + '.git'
5757
version {
58-
name = "2.8.1"
58+
name = "2.9.0-RC1"
5959
released = new Date()
6060
}
6161
}

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=2.8.1
3+
version=2.9.0-RC1

src/main/java/com/marklogic/client/modulesloader/impl/DefaultExtensionMetadataProvider.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
package com.marklogic.client.modulesloader.impl;
22

3-
import java.io.IOException;
4-
import java.net.URL;
5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
import java.util.List;
8-
9-
import org.jdom2.Element;
10-
import org.jdom2.input.SAXBuilder;
11-
import org.jdom2.output.XMLOutputter;
12-
import org.springframework.core.io.Resource;
13-
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14-
import org.springframework.core.io.support.ResourcePatternResolver;
15-
163
import com.marklogic.client.admin.ExtensionMetadata;
174
import com.marklogic.client.admin.ExtensionMetadata.ScriptLanguage;
185
import com.marklogic.client.admin.MethodType;
@@ -21,6 +8,18 @@
218
import com.marklogic.client.helper.LoggingObject;
229
import com.marklogic.client.modulesloader.ExtensionMetadataAndParams;
2310
import com.marklogic.client.modulesloader.ExtensionMetadataProvider;
11+
import org.jdom2.Element;
12+
import org.jdom2.input.SAXBuilder;
13+
import org.jdom2.output.XMLOutputter;
14+
import org.springframework.core.io.Resource;
15+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
16+
import org.springframework.core.io.support.ResourcePatternResolver;
17+
18+
import java.io.IOException;
19+
import java.net.URL;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.List;
2423

2524
public class DefaultExtensionMetadataProvider extends LoggingObject implements ExtensionMetadataProvider {
2625

@@ -68,6 +67,11 @@ public ExtensionMetadataAndParams provideExtensionMetadataAndParams(Resource r)
6867
mp.add(name, type);
6968
}
7069
}
70+
} catch (IOException ie) {
71+
// Log at debug level, this may just be due to the file missing
72+
logger.debug("Unable to build metadata from resource file: " + url.toString() + "; cause: "
73+
+ ie.getMessage());
74+
setDefaults(m, r);
7175
} catch (Exception e) {
7276
logger.warn("Unable to build metadata from resource file: " + url.toString() + "; cause: "
7377
+ e.getMessage());

src/main/java/com/marklogic/client/modulesloader/impl/XccAssetLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,8 @@ public void setFileFilter(FileFilter fileFilter) {
294294
public void setModuleTokenReplacer(ModuleTokenReplacer moduleTokenReplacer) {
295295
this.moduleTokenReplacer = moduleTokenReplacer;
296296
}
297+
298+
public ModuleTokenReplacer getModuleTokenReplacer() {
299+
return moduleTokenReplacer;
300+
}
297301
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.marklogic.client.modulesloader.tokenreplacer;
2+
3+
import com.marklogic.client.helper.LoggingObject;
4+
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
5+
import org.springframework.util.PropertyPlaceholderHelper;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Properties;
10+
11+
/**
12+
* Default implementation of ModuleTokenReplacer that relies on a list of PropertiesSource implementations for
13+
* finding tokens to replace in modules.
14+
* <p>Based on Roxy conventions of referencing properties in module text with "@ml." as a prefix, this class also by
15+
* default will attempt to find property names in module text with "@ml." as a prefix. This can be adjusted via the
16+
* propertyPrefix property.</p>
17+
*/
18+
public class DefaultModuleTokenReplacer extends LoggingObject implements ModuleTokenReplacer {
19+
20+
private Properties properties;
21+
private PropertyPlaceholderHelper helper;
22+
private List<PropertiesSource> propertiesSources = new ArrayList<>();
23+
private String propertyPrefix = "@ml.";
24+
25+
public void addPropertiesSource(PropertiesSource source) {
26+
this.propertiesSources.add(source);
27+
}
28+
29+
protected void initializeHelper() {
30+
helper = new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
31+
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
32+
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
33+
}
34+
35+
/**
36+
* Initialize the Properties instance based on all the PropertiesSources that have been registered.
37+
*/
38+
protected void initializeProperties() {
39+
properties = new Properties();
40+
for (PropertiesSource source : propertiesSources) {
41+
Properties p = source.getProperties();
42+
if (p != null) {
43+
properties.putAll(p);
44+
}
45+
}
46+
}
47+
48+
@Override
49+
public String replaceTokensInModule(String moduleText) {
50+
if (properties == null) {
51+
initializeProperties();
52+
}
53+
if (helper == null) {
54+
initializeHelper();
55+
}
56+
57+
for (Object key : properties.keySet()) {
58+
String skey = propertyPrefix != null ? propertyPrefix + key : key.toString();
59+
if (logger.isTraceEnabled()) {
60+
logger.trace("Checking for key in module text: " + skey);
61+
}
62+
if (moduleText.contains(skey)) {
63+
String value = properties.getProperty(key.toString());
64+
value = helper.replacePlaceholders(value, properties);
65+
if (logger.isDebugEnabled()) {
66+
logger.debug(format("Replacing %s with %s", skey, value));
67+
}
68+
moduleText = moduleText.replace(skey, value);
69+
}
70+
}
71+
return moduleText;
72+
}
73+
74+
public List<PropertiesSource> getPropertiesSources() {
75+
return propertiesSources;
76+
}
77+
78+
public void setPropertiesSources(List<PropertiesSource> propertiesSources) {
79+
this.propertiesSources = propertiesSources;
80+
}
81+
82+
public void setPropertyPlaceholderHelper(PropertyPlaceholderHelper helper) {
83+
this.helper = helper;
84+
}
85+
86+
public Properties getProperties() {
87+
return properties;
88+
}
89+
90+
public void setProperties(Properties properties) {
91+
this.properties = properties;
92+
}
93+
94+
public void setPropertyPrefix(String propertyPrefix) {
95+
this.propertyPrefix = propertyPrefix;
96+
}
97+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.marklogic.client.modulesloader.tokenreplacer;
2+
3+
import com.marklogic.client.helper.LoggingObject;
4+
5+
import java.io.File;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.util.Properties;
9+
10+
/**
11+
* Simple implementation of PropertiesSource that reads properties from a file.
12+
*/
13+
public class FilePropertiesSource extends LoggingObject implements PropertiesSource {
14+
15+
private Properties props;
16+
private File file;
17+
18+
public FilePropertiesSource(File file) {
19+
this.file = file;
20+
}
21+
22+
@Override
23+
public Properties getProperties() {
24+
if (props == null) {
25+
props = loadPropertiesFromFile(file);
26+
}
27+
return props;
28+
}
29+
30+
/**
31+
* Return a Properties instance based on properties from the given File.
32+
*
33+
* @param file
34+
* @return
35+
*/
36+
protected Properties loadPropertiesFromFile(File file) {
37+
Properties properties = new Properties();
38+
if (file.exists()) {
39+
FileReader reader = null;
40+
try {
41+
reader = new FileReader(file);
42+
if (logger.isDebugEnabled()) {
43+
logger.debug("Loading module properties from: " + file.getAbsolutePath());
44+
}
45+
properties.load(reader);
46+
} catch (IOException ex) {
47+
logger.warn(
48+
"Unable to load properties from file " + file.getAbsolutePath() + "; cause: " + ex.getMessage(),
49+
ex);
50+
} finally {
51+
if (reader != null) {
52+
try {
53+
reader.close();
54+
} catch (IOException ie) {
55+
// Ignore
56+
}
57+
}
58+
}
59+
}
60+
return properties;
61+
}
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.marklogic.client.modulesloader.tokenreplacer;
2+
3+
import java.util.Properties;
4+
5+
public interface PropertiesSource {
6+
7+
public Properties getProperties();
8+
}
Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,18 @@
11
package com.marklogic.client.modulesloader.tokenreplacer;
22

33
import java.io.File;
4-
import java.io.FileReader;
5-
import java.io.IOException;
6-
import java.util.ArrayList;
7-
import java.util.List;
8-
import java.util.Properties;
9-
10-
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
11-
import org.springframework.util.PropertyPlaceholderHelper;
12-
13-
import com.marklogic.client.helper.LoggingObject;
144

155
/**
16-
* Follows the conventions used by Roxy, where properties are prefixed with "@ml.".
17-
*
18-
* By default, this will load properties from "deploy/default.properties", "deploy/build.properties", and
6+
* Loads properties from typical Roxy locations of "deploy/default.properties", "deploy/build.properties", and
197
* "deploy/local.properties", if any of those exist.
208
*/
21-
public class RoxyModuleTokenReplacer extends LoggingObject implements ModuleTokenReplacer {
22-
23-
private Properties properties;
24-
private PropertyPlaceholderHelper helper;
25-
private String propertyPrefix = "@ml.";
9+
public class RoxyModuleTokenReplacer extends DefaultModuleTokenReplacer {
2610

2711
public RoxyModuleTokenReplacer() {
28-
List<String> filePaths = new ArrayList<>();
29-
filePaths.add("deploy/default.properties");
30-
filePaths.add("deploy/build.properties");
31-
filePaths.add("deploy/local.properties");
32-
initializeProperties(filePaths);
33-
initializeHelper();
34-
}
35-
36-
public RoxyModuleTokenReplacer(List<String> filePaths) {
37-
initializeProperties(filePaths);
38-
initializeHelper();
39-
}
40-
41-
protected void initializeProperties(List<String> filePaths) {
42-
properties = new Properties();
43-
for (String path : filePaths) {
44-
loadPropertiesFromFile(new File(path));
45-
}
46-
}
47-
48-
protected void initializeHelper() {
49-
helper = new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX,
50-
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX,
51-
PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true);
52-
}
53-
54-
protected void loadPropertiesFromFile(File file) {
55-
if (file.exists()) {
56-
FileReader reader = null;
57-
try {
58-
reader = new FileReader(file);
59-
if (logger.isInfoEnabled()) {
60-
logger.info("Loading module properties from: " + file.getAbsolutePath());
61-
}
62-
properties.load(reader);
63-
} catch (IOException ex) {
64-
logger.warn(
65-
"Unable to load properties from file " + file.getAbsolutePath() + "; cause: " + ex.getMessage(),
66-
ex);
67-
} finally {
68-
if (reader != null) {
69-
try {
70-
reader.close();
71-
} catch (IOException ie) {
72-
// Ignore
73-
}
74-
}
75-
}
76-
}
77-
}
78-
79-
@Override
80-
public String replaceTokensInModule(String moduleText) {
81-
for (Object key : properties.keySet()) {
82-
String skey = propertyPrefix != null ? propertyPrefix + key : key.toString();
83-
if (logger.isTraceEnabled()) {
84-
logger.trace("Checking for key in module text: " + skey);
85-
}
86-
if (moduleText.contains(skey)) {
87-
String value = properties.getProperty(key.toString());
88-
value = helper.replacePlaceholders(value, properties);
89-
if (logger.isDebugEnabled()) {
90-
logger.debug(format("Replacing %s with %s", skey, value));
91-
}
92-
moduleText = moduleText.replace(skey, value);
93-
}
94-
}
95-
return moduleText;
96-
}
97-
98-
public void setPropertyPrefix(String propertyPrefix) {
99-
this.propertyPrefix = propertyPrefix;
12+
super();
13+
addPropertiesSource(new FilePropertiesSource(new File("deploy/default.properties")));
14+
addPropertiesSource(new FilePropertiesSource(new File("deploy/build.properties")));
15+
addPropertiesSource(new FilePropertiesSource(new File("deploy/local.properties")));
10016
}
10117

10218
}

src/test/java/com/marklogic/client/modulesloader/impl/XmlExtensionMetadataProviderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ public void test() throws IOException {
3333
assertEquals("xs:boolean", method.get("edit").get(0));
3434
}
3535

36+
@Test
37+
public void missingMetadata() throws Exception {
38+
DefaultExtensionMetadataProvider p = new DefaultExtensionMetadataProvider();
39+
Resource resource = new ClassPathResource("sample-base-dir/services/another-sample.xq");
40+
ExtensionMetadataAndParams emap = p.provideExtensionMetadataAndParams(resource);
41+
assertEquals("Title should default to the filename minus the extension", "another-sample", emap.metadata.getTitle());
42+
}
3643
}

0 commit comments

Comments
 (0)