From 933f25dffbc74ce15a0b1b680af6f441d9dcf4c5 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 08 Oct 2015 09:20:45 +0000
Subject: [PATCH] JEStorage.java, PDBStorage.java: Removed duplicated code and extracted it in StorageUtils.java
---
opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEStorage.java | 201 ++++++++-----------------------------------------
1 files changed, 35 insertions(+), 166 deletions(-)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEStorage.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEStorage.java
index c951c66..1ef542b 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEStorage.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEStorage.java
@@ -30,8 +30,8 @@
import static org.forgerock.util.Utils.*;
import static org.opends.messages.BackendMessages.*;
-import static org.opends.messages.ConfigMessages.*;
import static org.opends.messages.UtilityMessages.*;
+import static org.opends.server.backends.pluggable.spi.StorageUtils.*;
import static org.opends.server.util.StaticUtils.*;
import java.io.File;
@@ -73,6 +73,7 @@
import org.opends.server.backends.pluggable.spi.Storage;
import org.opends.server.backends.pluggable.spi.StorageRuntimeException;
import org.opends.server.backends.pluggable.spi.StorageStatus;
+import org.opends.server.backends.pluggable.spi.StorageUtils;
import org.opends.server.backends.pluggable.spi.TreeName;
import org.opends.server.backends.pluggable.spi.UpdateFunction;
import org.opends.server.backends.pluggable.spi.WriteOperation;
@@ -84,7 +85,6 @@
import org.opends.server.types.BackupConfig;
import org.opends.server.types.BackupDirectory;
import org.opends.server.types.DirectoryException;
-import org.opends.server.types.FilePermission;
import org.opends.server.types.RestoreConfig;
import org.opends.server.util.BackupManager;
@@ -594,7 +594,7 @@
JEStorage(final JEBackendCfg cfg, ServerContext serverContext) throws ConfigException
{
this.serverContext = serverContext;
- backendDirectory = new File(getFileForPath(cfg.getDBDirectory()), cfg.getBackendId());
+ backendDirectory = getBackendDirectory(cfg);
config = cfg;
cfg.addJEChangeListener(this);
}
@@ -706,7 +706,7 @@
private void open0() throws ConfigException
{
- setupStorageFiles();
+ setupStorageFiles(backendDirectory, config.getDBDirectoryPermissions(), config.dn());
try
{
if (env != null)
@@ -722,12 +722,7 @@
{
throw new StorageRuntimeException(e);
}
- diskMonitor.registerMonitoredDirectory(
- config.getBackendId() + " backend",
- getDirectory(),
- config.getDiskLowThreshold(),
- config.getDiskFullThreshold(),
- this);
+ registerMonitoredDirectory(config);
}
@Override
@@ -840,8 +835,12 @@
@Override
public File getDirectory()
{
- File parentDir = getFileForPath(config.getDBDirectory());
- return new File(parentDir, config.getBackendId());
+ return getBackendDirectory(config);
+ }
+
+ private static File getBackendDirectory(JEBackendCfg cfg)
+ {
+ return getDBDirectory(cfg.getDBDirectory(), cfg.getBackendId());
}
@Override
@@ -988,6 +987,8 @@
/**
* Creates the filter for log files that are newer than provided file name
* or equal to provided file name and of larger size.
+ * @param latestFilename the latest file name
+ * @param latestFileSize the latest file size
*/
JELogFileFilter(String latestFilename, long latestFileSize)
{
@@ -1097,7 +1098,7 @@
* Checks newly created backend has a valid configuration.
* @param cfg the new configuration
* @param unacceptableReasons the list of accumulated errors and their messages
- * @param context TODO
+ * @param context the server context
* @return true if newly created backend has a valid configuration
*/
static boolean isConfigurationAcceptable(JEBackendCfg cfg, List<LocalizableMessage> unacceptableReasons,
@@ -1126,11 +1127,10 @@
List<LocalizableMessage> unacceptableReasons)
{
final ConfigChangeResult ccr = new ConfigChangeResult();
- File parentDirectory = getFileForPath(cfg.getDBDirectory());
- File newBackendDirectory = new File(parentDirectory, cfg.getBackendId());
+ File newBackendDirectory = getBackendDirectory(cfg);
checkDBDirExistsOrCanCreate(newBackendDirectory, ccr, true);
- checkDBDirPermissions(cfg, ccr);
+ checkDBDirPermissions(cfg.getDBDirectoryPermissions(), cfg.dn(), ccr);
if (!ccr.getMessages().isEmpty())
{
unacceptableReasons.addAll(ccr.getMessages());
@@ -1139,96 +1139,6 @@
return true;
}
- /**
- * Checks a directory exists or can actually be created.
- *
- * @param backendDir the directory to check for
- * @param ccr the list of reasons to return upstream or null if called from setupStorage()
- * @param cleanup true if the directory should be deleted after creation
- */
- private static void checkDBDirExistsOrCanCreate(File backendDir, ConfigChangeResult ccr, boolean cleanup)
- {
- if (!backendDir.exists())
- {
- if(!backendDir.mkdirs())
- {
- addErrorMessage(ccr, ERR_CREATE_FAIL.get(backendDir.getPath()));
- }
- if (cleanup)
- {
- backendDir.delete();
- }
- }
- else if (!backendDir.isDirectory())
- {
- addErrorMessage(ccr, ERR_DIRECTORY_INVALID.get(backendDir.getPath()));
- }
- }
-
- /**
- * Returns false if directory permissions in the configuration are invalid. Otherwise returns the
- * same value as it was passed in.
- *
- * @param cfg a (possibly new) backend configuration
- * @param ccr the current list of change results
- * @throws forwards a file exception
- */
- private static void checkDBDirPermissions(JEBackendCfg cfg, ConfigChangeResult ccr)
- {
- try
- {
- FilePermission backendPermission = decodeDBDirPermissions(cfg);
- // Make sure the mode will allow the server itself access to the database
- if(!backendPermission.isOwnerWritable() ||
- !backendPermission.isOwnerReadable() ||
- !backendPermission.isOwnerExecutable())
- {
- addErrorMessage(ccr, ERR_CONFIG_BACKEND_INSANE_MODE.get(cfg.getDBDirectoryPermissions()));
- }
- }
- catch(ConfigException ce)
- {
- addErrorMessage(ccr, ce.getMessageObject());
- }
- }
-
- /**
- * Sets files permissions on the backend directory.
- *
- * @param backendDir the directory to setup
- * @param curCfg a backend configuration
- */
- private void setDBDirPermissions(JEBackendCfg curCfg, File backendDir) throws ConfigException
- {
- FilePermission backendPermission = decodeDBDirPermissions(curCfg);
-
- // Get the backend database backendDirectory permissions and apply
- try
- {
- if(!FilePermission.setPermissions(backendDir, backendPermission))
- {
- logger.warn(WARN_UNABLE_SET_PERMISSIONS, backendPermission, backendDir);
- }
- }
- catch(Exception e)
- {
- // Log an warning that the permissions were not set.
- logger.warn(WARN_SET_PERMISSIONS_FAILED, backendDir, e);
- }
- }
-
- private static FilePermission decodeDBDirPermissions(JEBackendCfg curCfg) throws ConfigException
- {
- try
- {
- return FilePermission.decodeUNIXMode(curCfg.getDBDirectoryPermissions());
- }
- catch (Exception e)
- {
- throw new ConfigException(ERR_CONFIG_BACKEND_MODE_INVALID.get(curCfg.dn()));
- }
- }
-
@Override
public ConfigChangeResult applyConfigurationChange(JEBackendCfg cfg)
{
@@ -1236,11 +1146,10 @@
try
{
- File parentDirectory = getFileForPath(cfg.getDBDirectory());
- File newBackendDirectory = new File(parentDirectory, cfg.getBackendId());
+ File newBackendDirectory = getBackendDirectory(cfg);
// Create the directory if it doesn't exist.
- if(!cfg.getDBDirectory().equals(config.getDBDirectory()))
+ if (!cfg.getDBDirectory().equals(config.getDBDirectory()))
{
checkDBDirExistsOrCanCreate(newBackendDirectory, ccr, false);
if (!ccr.getMessages().isEmpty())
@@ -1255,20 +1164,19 @@
if (!cfg.getDBDirectoryPermissions().equalsIgnoreCase(config.getDBDirectoryPermissions())
|| !cfg.getDBDirectory().equals(config.getDBDirectory()))
{
- checkDBDirPermissions(cfg, ccr);
+ checkDBDirPermissions(cfg.getDBDirectoryPermissions(), cfg.dn(), ccr);
if (!ccr.getMessages().isEmpty())
{
return ccr;
}
- setDBDirPermissions(cfg, newBackendDirectory);
+ setDBDirPermissions(newBackendDirectory, cfg.getDBDirectoryPermissions(), cfg.dn(), ccr);
+ if (!ccr.getMessages().isEmpty())
+ {
+ return ccr;
+ }
}
- diskMonitor.registerMonitoredDirectory(
- config.getBackendId() + " backend",
- getDirectory(),
- cfg.getDiskLowThreshold(),
- cfg.getDiskFullThreshold(),
- this);
+ registerMonitoredDirectory(cfg);
config = cfg;
}
catch (Exception e)
@@ -1278,55 +1186,20 @@
return ccr;
}
- private static void addErrorMessage(final ConfigChangeResult ccr, LocalizableMessage message)
+ private void registerMonitoredDirectory(JEBackendCfg cfg)
{
- ccr.setResultCode(DirectoryServer.getServerErrorResultCode());
- ccr.addMessage(message);
- }
-
- private void setupStorageFiles() throws ConfigException
- {
- ConfigChangeResult ccr = new ConfigChangeResult();
-
- checkDBDirExistsOrCanCreate(backendDirectory, ccr, false);
- if (!ccr.getMessages().isEmpty())
- {
- throw new ConfigException(ccr.getMessages().get(0));
- }
- checkDBDirPermissions(config, ccr);
- if (!ccr.getMessages().isEmpty())
- {
- throw new ConfigException(ccr.getMessages().get(0));
- }
- setDBDirPermissions(config, backendDirectory);
+ diskMonitor.registerMonitoredDirectory(
+ cfg.getBackendId() + " backend",
+ getDirectory(),
+ cfg.getDiskLowThreshold(),
+ cfg.getDiskFullThreshold(),
+ this);
}
@Override
public void removeStorageFiles() throws StorageRuntimeException
{
- if (!backendDirectory.exists())
- {
- return;
- }
-
- if (!backendDirectory.isDirectory())
- {
- throw new StorageRuntimeException(ERR_DIRECTORY_INVALID.get(backendDirectory.getPath()).toString());
- }
-
- try
- {
- File[] files = backendDirectory.listFiles();
- for (File f : files)
- {
- f.delete();
- }
- }
- catch (Exception e)
- {
- logger.traceException(e);
- throw new StorageRuntimeException(ERR_REMOVE_FAIL.get(e.getMessage()).toString(), e);
- }
+ StorageUtils.removeStorageFiles(backendDirectory);
}
@Override
@@ -1337,16 +1210,12 @@
@Override
public void diskFullThresholdReached(File directory, long thresholdInBytes) {
- storageStatus = StorageStatus.unusable(
- WARN_DISK_SPACE_FULL_THRESHOLD_CROSSED.get(directory.getFreeSpace(), directory.getAbsolutePath(),
- thresholdInBytes, config.getBackendId()));
+ storageStatus = statusWhenDiskSpaceFull(directory, thresholdInBytes, config.getBackendId());
}
@Override
public void diskLowThresholdReached(File directory, long thresholdInBytes) {
- storageStatus = StorageStatus.lockedDown(
- WARN_DISK_SPACE_LOW_THRESHOLD_CROSSED.get(directory.getFreeSpace(), directory.getAbsolutePath(),
- thresholdInBytes, config.getBackendId()));
+ storageStatus = statusWhenDiskSpaceLow(directory, thresholdInBytes, config.getBackendId());
}
@Override
--
Gitblit v1.10.0