return file.isDirectory();
}
});
for (File dbDirectory : dbDirectories) {
String dbkey = dbDirectory.getName();
WGDatabase db = _core.getContentdbs().get(dbkey);
if (db == null) {
if (!_core.getWgaConfiguration().hasContentDatabase(dbkey) || !_core.getWgaConfiguration().getContentDatabase(dbkey).isEnabled()) {
if (dbDirectory.exists()) {
_core.getLog().info("Database '" + dbkey + "' is disabled or has been removed. Removing all cached entries from '" + dbDirectory.getAbsolutePath() + "'.");
WGUtils.delTree(dbDirectory);
if (dbDirectory.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + dbDirectory.getAbsolutePath() + "'. Cache might serve protected file data.");
}
}
}
} else if (db.isConnected()) {
try {
db.openSession();
if (db.isSessionOpen()) {
db.getSessionContext().setTask("ExternalFileServingMaintenanceTask");
if (!db.isAnonymousAccessible()) {
if (dbDirectory.exists()) {
_core.getLog().info("Database '" + db.getDbReference() + "' is not accessible by anonymous users. Removing all cached entries from '" + dbDirectory.getAbsolutePath() + "'.");
WGUtils.delTree(dbDirectory);
if (dbDirectory.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + dbDirectory.getAbsolutePath() + "'. Cache might serve protected file data.");
}
}
} else if (!db.getBooleanAttribute(WGACore.DBATTRIB_EXTERNAL_FILE_SERVING_ENABLED, false)) {
if (dbDirectory.exists()) {
_core.getLog().info("External file serving has been disabled for database '" + db.getDbReference() + "'. Removing all cached entries from '" + dbDirectory.getAbsolutePath() + "'.");
WGUtils.delTree(dbDirectory);
if (dbDirectory.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + dbDirectory.getAbsolutePath() + "'. Cache might serve stale file data.");
}
}
} else {
// perform file deletion checks
if (dbDirectory.exists()) {
File[] containers = dbDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
});
for (File container : containers) {
if (container.getName().startsWith("content:")) {
String contentKey = container.getName().substring("content:".length());
WGContent content = db.getContentByKey(contentKey);
if (content == null || !content.getStatus().equals(WGContent.STATUS_RELEASE)) {
// content has been deleted or archived - remove file data
_core.getLog().info("Content '" + contentKey + "' has been deleted or archived. Clearing external file serving cache.");
WGUtils.delTree(container);
if (container.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + container.getAbsolutePath() + "'. Cache might serve stale file data.");
}
} else {
// check general access to content
if (content.getReaders().size() > 0) {
// delete all deployed files of content
_core.getLog().info("Content '" + content.getContentKey() + "' is not accessible by anonymous. Clearing external file serving cache.");
WGUtils.delTree(container);
if (container.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + container.getAbsolutePath() + "'. Cache might serve stale file data.");
}
} else {
// check for existence and size of files
File[] deployedFiles = container.listFiles();
for (File deployedFile : deployedFiles) {
if (deployedFile.isFile()) {
if (!content.hasFile(deployedFile.getName())) {
_core.getLog().info("File '" + deployedFile.getName() + "' has been removed from content '" + content.getContentKey() + "' and will be removed from external file serving cache.");
if (!deployedFile.delete()) {
_core.getLog().warn("Unable to delete external file serving data '" + deployedFile.getAbsolutePath() + "'. File cache might serve stale data.");
}
} else if (deployedFile.length() < config.getThreshold()) {
_core.getLog().info("Deployed file '" + deployedFile.getName() + "' of content '" + content.getContentKey() + "' deceeds current file size threashold and will be removed from external file serving cache.");
if (!deployedFile.delete()) {
_core.getLog().warn("Unable to delete external file serving data '" + deployedFile.getAbsolutePath() + "'. File cache might serve stale data.");
}
}
}
}
}
}
} else if (container.getName().startsWith("filecontainer:")) {
String fileContainerName = container.getName().substring("filecontainer:".length());
WGFileContainer fileContainer = db.getFileContainer(fileContainerName);
if (fileContainer == null) {
// content has been deleted remove file data
_core.getLog().info("File container '" + fileContainerName + "' has been deleted. Clearing external file serving cache.");
WGUtils.delTree(container);
if (container.exists()) {
_core.getLog().warn("External file cache maintenance failed. Unable to remove directory '" + container.getAbsolutePath() + "'. Cache might serve stale file data.");
}
} else {
// check for existence and size of files
File[] deployedFiles = container.listFiles();
for (File deployedFile : deployedFiles) {
if (deployedFile.isFile()) {
if (!fileContainer.hasFile(deployedFile.getName())) {
_core.getLog().info("File '" + deployedFile.getName() + "' has been removed from file container '" + fileContainer.getName() + "' and will be removed from external file serving cache.");
if (!deployedFile.delete()) {
_core.getLog().warn("Unable to delete external file serving data '" + deployedFile.getAbsolutePath() + "'. File cache might serve stale data.");
}
} else if (deployedFile.length() < config.getThreshold()) {
_core.getLog().info("Deployed file '" + deployedFile.getName() + "' of file container '" + fileContainer.getName() + "' deceeds current file size threashold and will be removed from external file serving cache.");
if (!deployedFile.delete()) {
_core.getLog().warn("Unable to delete external file serving data '" + deployedFile.getAbsolutePath() + "'. File cache might serve stale data.");
}
}
}
}
}
}
}
}
}
} else {
_core.getLog().warn("External file serving maintenance failed. Unable to open session on database '" + db.getDbReference() + "'. Cache might serve protected file data.");
}
}
catch (Throwable e) {
_core.getLog().warn("External file serving maintenance failed. Cache might serve protected file data.", e);
} finally {