下面列出了javax.naming.directory.DirContext#bind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param ctx
* @param ouBase If organizational units are given by the given obj then this parameter will be ignored, otherwise this is the ou where
* the new object will be inserted.
* @param obj
* @param args
* @throws NamingException
*/
public void create(final DirContext ctx, final String ouBase, final T obj, final Object... args) throws NamingException
{
final String dn = buildDn(ouBase, obj);
log.info("Create " + getObjectClass() + ": " + dn + ": " + getLogInfo(obj));
final Attributes attrs = new BasicAttributes();
final List<ModificationItem> modificationItems = getModificationItems(new ArrayList<ModificationItem>(), obj);
modificationItems.add(createModificationItem(DirContext.ADD_ATTRIBUTE, "objectClass", getObjectClass()));
final String[] additionalObjectClasses = getAdditionalObjectClasses(obj);
if (additionalObjectClasses != null) {
for (final String objectClass : additionalObjectClasses) {
modificationItems.add(createModificationItem(DirContext.ADD_ATTRIBUTE, "objectClass", objectClass));
}
}
for (final ModificationItem modItem : modificationItems) {
final Attribute attr = modItem.getAttribute();
LdapUtils.putAttribute(attrs, attr.getID(), (String) attr.get());
}
LdapUtils.putAttribute(attrs, "cn", LdapUtils.escapeCommonName(obj.getCommonName()));
onBeforeBind(dn, attrs, args);
ctx.bind(dn, null, attrs);
}
@SuppressWarnings("deprecation")
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
try {
LdapName baseDn = (LdapName)
context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
LdifParser parser = new LdifParser(ldifFile);
parser.open();
while (parser.hasMoreRecords()) {
LdapAttributes record = parser.getRecord();
LdapName dn = record.getName();
if(baseDn != null) {
dn = LdapUtils.removeFirst(dn, baseDn);
}
if(!rootNode.isEmpty()) {
dn = LdapUtils.prepend(dn, rootNode);
}
context.bind(dn, null, record);
}
} catch (Exception e) {
throw new UncategorizedLdapException("Failed to populate LDIF", e);
}
}
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
try {
LdapName baseDn = (LdapName)
context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);
LdifParser parser = new LdifParser(ldifFile);
parser.open();
while (parser.hasMoreRecords()) {
LdapAttributes record = parser.getRecord();
LdapName dn = record.getName();
if(baseDn != null) {
dn = LdapUtils.removeFirst(dn, baseDn);
}
if(!rootNode.isEmpty()) {
dn = LdapUtils.prepend(dn, rootNode);
}
context.bind(dn, null, record);
}
} catch (Exception e) {
throw new UncategorizedLdapException("Failed to populate LDIF", e);
}
}
private static void createOu(String ou) throws NamingException {
DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
Attributes attrs = new BasicAttributes(true);
Attribute objclass = new BasicAttribute("objectClass");
objclass.add("top");
objclass.add("organizationalUnit");
attrs.put(objclass);
attrs.put("ou", ou);
ldapContext.bind(constructOuDn(ou), null, attrs);
}
/**
* adds the entry given when attributes were set, to the directory
* with the set DN.
*/
public void add() throws NamingException{
DirContext ctx = new InitialDirContext(env);
ldapEntry e = new ldapEntry(processAttributes());
ctx.bind(dn, e);
ctx.close();
}
private void addMyUser(DirContext ctx, String name)
throws Exception {
MyUser user = new MyUser("amilaj", "Jayasekara", "Amila");
ctx.bind(name, user);
// Lookup
DirContext obj = (DirContext)ctx.lookup(name);
assertNotNull(obj);
LOG.info("User is bound to: " + obj.getNameInNamespace());
}