| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | { |
| | | private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); |
| | | |
| | | |
| | | |
| | | /** The base DNs for this backend. */ |
| | | private Set<DN> baseDNs; |
| | | |
| | |
| | | { |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void openBackend() |
| | | throws ConfigException, InitializationException |
| | |
| | | readLDIF(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Reads the contents of the LDIF backing file into memory. |
| | | * |
| | |
| | | return; |
| | | } |
| | | |
| | | |
| | | try |
| | | { |
| | | importLDIF(new LDIFImportConfig(ldifFile.getAbsolutePath()), false); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | 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 |
| | |
| | | m, e); |
| | | } |
| | | |
| | | |
| | | for (Entry entry : entryMap.values()) |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void closeBackend() |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean isIndexed(AttributeType attributeType, IndexType indexType) |
| | | { |
| | |
| | | return true; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public ConditionResult hasSubordinates(DN entryDN) |
| | | throws DirectoryException |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public long getNumberOfChildren(DN parentDN) throws DirectoryException |
| | | { |
| | |
| | | return getNumberOfSubordinates(parentDN, false); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public long getNumberOfEntriesInBaseDN(DN baseDN) throws DirectoryException |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public Entry getEntry(DN entryDN) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean entryExists(DN entryDN) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void addEntry(Entry entry, AddOperation addOperation) |
| | | throws DirectoryException |
| | |
| | | } |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@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 |
| | |
| | | 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). |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Removes the specified entry and any subordinates that it may have from |
| | | * the backend. This method assumes that the caller holds the backend write |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void replaceEntry(Entry oldEntry, Entry newEntry, |
| | | ModifyOperation modifyOperation) throws DirectoryException |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void renameEntry(DN currentDN, Entry entry, |
| | | ModifyDNOperation modifyDNOperation) |
| | |
| | | } |
| | | 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); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Moves the specified entry and all of its children so that they are |
| | | * appropriately placed below the given new parent DN. This method assumes |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void search(SearchOperation searchOperation) |
| | | throws DirectoryException |
| | |
| | | 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)) |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public Set<String> getSupportedControls() |
| | | { |
| | | return supportedControls; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public Set<String> getSupportedFeatures() |
| | | { |
| | | return Collections.emptySet(); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean supports(BackendOperation backendOperation) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@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 |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@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 |
| | |
| | | { |
| | | 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) |
| | |
| | | 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 |
| | |
| | | continue; |
| | | } |
| | | |
| | | |
| | | // If the entry DN is a base DN, then add it with no more processing. |
| | | if (baseDNs.contains(entryDN)) |
| | | { |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | childDNSet.add(entryDN); |
| | | } |
| | | |
| | | |
| | | if (writeLDIF) |
| | | { |
| | | writeLDIF(); |
| | |
| | | } |
| | | 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 |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@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 |
| | |
| | | throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void removeBackup(BackupDirectory backupDirectory, String backupID) |
| | | throws DirectoryException |
| | |
| | | throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void restoreBackup(RestoreConfig restoreConfig) |
| | | throws DirectoryException |
| | |
| | | throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void configureBackend(LDIFBackendCfg config, ServerContext serverContext) throws ConfigException |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean isConfigurationChangeAcceptable(LDIFBackendCfg configuration, |
| | | List<LocalizableMessage> unacceptableReasons) |
| | |
| | | return configAcceptable; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public ConfigChangeResult applyConfigurationChange( |
| | | LDIFBackendCfg configuration) |
| | |
| | | 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() |
| | | { |