-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractXMLDBTask.java
More file actions
345 lines (290 loc) · 10.3 KB
/
Copy pathAbstractXMLDBTask.java
File metadata and controls
345 lines (290 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Elemental
* Copyright (C) 2024, Evolved Binary Ltd
*
* admin@evolvedbinary.com
* https://www.evolvedbinary.com | https://www.elemental.xyz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
* The original license header is included below.
*
* =====================================================================
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* info@exist-db.org
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.ant;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.exist.security.Permission;
import org.exist.security.internal.aider.UnixStylePermissionAider;
import org.exist.start.CompatibleJavaVersionCheck;
import org.exist.start.StartException;
import org.exist.util.SyntaxException;
import org.exist.xmldb.UserManagementService;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import javax.annotation.Nullable;
import java.util.StringTokenizer;
/**
* DOCUMENT ME!
*
* @author wolf
* @author andrzej@chaeron.com
*/
public abstract class AbstractXMLDBTask extends Task
{
protected String driver = "org.exist.xmldb.DatabaseImpl";
protected String user = "guest";
protected String password = "guest";
protected String uri = null;
protected boolean ssl = false;
protected boolean createDatabase = false;
protected String configuration = null;
protected boolean failonerror = true;
protected String permissions = null;
private final String UNIX_PERMS_REGEX = "([r-][w-][x-]){3}";
@Override
public void init() throws BuildException {
super.init();
try {
CompatibleJavaVersionCheck.checkForCompatibleJavaVersion();
} catch (final StartException e) {
throw new BuildException(e.getMessage());
}
}
/**
* Set the driver.
*
* @param driver the driver
*/
public void setDriver(final String driver )
{
this.driver = driver;
}
/**
* Set the password.
*
* @param password the password
*/
public void setPassword(final String password )
{
this.password = password;
}
/**
* Set the user.
*
* @param user the user.
*/
public void setUser(final String user )
{
this.user = user;
}
/**
* Set the URI.
*
* @param uri the URI
*/
public void setUri(final String uri )
{
this.uri = uri;
}
/**
* Set whether to use SSL
*
* @param ssl true to use SSL, false otherwise
*/
public void setSsl(final boolean ssl )
{
this.ssl = ssl;
}
/**
* Set whether to initialise the database.
*
* @param create true to initialise the database, false otherwise.
*/
public void setInitdb(final boolean create )
{
this.createDatabase = create;
}
public void setConfiguration(final String config )
{
this.configuration = config;
}
public void setFailonerror(final boolean failonerror )
{
this.failonerror = failonerror;
}
public void setPermissions(final String permissions )
{
this.permissions = permissions;
}
protected void registerDatabase() throws BuildException
{
try {
log( "Registering database", Project.MSG_DEBUG );
for( final Database database : DatabaseManager.getDatabases() ) {
if( database.acceptsURI( uri ) ) {
return;
}
}
final Class<?> clazz = Class.forName( driver );
final Database database = (Database)clazz.newInstance();
database.setProperty( "create-database", createDatabase ? "true" : "false" );
database.setProperty( "ssl-enable", ssl ? "true" : "false" );
if( configuration != null ) {
database.setProperty( "configuration", configuration );
}
DatabaseManager.registerDatabase( database );
log( "Database driver registered." );
}
catch( final Exception e ) {
throw new BuildException("failed to initialize XMLDB database driver", e);
}
}
protected final Collection mkcol(Collection collection, final String baseURI, String path, final String relPath) throws XMLDBException {
final StringTokenizer tokenizer = new StringTokenizer(relPath, "/");
while (tokenizer.hasMoreTokens()) {
final String token = tokenizer.nextToken();
if (path != null) {
path = path + "/" + token;
} else {
path = "/" + token;
}
Collection child = DatabaseManager.getCollection(baseURI + path, user, password);
if (child == null) {
final CollectionManagementService mgtService = collection.getService(CollectionManagementService.class);
log("Create child collection " + token, Project.MSG_DEBUG);
child = mgtService.createCollection(token);
log("Created collection " + child.getName() + '.', Project.MSG_DEBUG);
}
try {
// close the parent collection
collection.close();
} catch (final XMLDBException e) {
// no-op
}
collection = child;
}
return collection;
}
protected final void setPermissions(final Resource res) throws BuildException {
if (uri == null) {
throw new BuildException( "you have to specify an XMLDB collection URI");
}
log("Get base collection: " + uri, Project.MSG_DEBUG);
try (final Collection base = DatabaseManager.getCollection(uri, user, password)) {
if (base == null ) {
final String msg = "Collection " + uri + " could not be found.";
if (failonerror) {
throw new BuildException(msg);
} else {
log(msg, Project.MSG_ERR);
}
} else {
final UserManagementService service = base.getService(UserManagementService.class);
setPermissions(res, service);
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
protected final void setPermissions(final Collection col ) throws BuildException
{
try {
if( permissions != null ) {
setPermissions( null, col.getService( UserManagementService.class));
}
}
catch( final XMLDBException e ) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if( failonerror ) {
throw( new BuildException( msg, e ) );
} else {
log( msg, e, Project.MSG_ERR );
}
}
}
protected final void setPermissions(@Nullable final Resource res, final UserManagementService service ) throws BuildException
{
try {
if( permissions != null ) {
// if the permissions string matches the Unix Perms Regex, we use a unix style
// permission string approach, otherwise we assume permissions are specified
// in eXist's own syntax (user=+write,...).
if( permissions.matches( UNIX_PERMS_REGEX ) ) {
// Unix-style permissions string provided
final Permission perm = UnixStylePermissionAider.fromString( permissions );
if( res != null ) {
service.chmod( res, perm.getMode() );
} else {
service.chmod( perm.getMode() );
}
} else {
// eXist-style syntax for permission string (eg. user=+write,...)
if( res != null ) {
service.chmod( res, permissions );
} else {
service.chmod( permissions );
}
}
}
}
catch( final XMLDBException e ) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if( failonerror ) {
throw( new BuildException( msg, e ) );
} else {
log( msg, e, Project.MSG_ERR );
}
}
catch( final SyntaxException e ) {
final String msg = "Syntax error in permissions: " + permissions;
if( failonerror ) {
throw( new BuildException( msg, e ) );
} else {
log( msg, e, Project.MSG_ERR );
}
}
}
}