import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomLdapClass {
String objectClass() default "";
String ou() default "";
}
import com.novell.ldap.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class CustomLdapClassProcessor {
public static void process(Object object) throws IllegalAccessException {
Class<?> clazz = object.getClass();
if (clazz.isAnnotationPresent(CustomLdapClass.class)) {
CustomLdapClass customLdapClass = clazz.getAnnotation(CustomLdapClass.class);
String objectClass = customLdapClass.objectClass();
String ou = customLdapClass.ou();
ArrayList<LDAPAttribute> attributes = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.isAnnotationPresent(LdapAttribute.class)) {
LdapAttribute ldapAttribute = field.getAnnotation(LdapAttribute.class);
String attributeName = ldapAttribute.name();
String attributeValue = field.get(object).toString();
LDAPAttribute attribute = new LDAPAttribute(attributeName, attributeValue);
attributes.add(attribute);
}
}
LDAPModificationSet operation = new LDAPModificationSet();
for (LDAPAttribute attribute : attributes) {
operation.add(LDAPModification.ADD, attribute);
}
// ...
}
}
}
@CustomLdapClass(objectClass = "inetOrgPerson", ou = "People")
public class User {
@LdapAttribute(name = "cn")
private String commonName;
@LdapAttribute(name = "sn")
private String surname;
@LdapAttribute(name = "givenName")
private String givenName;
}