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
@@ -188,79 +213,67 @@ protected void registerDatabase() throws BuildException
188213 }
189214
190215
191- protected final Collection mkcol (final Collection rootCollection , final String baseURI , String path , final String relPath ) throws XMLDBException
192- {
193- CollectionManagementService mgtService ;
194- Collection current = rootCollection ;
195- Collection collection ;
196- String token ;
197-
198- ///TODO : use dedicated function in XmldbURI
199- final StringTokenizer tokenizer = new StringTokenizer ( relPath , "/" );
200-
201- while ( tokenizer .hasMoreTokens () ) {
202-
203- token = tokenizer .nextToken ();
216+ protected final Collection mkcol (Collection collection , final String baseURI , String path , final String relPath ) throws XMLDBException {
217+ final StringTokenizer tokenizer = new StringTokenizer (relPath , "/" );
204218
205- if ( path != null ) {
219+ while (tokenizer .hasMoreTokens ()) {
220+ final String token = tokenizer .nextToken ();
221+ if (path != null ) {
206222 path = path + "/" + token ;
207223 } else {
208224 path = "/" + token ;
209225 }
210226
211- log ( "Get collection " + baseURI + path , Project .MSG_DEBUG );
212- collection = DatabaseManager .getCollection ( baseURI + path , user , password );
213-
214- if ( collection == null ) {
215- log ( "Create collection management service for collection " + current .getName (), Project .MSG_DEBUG );
216- mgtService = (CollectionManagementService )current .getService ( "CollectionManagementService" , "1.0" );
217- log ( "Create child collection " + token , Project .MSG_DEBUG );
218- current = mgtService .createCollection ( token );
219- log ( "Created collection " + current .getName () + '.' , Project .MSG_DEBUG );
227+ Collection child = DatabaseManager .getCollection (baseURI + path , user , password );
228+ if (child == null ) {
229+ final CollectionManagementService mgtService = (CollectionManagementService ) collection .getService ("CollectionManagementService" , "1.0" );
230+ log ("Create child collection " + token , Project .MSG_DEBUG );
231+ child = mgtService .createCollection (token );
232+ log ("Created collection " + child .getName () + '.' , Project .MSG_DEBUG );
233+ }
220234
221- } else {
222- current = collection ;
235+ try {
236+ // close the parent collection
237+ collection .close ();
238+ } catch (final XMLDBException e ) {
239+ // no-op
223240 }
241+
242+ collection = child ;
224243 }
225- return ( current );
244+
245+ return collection ;
226246 }
227247
228248
229- protected final void setPermissions (final Resource res ) throws BuildException
230- {
231- Collection base = null ;
232- UserManagementService service = null ;
233-
234- if ( uri == null ) {
235- throw ( new BuildException ( "you have to specify an XMLDB collection URI" ) );
249+ protected final void setPermissions (final Resource res ) throws BuildException {
250+ if (uri == null ) {
251+ throw new BuildException ( "you have to specify an XMLDB collection URI" );
236252 }
237253
238- try {
239- log ( "Get base collection: " + uri , Project .MSG_DEBUG );
240- base = DatabaseManager .getCollection ( uri , user , password );
254+ log ("Get base collection: " + uri , Project .MSG_DEBUG );
255+ try (final Collection base = DatabaseManager .getCollection (uri , user , password )) {
241256
242- if ( base == null ) {
257+ if ( base == null ) {
243258 final String msg = "Collection " + uri + " could not be found." ;
244259
245- if ( failonerror ) {
246- throw ( new BuildException ( msg ) );
260+ if ( failonerror ) {
261+ throw new BuildException (msg );
247262 } else {
248- log ( msg , Project .MSG_ERR );
263+ log (msg , Project .MSG_ERR );
249264 }
250265 } else {
251- service = (UserManagementService )base .getService ( "UserManagementService" , "1.0" );
252-
253- setPermissions ( res , service );
266+ final UserManagementService service = (UserManagementService ) base .getService ( "UserManagementService" , "1.0" );
267+ setPermissions (res , service );
254268 }
255269
256- }
257- catch ( final XMLDBException e ) {
270+ } catch (final XMLDBException e ) {
258271 final String msg = "XMLDB exception caught: " + e .getMessage ();
259272
260- if ( failonerror ) {
261- throw ( new BuildException ( msg , e ) );
273+ if ( failonerror ) {
274+ throw new BuildException (msg , e );
262275 } else {
263- log ( msg , e , Project .MSG_ERR );
276+ log (msg , e , Project .MSG_ERR );
264277 }
265278 }
266279 }
@@ -285,7 +298,7 @@ protected final void setPermissions(final Collection col ) throws BuildException
285298 }
286299
287300
288- protected final void setPermissions (final Resource res , final UserManagementService service ) throws BuildException
301+ protected final void setPermissions (@ Nullable final Resource res , final UserManagementService service ) throws BuildException
289302 {
290303 try {
291304 if ( permissions != null ) {
0 commit comments