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

Commit 1c0ab0b

Browse files
committed
Folder ml-xcc-util into this project
1 parent b13cba5 commit 1c0ab0b

File tree

7 files changed

+138
-3
lines changed

7 files changed

+138
-3
lines changed

CHANGELOG.mdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ml-gradle releases
22

3+
## 2.5
4+
5+
* Added classes from ml-xcc-util
6+
37
## 2.4
48

59
* Repackaged to com.marklogic

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ targetCompatibility = "1.7"
1111

1212
repositories {
1313
jcenter()
14+
maven {url "http://developer.marklogic.com/maven2/"}
1415
}
1516

1617
dependencies {
1718
compile 'com.marklogic:java-client-api:3.0.4'
18-
compile 'com.marklogic:ml-xcc-util:2.2'
19+
compile 'com.marklogic:marklogic-xcc:8.0.4'
1920
compile 'org.jdom:jdom2:2.0.5'
2021
compile 'org.springframework:spring-context:4.1.5.RELEASE'
2122
testCompile 'junit:junit:4+'
@@ -56,7 +57,7 @@ if (project.hasProperty("myBintrayUser")) {
5657
licenses = ['Apache-2.0']
5758
vcsUrl = 'https://github.com/rjrudin/' + project.name + '.git'
5859
version {
59-
name = "2.4"
60+
name = "2.5"
6061
released = new Date()
6162
}
6263
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ javadocsDir=../gh-pages-marklogic-java/javadocs
1111
# 2.0.6 - #8 now supports loading rest-properties.json file
1212
# 2.0.7 - #8 Fix for not loading rest-properties.json when it's not been modified
1313
# 2.0.8 - Added TestServerModulesFinder
14-
version=2.4
14+
version=2.5
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.marklogic.xcc.template;
2+
3+
import com.marklogic.xcc.AdhocQuery;
4+
import com.marklogic.xcc.Session;
5+
import com.marklogic.xcc.exceptions.RequestException;
6+
7+
/**
8+
* Simple callback for executing an adhoc query. Could be subclassed for a variety of queries, such as getting all the
9+
* URIs in a collection or retrieving a single document.
10+
*/
11+
public class AdhocQueryCallback implements XccCallback<String> {
12+
13+
private String xquery;
14+
15+
public AdhocQueryCallback(String xquery) {
16+
this.xquery = xquery;
17+
}
18+
19+
@Override
20+
public String execute(Session session) throws RequestException {
21+
AdhocQuery q = session.newAdhocQuery(xquery);
22+
return session.submitRequest(q).asString();
23+
}
24+
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.marklogic.xcc.template;
2+
3+
import java.io.File;
4+
5+
import com.marklogic.xcc.Content;
6+
import com.marklogic.xcc.ContentCreateOptions;
7+
import com.marklogic.xcc.ContentFactory;
8+
import com.marklogic.xcc.DocumentFormat;
9+
import com.marklogic.xcc.Session;
10+
import com.marklogic.xcc.exceptions.RequestException;
11+
12+
/**
13+
* Simple callback for inserting a single document.
14+
*/
15+
public class InsertContentCallback implements XccCallback<Void> {
16+
17+
private String uri;
18+
private File file;
19+
private DocumentFormat format;
20+
21+
public InsertContentCallback(String uri, File file, DocumentFormat format) {
22+
this.uri = uri;
23+
this.file = file;
24+
this.format = format;
25+
}
26+
27+
public Void execute(Session session) throws RequestException {
28+
ContentCreateOptions opts = new ContentCreateOptions();
29+
opts.setFormat(format);
30+
Content c = ContentFactory.newContent(uri, file, opts);
31+
session.insertContent(c);
32+
return null;
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.marklogic.xcc.template;
2+
3+
import com.marklogic.xcc.Session;
4+
import com.marklogic.xcc.exceptions.RequestException;
5+
6+
/**
7+
* Spring-style callback interface that provides an open Session to an implementation, which then doesn't have to worry
8+
* about how to open or close the Session.
9+
*/
10+
public interface XccCallback<T> {
11+
12+
public T execute(Session session) throws RequestException;
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.marklogic.xcc.template;
2+
3+
import java.net.URI;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.marklogic.xcc.ContentSource;
9+
import com.marklogic.xcc.ContentSourceFactory;
10+
import com.marklogic.xcc.Session;
11+
import com.marklogic.xcc.exceptions.RequestException;
12+
13+
/**
14+
* Spring-style Template class that handles instantiating an XCC Session and then closing it, allowing the Callback
15+
* implementation to focus on what to do with the Session.
16+
*/
17+
public class XccTemplate {
18+
19+
protected final Logger logger = LoggerFactory.getLogger(getClass());
20+
21+
private ContentSource contentSource;
22+
23+
public XccTemplate(String uri) {
24+
try {
25+
contentSource = ContentSourceFactory.newContentSource(new URI(uri));
26+
if (logger.isInfoEnabled()) {
27+
String[] tokens = uri.split("@");
28+
if (tokens.length > 1) {
29+
String hostAndPort = tokens[tokens.length - 1];
30+
logger.info("Will submit requests to XDBC server at " + hostAndPort);
31+
}
32+
}
33+
} catch (Exception e) {
34+
throw new RuntimeException(e);
35+
}
36+
}
37+
38+
public <T> T execute(XccCallback<T> callback) {
39+
Session session = contentSource.newSession();
40+
try {
41+
return callback.execute(session);
42+
} catch (RequestException re) {
43+
throw new RuntimeException(re);
44+
} finally {
45+
session.close();
46+
}
47+
}
48+
49+
/**
50+
* Convenience method for executing any adhoc query.
51+
*
52+
* @param xquery the XQuery statement to execute
53+
* @return the response from MarkLogic as a String
54+
*/
55+
public String executeAdhocQuery(String xquery) {
56+
return execute(new AdhocQueryCallback(xquery));
57+
}
58+
}

0 commit comments

Comments
 (0)