in src/main/java/org/apache/sling/event/impl/jobs/tasks/CleanUpTask.java [278:388]
private void fullEmptyFolderCleanup(final TopologyCapabilities caps, final String basePath) {
this.logger.debug("Cleaning up job resource tree: removing ALL empty folders");
final ResourceResolver resolver = this.configuration.createResourceResolver();
if ( resolver == null ) {
return;
}
try {
final Resource baseResource = resolver.getResource(basePath);
// sanity check - should never be null
if ( baseResource != null ) {
final Calendar now = getCalendarInstance();
final int removeYear = now.get(Calendar.YEAR);
final int removeMonth = now.get(Calendar.MONTH) + 1;
final int removeDay = now.get(Calendar.DAY_OF_MONTH);
final int removeHour = now.get(Calendar.HOUR_OF_DAY);
final Iterator<Resource> topicIter = baseResource.listChildren();
while ( caps.isActive() && topicIter.hasNext() ) {
final Resource topicResource = topicIter.next();
// now years
final Iterator<Resource> yearIter = topicResource.listChildren();
while ( caps.isActive() && yearIter.hasNext() ) {
final Resource yearResource = yearIter.next();
final int year = Integer.valueOf(yearResource.getName());
// we should not have a year higher than "now", but we test anyway
if ( year > removeYear ) {
continue;
}
final boolean oldYear = year < removeYear;
// months
final Iterator<Resource> monthIter = yearResource.listChildren();
while ( caps.isActive() && monthIter.hasNext() ) {
final Resource monthResource = monthIter.next();
final int month = Integer.valueOf(monthResource.getName());
if ( !oldYear && month > removeMonth ) {
continue;
}
final boolean oldMonth = oldYear || month < removeMonth;
// days
final Iterator<Resource> dayIter = monthResource.listChildren();
while ( caps.isActive() && dayIter.hasNext() ) {
final Resource dayResource = dayIter.next();
final int day = Integer.valueOf(dayResource.getName());
if ( !oldMonth && day > removeDay ) {
continue;
}
final boolean oldDay = oldMonth || day < removeDay;
// hours
final Iterator<Resource> hourIter = dayResource.listChildren();
while ( caps.isActive() && hourIter.hasNext() ) {
final Resource hourResource = hourIter.next();
final int hour = Integer.valueOf(hourResource.getName());
if ( !oldDay && hour > removeHour ) {
continue;
}
final boolean oldHour = (oldDay && (oldMonth || removeHour > 0)) || hour < (removeHour -1);
// we only remove minutes if the hour is old
if ( oldHour ) {
final Iterator<Resource> minuteIter = hourResource.listChildren();
while ( caps.isActive() && minuteIter.hasNext() ) {
final Resource minuteResource = minuteIter.next();
// check if we can delete the minute
if ( !minuteResource.listChildren().hasNext() ) {
resolver.delete(minuteResource);
resolver.commit();
}
}
}
// check if we can delete the hour
if ( caps.isActive() && oldHour && !hourResource.listChildren().hasNext()) {
resolver.delete(hourResource);
resolver.commit();
}
}
// check if we can delete the day
if ( caps.isActive() && oldDay && !dayResource.listChildren().hasNext()) {
resolver.delete(dayResource);
resolver.commit();
}
}
// check if we can delete the month
if ( caps.isActive() && oldMonth && !monthResource.listChildren().hasNext() ) {
resolver.delete(monthResource);
resolver.commit();
}
}
// check if we can delete the year
if ( caps.isActive() && oldYear && !yearResource.listChildren().hasNext() ) {
resolver.delete(yearResource);
resolver.commit();
}
}
}
}
} catch (final PersistenceException pe) {
// in the case of an error, we just log this as a warning
this.logger.warn("Exception during job resource tree cleanup.", pe);
} finally {
resolver.close();
}
}