Skip to content

Commit 37ce315

Browse files
authored
Merge pull request eXist-db#4220 from reinhapa/xmldb-api-next
[upgrade] eXist changes for next XML:DB API version
2 parents b691bd2 + 0fd1c03 commit 37ce315

File tree

199 files changed

+3298
-2757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+3298
-2757
lines changed

exist-ant/src/main/java/org/exist/ant/AbstractXMLDBTask.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ protected void registerDatabase() throws BuildException
159159
{
160160
try {
161161
log( "Registering database", Project.MSG_DEBUG );
162-
final Database[] allDataBases = DatabaseManager.getDatabases();
163-
164-
for( final Database database : allDataBases ) {
162+
for( final Database database : DatabaseManager.getDatabases() ) {
165163

166164
if( database.acceptsURI( uri ) ) {
167165
return;
@@ -213,7 +211,7 @@ protected final Collection mkcol(final Collection rootCollection, final String b
213211

214212
if( collection == null ) {
215213
log( "Create collection management service for collection " + current.getName(), Project.MSG_DEBUG );
216-
mgtService = (CollectionManagementService)current.getService( "CollectionManagementService", "1.0" );
214+
mgtService = current.getService( CollectionManagementService.class );
217215
log( "Create child collection " + token, Project.MSG_DEBUG );
218216
current = mgtService.createCollection( token );
219217
log( "Created collection " + current.getName() + '.', Project.MSG_DEBUG );
@@ -248,7 +246,7 @@ protected final void setPermissions(final Resource res ) throws BuildException
248246
log( msg, Project.MSG_ERR );
249247
}
250248
} else {
251-
service = (UserManagementService)base.getService( "UserManagementService", "1.0" );
249+
service = base.getService( UserManagementService.class);
252250

253251
setPermissions( res, service );
254252
}
@@ -270,7 +268,7 @@ protected final void setPermissions(final Collection col ) throws BuildException
270268
{
271269
try {
272270
if( permissions != null ) {
273-
setPermissions( null, (UserManagementService)col.getService( "UserManagementService", "1.0" ) );
271+
setPermissions( null, col.getService( UserManagementService.class));
274272
}
275273
}
276274
catch( final XMLDBException e ) {

exist-ant/src/main/java/org/exist/ant/RestoreTask.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
*/
4444
public class RestoreTask extends AbstractXMLDBTask
4545
{
46-
private Path zipFile = null;
47-
private Path dir = null;
48-
private DirSet dirSet = null;
49-
private String restorePassword = null;
50-
private boolean overwriteApps = false;
46+
private Path zipFile;
47+
private Path dir;
48+
private DirSet dirSet;
49+
private String restorePassword;
50+
private boolean overwriteApps;
5151

5252
@Override
5353
public void execute() throws BuildException
@@ -61,7 +61,7 @@ public void execute() throws BuildException
6161
}
6262

6363
if( ( dir != null ) && !Files.isReadable(dir)) {
64-
final String msg = "Cannot read restore file: " + dir.toAbsolutePath().toString();
64+
final String msg = "Cannot read restore file: " + dir.toAbsolutePath();
6565

6666
if( failonerror ) {
6767
throw( new BuildException( msg ) );
@@ -75,11 +75,11 @@ public void execute() throws BuildException
7575
try {
7676

7777
if( dir != null ) {
78-
log( "Restoring from " + dir.toAbsolutePath().toString(), Project.MSG_INFO );
78+
log( "Restoring from " + dir.toAbsolutePath(), Project.MSG_INFO );
7979
final Path file = dir.resolve("__contents__.xml" );
8080

8181
if( !Files.exists(file)) {
82-
final String msg = "Could not find file " + file.toAbsolutePath().toString();
82+
final String msg = "Could not find file " + file.toAbsolutePath();
8383

8484
if( failonerror ) {
8585
throw( new BuildException( msg ) );
@@ -89,7 +89,7 @@ public void execute() throws BuildException
8989
} else {
9090
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
9191
final Collection collection = DatabaseManager.getCollection(uri, user, password);
92-
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
92+
final EXistRestoreService service = collection.getService(EXistRestoreService.class);
9393
service.restore(file.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
9494
}
9595

@@ -104,29 +104,29 @@ public void execute() throws BuildException
104104
final Path contentsFile = dir.resolve("__contents__.xml");
105105

106106
if( !Files.exists(contentsFile)) {
107-
final String msg = "Did not found file " + contentsFile.toAbsolutePath().toString();
107+
final String msg = "Did not found file " + contentsFile.toAbsolutePath();
108108

109109
if( failonerror ) {
110110
throw( new BuildException( msg ) );
111111
} else {
112112
log( msg, Project.MSG_ERR );
113113
}
114114
} else {
115-
log( "Restoring from " + contentsFile.toAbsolutePath().toString() + " ...\n" );
115+
log( "Restoring from " + contentsFile.toAbsolutePath() + " ...\n" );
116116

117117
// TODO subdirectories as sub-collections?
118118
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
119119
final Collection collection = DatabaseManager.getCollection(uri, user, password);
120-
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
120+
final EXistRestoreService service = collection.getService(EXistRestoreService.class);
121121
service.restore(contentsFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
122122
}
123123
}
124124

125125
} else if( zipFile != null ) {
126-
log( "Restoring from " + zipFile.toAbsolutePath().toString(), Project.MSG_INFO );
126+
log( "Restoring from " + zipFile.toAbsolutePath(), Project.MSG_INFO );
127127

128128
if( !Files.exists(zipFile)) {
129-
final String msg = "File not found: " + zipFile.toAbsolutePath().toString();
129+
final String msg = "File not found: " + zipFile.toAbsolutePath();
130130

131131
if( failonerror ) {
132132
throw( new BuildException( msg ) );
@@ -136,7 +136,7 @@ public void execute() throws BuildException
136136
} else {
137137
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
138138
final Collection collection = DatabaseManager.getCollection(uri, user, password);
139-
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
139+
final EXistRestoreService service = collection.getService(EXistRestoreService.class);
140140
service.restore(zipFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
141141
}
142142
}
@@ -184,4 +184,9 @@ public void setRestorePassword(final String pass )
184184
{
185185
this.restorePassword = pass;
186186
}
187+
188+
public void setOverwriteApps(final boolean overwriteApps)
189+
{
190+
this.overwriteApps = overwriteApps;
191+
}
187192
}

exist-ant/src/main/java/org/exist/ant/UserTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void execute() throws BuildException
6464
log( msg, Project.MSG_ERR );
6565
}
6666
} else {
67-
service = (UserManagementService)base.getService( "UserManagementService", "1.0" );
67+
service = base.getService( UserManagementService.class);
6868
}
6969

7070
}

exist-ant/src/main/java/org/exist/ant/XMLDBCopyTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void execute() throws BuildException
7373

7474
} else {
7575
log( "Create collection management service for collection " + base.getName(), Project.MSG_DEBUG );
76-
final EXistCollectionManagementService service = (EXistCollectionManagementService)base.getService( "CollectionManagementService", "1.0" );
76+
final EXistCollectionManagementService service = base.getService(EXistCollectionManagementService.class);
7777

7878
if( resource != null ) {
7979
log( "Copying resource: " + resource, Project.MSG_INFO );

exist-ant/src/main/java/org/exist/ant/XMLDBCreateTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private Collection mkcol(final Collection root, final String base, /*String path
136136

137137
if( c == null ) {
138138
log( "Create collection management service for collection " + current.getName(), Project.MSG_DEBUG );
139-
mgtService = (CollectionManagementService)current.getService( "CollectionManagementService", "1.0" );
139+
mgtService = current.getService( CollectionManagementService.class);
140140
log( "Create child collection " + segment );
141141
current = mgtService.createCollection( segment.toString() );
142142
log( "Created collection " + current.getName() + '.' );

exist-ant/src/main/java/org/exist/ant/XMLDBExtractTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.*;
3737
import java.nio.file.Files;
3838
import java.nio.file.Path;
39+
import java.util.List;
3940
import java.util.Properties;
4041

4142
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -133,8 +134,8 @@ public void execute() throws BuildException {
133134
* @throws IOException if an I/O error occurs
134135
*/
135136
private void extractResources(final Collection base, final String path) throws XMLDBException, IOException {
136-
final String[] resources = base.listResources();
137-
if (resources != null) {
137+
final List<String> resources = base.listResources();
138+
if (!resources.isEmpty()) {
138139
Path dir = destDir;
139140

140141
log("Extracting to directory " + destDir.toAbsolutePath().toString(), Project.MSG_DEBUG);
@@ -169,9 +170,8 @@ private void extractResources(final Collection base, final String path) throws X
169170
* @throws IOException if an I/O error occurs
170171
*/
171172
private void extractSubCollections(final Collection base, final String path) throws XMLDBException, IOException {
172-
final String[] childCols = base.listChildCollections();
173-
174-
if (childCols != null) {
173+
final List<String> childCols = base.listChildCollections();
174+
if (!childCols.isEmpty()) {
175175
for (final String childCol : childCols) {
176176
final Collection col = base.getChildCollection(childCol);
177177

exist-ant/src/main/java/org/exist/ant/XMLDBListTask.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.xmldb.api.base.Collection;
2828
import org.xmldb.api.base.XMLDBException;
2929

30+
import java.util.List;
31+
3032

3133
/**
3234
* an ant task to list the sub-collections or resources in a collection.
@@ -66,15 +68,15 @@ public void execute() throws BuildException
6668
throw( new BuildException( msg ) );
6769
} else {
6870
log( msg, Project.MSG_ERR );
71+
return;
6972
}
7073
}
7174

7275
final StringBuilder buffer = new StringBuilder();
7376

7477
if( hasCollections ) {
75-
final String[] childCollections = base.listChildCollections();
76-
77-
if( childCollections != null ) {
78+
final List<String> childCollections = base.listChildCollections();
79+
if( !childCollections.isEmpty() ) {
7880
log( "Listing child collections", Project.MSG_DEBUG );
7981
boolean isFirst = true;
8082

@@ -94,9 +96,8 @@ public void execute() throws BuildException
9496

9597
if( hasResources ) {
9698
log( "Listing resources", Project.MSG_DEBUG );
97-
final String[] resources = base.listResources();
98-
99-
if( resources != null ) {
99+
final List<String> resources = base.listResources();
100+
if( !resources.isEmpty() ) {
100101

101102
if( buffer.length() > 0 ) {
102103
buffer.append( separator );

exist-ant/src/main/java/org/exist/ant/XMLDBMoveTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void execute() throws BuildException
7373
}
7474

7575
log( "Create collection management service for collection " + base.getName(), Project.MSG_DEBUG );
76-
final EXistCollectionManagementService service = (EXistCollectionManagementService)base.getService( "CollectionManagementService", "1.0" );
76+
final EXistCollectionManagementService service = base.getService(EXistCollectionManagementService.class);
7777

7878
if( resource != null ) {
7979
log( "Moving resource: " + resource, Project.MSG_INFO );

exist-ant/src/main/java/org/exist/ant/XMLDBRemoveTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void execute() throws BuildException
8282

8383
} else {
8484
log( "Removing collection: " + collection, Project.MSG_INFO );
85-
final CollectionManagementService service = (CollectionManagementService)base.getService( "CollectionManagementService", "1.0" );
85+
final CollectionManagementService service = base.getService(CollectionManagementService.class);
8686
service.removeCollection( collection );
8787
}
8888

exist-ant/src/main/java/org/exist/ant/XMLDBShutdownTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void execute() throws BuildException
6464
}
6565

6666
} else {
67-
final DatabaseInstanceManager mgr = (DatabaseInstanceManager)root.getService( "DatabaseInstanceManager", "1.0" );
67+
final DatabaseInstanceManager mgr = root.getService(DatabaseInstanceManager.class);
6868
log( "Shutdown database instance", Project.MSG_INFO );
6969
mgr.shutdown();
7070
}

exist-ant/src/main/java/org/exist/ant/XMLDBStoreTask.java

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -123,63 +123,55 @@ public void execute() throws BuildException {
123123
}
124124

125125
} else {
126-
Resource res;
127126
Collection col = root;
128127
String relDir;
129128
String prevDir = null;
130129

131130
if (srcFile != null) {
132131
log("Storing " + srcFile.getName());
133132

134-
MimeType mime = getMimeTable().getContentTypeFor(srcFile.getName());
135133
final String baseMimeType;
136-
137134
if (forceMimeType != null) {
138135
baseMimeType = forceMimeType;
139-
140-
} else if (mime != null) {
141-
baseMimeType = mime.getName();
142-
143136
} else {
144-
baseMimeType = defaultMimeType;
145-
}
146-
147-
if (type != null) {
148-
149-
if ("xml".equals(type)) {
150-
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.XML)) : MimeType.XML_TYPE;
151-
} else if ("binary".equals(type)) {
152-
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
137+
final MimeType fileMime = getMimeTable().getContentTypeFor(srcFile.getName());
138+
if (fileMime != null) {
139+
baseMimeType = fileMime.getName();
140+
} else {
141+
baseMimeType = defaultMimeType;
153142
}
154143
}
155144

156-
// single file
157-
if (mime == null) {
145+
final MimeType mime;
146+
if ("xml".equals(type)) {
147+
mime = (baseMimeType != null) ? new MimeType(baseMimeType, MimeType.XML) : MimeType.XML_TYPE;
148+
} else if ("binary".equals(type)) {
149+
mime = (baseMimeType != null) ? new MimeType(baseMimeType, MimeType.BINARY) : MimeType.BINARY_TYPE;
150+
} else {
158151
final String msg = "Cannot guess mime-type kind for " + srcFile.getName() + ". Treating it as a binary.";
159152
log(msg, Project.MSG_ERR);
160-
mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
153+
mime = (baseMimeType != null) ? new MimeType(baseMimeType, MimeType.BINARY) : MimeType.BINARY_TYPE;
161154
}
162155

163-
final String resourceType = mime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
164-
165156
if (targetFile == null) {
166157
targetFile = srcFile.getName();
167158
}
168159

169160
try {
170-
log("Creating resource " + targetFile + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + mime.getName(), Project.MSG_DEBUG);
171-
res = col.createResource(targetFile, resourceType);
172-
173-
if (srcFile.length() == 0) {
174-
// note: solves bug id 2429889 when this task hits empty files
175-
} else {
176-
res.setContent(srcFile);
177-
((EXistResource) res).setMimeType(mime.getName());
178-
col.storeResource(res);
179-
}
161+
final Class<? extends Resource> resourceType = mime.isXMLType() ? XMLResource.class : BinaryResource.class;
162+
log("Creating resource " + targetFile + " in collection " + col.getName() + " of type " + resourceType.getName() + " with mime-type: " + mime.getName(), Project.MSG_DEBUG);
163+
try (Resource res = col.createResource(targetFile, resourceType)) {
164+
if (srcFile.length() == 0) {
165+
// note: solves bug id 2429889 when this task hits empty files
166+
} else {
167+
res.setContent(srcFile);
168+
((EXistResource) res).setMimeType(mime.getName());
169+
col.storeResource(res);
170+
}
180171

181-
if (permissions != null) {
182-
setPermissions(res);
172+
if (permissions != null) {
173+
setPermissions(res);
174+
}
183175
}
184176
} catch (final XMLDBException e) {
185177
final String msg = "XMLDB exception caught: " + e.getMessage();
@@ -303,17 +295,17 @@ public void execute() throws BuildException {
303295
currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
304296
}
305297

306-
final String resourceType = currentMime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
307-
log("Creating resource " + file.getName() + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + currentMime.getName(), Project.MSG_DEBUG);
308-
res = col.createResource(file.getName(), resourceType);
309-
res.setContent(file);
310-
((EXistResource) res).setMimeType(currentMime.getName());
311-
col.storeResource(res);
298+
final Class<? extends Resource> resourceType = currentMime.isXMLType() ? XMLResource.class : BinaryResource.class;
299+
log("Creating resource " + file.getName() + " in collection " + col.getName() + " of type " + resourceType.getName() + " with mime-type: " + currentMime.getName(), Project.MSG_DEBUG);
300+
try (Resource res = col.createResource(file.getName(), resourceType)) {
301+
res.setContent(file);
302+
((EXistResource) res).setMimeType(currentMime.getName());
303+
col.storeResource(res);
312304

313-
if (permissions != null) {
314-
setPermissions(res);
305+
if (permissions != null) {
306+
setPermissions(res);
307+
}
315308
}
316-
317309
} catch (final XMLDBException e) {
318310
final String msg = "XMLDB exception caught: " + e.getMessage();
319311

0 commit comments

Comments
 (0)