BackendImpl.java revision 75fceea66e311b3de58d76a4c993af0fec13dd3d
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2007-2010 Sun Microsystems, Inc.
* Portions Copyright 2013-2015 ForgeRock AS
*/
/**
* This is an implementation of a Directory Server Backend which stores entries locally
* in a pluggable storage.
*
* @param <C>
* the type of the BackendCfg for the current backend
*/
{
/** The configuration of this backend. */
private PluggableBackendCfg cfg;
/** The root JE container to use for this backend. */
private RootContainer rootContainer;
// FIXME: this is broken. Replace with read-write lock.
/** A count of the total operation threads currently in the backend. */
/** The base DNs defined for this backend instance. */
private MonitorProvider<?> rootContainerMonitor;
/** The underlying storage engine. */
/** The controls supported by this backend. */
/**
* Begin a Backend API method that accesses the database and returns the <code>EntryContainer</code> for
* <code>entryDN</code>.
* @param operation requesting the storage
* @param entryDN the target DN for the operation
* @return <code>EntryContainer</code> where <code>entryDN</code> resides
*/
{
{
throw new DirectoryException(ResultCode.UNDEFINED, ERR_BACKEND_ENTRY_DOESNT_EXIST.get(entryDN, getBackendID()));
}
return ec;
}
/** End a Backend API method that accesses the database. */
private void accessEnd()
{
}
/**
* Wait until there are no more threads accessing the database. It is assumed
* that new threads have been prevented from entering the database at the time
* this method is called.
*/
private void waitUntilQuiescent()
{
{
// Still have threads in the database so sleep a little
try
{
}
catch (InterruptedException e)
{
logger.traceException(e);
}
}
}
/** {@inheritDoc} */
{
}
/** {@inheritDoc} */
{
if (mustOpenRootContainer())
{
}
// Preload the database cache.
try
{
// Log an informational message about the number of entries.
}
catch (StorageRuntimeException e)
{
throw new InitializationException(message, e);
}
{
try
{
}
catch (Exception e)
{
}
}
// Register a monitor provider for the environment.
// Register this backend as a change listener.
cfg.addPluggableChangeListener(this);
}
/** {@inheritDoc} */
public void closeBackend()
{
cfg.removePluggableChangeListener(this);
// Deregister our base DNs.
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
}
// We presume the server will prevent more operations coming into this
// backend, but there may be existing operations already in the
// backend. We need to wait for them to finish.
// Close the database.
try
{
}
catch (StorageRuntimeException e)
{
logger.traceException(e);
}
// Make sure the thread counts are zero for next initialization.
// Log an informational message.
}
/** {@inheritDoc} */
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
return false;
}
}
/** {@inheritDoc} */
{
switch (backendOperation)
{
case BACKUP:
case RESTORE:
// Responsibility of the underlying storage.
return storage.supportsBackupAndRestore();
default: // INDEXING, LDIF_EXPORT, LDIF_IMPORT
// Responsibility of this pluggable backend.
return true;
}
}
/** {@inheritDoc} */
{
return Collections.emptySet();
}
/** {@inheritDoc} */
{
return supportedControls;
}
/** {@inheritDoc} */
public DN[] getBaseDNs()
{
return baseDNs;
}
/** {@inheritDoc} */
public long getEntryCount()
{
if (rootContainer != null)
{
try
{
return rootContainer.getEntryCount();
}
catch (Exception e)
{
logger.traceException(e);
}
}
return -1;
}
/** {@inheritDoc} */
{
try {
}
catch (DirectoryException de)
{
{
return ConditionResult.UNDEFINED;
}
throw de;
}
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
final EntryContainer ec;
try {
}
catch (DirectoryException de)
{
throw de;
}
try
{
return ec.getNumberOfEntriesInBaseDN();
}
catch (Exception e)
{
throw new DirectoryException(
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
/*
* Only place where we need special handling. Should return -1 instead of an
* error if the EntryContainer is null...
*/
try {
}
catch (DirectoryException de)
{
{
return -1;
}
throw de;
}
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
public void addEntry(Entry entry, AddOperation addOperation) throws DirectoryException, CanceledOperationException
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
{
if (currentContainer != container)
{
accessEnd();
// FIXME: No reason why we cannot implement a move between containers
// since the containers share the same database environment.
throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, WARN_JEB_FUNCTION_NOT_SUPPORTED.get());
}
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
/** {@inheritDoc} */
public void search(SearchOperation searchOperation) throws DirectoryException, CanceledOperationException
{
try
{
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
accessEnd();
}
}
private void checkRootContainerInitialized() throws DirectoryException
{
if (rootContainer == null)
{
}
}
/** {@inheritDoc} */
throws DirectoryException
{
// If the backend already has the root container open, we must use the same
// underlying root container
boolean openRootContainer = mustOpenRootContainer();
try
{
if (openRootContainer)
{
}
}
catch (IOException ioe)
{
}
catch (StorageRuntimeException de)
{
throw createDirectoryException(de);
}
catch (ConfigException ce)
{
}
catch (IdentifiedException e)
{
if (e instanceof DirectoryException)
{
throw (DirectoryException) e;
}
}
finally
{
}
}
private boolean mustOpenRootContainer()
{
return rootContainer == null;
}
/** {@inheritDoc} */
throws DirectoryException
{
// If the rootContainer is open, the backend is initialized by something else.
// We can't do import while the backend is online.
if (rootContainer != null)
{
}
try
{
{
try
{
// clear all files before opening the root container
}
catch (Exception e)
{
throw new DirectoryException(getServerErrorResultCode(), m, e);
}
}
}
catch (StorageRuntimeException e)
{
throw new DirectoryException(getServerErrorResultCode(), LocalizableMessage.raw(e.getMessage()), e);
}
catch (DirectoryException e)
{
throw e;
}
catch (OpenDsException e)
{
}
catch (ConfigException e)
{
}
finally
{
try
{
if (rootContainer != null)
{
}
}
catch (StorageRuntimeException de)
{
}
}
}
/** {@inheritDoc} */
{
// If the backend already has the root container open, we must use the same
// underlying root container
final boolean openRootContainer = mustOpenRootContainer();
try
{
if (openRootContainer)
{
}
}
catch (StorageRuntimeException e)
{
throw createDirectoryException(e);
}
finally
{
}
}
/**
* If a root container was opened in the calling method method as read only,
* close it to leave the backend in the same state.
*/
private void closeTemporaryRootContainer(boolean openRootContainer)
{
{
try
{
}
catch (StorageRuntimeException e)
{
logger.traceException(e);
}
}
}
/** {@inheritDoc} */
{
// If the backend already has the root container open, we must use the same
// underlying root container
boolean openRootContainer = mustOpenRootContainer();
/*
* If the rootContainer is open, the backend is initialized by something else.
* We can't do any rebuild of system indexes while others are using this backend.
*/
{
}
try
{
if (openRootContainer)
{
}
}
catch (ExecutionException execEx)
{
}
catch (InterruptedException intEx)
{
}
catch (ConfigException ce)
{
}
catch (StorageRuntimeException e)
{
}
catch (InitializationException e)
{
throw new InitializationException(e.getMessageObject(), e);
}
finally
{
}
}
/** {@inheritDoc} */
{
}
/** {@inheritDoc} */
throws DirectoryException
{
}
/** {@inheritDoc} */
{
}
/**
* Creates the storage engine which will be used by this pluggable backend. Implementations should
* create and configure a new storage engine but not open it.
*
* @param cfg
* the configuration object
* @param serverContext
* this Directory Server intsance's server context
* @return The storage engine to be used by this pluggable backend.
* @throws ConfigException
* If there is an error in the configuration.
*/
protected abstract Storage configureStorage(C cfg, ServerContext serverContext) throws ConfigException;
/** {@inheritDoc} */
{
}
/** {@inheritDoc} */
public boolean isConfigurationChangeAcceptable(PluggableBackendCfg cfg, List<LocalizableMessage> unacceptableReasons)
{
return true;
}
/** {@inheritDoc} */
{
try
{
if(rootContainer != null)
{
{
{
// Check for changes to the base DNs.
{
return;
}
// Put the new configuration in place.
}
});
}
}
catch (Exception e)
{
}
return ccr;
}
private void removeDeletedBaseDNs(SortedSet<DN> newBaseDNs, WriteableTransaction txn) throws DirectoryException
{
{
{
// The base DN was deleted.
}
}
}
private boolean createNewBaseDNs(DN[] newBaseDNsArray, ConfigChangeResult ccr, WriteableTransaction txn)
{
{
{
try
{
// The base DN was added.
}
catch (Exception e)
{
logger.traceException(e);
return false;
}
}
}
return true;
}
/**
* Returns a handle to the JE root container currently used by this backend.
* The rootContainer could be NULL if the backend is not initialized.
*
* @return The RootContainer object currently used by this backend.
*/
public final RootContainer getRootContainer()
{
return rootContainer;
}
/**
* Returns a new read-only handle to the JE root container for this backend.
* The caller is responsible for closing the root container after use.
*
* @return The read-only RootContainer object for this backend.
*
* @throws ConfigException If an unrecoverable problem arises during
* initialization.
* @throws InitializationException If a problem occurs during initialization
* that is not related to the server
* configuration.
*/
private final RootContainer getReadOnlyRootContainer()
{
return initializeRootContainer();
}
/**
* Creates a customized DirectoryException from the StorageRuntimeException
* thrown by JE backend.
*
* @param e
* The StorageRuntimeException to be converted.
* @return DirectoryException created from exception.
*/
{
if (cause instanceof OpenDsException)
{
return new DirectoryException(
}
else
{
return new DirectoryException(
}
}
private RootContainer initializeRootContainer()
throws ConfigException, InitializationException {
// Open the database environment
try {
return rc;
}
catch (StorageRuntimeException e)
{
throw new InitializationException(message, e);
}
}
/** {@inheritDoc} */
public void preloadEntryCache() throws UnsupportedOperationException
{
}
{
return storage;
}
}