11/*
2+ * Elemental
3+ * Copyright (C) 2024, Evolved Binary Ltd
4+ *
5+ * admin@evolvedbinary.com
6+ * https://www.evolvedbinary.com | https://www.elemental.xyz
7+ *
8+ * This library is free software; you can redistribute it and/or
9+ * modify it under the terms of the GNU Lesser General Public
10+ * License as published by the Free Software Foundation; version 2.1.
11+ *
12+ * This library is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * Lesser General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with this library; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+ *
21+ * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+ * The original license header is included below.
23+ *
24+ * =====================================================================
25+ *
226 * eXist-db Open Source Native XML Database
327 * Copyright (C) 2001 The eXist-db Authors
428 *
3761import org .xmldb .api .base .XMLDBException ;
3862import org .xmldb .api .modules .CollectionManagementService ;
3963
64+ import javax .annotation .Nullable ;
4065import java .util .StringTokenizer ;
4166
4267
@@ -186,79 +211,67 @@ protected void registerDatabase() throws BuildException
186211 }
187212
188213
189- protected final Collection mkcol (final Collection rootCollection , final String baseURI , String path , final String relPath ) throws XMLDBException
190- {
191- CollectionManagementService mgtService ;
192- Collection current = rootCollection ;
193- Collection collection ;
194- String token ;
195-
196- ///TODO : use dedicated function in XmldbURI
197- final StringTokenizer tokenizer = new StringTokenizer ( relPath , "/" );
198-
199- while ( tokenizer .hasMoreTokens () ) {
200-
201- token = tokenizer .nextToken ();
214+ protected final Collection mkcol (Collection collection , final String baseURI , String path , final String relPath ) throws XMLDBException {
215+ final StringTokenizer tokenizer = new StringTokenizer (relPath , "/" );
202216
203- if ( path != null ) {
217+ while (tokenizer .hasMoreTokens ()) {
218+ final String token = tokenizer .nextToken ();
219+ if (path != null ) {
204220 path = path + "/" + token ;
205221 } else {
206222 path = "/" + token ;
207223 }
208224
209- log ( "Get collection " + baseURI + path , Project .MSG_DEBUG );
210- collection = DatabaseManager .getCollection ( baseURI + path , user , password );
211-
212- if ( collection == null ) {
213- log ( "Create collection management service for collection " + current .getName (), Project .MSG_DEBUG );
214- mgtService = current .getService ( CollectionManagementService .class );
215- log ( "Create child collection " + token , Project .MSG_DEBUG );
216- current = mgtService .createCollection ( token );
217- log ( "Created collection " + current .getName () + '.' , Project .MSG_DEBUG );
225+ Collection child = DatabaseManager .getCollection (baseURI + path , user , password );
226+ if (child == null ) {
227+ final CollectionManagementService mgtService = collection .getService (CollectionManagementService .class );
228+ log ("Create child collection " + token , Project .MSG_DEBUG );
229+ child = mgtService .createCollection (token );
230+ log ("Created collection " + child .getName () + '.' , Project .MSG_DEBUG );
231+ }
218232
219- } else {
220- current = collection ;
233+ try {
234+ // close the parent collection
235+ collection .close ();
236+ } catch (final XMLDBException e ) {
237+ // no-op
221238 }
239+
240+ collection = child ;
222241 }
223- return ( current );
242+
243+ return collection ;
224244 }
225245
226246
227- protected final void setPermissions (final Resource res ) throws BuildException
228- {
229- Collection base = null ;
230- UserManagementService service = null ;
231-
232- if ( uri == null ) {
233- throw ( new BuildException ( "you have to specify an XMLDB collection URI" ) );
247+ protected final void setPermissions (final Resource res ) throws BuildException {
248+ if (uri == null ) {
249+ throw new BuildException ( "you have to specify an XMLDB collection URI" );
234250 }
235251
236- try {
237- log ( "Get base collection: " + uri , Project .MSG_DEBUG );
238- base = DatabaseManager .getCollection ( uri , user , password );
252+ log ("Get base collection: " + uri , Project .MSG_DEBUG );
253+ try (final Collection base = DatabaseManager .getCollection (uri , user , password )) {
239254
240- if ( base == null ) {
255+ if ( base == null ) {
241256 final String msg = "Collection " + uri + " could not be found." ;
242257
243- if ( failonerror ) {
244- throw ( new BuildException ( msg ) );
258+ if ( failonerror ) {
259+ throw new BuildException (msg );
245260 } else {
246- log ( msg , Project .MSG_ERR );
261+ log (msg , Project .MSG_ERR );
247262 }
248263 } else {
249- service = base .getService ( UserManagementService .class );
250-
251- setPermissions ( res , service );
264+ final UserManagementService service = base .getService (UserManagementService .class );
265+ setPermissions (res , service );
252266 }
253267
254- }
255- catch ( final XMLDBException e ) {
268+ } catch (final XMLDBException e ) {
256269 final String msg = "XMLDB exception caught: " + e .getMessage ();
257270
258- if ( failonerror ) {
259- throw ( new BuildException ( msg , e ) );
271+ if ( failonerror ) {
272+ throw new BuildException (msg , e );
260273 } else {
261- log ( msg , e , Project .MSG_ERR );
274+ log (msg , e , Project .MSG_ERR );
262275 }
263276 }
264277 }
@@ -283,7 +296,7 @@ protected final void setPermissions(final Collection col ) throws BuildException
283296 }
284297
285298
286- protected final void setPermissions (final Resource res , final UserManagementService service ) throws BuildException
299+ protected final void setPermissions (@ Nullable final Resource res , final UserManagementService service ) throws BuildException
287300 {
288301 try {
289302 if ( permissions != null ) {
0 commit comments