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

Commit 1a39650

Browse files
committed
#338 Added XML support for unmarshalling XDBC/ODBC servers
1 parent 03d9397 commit 1a39650

File tree

7 files changed

+121
-21
lines changed

7 files changed

+121
-21
lines changed

src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,17 @@ protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context,
160160
String payload = readResourceFromFile(mgr, context, resourceFile);
161161

162162
if (payload != null && resourceMergingIsSupported(context)) {
163-
storeResourceInCommandContextMap(context, resourceFile, payload);
164-
return null;
163+
try {
164+
storeResourceInCommandContextMap(context, resourceFile, payload);
165+
return null;
166+
} catch (Exception ex) {
167+
/**
168+
* As a worst case, if the payload cannot be unmarshalled (and converted if necessary) into an ObjectNode,
169+
* this warning will be logged and the resource will be saved immediately.
170+
*/
171+
logger.warn("Unable to store resource in context map so it can be merged (if needed) and " +
172+
"saved later, so the resource will instead be saved immediately. Error cause: " + ex.getMessage());
173+
}
165174
}
166175

167176
return saveResource(mgr, context, payload);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.marklogic.mgmt.api.server;
2+
3+
import com.marklogic.mgmt.api.API;
4+
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@XmlRootElement(name = "http-server-properties")
8+
public class HttpServer extends Server {
9+
10+
public HttpServer() {
11+
}
12+
13+
public HttpServer(API api, String serverName) {
14+
super(api, serverName);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.marklogic.mgmt.api.server;
2+
3+
import com.marklogic.mgmt.api.API;
4+
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@XmlRootElement(name = "odbc-server-properties")
8+
public class OdbcServer extends Server {
9+
10+
public OdbcServer() {
11+
}
12+
13+
public OdbcServer(API api, String serverName) {
14+
super(api, serverName);
15+
}
16+
}

src/main/java/com/marklogic/mgmt/api/server/Server.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import javax.xml.bind.annotation.*;
1313
import java.util.List;
1414

15-
@XmlRootElement(name = "http-server-properties")
1615
@XmlAccessorType(XmlAccessType.FIELD)
1716
public class Server extends Resource {
1817

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.marklogic.mgmt.api.server;
2+
3+
import com.marklogic.mgmt.api.API;
4+
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@XmlRootElement(name = "xdbc-server-properties")
8+
public class XdbcServer extends Server {
9+
10+
public XdbcServer() {
11+
}
12+
13+
public XdbcServer(API api, String serverName) {
14+
super(api, serverName);
15+
}
16+
}
Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.marklogic.mgmt.mapper;
22

3+
import com.marklogic.mgmt.PayloadParser;
34
import com.marklogic.mgmt.api.API;
45
import com.marklogic.mgmt.api.Resource;
5-
import com.marklogic.mgmt.api.security.Role;
6+
import com.marklogic.mgmt.api.server.HttpServer;
7+
import com.marklogic.mgmt.api.server.OdbcServer;
8+
import com.marklogic.mgmt.api.server.Server;
9+
import com.marklogic.mgmt.api.server.XdbcServer;
610

711
import javax.xml.bind.JAXBContext;
812
import java.io.StringReader;
@@ -17,6 +21,7 @@ public class DefaultResourceMapper implements ResourceMapper {
1721

1822
private API api;
1923
private Map<Class<?>, JAXBContext> jaxbContextMap;
24+
private PayloadParser payloadParser = new PayloadParser();
2025

2126
public DefaultResourceMapper() {
2227
this.jaxbContextMap = new HashMap<>();
@@ -30,17 +35,43 @@ public DefaultResourceMapper(API api) {
3035
@Override
3136
public <T extends Resource> T readResource(String payload, Class<T> resourceType) {
3237
try {
33-
T resource = null;
34-
if (isJsonPayload(payload)) {
38+
T resource;
39+
if (payloadParser.isJsonPayload(payload)) {
3540
resource = api.getObjectMapper().readerFor(resourceType).readValue(payload);
36-
}
37-
else {
38-
JAXBContext context = jaxbContextMap.get(resourceType);
41+
} else {
42+
JAXBContext context;
43+
44+
/**
45+
* Can't figure out how to use a JAXB ObjectFactory to support the 3 different kinds of servers, so using
46+
* this hacky approach.
47+
*/
48+
if (resourceType.equals(Server.class)) {
49+
if (payload.contains("xdbc-server-properties")) {
50+
context = jaxbContextMap.get(XdbcServer.class);
51+
} else if (payload.contains("odbc-server-properties")) {
52+
context = jaxbContextMap.get(OdbcServer.class);
53+
} else {
54+
context = jaxbContextMap.get(HttpServer.class);
55+
}
56+
} else {
57+
context = jaxbContextMap.get(resourceType);
58+
}
59+
3960
if (context == null) {
40-
context = JAXBContext.newInstance(resourceType);
61+
if (resourceType.equals(Server.class)) {
62+
if (payload.contains("xdbc-server-properties")) {
63+
context = JAXBContext.newInstance(XdbcServer.class);
64+
} else if (payload.contains("odbc-server-properties")) {
65+
context = JAXBContext.newInstance(OdbcServer.class);
66+
} else {
67+
context = JAXBContext.newInstance(HttpServer.class);
68+
}
69+
} else {
70+
context = JAXBContext.newInstance(resourceType);
71+
}
4172
jaxbContextMap.put(resourceType, context);
4273
}
43-
resource = (T)context.createUnmarshaller().unmarshal(new StringReader(payload));
74+
resource = (T) context.createUnmarshaller().unmarshal(new StringReader(payload));
4475
}
4576
if (api != null) {
4677
resource.setApi(api);
@@ -52,14 +83,4 @@ public <T extends Resource> T readResource(String payload, Class<T> resourceType
5283
}
5384
}
5485

55-
/**
56-
* TODO Move this to util class?
57-
*
58-
* @param payload
59-
* @return
60-
*/
61-
protected boolean isJsonPayload(String payload) {
62-
String s = payload.trim();
63-
return s.startsWith("{") || s.startsWith("[");
64-
}
6586
}

src/test/java/com/marklogic/mgmt/api/server/ServerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.node.ObjectNode;
44
import com.marklogic.mgmt.api.API;
5+
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
56
import com.marklogic.mgmt.util.ObjectMapperFactory;
67
import org.junit.Assert;
78
import org.junit.Test;
@@ -28,4 +29,26 @@ public void disableSslProperties() throws Exception {
2829
assertTrue(node.get("ssl-disable-tlsv1-1").asBoolean());
2930
assertTrue(node.get("ssl-disable-tlsv1-2").asBoolean());
3031
}
32+
33+
@Test
34+
public void xdbc() {
35+
String xml = "<xdbc-server-properties xmlns=\"http://marklogic.com/manage\">\n" +
36+
" <server-name>example-xdbc</server-name>" +
37+
"</xdbc-server-properties>";
38+
39+
Server s = new DefaultResourceMapper(new API(null)).readResource(xml, Server.class);
40+
assertTrue(s instanceof XdbcServer);
41+
assertEquals("example-xdbc", s.getServerName());
42+
}
43+
44+
@Test
45+
public void odbc() {
46+
String xml = "<odbc-server-properties xmlns=\"http://marklogic.com/manage\">\n" +
47+
" <server-name>example-odbc</server-name>" +
48+
"</odbc-server-properties>";
49+
50+
Server s = new DefaultResourceMapper(new API(null)).readResource(xml, Server.class);
51+
assertTrue(s instanceof OdbcServer);
52+
assertEquals("example-odbc", s.getServerName());
53+
}
3154
}

0 commit comments

Comments
 (0)