📄 dxattributes.java
字号:
*/
/*
public Vector getOrderedStructOCs()
{
try
{
if (orderedSOCs.size() != 0) return orderedSOCs;
if (baseObjectClass == null) setAllObjectClasses();
setOrderedSOCs(baseObjectClass);
return orderedSOCs;
}
catch (Exception e)
{
return orderedSOCs;
}
}
*/
/**
* Sets the vector of structural object classes
* in this attribute set.
* @param oc the object class to find (and add) the structural parents of.
*/
void setOrderedSOCs(String oc)
throws NamingException
{
orderedSOCs.add(oc);
if (oc.equalsIgnoreCase("top")) return; // recursive search finished.
String parent = schema.schemaLookup("ClassDefinition/" + oc, "SUP");
String struct = schema.schemaLookup("ClassDefinition/" + parent, "STRUCTURAL");
// try to figure out if that was a structural object class...
if ("true".equalsIgnoreCase(struct) )
{
setOrderedSOCs(parent); // recurse to next set of parents.
return; // finished.
}
/*
Attributes parents = null;
parents = schema.getAttributes("ClassDefinition/" + oc, new String[] {"SUP"}); // returns one attribute
NamingEnumeration parentList = parents.get("SUP").getAll(); // get that attribute's values.
while (parentList.hasMore())
{
String id = parentList.next().toString();
Attributes struct = schema.getAttributes("ClassDefinition/" + id, new String[] {"STRUCTURAL"});
// try to figure out if that was a structural object class...
if (struct != null && struct.get("STRUCTURAL") != null && ("true".equalsIgnoreCase(struct.get("STRUCTURAL").get().toString())) )
{
setOrderedSOCs(id); // recurse to next set of parents.
return; // finished.
}
}
*/
}
/**
* Adds an OID / ldapName combination to the hashtable
* of all oid / ldapNames.<p>
*
* If the oid is actually an ldap String (different servers
* return different things...) it doesn't bother registering
* the string...
*
* @param oid the numeric oid to register
* @param ldapName the name of the corresponding ldap descr
* @return true if new oid was registered, false if it was already known
*/
public boolean registerOID(String oid, String ldapName)
{
// Don't register non-oids...
char test = oid.charAt(0);
if (test < '0' || test > '9') // not an oid
{
return false;
}
// XXX ;binary hack for ASN1 attributes
if (oid.endsWith(";binary")) oid = oid.substring(0,oid.indexOf(";binary"));
if (ldapName.endsWith(";binary")) ldapName = ldapName.substring(0,ldapName.indexOf(";binary"));
if (attOIDs.contains(oid)==true) return false;
attOIDs.put(oid, ldapName); // add it to the list...
return true;
}
/**
* This method trims all empty attributes (attributes without values) from
* the DXAttributes object.
*/
public void removeEmptyAttributes()
{
Enumeration atts = getAll();
while (atts.hasMoreElements())
{
Attribute att = (Attribute)atts.nextElement();
if (att.size() == 0)
remove(att.getID());
}
}
/**
* <p>Sets the internal list of all attribute IDs needed for
* a given set of object classes, as well as noting which
* are mandatory and which optional.</p>
*
* <p>As an added wrinkle, this must be able to cope with attribute
* ID's expressed either as ldap strings, or as numeric OIDs. It
* does this by automatically detecting OIDs, and translating them
* to strings using schema lookups.</p>
*
* <p>This method uses the schema to create empty valued attributes for
* attributes which don't currently exist, but which are allowed.</p>
*
*/
public void expandAllAttributes()
{
if (schema == null) return;
Attribute oc = null;
oc = getAllObjectClasses();
try
{
// Quick Hack to eliminate 'fake attributes' used for top level of syntaxes...
//XXX Might want to redo if efficiency is ever a concern :-)
if (oc.contains(SchemaOps.SCHEMA_FAKE_OBJECT_CLASS_NAME) )
return; // ignore the synthetic 'schema' object classes...
NamingEnumeration ocs = oc.getAll();
// cycle through all object classes, finding attributes for each
while (ocs.hasMore())
{
String objectClass = (String)ocs.next();
Attributes ocAttrs = schema.getAttributes("ClassDefinition/" + objectClass);
Attribute mustAtt = ocAttrs.get("MUST"); // get mandatory attribute IDs
Attribute mayAtt = ocAttrs.get("MAY"); // get optional attribute IDs
if (mustAtt != null)
{
NamingEnumeration musts = mustAtt.getAll();
while (musts.hasMore())
{
String attOID = (String) musts.next();
//XXX ;binary hack
if (attOID.indexOf(";binary")>0) attOID = attOID.substring(0,attOID.indexOf(";binary"));
String ldapName = getldapName(attOID);
registerOID(attOID, ldapName);
if (get(ldapName)==null) // if we don't already have this attribute...
{
put(new DXAttribute(getldapName(attOID))); // ... add it to the list
//CB empty atts now. put(new DXAttribute(getldapName(attOID), null)); // ... add it to the list
}
if (must.contains(ldapName.toLowerCase())==false) // and if it isn't already mandatory
{
must.add(ldapName.toLowerCase()); // ... add it to the mandatory list as well
}
}
}
if (mayAtt != null)
{
NamingEnumeration mays = mayAtt.getAll();
while (mays.hasMore())
{
String attOID = (String) mays.next();
//XXX isNonString hack
if (attOID.indexOf(";binary")>0) attOID = attOID.substring(0,attOID.indexOf(";binary"));
String ldapName = getldapName(attOID);
registerOID(attOID, ldapName);
if (get(ldapName)==null) // if we don't already have this one...
{
put(new DXAttribute(getldapName(attOID))); // ... add it to the list
//put(new DXAttribute(getldapName(attOID), null)); // ... add it to the list
}
}
}
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "unable to read attribute list for object classes: ", e);
try
{
CBUtility.printEnumeration(oc.getAll());
}
catch (NamingException e2)
{
log.warning("...(further error: can't print object class list)...");
}
return;
}
catch (NullPointerException e)
{
log.log(Level.WARNING, "ERROR: unable to read list of object classes from schema - some functionality will not be available", e);
}
}
/**
* This method does it's darndnest to return a string ldap name.<p>
* First, it checks whether the string is <i>already</i> an ldap
* name; if it is, it returns it unchanged.<p>
*
* Secondly, it tries to find the ldap text name for an oid
* (i.e. converts 2.5.4.0 to 'objectClass').<p>
*
* Finally, if it <b>can't</b> find the name it returns the oid instead...
* (This shouldn't really happen, but means that the system may still work,
* although the raw OIDs aren't very user friendly)<p>
*/
public String getldapName(String attOID)
{
return (schema==null?attOID:schema.translateOID(attOID));
/*
try
{
// Don't register non-oids...
char test = attOID.charAt(0);
if (test < '0' || test > '9') // not an oid
{
return attOID;
}
if (attOIDs.containsKey(attOID)) // check if we already have one
return (String) attOIDs.get(attOID);
// specify search constraints to search subtree
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
constraints.setCountLimit(1);
constraints.setTimeLimit(0);
String searchfilter = "(NUMERICOID="+attOID + ")";
NamingEnumeration results = schema.search("AttributeDefinition", searchfilter, constraints);
if (results.hasMoreElements())
{
SearchResult result = (SearchResult) results.next();
Attributes a = result.getAttributes(); // may throw null pointer exception, caught below?
Attribute ldapName = (Attribute)a.get("NAME"); // may throw null pointer exception, caught below?
String name = (String)ldapName.get(); // may throw null pointer exception, caught below?
//XXX ;binary hack
if (name.indexOf(";binary")>0) name = name.substring(0,name.indexOf(";binary"));
return name;
}
else
{CBUtility.log("can't read schema for: " + attOID + "... no results"); return attOID; }
}
catch (Exception e)
{
{CBUtility.log("can't read schema for: " + attOID + "\n " +e); return attOID; }
}
*/
}
public String toString()
{
StringBuffer text = new StringBuffer("size (" + size() + ")\n");
NamingEnumeration allatts = this.getAll();
while (allatts.hasMoreElements())
{
Attribute fnord = (Attribute) allatts.nextElement();
if (fnord == null)
log.warning("bizarre null attribute in element list");
else
{
if (must != null && must.contains(fnord.getID()))
text.append("must ");
if (fnord instanceof DXAttribute)
text.append(" dx ").append(((DXAttribute)fnord).toDebugString()).append(" ");
else
{
String ID = fnord.getID();
text.append("\n " + ID + " (not DXAttribute)" );
try
{
if (fnord.size() == 0)
text.append(" " + " (empty) ");
else
{
Enumeration vals = fnord.getAll();
while (vals.hasMoreElements())
{
Object val = vals.nextElement();
String fnordel = (val==null)?"*null*":val.toString();
text.append(" '" + fnordel + "'");
}
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "whoops: Naming Exception reading " + ID, e);
}
}
}
}
return text.toString();
}
public void print() { print(null); }
public void print(String msg)
{
if (msg!=null) System.out.println(msg);
printAttributes(this);
}
public static void printAttributes(Attributes a)
{
if (a==null) System.out.println("null attributes set");
NamingEnumeration allatts = a.getAll();
printAttributeList(allatts);
}
public static void printAttributeList(NamingEnumeration en)
{
while (en.hasMoreElements())
{
Attribute fnord = (Attribute) en.nextElement();
if (fnord == null)
log.warning("bizarre null attribute in element list");
else
{
String ID = fnord.getID();
System.out.println(" " + ID);
try
{
Enumeration vals = fnord.getAll();
while (vals.hasMoreElements())
{
Object val = vals.nextElement();
String fnordel = (val==null)?"*null*":val.toString();
System.out.println(" " + fnordel);
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "whoops: Naming Exception reading " + ID, e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -