Skip to content

Commit 7260a23

Browse files
committed
fixed #91 - check plugins during install for errors
1 parent c170490 commit 7260a23

File tree

10 files changed

+394
-178
lines changed

10 files changed

+394
-178
lines changed

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.io.ClassPathResource;
3434
import org.springframework.web.client.ResourceAccessException;
3535

36+
import com.fasterxml.jackson.databind.JsonNode;
3637
import com.marklogic.appdeployer.AppConfig;
3738
import com.marklogic.appdeployer.ConfigDir;
3839
import com.marklogic.appdeployer.command.Command;
@@ -45,8 +46,13 @@
4546
import com.marklogic.appdeployer.impl.SimpleAppDeployer;
4647
import com.marklogic.client.DatabaseClient;
4748
import com.marklogic.client.DatabaseClientFactory;
49+
import com.marklogic.client.extensions.ResourceManager;
50+
import com.marklogic.client.extensions.ResourceServices.ServiceResult;
51+
import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator;
52+
import com.marklogic.client.io.JacksonHandle;
4853
import com.marklogic.client.modulesloader.impl.PropertiesModuleManager;
4954
import com.marklogic.client.modulesloader.impl.XccAssetLoader;
55+
import com.marklogic.client.util.RequestParameters;
5056
import com.marklogic.hub.commands.DeployHubDatabaseCommand;
5157
import com.marklogic.hub.commands.DeployModulesDatabaseCommand;
5258
import com.marklogic.hub.commands.DeployRestApiCommand;
@@ -198,6 +204,16 @@ public void install() throws IOException {
198204
deployer.deploy(config);
199205
}
200206

207+
private DatabaseClient getDatabaseClient(int port) {
208+
AppConfig config = new AppConfig();
209+
config.setHost(host);
210+
config.setName(HUB_NAME);
211+
config.setRestAdminUsername(username);
212+
config.setRestAdminPassword(password);
213+
DatabaseClient client = DatabaseClientFactory.newClient(host, port, username, password,
214+
config.getRestAuthentication(), config.getRestSslContext(), config.getRestSslHostnameVerifier());
215+
return client;
216+
}
201217
/**
202218
* Installs User Provided modules into the Data Hub
203219
*
@@ -215,10 +231,8 @@ public Set<File> installUserModules(String pathToUserModules) throws IOException
215231
config.setRestAdminUsername(username);
216232
config.setRestAdminPassword(password);
217233

218-
DatabaseClient stagingClient = DatabaseClientFactory.newClient(host, stagingRestPort, username, password,
219-
config.getRestAuthentication(), config.getRestSslContext(), config.getRestSslHostnameVerifier());
220-
DatabaseClient finalClient = DatabaseClientFactory.newClient(host, finalRestPort, username, password,
221-
config.getRestAuthentication(), config.getRestSslContext(), config.getRestSslHostnameVerifier());
234+
DatabaseClient stagingClient = getDatabaseClient(stagingRestPort);
235+
DatabaseClient finalClient = getDatabaseClient(finalRestPort);
222236

223237

224238
Set<File> loadedFiles = new HashSet<File>();
@@ -259,6 +273,12 @@ else if (isConformanceDir) {
259273
return loadedFiles;
260274
}
261275

276+
public JsonNode validateUserModules() {
277+
DatabaseClient client = getDatabaseClient(stagingRestPort);
278+
EntitiesValidator ev = new EntitiesValidator(client);
279+
return ev.validate();
280+
}
281+
262282
private List<Command> getCommands(AppConfig config) {
263283
List<Command> commands = new ArrayList<Command>();
264284

@@ -310,4 +330,24 @@ public void uninstall() throws IOException {
310330
PropertiesModuleManager moduleManager = new PropertiesModuleManager(this.assetInstallTimeFile);
311331
moduleManager.deletePropertiesFile();
312332
}
333+
334+
class EntitiesValidator extends ResourceManager {
335+
private static final String NAME = "validate";
336+
337+
public EntitiesValidator(DatabaseClient client) {
338+
super();
339+
client.init(NAME, this);
340+
}
341+
342+
public JsonNode validate() {
343+
RequestParameters params = new RequestParameters();
344+
ServiceResultIterator resultItr = this.getServices().get(params);
345+
if (resultItr == null || ! resultItr.hasNext()) {
346+
return null;
347+
}
348+
ServiceResult res = resultItr.next();
349+
JacksonHandle handle = new JacksonHandle();
350+
return res.getContent(handle).get();
351+
}
352+
}
313353
}

marklogic-data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/lib/flow-lib.xqy

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,69 @@ declare function flow:run-writer(
574574
return
575575
$func($identifier, $envelope, $options)
576576
};
577+
578+
declare function flow:make-error-json($ex) {
579+
map:new((
580+
map:entry("msg", $ex/error:format-string/fn:data()),
581+
let $f := $ex/error:stack/error:frame[1]
582+
return
583+
(
584+
map:entry("uri", $f/error:uri/fn:data()),
585+
map:entry("line", $f/error:line/fn:data()),
586+
map:entry("column", $f/error:column/fn:data())
587+
)
588+
))
589+
};
590+
591+
declare function flow:validate-entities()
592+
{
593+
let $errors := json:array()
594+
let $identifier := "123"
595+
let $content :=
596+
map:new((
597+
map:entry("identifier", $identifier),
598+
map:entry("content", <x/>)
599+
))
600+
let $options := map:map()
601+
let $_ :=
602+
for $entity in flow:get-entities()/hub:entity
603+
for $flow in $entity/hub:flows/hub:flow
604+
let $data-format := $flow/hub:data-format
605+
(: validate collector :)
606+
let $_ :=
607+
let $collector := $flow/hub:collector
608+
return
609+
if ($collector) then
610+
try {
611+
flow:run-collector($collector/@module, $options)
612+
}
613+
catch($ex) {
614+
json:array-push($errors, flow:make-error-json($ex))
615+
}
616+
else ()
617+
(: validate plugins :)
618+
let $_ :=
619+
for $plugin in $flow/hub:plugins/hub:plugin
620+
let $destination := $plugin/@dest
621+
return
622+
try {
623+
flow:run-plugin(
624+
$plugin,
625+
$data-format,
626+
$identifier,
627+
map:get($content, "content"),
628+
map:get($content, "headers"),
629+
map:get($content, "triple"),
630+
$options)
631+
}
632+
catch($ex) {
633+
json:array-push($errors, flow:make-error-json($ex))
634+
}
635+
return
636+
()
637+
return
638+
map:new(
639+
map:entry("errors", $errors)
640+
)
641+
};
642+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Copyright 2016 MarkLogic Corporation. All Rights Reserved. -->
2+
<metadata>
3+
<title>validate</title>
4+
<description>
5+
<div>
6+
Validates Installed User Modules
7+
</div>
8+
</description>
9+
<method name="GET">
10+
</method>
11+
</metadata>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
xquery version "1.0-ml";
17+
18+
module namespace service = "http://marklogic.com/rest-api/resource/validate";
19+
20+
import module namespace debug = "http://marklogic.com/data-hub/debug-lib"
21+
at "/com.marklogic.hub/lib/debug-lib.xqy";
22+
23+
import module namespace flow = "http://marklogic.com/data-hub/flow-lib"
24+
at "/com.marklogic.hub/lib/flow-lib.xqy";
25+
26+
declare option xdmp:mapping "false";
27+
28+
(:~
29+
: Entry point for java to get flow(s).
30+
:
31+
: if the "flow-name" param is given then return a flow. Otherwise
32+
: return all flows.
33+
:
34+
:)
35+
declare function get(
36+
$context as map:map,
37+
$params as map:map
38+
) as document-node()*
39+
{
40+
debug:dump-env(),
41+
xdmp:set-response-content-type("application/json"),
42+
document {
43+
xdmp:to-json(flow:validate-entities())
44+
}
45+
};

quick-start/src/main/java/com/marklogic/hub/service/DataHubService.java

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.stereotype.Service;
99

10+
import com.fasterxml.jackson.databind.JsonNode;
1011
import com.marklogic.hub.DataHub;
1112
import com.marklogic.hub.ServerValidationException;
1213
import com.marklogic.hub.config.EnvironmentConfiguration;
@@ -15,19 +16,19 @@
1516
@Service
1617
public class DataHubService {
1718

18-
private static final Logger LOGGER = LoggerFactory.getLogger(DataHubService.class);
19+
private static final Logger LOGGER = LoggerFactory.getLogger(DataHubService.class);
1920

20-
@Autowired
21-
private EnvironmentConfiguration environmentConfiguration;
21+
@Autowired
22+
private EnvironmentConfiguration environmentConfiguration;
2223

23-
public void install() throws DataHubException {
24-
DataHub dataHub = getDataHub();
25-
try {
26-
dataHub.install();
27-
} catch(Throwable e) {
28-
throw new DataHubException(e.getMessage(), e);
29-
}
30-
}
24+
public void install() throws DataHubException {
25+
DataHub dataHub = getDataHub();
26+
try {
27+
dataHub.install();
28+
} catch(Throwable e) {
29+
throw new DataHubException(e.getMessage(), e);
30+
}
31+
}
3132

3233
public void installUserModules() throws DataHubException {
3334
DataHub dataHub = getDataHub();
@@ -38,8 +39,13 @@ public void installUserModules() throws DataHubException {
3839
}
3940
}
4041

41-
private DataHub getDataHub() throws DataHubException {
42-
try {
42+
public JsonNode validateUserModules() {
43+
DataHub dataHub = getDataHub();
44+
return dataHub.validateUserModules();
45+
}
46+
47+
private DataHub getDataHub() throws DataHubException {
48+
try {
4349
LOGGER.info("Connecting to DataHub at host is {}:{} with user={}",
4450
new Object[] {
4551
environmentConfiguration.getMLHost()
@@ -54,39 +60,39 @@ private DataHub getDataHub() throws DataHubException {
5460
dataHub.setAssetInstallTimeFile(new File(environmentConfiguration.getAssetInstallTimeFilePath()));
5561

5662
return dataHub;
57-
} catch(Throwable e) {
58-
throw new DataHubException(e.getMessage(), e);
59-
}
60-
}
61-
62-
public boolean isInstalled() throws DataHubException {
63-
DataHub dataHub = getDataHub();
64-
try {
65-
return dataHub.isInstalled();
66-
} catch(Throwable e) {
67-
throw new DataHubException(e.getMessage(), e);
68-
}
69-
}
70-
71-
public boolean isServerAcceptable() throws DataHubException {
72-
DataHub dataHub = getDataHub();
73-
try {
74-
dataHub.validateServer();
75-
return true;
76-
} catch(ServerValidationException exception) {
77-
return false;
78-
} catch(Throwable e) {
79-
throw new DataHubException(e.getMessage(), e);
80-
}
81-
}
82-
83-
public void uninstall() throws DataHubException {
84-
DataHub dataHub = getDataHub();
85-
try {
86-
dataHub.uninstall();
87-
} catch(Throwable e) {
88-
throw new DataHubException(e.getMessage(), e);
89-
}
63+
} catch(Throwable e) {
64+
throw new DataHubException(e.getMessage(), e);
65+
}
66+
}
67+
68+
public boolean isInstalled() throws DataHubException {
69+
DataHub dataHub = getDataHub();
70+
try {
71+
return dataHub.isInstalled();
72+
} catch(Throwable e) {
73+
throw new DataHubException(e.getMessage(), e);
74+
}
75+
}
76+
77+
public boolean isServerAcceptable() throws DataHubException {
78+
DataHub dataHub = getDataHub();
79+
try {
80+
dataHub.validateServer();
81+
return true;
82+
} catch(ServerValidationException exception) {
83+
return false;
84+
} catch(Throwable e) {
85+
throw new DataHubException(e.getMessage(), e);
86+
}
87+
}
88+
89+
public void uninstall() throws DataHubException {
90+
DataHub dataHub = getDataHub();
91+
try {
92+
dataHub.uninstall();
93+
} catch(Throwable e) {
94+
throw new DataHubException(e.getMessage(), e);
95+
}
9096
}
9197

9298

0 commit comments

Comments
 (0)