Does anyone have a basic example of using these two annotations from UnboundID's SDK to persist objects in an LDAP directory? I can't seem to find any info about the type of the argument to the @LDAPSetter
method or the valu开发者_开发知识库e returned from the @LDAPGetter
method.
To put it another way, how do I fill in the <?>
s:
/**
* Called when creating a Java object from an LDAP entry.
*/
@LDAPSetter(attribute="roleOccupants")
void initMembers(<?> occupants) {
throw new UnsupportedOperationException();
}
/**
* Called when turning a Java object into an LDAP entry.
*/
@LDAPGetter(attribute="roleOccupants")
<?> storeMembers() {
throw new UnsupportedOperationException();
}
It really depends on the value of the roleOccupants
attribute. One of the optional fields of the @LDAPGetter
and @LDAPSetter
annotations is encoderClass
, which allows you to specify an object encoder that will be used to convert between LDAP attribute values and Java data types. If you don't specify a custom encoder (and most of the time you shouldn't need to), then it will use the DefaultObjectEncoder
class to perform the work, and it supports the following data types:
- boolean
- byte[]
- char[]
- double
- float
- int
- long
- short
- java.lang.Boolean
- java.lang.Double
- java.lang.Float
- java.lang.Integer
- java.lang.Long
- java.lang.Short
- java.lang.String
- java.lang.StringBuffer
- java.lang.StringBuilder
- java.math.BigDecimal
- java.math.BigInteger
- java.net.URI
- java.net.URL
- java.util.Date
- java.util.UUID
- java.util.concurrent.atomic.AtomicInteger
- java.util.concurrent.atomic.AtomicLong
- com.unboundid.ldap.sdk.DN
- com.unboundid.ldap.sdk.Filter
- com.unboundid.ldap.sdk.LDAPURL
- com.unboundid.ldap.sdk.RDN
It also supports arrays, lists, and sets of any of the above types, and any kind of enumeration. See the class-level documentation for the com.unboundid.ldap.sdk.persist.DefaultObjectEncoder class for a more complete description of the supported data types and any constraints with u.
So really, the data type that you choose depends on which of the above is most appropriate for the kind of information that is stored in the corresponding LDAP attribute. In many cases, String may be the most appropriate, but in others you may rather have it treated as a number or a DN or a timestamp or something like that. If none of the default types is acceptable, then you can create your own custom ObjectEncoder instance to perform whatever translation you want.
Here's an example of usage for anyone else that's wondering. It automatically loads user data from ldap when you read an organizationalRole
.
UserRole
class:
@LDAPObject(structuralClass="organizationalRole", defaultParentDN="dc=Roles,dc=example,dc=com")
public class UserRole extends LDAPModel implements models.deadbolt.Role {
@LDAPField(attribute="cn",inRDN=true,filterUsage=FilterUsage.ALWAYS_ALLOWED)
public String name;
private List<User> members;
public List<User> getMembers() {
return members;
}
/**
* Called when creating a Java object from an LDAP entry.
*/
@LDAPSetter(attribute="roleOccupant")
void initMembers(String[] occupants) throws LDAPException {
members = new ArrayList<User>();
if (occupants == null || occupants.length == 0)
return;
for (String dn : occupants)
members.add(User.findByDN(dn));
}
/**
* Called when turning a Java object into an LDAP entry.
*/
@LDAPGetter(attribute="roleOccupant")
String[] storeMembers() {
if (members == null)
return null;
String[] member_strings = new String[members.size()];
for (int i = 0; i < members.size(); i++)
member_strings[i] = members.get(i).getDN();
return member_strings;
}
}
User
class:
@LDAPObject(structuralClass="inetOrgPerson",
auxiliaryClass={"mozillaOrgPerson","posixAccount","sambaSamAccount","shadowAccount"},
defaultParentDN="dc=People,dc=example,dc=com")
public class User extends LDAPModel implements RoleHolder {
...
@LDAPField(attribute="givenName",filterUsage=FilterUsage.ALWAYS_ALLOWED)
public String fname;
@LDAPField(attribute="sn",filterUsage=FilterUsage.ALWAYS_ALLOWED)
public String lname;
public static User findByDN(String dn) throws LDAPException {
LDAPConnection conn = ...
LDAPPersister<User> persister = LDAPPersister.getInstance(User.class);
return persister.get(dn, conn);
}
精彩评论