BooleanArgument dontWrap;
BooleanArgument overwriteExisting;
BooleanArgument showUsage;
StringArgument filterFile;
IntegerArgument sizeLimit;
IntegerArgument timeLimit;
MultiChoiceArgument scopeString;
StringArgument baseDNString;
StringArgument configClass;
StringArgument configFile;
StringArgument ldifFile;
StringArgument outputFile;
Message toolDescription = INFO_LDIFSEARCH_TOOL_DESCRIPTION.get();
ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription,
false, true, 0, 0,
"[filter] [attributes ...]");
try
{
ldifFile = new StringArgument(
"ldiffile", 'l', "ldifFile", false, true,
true, INFO_LDIFFILE_PLACEHOLDER.get(), null, null,
INFO_LDIFSEARCH_DESCRIPTION_LDIF_FILE.get());
argParser.addArgument(ldifFile);
baseDNString = new StringArgument(
"basedn", OPTION_SHORT_BASEDN,
OPTION_LONG_BASEDN, false, true,
true, INFO_BASEDN_PLACEHOLDER.get(), "", null,
INFO_LDIFSEARCH_DESCRIPTION_BASEDN.get());
argParser.addArgument(baseDNString);
scopeString = new MultiChoiceArgument(
"scope", 's', "searchScope", false, false,
true, INFO_SCOPE_PLACEHOLDER.get(), SCOPE_STRING_SUB,
null, scopeStrings, false,
INFO_LDIFSEARCH_DESCRIPTION_SCOPE.get());
argParser.addArgument(scopeString);
configFile = new StringArgument(
"configfile", 'c', "configFile", false,
false, true, INFO_CONFIGFILE_PLACEHOLDER.get(), null, null,
INFO_DESCRIPTION_CONFIG_FILE.get());
configFile.setHidden(true);
argParser.addArgument(configFile);
configClass = new StringArgument("configclass", OPTION_SHORT_CONFIG_CLASS,
OPTION_LONG_CONFIG_CLASS, false,
false, true, INFO_CONFIGCLASS_PLACEHOLDER.get(),
ConfigFileHandler.class.getName(), null,
INFO_DESCRIPTION_CONFIG_CLASS.get());
configClass.setHidden(true);
argParser.addArgument(configClass);
filterFile = new StringArgument("filterfile", 'f', "filterFile", false,
false, true, INFO_FILTER_FILE_PLACEHOLDER.get(), null, null,
INFO_LDIFSEARCH_DESCRIPTION_FILTER_FILE.get());
argParser.addArgument(filterFile);
outputFile = new StringArgument(
"outputfile", 'o', "outputFile", false,
false, true, INFO_OUTPUT_FILE_PLACEHOLDER.get(), null, null,
INFO_LDIFSEARCH_DESCRIPTION_OUTPUT_FILE.get());
argParser.addArgument(outputFile);
overwriteExisting =
new BooleanArgument(
"overwriteexisting", 'O',"overwriteExisting",
INFO_LDIFSEARCH_DESCRIPTION_OVERWRITE_EXISTING.get());
argParser.addArgument(overwriteExisting);
dontWrap = new BooleanArgument(
"dontwrap", 'T', "dontWrap",
INFO_LDIFSEARCH_DESCRIPTION_DONT_WRAP.get());
argParser.addArgument(dontWrap);
sizeLimit = new IntegerArgument(
"sizelimit", 'z', "sizeLimit", false,
false, true, INFO_SIZE_LIMIT_PLACEHOLDER.get(), 0, null,
true, 0, false, 0,
INFO_LDIFSEARCH_DESCRIPTION_SIZE_LIMIT.get());
argParser.addArgument(sizeLimit);
timeLimit = new IntegerArgument(
"timelimit", 't', "timeLimit", false,
false, true, INFO_TIME_LIMIT_PLACEHOLDER.get(), 0, null,
true, 0, false, 0,
INFO_LDIFSEARCH_DESCRIPTION_TIME_LIMIT.get());
argParser.addArgument(timeLimit);
showUsage = new BooleanArgument(
"help", OPTION_SHORT_HELP,
OPTION_LONG_HELP,
INFO_DESCRIPTION_USAGE.get());
argParser.addArgument(showUsage);
argParser.setUsageArgument(showUsage);
}
catch (ArgumentException ae)
{
Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
err.println(message);
return 1;
}
// Parse the command-line arguments provided to the program.
try
{
argParser.parseArguments(args);
}
catch (ArgumentException ae)
{
Message message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
err.println(message);
err.println(argParser.getUsage());
return LDAPResultCode.CLIENT_SIDE_PARAM_ERROR;
}
// If we should just display usage or version information,
// then print it and exit.
if (argParser.usageOrVersionDisplayed())
{
return 0;
}
// Make sure that at least one filter was provided. Also get the attribute
// list at the same time because it may need to be specified in the same
// way.
boolean allUserAttrs = false;
boolean allOperationalAttrs = false;
//Return objectclass attribute unless analysis of the arguments determines
//otherwise.
boolean includeObjectclassAttrs = true;
LinkedList<String> attributeNames;
LinkedList<String> objectClassNames = new LinkedList<String>();
LinkedList<String> filterStrings = new LinkedList<String>();
if (filterFile.isPresent())
{
BufferedReader in = null;
try
{
String fileNameValue = filterFile.getValue();
in = new BufferedReader(new FileReader(fileNameValue));
String line = null;
while ((line = in.readLine()) != null)
{
if(line.trim().equals(""))
{
// ignore empty lines.
continue;
}
filterStrings.add(line);
}
} catch(Exception e)
{
err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
return 1;
}
finally
{
if(in != null)
{
try
{
in.close();
} catch (IOException ioe) {}
}
}
ArrayList<String> trailingArguments = argParser.getTrailingArguments();
if ((trailingArguments == null) || trailingArguments.isEmpty())
{
attributeNames = new LinkedList<String>();
}
else
{
attributeNames = new LinkedList<String>();
for (String attributeName : trailingArguments)
{
String lowerName = toLowerCase(attributeName);
if (lowerName.equals("*"))
{
allUserAttrs = true;
}
else if (lowerName.equals("+"))
{
allOperationalAttrs = true;
}
else if (lowerName.startsWith("@"))
{
objectClassNames.add(lowerName.substring(1));
}
else
{
attributeNames.add(lowerName);
}
}
}
}
else
{
ArrayList<String> trailingArguments = argParser.getTrailingArguments();
if ((trailingArguments == null) || trailingArguments.isEmpty())
{
Message message = ERR_LDIFSEARCH_NO_FILTER.get();
err.println(message);
return 1;
}
else
{
Iterator<String> iterator = trailingArguments.iterator();
filterStrings = new LinkedList<String>();
filterStrings.add(iterator.next());
attributeNames = new LinkedList<String>();
while (iterator.hasNext())
{
String lowerName = toLowerCase(iterator.next());
if (lowerName.equals("*"))
{
allUserAttrs = true;
}
else if (lowerName.equals("+"))
{
allOperationalAttrs = true;
}
else if (lowerName.startsWith("@"))
{
objectClassNames.add(lowerName.substring(1));
}
else
{
attributeNames.add(lowerName);
}
}
}
}
if (attributeNames.isEmpty() && objectClassNames.isEmpty() &&
(! allOperationalAttrs))
{
// This will be true if no attributes were requested, which is effectively
// all user attributes. It will also be true if just "*" was included,
// but the net result will be the same.
allUserAttrs = true;
}
//Determine if objectclass attribute should be returned.
if(!allUserAttrs) {
//Single '+', never return objectclass.
if(allOperationalAttrs && objectClassNames.isEmpty() &&
attributeNames.isEmpty())
includeObjectclassAttrs=false;
//If "objectclass" isn't specified in the attributes to return, then
//don't include objectclass attribiute.
if(!attributeNames.isEmpty() && objectClassNames.isEmpty() &&
!attributeNames.contains("objectclass"))
includeObjectclassAttrs=false;
}
// Bootstrap the Directory Server configuration for use as a client.
DirectoryServer directoryServer = DirectoryServer.getInstance();
// If we're to use the configuration then initialize it, along with the
// schema.
boolean checkSchema = configFile.isPresent();
if(initializeServer) {
DirectoryServer.bootstrapClient();
if (checkSchema)
{
try
{
DirectoryServer.initializeJMX();
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_INITIALIZE_JMX.get(
String.valueOf(configFile.getValue()),
e.getMessage());
err.println(message);
return 1;
}
try
{
directoryServer.initializeConfiguration(configClass.getValue(),
configFile.getValue());
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_INITIALIZE_CONFIG.get(
String.valueOf(configFile.getValue()),
e.getMessage());
err.println(message);
return 1;
}
try
{
directoryServer.initializeSchema();
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_INITIALIZE_SCHEMA.get(
String.valueOf(configFile.getValue()),
e.getMessage());
err.println(message);
return 1;
}
}
}
// Choose the desired search scope.
SearchScope searchScope;
if (scopeString.isPresent())
{
String scopeStr = toLowerCase(scopeString.getValue());
if (scopeStr.equals(SCOPE_STRING_BASE))
{
searchScope = SearchScope.BASE_OBJECT;
}
else if (scopeStr.equals(SCOPE_STRING_ONE))
{
searchScope = SearchScope.SINGLE_LEVEL;
}
else if (scopeStr.equals(SCOPE_STRING_SUBORDINATE))
{
searchScope = SearchScope.SUBORDINATE_SUBTREE;
}
else
{
searchScope = SearchScope.WHOLE_SUBTREE;
}
}
else
{
searchScope = SearchScope.WHOLE_SUBTREE;
}
// Create the list of filters that will be used to process the searches.
LinkedList<SearchFilter> searchFilters = new LinkedList<SearchFilter>();
for (String filterString : filterStrings)
{
try
{
searchFilters.add(SearchFilter.createFilterFromString(filterString));
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_PARSE_FILTER.get(
filterString, e.getMessage());
err.println(message);
return 1;
}
}
// Transform the attributes to return from strings to attribute types.
LinkedHashSet<AttributeType> userAttributeTypes =
new LinkedHashSet<AttributeType>();
LinkedHashSet<AttributeType> operationalAttributeTypes =
new LinkedHashSet<AttributeType>();
for (String attributeName : attributeNames)
{
AttributeType t = DirectoryServer.getAttributeType(attributeName, true);
if (t.isOperational())
{
operationalAttributeTypes.add(t);
}
else
{
userAttributeTypes.add(t);
}
}
for (String objectClassName : objectClassNames)
{
ObjectClass c = DirectoryServer.getObjectClass(objectClassName, true);
for (AttributeType t : c.getRequiredAttributeChain())
{
if (t.isOperational())
{
operationalAttributeTypes.add(t);
}
else
{
userAttributeTypes.add(t);
}
}
for (AttributeType t : c.getOptionalAttributeChain())
{
if (t.isOperational())
{
operationalAttributeTypes.add(t);
}
else
{
userAttributeTypes.add(t);
}
}
}
// Set the base DNs for the import config.
LinkedList<DN> baseDNs = new LinkedList<DN>();
if (baseDNString.isPresent())
{
for (String dnString : baseDNString.getValues())
{
try
{
baseDNs.add(DN.decode(dnString));
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_PARSE_BASE_DN.get(
dnString, e.getMessage());
err.println(message);
return 1;
}
}
}
else
{
baseDNs.add(DN.nullDN());
}
// Get the time limit in milliseconds.
long timeLimitMillis;
try
{
if (timeLimit.isPresent())
{
timeLimitMillis = 1000L * timeLimit.getIntValue();
}
else
{
timeLimitMillis = 0;
}
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_PARSE_TIME_LIMIT.get(
String.valueOf(e));
err.println(message);
return 1;
}
// Convert the size limit to an integer.
int sizeLimitValue;
try
{
if (sizeLimit.isPresent())
{
sizeLimitValue = sizeLimit.getIntValue();
}
else
{
sizeLimitValue =0;
}
}
catch (Exception e)
{
Message message = ERR_LDIFSEARCH_CANNOT_PARSE_SIZE_LIMIT.get(
String.valueOf(e));
err.println(message);
return 1;
}
// Create the LDIF import configuration that will be used to read the source
// data.
LDIFImportConfig importConfig;
if (ldifFile.isPresent())
{
importConfig = new LDIFImportConfig(ldifFile.getValues());
}
else
{
importConfig = new LDIFImportConfig(System.in);
}
// Create the LDIF export configuration that will be used to write the
// matching entries.
LDIFExportConfig exportConfig;
if (outputFile.isPresent())
{
if (overwriteExisting.isPresent())
{
exportConfig = new LDIFExportConfig(outputFile.getValue(),
ExistingFileBehavior.OVERWRITE);
}
else
{
exportConfig = new LDIFExportConfig(outputFile.getValue(),
ExistingFileBehavior.APPEND);
}
}
else
{