InternalClientConnection cc =
InternalClientConnection.getRootConnection();
ByteString dnByteString =
ByteString.valueOf(
cre.getDN().toString());
ResultCode rc;
switch (cre.getChangeOperationType()) {
case MODIFY:
LOG.log(Level.INFO, "proparing to modify " + dnByteString);
ModifyChangeRecordEntry mcre =
(ModifyChangeRecordEntry) cre;
ModifyOperation op =
cc.processModify(dnByteString, mcre.getModifications());
rc = op.getResultCode();
if (rc.equals(ResultCode.
SUCCESS)) {
LOG.log(Level.INFO, "processed server modification " +
modListToString(op.getModifications()));
} else if (rc.equals(
ResultCode.
ATTRIBUTE_OR_VALUE_EXISTS)) {
// ignore this error
LOG.log(Level.INFO, "ignoring attribute that already exists: " +
modListToString(op.getModifications()));
} else if (rc.equals(ResultCode.NO_SUCH_ATTRIBUTE)) {
// This can happen if for instance the old configuration was
// changed so that the value of an attribute matches the default
// value of the attribute in the new configuration.
// Just log it and move on.
LOG.log(Level.INFO, "Ignoring attribute not found: " +
modListToString(op.getModifications()));
} else {
// report the error to the user
MessageBuilder error = op.getErrorMessage();
throw new ApplicationException(
ReturnCode.IMPORT_ERROR,
INFO_ERROR_APPLY_LDIF_MODIFY.get(dnByteString.toString(),
error != null ? error.toString() : ""),
null);
}
break;
case ADD:
LOG.log(Level.INFO, "preparing to add " + dnByteString);
AddChangeRecordEntry acre = (AddChangeRecordEntry) cre;
List<Attribute> attrs = acre.getAttributes();
ArrayList<RawAttribute> rawAttrs =
new ArrayList<RawAttribute>(attrs.size());
for (Attribute a : attrs) {
rawAttrs.add(new LDAPAttribute(a));
}
AddOperation addOp = cc.processAdd(dnByteString, rawAttrs);
rc = addOp.getResultCode();
if (rc.equals(ResultCode.SUCCESS)) {
LOG.log(Level.INFO, "processed server add " + addOp.getEntryDN());
} else if (rc.equals(ResultCode.ENTRY_ALREADY_EXISTS)) {
// Compare the attributes with the existing entry to see if we
// can ignore this add.
boolean ignore = true;
for (RawAttribute attr : rawAttrs) {
ArrayList<ByteString> values = attr.getValues();
for (ByteString value : values) {
CompareOperation compOp =
cc.processCompare(dnByteString, attr.getAttributeType(), value);
if (ResultCode.ASSERTION_FAILED.equals(compOp.getResultCode())) {
ignore = false;
break;
}
}
}
if (!ignore) {
MessageBuilder error = addOp.getErrorMessage();
throw new ApplicationException(
ReturnCode.IMPORT_ERROR,
INFO_ERROR_APPLY_LDIF_ADD.get(dnByteString.toString(),
error != null ? error.toString() : ""),
null);
}
} else {
boolean ignore = false;
if (rc.equals(ResultCode.ENTRY_ALREADY_EXISTS)) {
// The entry already exists. Compare the attributes with the
// existing entry to see if we can ignore this add.
try {
InternalSearchOperation searchOp =
cc.processSearch(
cre.getDN(),
SearchScope.BASE_OBJECT,
SearchFilter.createFilterFromString(
"objectclass=*"));
LinkedList<SearchResultEntry> se = searchOp.getSearchEntries();
if (se.size() > 0) {
SearchResultEntry e = se.get(0);
List<Attribute> eAttrs = new ArrayList<Attribute>();
eAttrs.addAll(e.getAttributes());
eAttrs.add(e.getObjectClassAttribute());
if (compareUserAttrs(attrs, eAttrs)) {
LOG.log(Level.INFO, "Ignoring failure to add " +
dnByteString + " since the existing entry's " +
"attributes are identical");
ignore = true;
}
}
} catch (Exception e) {
LOG.log(Level.INFO, "Error attempting to compare rejected add " +
"entry with existing entry", e);
}
}
if (!ignore) {
MessageBuilder error = addOp.getErrorMessage();
throw new ApplicationException(
ReturnCode.IMPORT_ERROR,
INFO_ERROR_APPLY_LDIF_ADD.get(dnByteString.toString(),
error != null ? error.toString() : ""),
null);
}
}
break;
case DELETE:
LOG.log(Level.INFO, "preparing to delete " + dnByteString);
DeleteOperation delOp = cc.processDelete(dnByteString);
rc = delOp.getResultCode();
if (rc.equals(ResultCode.SUCCESS)) {
LOG.log(Level.INFO, "processed server delete " +
delOp.getEntryDN());
} else {
// report the error to the user
MessageBuilder error = delOp.getErrorMessage();