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

Commit affa9ff

Browse files
committed
Lazily creating connection in AdminManager now too
Previous change was just for ManageClient, need this for AdminManager as well.
1 parent 513761c commit affa9ff

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/main/java/com/marklogic/mgmt/admin/AdminManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public AdminManager(AdminConfig adminConfig) {
5656
*/
5757
public void setAdminConfig(AdminConfig adminConfig) {
5858
this.adminConfig = adminConfig;
59-
this.restTemplate = RestTemplateUtil.newRestTemplate(adminConfig);
6059
}
6160

6261
public void init() {
@@ -82,7 +81,7 @@ public boolean execute() {
8281
headers.setContentType(MediaType.APPLICATION_JSON);
8382
HttpEntity<String> entity = new HttpEntity<>(payload, headers);
8483
try {
85-
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
84+
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
8685
logger.info("Initialization response: " + response);
8786
// According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
8887
// restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -132,7 +131,7 @@ public boolean execute() {
132131
headers.setContentType(MediaType.APPLICATION_JSON);
133132
HttpEntity<String> entity = new HttpEntity<>(payload, headers);
134133
try {
135-
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
134+
ResponseEntity<String> response = getRestTemplate().exchange(uri, HttpMethod.POST, entity, String.class);
136135
logger.info("Admin installation response: " + response);
137136
// According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
138137
// restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -166,7 +165,7 @@ public void invokeActionRequiringRestart(ActionRequiringRestart action) {
166165
}
167166

168167
public String getLastRestartTimestamp() {
169-
return restTemplate.getForEntity(adminConfig.buildUri("/admin/v1/timestamp"), String.class).getBody();
168+
return getRestTemplate().getForEntity(adminConfig.buildUri("/admin/v1/timestamp"), String.class).getBody();
170169
}
171170

172171
public void waitForRestart() {
@@ -230,7 +229,7 @@ public boolean execute() {
230229
}
231230

232231
public Fragment getServerConfig() {
233-
return new Fragment(restTemplate.getForObject(adminConfig.buildUri("/admin/v1/server-config"), String.class));
232+
return new Fragment(getRestTemplate().getForObject(adminConfig.buildUri("/admin/v1/server-config"), String.class));
234233
}
235234

236235
public String getServerVersion() {
@@ -271,7 +270,7 @@ public byte[] postJoiningHostConfig(Fragment joiningHostConfig, String group, St
271270
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
272271

273272
URI url = adminConfig.buildUri("/admin/v1/cluster-config");
274-
ResponseEntity<byte[]> bytes = restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class);
273+
ResponseEntity<byte[]> bytes = getRestTemplate().exchange(url, HttpMethod.POST, entity, byte[].class);
275274
return bytes.getBody();
276275
}
277276

@@ -288,7 +287,7 @@ public void postClustConfigToJoiningHost(byte[] clusterConfigZipBytes) {
288287
URI clusterConfigUri = adminConfig.buildUri("/admin/v1/cluster-config");
289288

290289
HttpEntity<Resource> resourceEntity = new HttpEntity<>(new ByteArrayResource(clusterConfigZipBytes), headers);
291-
ResponseEntity<String> response = restTemplate.exchange(clusterConfigUri, HttpMethod.POST, resourceEntity, String.class);
290+
ResponseEntity<String> response = getRestTemplate().exchange(clusterConfigUri, HttpMethod.POST, resourceEntity, String.class);
292291
if(response.getStatusCode().value() == 202){
293292
waitForRestart();
294293
}
@@ -299,7 +298,7 @@ public void postClustConfigToJoiningHost(byte[] clusterConfigZipBytes) {
299298
* Note that once it does so, the server will need to be initialized again
300299
*/
301300
public void leaveCluster() {
302-
ResponseEntity<String> response = restTemplate.exchange(adminConfig.buildUri("/admin/v1/host-config"), HttpMethod.DELETE, null, String.class);
301+
ResponseEntity<String> response = getRestTemplate().exchange(adminConfig.buildUri("/admin/v1/host-config"), HttpMethod.DELETE, null, String.class);
303302
if (response.getStatusCode().value() == 202) {
304303
waitForRestart();
305304
}
@@ -310,6 +309,9 @@ public AdminConfig getAdminConfig() {
310309
}
311310

312311
public RestTemplate getRestTemplate() {
313-
return restTemplate;
312+
if (this.restTemplate == null) {
313+
this.restTemplate = RestTemplateUtil.newRestTemplate(adminConfig);
314+
}
315+
return this.restTemplate;
314316
}
315317
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.marklogic.mgmt.admin;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
public class AdminManagerTest {
9+
10+
@Test
11+
void invalidConfig() {
12+
// As of 4.5.1, this should no longer throw an error as the connection to MarkLogic should
13+
// be lazily constructed.
14+
AdminManager mgr = new AdminManager(new AdminConfig("localhost", 8001, null, null));
15+
16+
RuntimeException ex = assertThrows(RuntimeException.class, () -> mgr.getRestTemplate());
17+
assertTrue(ex.getMessage().contains("username must be of type String"),
18+
"The call to get a RestTemplate is expected to fail because no username/password has been " +
19+
"provided, and digest auth is used by default. Prior to 4.5.1, this failed when the AdminManager " +
20+
"was instantiated, which proved problematic in 4.5.0 when the user tried to run an ml-gradle task " +
21+
"without providing any credentials.");
22+
}
23+
}

0 commit comments

Comments
 (0)