Skip to content

Commit ca7eae0

Browse files
committed
fixed #477
1 parent 6595eb2 commit ca7eae0

File tree

15 files changed

+483
-95
lines changed

15 files changed

+483
-95
lines changed

marklogic-data-hub/src/main/java/com/marklogic/hub/DataHub.java

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ private ServerManager getServerManager() {
104104
}
105105
return this._serverManager;
106106
}
107+
void setServerManager(ServerManager manager) { this._serverManager = manager; }
107108
/**
108109
* Determines if the data hub is installed in MarkLogic
109110
* @return true if installed, false otherwise
@@ -155,14 +156,21 @@ public InstallInfo isInstalled() {
155156

156157
/**
157158
* Validates the MarkLogic server to ensure compatibility with the hub
159+
* @return true if valid, false otherwise
158160
* @throws ServerValidationException if the server is not compatible
159161
*/
160-
public void validateServer() throws ServerValidationException {
162+
public boolean isServerVersionValid() {
163+
return isServerVersionValid(null);
164+
}
165+
166+
public boolean isServerVersionValid(String versionString) {
161167
try {
162-
String versionString = getAdminManager().getServerVersion();
168+
if (versionString == null) {
169+
versionString = getMarkLogicVersion();
170+
}
163171
int major = Integer.parseInt(versionString.replaceAll("([^.]+)\\..*", "$1"));
164172
if (major < 8) {
165-
throw new ServerValidationException("Invalid MarkLogic Server Version: " + versionString);
173+
return false;
166174
}
167175
boolean isNightly = versionString.matches("[^-]+-(\\d{4})(\\d{2})(\\d{2})");
168176
if (major == 8) {
@@ -172,7 +180,7 @@ public void validateServer() throws ServerValidationException {
172180
}
173181
int ver = Integer.parseInt(alteredString.substring(0, 4));
174182
if (!isNightly && ver < 8070) {
175-
throw new ServerValidationException("Invalid MarkLogic Server Version: " + versionString);
183+
return false;
176184
}
177185
}
178186
if (major == 9) {
@@ -182,22 +190,23 @@ public void validateServer() throws ServerValidationException {
182190
}
183191
int ver = Integer.parseInt(alteredString.substring(0, 4));
184192
if (!isNightly && ver < 9011) {
185-
throw new ServerValidationException("Invalid MarkLogic Server Version: " + versionString);
193+
return false;
186194
}
187195
}
188196
if (isNightly) {
189197
String dateString = versionString.replaceAll("[^-]+-(\\d{4})(\\d{2})(\\d{2})", "$1-$2-$3");
190198
Date minDate = new GregorianCalendar(2017, 6, 1).getTime();
191199
Date date = new SimpleDateFormat("y-M-d").parse(dateString);
192200
if (date.before(minDate)) {
193-
throw new ServerValidationException("Invalid MarkLogic Server Version: " + versionString);
201+
return false;
194202
}
195203
}
196204

197205
}
198206
catch(Exception e) {
199207
throw new ServerValidationException(e.toString());
200208
}
209+
return true;
201210
}
202211

203212
public void initProject() {
@@ -368,6 +377,52 @@ public Map<String, List<Command>> getCommands() {
368377
return commandMap;
369378
}
370379

380+
public Map<Integer, String> getServerPortsInUse() {
381+
Map<Integer, String> portsInUse = new HashMap<>();
382+
ResourcesFragment srf = getServerManager().getAsXml();
383+
srf.getListItemNameRefs().forEach(s -> {
384+
Fragment fragment = getServerManager().getPropertiesAsXml(s);
385+
int port = Integer.parseInt(fragment.getElementValue("//m:port"));
386+
portsInUse.put(port, s);
387+
});
388+
return portsInUse;
389+
}
390+
391+
public PreInstallCheck runPreInstallCheck() {
392+
PreInstallCheck check = new PreInstallCheck();
393+
394+
Map<Integer, String> portsInUse = getServerPortsInUse();
395+
Set<Integer> ports = portsInUse.keySet();
396+
397+
String serverName = portsInUse.get(hubConfig.stagingPort);
398+
check.stagingPortInUse = ports.contains(hubConfig.stagingPort) && serverName != null && !serverName.equals(hubConfig.stagingHttpName);
399+
if (check.stagingPortInUse) {
400+
check.stagingPortInUseBy = serverName;
401+
}
402+
403+
serverName = portsInUse.get(hubConfig.finalPort);
404+
check.finalPortInUse = ports.contains(hubConfig.finalPort) && serverName != null && !serverName.equals(hubConfig.finalHttpName);
405+
if (check.finalPortInUse) {
406+
check.finalPortInUseBy = serverName;
407+
}
408+
409+
serverName = portsInUse.get(hubConfig.jobPort);
410+
check.jobPortInUse = ports.contains(hubConfig.jobPort) && serverName != null && !serverName.equals(hubConfig.jobHttpName);
411+
if (check.jobPortInUse) {
412+
check.jobPortInUseBy = serverName;
413+
}
414+
415+
serverName = portsInUse.get(hubConfig.tracePort);
416+
check.tracePortInUse = ports.contains(hubConfig.tracePort) && serverName != null && !serverName.equals(hubConfig.traceHttpName);
417+
if (check.tracePortInUse) {
418+
check.tracePortInUseBy = serverName;
419+
}
420+
421+
check.serverVersion = getMarkLogicVersion();
422+
check.serverVersionOk = isServerVersionValid(check.serverVersion);
423+
return check;
424+
}
425+
371426
/**
372427
* Installs the data hub configuration and server-side modules into MarkLogic
373428
*/
@@ -433,15 +488,7 @@ public String getHubVersion() {
433488
}
434489

435490
public String getMarkLogicVersion() {
436-
ServerEvaluationCall eval = hubConfig.newAppServicesClient().newServerEval();
437-
String xqy = "xdmp:version()";
438-
EvalResultIterator result = eval.xquery(xqy).eval();
439-
if (result.hasNext()) {
440-
return result.next().getString();
441-
}
442-
else {
443-
throw new RuntimeException("Couldn't determine MarkLogic Version");
444-
}
491+
return getAdminManager().getServerVersion();
445492
}
446493

447494
public static int versionCompare(String v1, String v2) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2016 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.hub;
17+
18+
public class PreInstallCheck {
19+
public boolean stagingPortInUse;
20+
public String stagingPortInUseBy;
21+
public boolean finalPortInUse;
22+
public String finalPortInUseBy;
23+
public boolean jobPortInUse;
24+
public String jobPortInUseBy;
25+
public boolean tracePortInUse;
26+
public String tracePortInUseBy;
27+
public boolean serverVersionOk;
28+
public String serverVersion;
29+
30+
public boolean isSafeToInstall() {
31+
return !(stagingPortInUse ||
32+
finalPortInUse ||
33+
jobPortInUse ||
34+
tracePortInUse) && serverVersionOk;
35+
}
36+
}

marklogic-data-hub/src/main/java/com/marklogic/hub/error/ServerValidationException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.marklogic.hub.error;
1717

18-
public class ServerValidationException extends Exception {
18+
public class ServerValidationException extends RuntimeException {
1919

2020
private static final long serialVersionUID = -958991214928671059L;
2121

0 commit comments

Comments
 (0)