From 17ec2cfa9d04e6942a694ab5c3778ac26f17bb35 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 19 Apr 2016 12:59:39 +0000
Subject: [PATCH] code cleanup
---
opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java | 220 ++++++++++++++++++++----------------------------------
1 files changed, 81 insertions(+), 139 deletions(-)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
index fcfa3b8..7410ac7 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/LDIFBackend.java
@@ -22,6 +22,7 @@
import static org.opends.server.util.StaticUtils.*;
import java.io.File;
+import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -36,12 +37,12 @@
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.config.server.ConfigChangeResult;
import org.forgerock.opendj.config.server.ConfigException;
+import org.forgerock.opendj.config.server.ConfigurationChangeListener;
import org.forgerock.opendj.ldap.ConditionResult;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.opendj.ldap.schema.AttributeType;
-import org.forgerock.opendj.config.server.ConfigurationChangeListener;
import org.forgerock.opendj.server.config.server.LDIFBackendCfg;
import org.opends.server.api.AlertGenerator;
import org.opends.server.api.Backend;
@@ -84,8 +85,6 @@
{
private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
-
-
/** The base DNs for this backend. */
private Set<DN> baseDNs;
@@ -117,7 +116,6 @@
{
}
- /** {@inheritDoc} */
@Override
public void openBackend()
throws ConfigException, InitializationException
@@ -152,8 +150,6 @@
readLDIF();
}
-
-
/**
* Reads the contents of the LDIF backing file into memory.
*
@@ -178,7 +174,6 @@
return;
}
-
try
{
importLDIF(new LDIFImportConfig(ldifFile.getAbsolutePath()), false);
@@ -189,8 +184,6 @@
}
}
-
-
/**
* Writes the current set of entries to the target LDIF file. The new LDIF
* will first be created as a temporary file and then renamed into place. The
@@ -207,7 +200,6 @@
File tempFile = new File(ldifFile.getAbsolutePath() + ".new");
File oldFile = new File(ldifFile.getAbsolutePath() + ".old");
-
// Write the new data to a temporary file.
LDIFWriter writer;
try
@@ -231,7 +223,6 @@
m, e);
}
-
for (Entry entry : entryMap.values())
{
try
@@ -332,7 +323,6 @@
}
}
- /** {@inheritDoc} */
@Override
public void closeBackend()
{
@@ -387,7 +377,6 @@
}
}
- /** {@inheritDoc} */
@Override
public boolean isIndexed(AttributeType attributeType, IndexType indexType)
{
@@ -395,7 +384,6 @@
return true;
}
- /** {@inheritDoc} */
@Override
public ConditionResult hasSubordinates(DN entryDN)
throws DirectoryException
@@ -430,7 +418,6 @@
}
}
- /** {@inheritDoc} */
@Override
public long getNumberOfChildren(DN parentDN) throws DirectoryException
{
@@ -438,7 +425,6 @@
return getNumberOfSubordinates(parentDN, false);
}
- /** {@inheritDoc} */
@Override
public long getNumberOfEntriesInBaseDN(DN baseDN) throws DirectoryException
{
@@ -490,7 +476,6 @@
}
}
- /** {@inheritDoc} */
@Override
public Entry getEntry(DN entryDN)
{
@@ -506,7 +491,6 @@
}
}
- /** {@inheritDoc} */
@Override
public boolean entryExists(DN entryDN)
{
@@ -522,7 +506,6 @@
}
}
- /** {@inheritDoc} */
@Override
public void addEntry(Entry entry, AddOperation addOperation)
throws DirectoryException
@@ -565,27 +548,8 @@
}
else
{
- DN matchedDN = null;
- if (parentDN != null)
- {
- while (true)
- {
- parentDN = DirectoryServer.getParentDNInSuffix(parentDN);
- if (parentDN == null)
- {
- break;
- }
-
- if (entryMap.containsKey(parentDN))
- {
- matchedDN = parentDN;
- break;
- }
- }
- }
-
LocalizableMessage m = ERR_LDIF_BACKEND_ADD_MISSING_PARENT.get(entryDN);
- throw new DirectoryException(ResultCode.NO_SUCH_OBJECT, m, matchedDN, null);
+ throw new DirectoryException(ResultCode.NO_SUCH_OBJECT, m, findMatchedDN(parentDN), null);
}
}
}
@@ -595,7 +559,26 @@
}
}
- /** {@inheritDoc} */
+ private DN findMatchedDN(DN parentDN)
+ {
+ if (parentDN != null)
+ {
+ while (true)
+ {
+ parentDN = DirectoryServer.getParentDNInSuffix(parentDN);
+ if (parentDN == null)
+ {
+ return null;
+ }
+ else if (entryMap.containsKey(parentDN))
+ {
+ return parentDN;
+ }
+ }
+ }
+ return null;
+ }
+
@Override
public void deleteEntry(DN entryDN, DeleteOperation deleteOperation)
throws DirectoryException
@@ -628,7 +611,6 @@
throw new DirectoryException(ResultCode.NO_SUCH_OBJECT, m, matchedDN, null);
}
-
// See if the target entry has any children. If so, then we'll only
// delete it if the request contains the subtree delete control (in
// which case we'll delete the entire subtree).
@@ -693,8 +675,6 @@
}
}
-
-
/**
* Removes the specified entry and any subordinates that it may have from
* the backend. This method assumes that the caller holds the backend write
@@ -716,7 +696,6 @@
}
}
- /** {@inheritDoc} */
@Override
public void replaceEntry(Entry oldEntry, Entry newEntry,
ModifyOperation modifyOperation) throws DirectoryException
@@ -756,7 +735,6 @@
}
}
- /** {@inheritDoc} */
@Override
public void renameEntry(DN currentDN, Entry entry,
ModifyDNOperation modifyDNOperation)
@@ -823,7 +801,6 @@
}
parentChildDNs.add(newDN);
-
// If the entry has children, then we'll need to work on the whole
// subtree. Otherwise, just work on the target entry.
Set<DN> childDNSet = childDNs.remove(currentDN);
@@ -844,8 +821,6 @@
}
}
-
-
/**
* Moves the specified entry and all of its children so that they are
* appropriately placed below the given new parent DN. This method assumes
@@ -893,7 +868,6 @@
}
}
- /** {@inheritDoc} */
@Override
public void search(SearchOperation searchOperation)
throws DirectoryException
@@ -907,7 +881,6 @@
SearchScope scope = searchOperation.getScope();
SearchFilter filter = searchOperation.getFilter();
-
// Make sure the base entry exists if it's supposed to be in this backend.
Entry baseEntry = entryMap.get(baseDN);
if (baseEntry == null && handlesEntry(baseDN))
@@ -961,21 +934,18 @@
}
}
- /** {@inheritDoc} */
@Override
public Set<String> getSupportedControls()
{
return supportedControls;
}
- /** {@inheritDoc} */
@Override
public Set<String> getSupportedFeatures()
{
return Collections.emptySet();
}
- /** {@inheritDoc} */
@Override
public boolean supports(BackendOperation backendOperation)
{
@@ -990,53 +960,33 @@
}
}
- /** {@inheritDoc} */
@Override
public void exportLDIF(LDIFExportConfig exportConfig)
throws DirectoryException
{
backendLock.readLock().lock();
- try
+ try (LDIFWriter ldifWriter = newLDIFWriter(exportConfig))
{
- // Create the LDIF writer.
- LDIFWriter ldifWriter;
- try
- {
- ldifWriter = new LDIFWriter(exportConfig);
- }
- catch (Exception e)
- {
- logger.traceException(e);
-
- LocalizableMessage m = ERR_LDIF_BACKEND_CANNOT_CREATE_LDIF_WRITER.get(
- stackTraceToSingleLineString(e));
- throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
- m, e);
- }
-
-
// Walk through all the entries and write them to LDIF.
- DN entryDN = null;
- try
+ for (Entry entry : entryMap.values())
{
- for (Entry entry : entryMap.values())
+ DN entryDN = entry.getName();
+ try
{
- entryDN = entry.getName();
ldifWriter.writeEntry(entry);
}
+ catch (Exception e)
+ {
+ LocalizableMessage m =
+ ERR_LDIF_BACKEND_CANNOT_WRITE_ENTRY_TO_LDIF.get(entryDN, stackTraceToSingleLineString(e));
+ throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), m, e);
+ }
}
- catch (Exception e)
- {
- LocalizableMessage m = ERR_LDIF_BACKEND_CANNOT_WRITE_ENTRY_TO_LDIF.get(
- entryDN, stackTraceToSingleLineString(e));
- throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
- m, e);
- }
- finally
- {
- StaticUtils.close(ldifWriter);
- }
+ }
+ catch (IOException ignoreOnClose)
+ {
+ logger.traceException(ignoreOnClose);
}
finally
{
@@ -1044,7 +994,20 @@
}
}
- /** {@inheritDoc} */
+ private LDIFWriter newLDIFWriter(LDIFExportConfig exportConfig) throws DirectoryException
+ {
+ try
+ {
+ return new LDIFWriter(exportConfig);
+ }
+ catch (Exception e)
+ {
+ logger.traceException(e);
+ LocalizableMessage m = ERR_LDIF_BACKEND_CANNOT_CREATE_LDIF_WRITER.get(stackTraceToSingleLineString(e));
+ throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), m, e);
+ }
+ }
+
@Override
public LDIFImportResult importLDIF(LDIFImportConfig importConfig, ServerContext serverContext)
throws DirectoryException
@@ -1068,25 +1031,11 @@
{
backendLock.writeLock().lock();
- try
+ try (LDIFReader reader = newLDIFReader(importConfig))
{
- LDIFReader reader;
- try
- {
- reader = new LDIFReader(importConfig);
- }
- catch (Exception e)
- {
- LocalizableMessage m = ERR_LDIF_BACKEND_CANNOT_CREATE_LDIF_READER.get(
- stackTraceToSingleLineString(e));
- throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
- m, e);
- }
-
entryMap.clear();
childDNs.clear();
-
try
{
while (true)
@@ -1109,10 +1058,7 @@
throw new DirectoryException(
DirectoryServer.getServerErrorResultCode(), m, le);
}
- else
- {
- continue;
- }
+ continue;
}
// Make sure that we don't already have an entry with the same DN. If
@@ -1127,7 +1073,6 @@
continue;
}
-
// If the entry DN is a base DN, then add it with no more processing.
if (baseDNs.contains(entryDN))
{
@@ -1135,19 +1080,8 @@
continue;
}
-
// Make sure that the parent exists. If not, then reject the entry.
- boolean isBelowBaseDN = false;
- for (DN baseDN : baseDNs)
- {
- if (baseDN.isSuperiorOrEqualTo(entryDN))
- {
- isBelowBaseDN = true;
- break;
- }
- }
-
- if (! isBelowBaseDN)
+ if (!isBelowBaseDN(entryDN))
{
LocalizableMessage m = ERR_LDIF_BACKEND_ENTRY_OUT_OF_SCOPE.get(
ldifFilePath, currentConfig.dn(), entryDN);
@@ -1166,7 +1100,6 @@
continue;
}
-
// The entry does not exist but its parent does, so add it and update
// the set of children for the parent.
entryMap.put(entryDN, e);
@@ -1181,7 +1114,6 @@
childDNSet.add(entryDN);
}
-
if (writeLDIF)
{
writeLDIF();
@@ -1197,14 +1129,8 @@
}
catch (Exception e)
{
- LocalizableMessage m = ERR_LDIF_BACKEND_ERROR_READING_LDIF.get(
- stackTraceToSingleLineString(e));
- throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
- m, e);
- }
- finally
- {
- StaticUtils.close(reader);
+ LocalizableMessage m = ERR_LDIF_BACKEND_ERROR_READING_LDIF.get(stackTraceToSingleLineString(e));
+ throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), m, e);
}
}
finally
@@ -1213,7 +1139,31 @@
}
}
- /** {@inheritDoc} */
+ private boolean isBelowBaseDN(DN entryDN)
+ {
+ for (DN baseDN : baseDNs)
+ {
+ if (baseDN.isSuperiorOrEqualTo(entryDN))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private LDIFReader newLDIFReader(LDIFImportConfig importConfig) throws DirectoryException
+ {
+ try
+ {
+ return new LDIFReader(importConfig);
+ }
+ catch (Exception e)
+ {
+ LocalizableMessage m = ERR_LDIF_BACKEND_CANNOT_CREATE_LDIF_READER.get(stackTraceToSingleLineString(e));
+ throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), m, e);
+ }
+ }
+
@Override
public void createBackup(BackupConfig backupConfig)
throws DirectoryException
@@ -1222,7 +1172,6 @@
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
- /** {@inheritDoc} */
@Override
public void removeBackup(BackupDirectory backupDirectory, String backupID)
throws DirectoryException
@@ -1231,7 +1180,6 @@
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
- /** {@inheritDoc} */
@Override
public void restoreBackup(RestoreConfig restoreConfig)
throws DirectoryException
@@ -1240,7 +1188,6 @@
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
}
- /** {@inheritDoc} */
@Override
public void configureBackend(LDIFBackendCfg config, ServerContext serverContext) throws ConfigException
{
@@ -1259,7 +1206,6 @@
}
}
- /** {@inheritDoc} */
@Override
public boolean isConfigurationChangeAcceptable(LDIFBackendCfg configuration,
List<LocalizableMessage> unacceptableReasons)
@@ -1276,7 +1222,6 @@
return configAcceptable;
}
- /** {@inheritDoc} */
@Override
public ConfigChangeResult applyConfigurationChange(
LDIFBackendCfg configuration)
@@ -1307,21 +1252,18 @@
return ccr;
}
- /** {@inheritDoc} */
@Override
public DN getComponentEntryDN()
{
return currentConfig.dn();
}
- /** {@inheritDoc} */
@Override
public String getClassName()
{
return LDIFBackend.class.getName();
}
- /** {@inheritDoc} */
@Override
public Map<String,String> getAlerts()
{
--
Gitblit v1.10.0