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

Commit 082c93a

Browse files
committed
Initial cut at managing workspaces
1 parent 7420550 commit 082c93a

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.marklogic.client.qconsole;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
/**
7+
* Interface for exporting and importing qconsole workspaces to/from disk.
8+
*/
9+
public interface WorkspaceManager {
10+
11+
/**
12+
* @param user
13+
* @param workspaceNames
14+
* @return a list of files that workspaces for the given user were exported to
15+
*/
16+
public List<File> exportWorkspaces(String user, String... workspaceNames);
17+
18+
/**
19+
* @param user
20+
* @param workspaceNames
21+
* @return a list of files that workspaces for the given user were imported from
22+
*/
23+
public List<File> importWorkspaces(String user, String... workspaceNames);
24+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.marklogic.client.qconsole.impl;
2+
3+
import com.marklogic.client.DatabaseClient;
4+
import com.marklogic.client.eval.EvalResultIterator;
5+
import com.marklogic.client.helper.LoggingObject;
6+
import com.marklogic.client.io.DOMHandle;
7+
import com.marklogic.client.io.FileHandle;
8+
import com.marklogic.client.io.Format;
9+
import com.marklogic.client.qconsole.WorkspaceManager;
10+
import org.springframework.util.FileCopyUtils;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/**
18+
* Default implementation that uses the ML Java Client API to manage workspaces. By default, workspaces
19+
* will be exported and imported to/from ~/.qconsole/workspaces/(ML username).
20+
*/
21+
public class DefaultWorkspaceManager extends LoggingObject implements WorkspaceManager {
22+
23+
private DatabaseClient client;
24+
25+
private File baseDir;
26+
27+
public DefaultWorkspaceManager(DatabaseClient client) {
28+
this.client = client;
29+
}
30+
31+
@Override
32+
public List<File> exportWorkspaces(String user, String... workspaceNames) {
33+
String xquery = "declare namespace qconsole='http://marklogic.com/appservices/qconsole'; " +
34+
"declare variable $user external; " +
35+
"cts:search(/qconsole:workspace, cts:element-value-query(xs:QName('qconsole:userid'), string(xdmp:user($user))))";
36+
37+
EvalResultIterator result = client.newServerEval()
38+
.addVariable("user", user)
39+
.xquery(xquery).eval();
40+
41+
if (baseDir == null) {
42+
baseDir = getDefaultWorkspacesDir();
43+
}
44+
45+
File userDir = new File(baseDir, user);
46+
userDir.mkdirs();
47+
48+
List<File> files = new ArrayList<>();
49+
50+
while (result.hasNext()) {
51+
DOMHandle dom = result.next().get(new DOMHandle());
52+
String workspaceId = getWorkspaceId(dom);
53+
File f = new File(userDir, workspaceId + ".xml");
54+
try {
55+
FileCopyUtils.copy(dom.toBuffer(), f);
56+
if (logger.isInfoEnabled()) {
57+
logger.info(format("Exported workspace %s to %s", workspaceId, f.getAbsolutePath()));
58+
}
59+
files.add(f);
60+
} catch (IOException ie) {
61+
throw new RuntimeException("Unable to write workspace XML to file, workspace ID: " + workspaceId + "; cause: " + ie.getMessage());
62+
}
63+
}
64+
65+
return files;
66+
}
67+
68+
protected String getWorkspaceId(DOMHandle dom) {
69+
return dom.get().getDocumentElement().getElementsByTagNameNS("http://marklogic.com/appservices/qconsole", "id").item(0).getTextContent();
70+
}
71+
72+
@Override
73+
public List<File> importWorkspaces(String user, String... workspaceNames) {
74+
if (baseDir == null) {
75+
baseDir = getDefaultWorkspacesDir();
76+
}
77+
78+
List<File> files = new ArrayList<>();
79+
80+
File userDir = new File(baseDir, user);
81+
if (!userDir.exists()) {
82+
return files;
83+
}
84+
85+
String xquery = "import module namespace amped-qconsole = 'http://marklogic.com/appservices/qconsole/util-amped' at '/MarkLogic/appservices/qconsole/qconsole-amped.xqy'; " +
86+
"declare variable $workspace external; " +
87+
"declare variable $uri external; " +
88+
"amped-qconsole:qconsole-document-insert($uri, $workspace)";
89+
90+
for (File f : userDir.listFiles()) {
91+
if (f.isFile() && f.getName().endsWith(".xml")) {
92+
client.newServerEval()
93+
.addVariable("uri", "/workspaces/" + f.getName())
94+
.addVariable("workspace", new FileHandle(f).withFormat(Format.XML))
95+
.xquery(xquery).eval();
96+
97+
if (logger.isInfoEnabled()) {
98+
logger.info(format("Imported workspace from %s", f.getAbsolutePath()));
99+
}
100+
101+
files.add(f);
102+
}
103+
}
104+
105+
return files;
106+
}
107+
108+
/**
109+
* Defaults to ~/.qconsole/workspaces.
110+
*
111+
* @return
112+
*/
113+
protected File getDefaultWorkspacesDir() {
114+
File homeDir = new File(System.getProperty("user.home"));
115+
File qcDir = new File(homeDir, ".qconsole");
116+
File dir = new File(qcDir, "workspaces");
117+
dir.mkdirs();
118+
return dir;
119+
}
120+
121+
122+
public void setBaseDir(File baseDir) {
123+
this.baseDir = baseDir;
124+
}
125+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.marklogic.client.qconsole.impl;
2+
3+
import com.marklogic.client.DatabaseClient;
4+
import com.marklogic.client.DatabaseClientFactory;
5+
6+
import java.io.IOException;
7+
8+
/**
9+
* Debug-style program for manually verifying how DefaultWorkspaceManager works.
10+
* <p>
11+
* If the content source ID (an appserver ID) is not valid, that's fine - the content source just defaults
12+
* to the first app server.
13+
* <p>
14+
* If the user doesn't exist, that's fine - it just won't be accessible.
15+
*/
16+
public class DefaultWorkspaceManagerDebug {
17+
18+
public static void main(String[] args) throws IOException {
19+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8000, "App-Services", "admin", "admin", DatabaseClientFactory.Authentication.DIGEST);
20+
DefaultWorkspaceManager dwm = new DefaultWorkspaceManager(client);
21+
String user = "admin";
22+
try {
23+
System.out.println(dwm.exportWorkspaces(user));
24+
System.out.println(dwm.importWorkspaces(user));
25+
} finally {
26+
client.release();
27+
}
28+
}
29+
30+
}

0 commit comments

Comments
 (0)