}
private void loadLdifs( DirectoryService service ) throws NamingException
{
ServerStartupConfiguration cfg =
( ServerStartupConfiguration ) service.getConfiguration().getStartupConfiguration();
// log and bail if property not set
if ( cfg.getLdifDirectory() == null )
{
log.info( "LDIF load directory not specified. No LDIF files will be loaded." );
return;
}
// log and bail if LDIF directory does not exists
if ( !cfg.getLdifDirectory().exists() )
{
log.warn( "LDIF load directory '" + getCanonical( cfg.getLdifDirectory() )
+ "' does not exist. No LDIF files will be loaded.");
return;
}
// get an initial context to the rootDSE for creating the LDIF entries
Hashtable env = ( Hashtable ) service.getConfiguration().getEnvironment().clone();
env.put( Context.PROVIDER_URL, "" );
DirContext root = ( DirContext ) this.getInitialContext( env );
// make sure the configuration area for loaded ldif files is present
ensureLdifFileBase( root );
// if ldif directory is a file try to load it
if ( !cfg.getLdifDirectory().isDirectory() )
{
log.info( "LDIF load directory '" + getCanonical( cfg.getLdifDirectory() )
+ "' is a file. Will attempt to load as LDIF." );
Attributes fileEntry = getLdifFileEntry( root, cfg.getLdifDirectory() );
if ( fileEntry != null )
{
String time = ( String ) fileEntry.get( "createTimestamp" ).get();
log.info( "Load of LDIF file '" + getCanonical( cfg.getLdifDirectory() )
+ "' skipped. It has already been loaded on " + time + "." );
return;
}
LdifFileLoader loader = new LdifFileLoader( root, cfg.getLdifDirectory(), cfg.getLdifFilters() );
loader.execute();
addFileEntry( root, cfg.getLdifDirectory() );
return;
}
// get all the ldif files within the directory (should be sorted alphabetically)
File[] ldifFiles = cfg.getLdifDirectory().listFiles( new FileFilter()
{
public boolean accept( File pathname )
{
boolean isLdif = pathname.getName().toLowerCase().endsWith( ".ldif" );
return pathname.isFile() && pathname.canRead() && isLdif;
}
});
// log and bail if we could not find any LDIF files
if ( ldifFiles == null || ldifFiles.length == 0 )
{
log.warn( "LDIF load directory '" + getCanonical( cfg.getLdifDirectory() )
+ "' does not contain any LDIF files. No LDIF files will be loaded.");
return;
}
// load all the ldif files and load each one that is loaded
for ( int ii = 0; ii < ldifFiles.length; ii++ )
{
Attributes fileEntry = getLdifFileEntry( root, ldifFiles[ii] );
if ( fileEntry != null )
{
String time = ( String ) fileEntry.get( "createTimestamp" ).get();
log.info( "Load of LDIF file '" + getCanonical( ldifFiles[ii] )
+ "' skipped. It has already been loaded on " + time + "." );
continue;
}
LdifFileLoader loader = new LdifFileLoader( root, ldifFiles[ii], cfg.getLdifFilters() );
int count = loader.execute();
log.info( "Loaded " + count + " entries from LDIF file '" + getCanonical( ldifFiles[ii] ) + "'" );
if ( fileEntry == null )
{
addFileEntry( root, ldifFiles[ii] );