public static AutoCloseable withLockedResources(final CmsObject cms, CmsResource... resources) throws Exception {

        final Map<CmsResource, CmsLockActionRecord> lockMap = Maps.newHashMap();
        Closeable result = new Closeable() {

            @SuppressWarnings("synthetic-access")
            public void close() {

                for (Map.Entry<CmsResource, CmsLockActionRecord> entry : lockMap.entrySet()) {
                    if (entry.getValue().getChange() == LockChange.locked) {
                        CmsResource resourceToUnlock = entry.getKey();
                        // the resource may have been moved, so we read it again to get the correct path
                        try {
                            resourceToUnlock = cms.readResource(entry.getKey().getStructureId(), CmsResourceFilter.ALL);
                        } catch (CmsException e) {
                            LOG.error(e.getLocalizedMessage(), e);
                        }
                        try {
                            cms.unlockResource(resourceToUnlock);
                        } catch (CmsException e) {
                            LOG.warn(e.getLocalizedMessage(), e);
                        }
                    }

                }
            }
        };
        try {
            for (CmsResource resource : resources) {
                CmsLockActionRecord record = ensureLock(cms, resource);
                lockMap.put(resource, record);
            }
        } catch (CmsException e) {
            result.close();
            throw e;
        }
        return result;
    }
public static AutoCloseable withLockedResources ( final CmsObject cms , CmsResource ... resources ) throws Exception { final Map < CmsResource , CmsLockActionRecord > lockMap = Maps . newHashMap ( ) ; Closeable result = new Closeable ( ) { @ SuppressWarnings ( "synthetic-access" ) public void close ( ) { for ( Map . Entry < CmsResource , CmsLockActionRecord > entry : lockMap . entrySet ( ) ) { if ( entry . getValue ( ) . getChange ( ) == LockChange . locked ) { CmsResource resourceToUnlock = entry . getKey ( ) ; try { resourceToUnlock = cms . readResource ( entry . getKey ( ) . getStructureId ( ) , CmsResourceFilter . ALL ) ; } catch ( CmsException e ) { LOG . error ( e . getLocalizedMessage ( ) , e ) ; } try { cms . unlockResource ( resourceToUnlock ) ; } catch ( CmsException e ) { LOG . warn ( e . getLocalizedMessage ( ) , e ) ; } } } } } ; try { for ( CmsResource resource : resources ) { CmsLockActionRecord record = ensureLock ( cms , resource ) ; lockMap . put ( resource , record ) ; } } catch ( CmsException e ) { result . close ( ) ; throw e ; } return result ; }
Utility method for locking and unlocking a set of resources conveniently with the try-with syntax
from Java 1.7.<p>

This method locks a set of resources and returns a Closeable instance that will unlock the locked resources
when its close() method is called.

@param cms the CMS context
@param resources the resources to lock

@return the Closeable used to unlock the resources
@throws Exception if something goes wrong