opendj-sdk/opends/src/server/org/opends/server/extensions/ConfigFileHandler.java
@@ -44,6 +44,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.TreeSet; import java.util.TreeMap; import java.util.zip.Deflater; import java.util.zip.GZIPInputStream; @@ -76,6 +77,7 @@ import org.opends.server.protocols.asn1.ASN1OctetString; import org.opends.server.schema.GeneralizedTimeSyntax; import org.opends.server.tools.LDIFModify; import org.opends.server.types.DirectoryEnvironmentConfig; import org.opends.server.types.*; @@ -130,6 +132,9 @@ // Indicates whether to maintain a configuration archive. private boolean maintainConfigArchive; // A SHA-1 digest of the last known configuration. This should only be // incorrect if the server configuration file has been manually edited with // the server online, which is a bad thing. @@ -145,6 +150,9 @@ // The set of base DNs for this config handler backend. private DN[] baseDNs; // The maximum config archive size to maintain. private int maxConfigArchiveSize; // The write lock used to ensure that only one thread can apply a // configuration update at any given time. private ReentrantLock configLock; @@ -228,6 +236,12 @@ // Check to see if a configuration archive exists. If not, then create one. // If so, then check whether the current configuration matches the last // configuration in the archive. If it doesn't, then archive it. DirectoryEnvironmentConfig envConfig = DirectoryServer.getEnvironmentConfig(); maintainConfigArchive = envConfig.maintainConfigArchive(); maxConfigArchiveSize = envConfig.getMaxConfigArchiveSize(); if (maintainConfigArchive) { try { configurationDigest = calculateConfigDigest(); @@ -253,6 +267,7 @@ { writeConfigArchive(); } } @@ -267,10 +282,13 @@ if (changesFile.exists()) { applyChangesFile(f, changesFile); if (maintainConfigArchive) { configurationDigest = calculateConfigDigest(); writeConfigArchive(); } } } catch (Exception e) { if (debugEnabled()) @@ -628,7 +646,7 @@ // Determine the appropriate server root. If it's not defined in the // environment config, then try to figure it out from the location of the // configuration file. File rootFile = DirectoryServer.getEnvironmentConfig().getServerRoot(); File rootFile = envConfig.getServerRoot(); if (rootFile == null) { try @@ -1952,6 +1970,8 @@ // copy the current config off to the side before writing the new config // so that the manual changes don't get lost but also don't get applied. // Also, send an admin alert notifying administrators about the problem. if (maintainConfigArchive) { try { byte[] currentDigest = calculateConfigDigest(); @@ -2007,6 +2027,7 @@ DirectoryServer.sendAlertNotification(this, ALERT_TYPE_MANUAL_CONFIG_EDIT_HANDLED, message); } } // Write the new configuration to a temporary file. @@ -2064,8 +2085,11 @@ // Try to write the archive for the new configuration. if (maintainConfigArchive) { writeConfigArchive(); } } @@ -2075,6 +2099,11 @@ */ private void writeConfigArchive() { if (! maintainConfigArchive) { return; } // Determine the path to the directory that will hold the archived // configuration files. File configDirectory = new File(configFile).getParentFile(); @@ -2196,6 +2225,41 @@ outputStream.close(); } catch (Exception e) {} } // If we should enforce a maximum number of archived configurations, then // see if there are any old ones that we need to delete. if (maxConfigArchiveSize > 0) { String[] archivedFileList = archiveDirectory.list(); int numToDelete = archivedFileList.length - maxConfigArchiveSize; if (numToDelete > 0) { TreeSet<String> archiveSet = new TreeSet<String>(); for (String name : archivedFileList) { if (! name.startsWith("config-")) { continue; } // Simply ordering by filename should work, even when there are // timestamp conflicts, because the dash comes before the period in // the ASCII character set. archiveSet.add(name); } Iterator<String> iterator = archiveSet.iterator(); for (int i=0; ((i < numToDelete) && iterator.hasNext()); i++) { File f = new File(archiveDirectory, iterator.next()); try { f.delete(); } catch (Exception e) {} } } } } opendj-sdk/opends/src/server/org/opends/server/types/DirectoryEnvironmentConfig.java
@@ -470,6 +470,168 @@ /** * Indicates whether the Directory Server should maintain an archive * of previous configurations. If no explicit value is defined, * then a default result of {@code true} will be returned. * * @return {@code true} if the Directory Server should maintain an * archive of previous configurations, or {@code false} if * not. */ public boolean maintainConfigArchive() { String maintainArchiveStr = getProperty(PROPERTY_MAINTAIN_CONFIG_ARCHIVE); if (maintainArchiveStr == null) { return true; } return (! maintainArchiveStr.equalsIgnoreCase("false")); } /** * Specifies whether the Directory Server should maintain an archive * of previous configurations. * * @param maintainConfigArchive Indicates whether the Directory * Server should maintain an archive * of previous configurations. * * @return The previous setting for this configuration option. If * no previous value was specified, then {@code true} will * be returned. * * @throws InitializationException If the Directory Server is * already running. */ public boolean setMaintainConfigArchive( boolean maintainConfigArchive) throws InitializationException { if (DirectoryServer.isRunning()) { throw new InitializationException( ERR_DIRCFG_SERVER_ALREADY_RUNNING.get()); } String oldMaintainStr = setProperty(PROPERTY_MAINTAIN_CONFIG_ARCHIVE, String.valueOf(maintainConfigArchive)); if (oldMaintainStr == null) { return true; } else { return (! oldMaintainStr.equalsIgnoreCase("false")); } } /** * Retrieves the maximum number of archived configurations that the * Directory Server should maintain. If no value is defined, then a * value of zero will be returned. * * @return The maximum number of archived configurations that the * Directory Server should maintain, or zero if there * should not be any limit. */ public int getMaxConfigArchiveSize() { String maxSizeStr = getProperty(PROPERTY_MAX_CONFIG_ARCHIVE_SIZE); if (maxSizeStr == null) { return 0; } try { int maxSize = Integer.parseInt(maxSizeStr); if (maxSize > 0) { return maxSize; } else { return 0; } } catch (Exception e) { return 0; } } /** * Specifies the maximum number of archived configurations that the * Directory Server should maintain. A value that is less than or * equal to zero may be used to indicate that there should not be * any limit to the number of archived configurations. * * @param maxConfigArchiveSize The maximum number of archived * configurations that the Directory * Server should maintain. * * @return The previous setting for this configuration option. If * no previous value was specified, then zero will be * returned. * * @throws InitializationException If the Directory Server is * already running. */ public int setMaxConfigArchiveSize(int maxConfigArchiveSize) throws InitializationException { if (DirectoryServer.isRunning()) { throw new InitializationException( ERR_DIRCFG_SERVER_ALREADY_RUNNING.get()); } if (maxConfigArchiveSize < 0) { maxConfigArchiveSize = 0; } String oldMaxSizeStr = setProperty(PROPERTY_MAX_CONFIG_ARCHIVE_SIZE, String.valueOf(maxConfigArchiveSize)); if (oldMaxSizeStr == null) { return 0; } else { try { int oldMaxSize = Integer.parseInt(oldMaxSizeStr); if (oldMaxSize > 0) { return oldMaxSize; } else { return 0; } } catch (Exception e) { return 0; } } } /** * Retrieves the directory that contains the server schema * configuration files. If no value is defined, but a default * directory of "config/schema" exists below the server root, then opendj-sdk/opends/src/server/org/opends/server/util/ServerConstants.java
@@ -2531,6 +2531,28 @@ /** * The name of the system property that can be used to determine whether the * server should maintain an archive of previous configurations. If this is * not set, or if the value is anything other than "false", then the server * will maintain a configuration archive. */ public static final String PROPERTY_MAINTAIN_CONFIG_ARCHIVE = "org.opends.server.MaintainConfigArchive"; /** * The name of the system property that can be used to specify the maximum * number of archived configurations to maintain. If this is not set, or if * it set to a zero or negative value, then there will be no limit on the * number of archived configurations. */ public static final String PROPERTY_MAX_CONFIG_ARCHIVE_SIZE = "org.opends.server.MaxConfigArchiveSize"; /** * The name of the system property that can be used to determine whether the * Directory Server is starting up for the purpose of running the unit tests. */ public static final String PROPERTY_RUNNING_UNIT_TESTS =