📄 schemaops.java
字号:
/**
*
*
* Author: Chris Betts
* Date: 28/11/2002 / 17:02:19
*/
package com.ca.commons.jndi;
import javax.naming.directory.*;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import java.util.*;
import java.util.logging.*;
/**
* <p>The Schema Ops provides a number of convenience methods for accessing schema information.</p>
* <p/>
* <p>In addition, it allows the schema to be accessed even if a particular jndi provider
* does not provide a getSchemaOps() call, by attempting to access a subschema subentry call directly.
* (e.g. for a DSML directory).</p>
*/
public class SchemaOps
{
protected DirContext ctx = null; // the root jndi context used for all directory queries.
Attributes rawSchemaAttributes = null; // what is read from the directory subschema subentry
private String schemaRoot = null; // the entry in the directory that holds the schema attribute set - usually cn=schema
HashMap oids = new HashMap(1000); // a fast lookup list of oids to descriptive names.
final static String subschemaAttributeName = "subschemaSubentry";
private final static Logger log = Logger.getLogger(SchemaOps.class.getName());
public static final String SCHEMA_FAKE_OBJECT_CLASS_NAME = "synthetic_JXplorer_schema_object";
private static final BasicAttribute schemaObjectClassAttribute = new BasicAttribute("objectClass");
private ArrayList fullObjectClassArray = null; // cache the complete list of object classes; it gets used a bit.
private ArrayList fullAttributeNameArray = null; // cache the complete list of attribute names; it gets used a bit.
static
{
schemaObjectClassAttribute.add("top");
schemaObjectClassAttribute.add(SCHEMA_FAKE_OBJECT_CLASS_NAME);
}
/**
* <p>Initialise the SchemaOps object, and read the
* full schema from the directory. Note that this is a very
* expensive operation, that can involve downloading 10-100k from
* the directory!</p>
*/
public SchemaOps(DirContext context)
throws NamingException
{
ctx = context;
if (ctx == null)
{
setSchemaRoot("cn=schema"); // default - but doesn't do anything since ctx==null
loadOIDs(); // load static OIDs - for testing, and future off-line schema functionality
return;
}
log.finest("Reading Schema info from directory context");
setSchemaRoot(getSchemaRoot());
rawSchemaAttributes = getRawSchema();
//printRawSchema();
//System.out.println("\n\n\n\n");
loadOIDs();
}
/**
* <p>Initialise the SchemaOps object from an Attributes list.
* This constructor is intended for testing.
* </p>
*/
// package visibility for testing
public SchemaOps(Attributes rawSchemaAtts)
// throws NamingException
{
ctx = null;
rawSchemaAttributes = rawSchemaAtts;
setSchemaRoot("cn=schema");
loadOIDs();
log.finest("SCHEMA ROOTX:" + getSchemaRoot());
}
/**
* This attempts to translate an OID into a descriptive name. If it cannot
* find a translation, it will return the oid unchanged.
*
* @param oid the 'dot form' OID, e.g. "2.5.6.2" or whatever
* @return the human readable string form of the OID (e.g. "country")
*/
public String translateOID(String oid)
{
if (oids.containsKey(oid))
return (String) oids.get(oid);
else
return oid;
}
/**
* setup the global list of oids vs readable strings, by loading the schema and using the rfc defaults
*/
protected void loadOIDs()
{
loadOIDsFromSchema();
loadStaticOIDs();
}
/**
* Iterates through the schema finding OIDs and their corresponding descriptions.
*/
protected void loadOIDsFromSchema()
{
if (rawSchemaAttributes == null)
return;
try
{
NamingEnumeration rawSchemaAtts = rawSchemaAttributes.getAll();
while (rawSchemaAtts.hasMoreElements())
{
Attribute rawSchemaAtt = (Attribute) rawSchemaAtts.nextElement();
NamingEnumeration values = rawSchemaAtt.getAll();
while (values.hasMoreElements())
{
String value = (String) values.nextElement();
if (value.indexOf('(') == -1)
log.finest("skipping non schema attribute: " + rawSchemaAtt.getID() + ":" + value);
else
oids.put(getOID(value), getFirstName(value));
}
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "Unable to read schema oids: ", e);
}
}
protected void loadStaticOIDs()
{
// a quick pick of common syntaxes for Active Directory support
// (and other servers that don't publish syntax descriptions)
// taken from rfc 2252
oids.put("1.3.6.1.4.1.1466.115.121.1.1", "ACI Item");
oids.put("1.3.6.1.4.1.1466.115.121.1.2", "Access Point");
oids.put("1.3.6.1.4.1.1466.115.121.1.3", "Attribute Type Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.4", "Audio");
oids.put("1.3.6.1.4.1.1466.115.121.1.5", "Binary");
oids.put("1.3.6.1.4.1.1466.115.121.1.6", "Bit String");
oids.put("1.3.6.1.4.1.1466.115.121.1.7", "Boolean");
oids.put("1.3.6.1.4.1.1466.115.121.1.8", "Certificate");
oids.put("1.3.6.1.4.1.1466.115.121.1.9", "Certificate List");
oids.put("1.3.6.1.4.1.1466.115.121.1.10", "Certificate Pair");
oids.put("1.3.6.1.4.1.1466.115.121.1.11", "Country String");
oids.put("1.3.6.1.4.1.1466.115.121.1.12", "DN");
oids.put("1.3.6.1.4.1.1466.115.121.1.13", "Data Quality Syntax");
oids.put("1.3.6.1.4.1.1466.115.121.1.14", "Delivery Method");
oids.put("1.3.6.1.4.1.1466.115.121.1.15", "Directory String");
oids.put("1.3.6.1.4.1.1466.115.121.1.16", "DIT Content Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.17", "DIT Structure Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.18", "DL Submit Permission");
oids.put("1.3.6.1.4.1.1466.115.121.1.19", "DSA Quality Syntax");
oids.put("1.3.6.1.4.1.1466.115.121.1.20", "DSE Type");
oids.put("1.3.6.1.4.1.1466.115.121.1.21", "Enhanced Guide");
oids.put("1.3.6.1.4.1.1466.115.121.1.22", "Facsimile Telephone Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.23", "Fax");
oids.put("1.3.6.1.4.1.1466.115.121.1.24", "Generalized Time");
oids.put("1.3.6.1.4.1.1466.115.121.1.25", "Guide");
oids.put("1.3.6.1.4.1.1466.115.121.1.26", "IA5 String");
oids.put("1.3.6.1.4.1.1466.115.121.1.27", "INTEGER");
oids.put("1.3.6.1.4.1.1466.115.121.1.28", "JPEG");
oids.put("1.3.6.1.4.1.1466.115.121.1.54", "LDAP Syntax Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.56", "LDAP Schema Definition");
oids.put("1.3.6.1.4.1.1466.115.121.1.57", "LDAP Schema Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.29", "Master And Shadow Access Points");
oids.put("1.3.6.1.4.1.1466.115.121.1.30", "Matching Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.31", "Matching Rule Use Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.32", "Mail Preference");
oids.put("1.3.6.1.4.1.1466.115.121.1.33", "MHS OR Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.55", "Modify Rights");
oids.put("1.3.6.1.4.1.1466.115.121.1.34", "Name And Optional UID");
oids.put("1.3.6.1.4.1.1466.115.121.1.35", "Name Form Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.36", "Numeric String");
oids.put("1.3.6.1.4.1.1466.115.121.1.37", "Object Class Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.40", "Octet String");
oids.put("1.3.6.1.4.1.1466.115.121.1.38", "OID");
oids.put("1.3.6.1.4.1.1466.115.121.1.39", "Other Mailbox");
oids.put("1.3.6.1.4.1.1466.115.121.1.41", "Postal Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.42", "Protocol Information");
oids.put("1.3.6.1.4.1.1466.115.121.1.43", "Presentation Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.44", "Printable String");
oids.put("1.3.6.1.4.1.1466.115.121.1.58", "Substring Assertion");
oids.put("1.3.6.1.4.1.1466.115.121.1.45", "Subtree Specification");
oids.put("1.3.6.1.4.1.1466.115.121.1.46", "Supplier Information");
oids.put("1.3.6.1.4.1.1466.115.121.1.47", "Supplier Or Consumer");
oids.put("1.3.6.1.4.1.1466.115.121.1.48", "Supplier And Consumer");
oids.put("1.3.6.1.4.1.1466.115.121.1.49", "Supported Algorithm");
oids.put("1.3.6.1.4.1.1466.115.121.1.50", "Telephone Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.51", "Teletex Terminal Identifier");
oids.put("1.3.6.1.4.1.1466.115.121.1.52", "Telex Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.53", "UTC Time");
oids.put("1.3.6.1.4.1.1466.115.121.1.1", "ACI Item");
oids.put("1.3.6.1.4.1.1466.115.121.1.2", "Access Point");
oids.put("1.3.6.1.4.1.1466.115.121.1.3", "Attribute Type Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.4", "Audio");
oids.put("1.3.6.1.4.1.1466.115.121.1.5", "Binary");
oids.put("1.3.6.1.4.1.1466.115.121.1.6", "Bit String");
oids.put("1.3.6.1.4.1.1466.115.121.1.7", "Boolean");
oids.put("1.3.6.1.4.1.1466.115.121.1.8", "Certificate");
oids.put("1.3.6.1.4.1.1466.115.121.1.9", "Certificate List");
oids.put("1.3.6.1.4.1.1466.115.121.1.10", "Certificate Pair");
oids.put("1.3.6.1.4.1.1466.115.121.1.11", "Country String");
oids.put("1.3.6.1.4.1.1466.115.121.1.12", "DN");
oids.put("1.3.6.1.4.1.1466.115.121.1.13", "Data Quality Syntax");
oids.put("1.3.6.1.4.1.1466.115.121.1.14", "Delivery Method");
oids.put("1.3.6.1.4.1.1466.115.121.1.15", "Directory String");
oids.put("1.3.6.1.4.1.1466.115.121.1.16", "DIT Content Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.17", "DIT Structure Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.18", "DL Submit Permission");
oids.put("1.3.6.1.4.1.1466.115.121.1.19", "DSA Quality Syntax");
oids.put("1.3.6.1.4.1.1466.115.121.1.20", "DSE Type");
oids.put("1.3.6.1.4.1.1466.115.121.1.21", "Enhanced Guide");
oids.put("1.3.6.1.4.1.1466.115.121.1.22", "Facsimile Telephone Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.23", "Fax");
oids.put("1.3.6.1.4.1.1466.115.121.1.24", "Generalized Time");
oids.put("1.3.6.1.4.1.1466.115.121.1.25", "Guide");
oids.put("1.3.6.1.4.1.1466.115.121.1.26", "IA5 String");
oids.put("1.3.6.1.4.1.1466.115.121.1.27", "INTEGER");
oids.put("1.3.6.1.4.1.1466.115.121.1.28", "JPEG");
oids.put("1.3.6.1.4.1.1466.115.121.1.54", "LDAP Syntax Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.56", "LDAP Schema Definition");
oids.put("1.3.6.1.4.1.1466.115.121.1.57", "LDAP Schema Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.29", "Master And Shadow Access Points");
oids.put("1.3.6.1.4.1.1466.115.121.1.30", "Matching Rule Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.31", "Matching Rule Use Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.32", "Mail Preference");
oids.put("1.3.6.1.4.1.1466.115.121.1.33", "MHS OR Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.55", "Modify Rights");
oids.put("1.3.6.1.4.1.1466.115.121.1.34", "Name And Optional UID");
oids.put("1.3.6.1.4.1.1466.115.121.1.35", "Name Form Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.36", "Numeric String");
oids.put("1.3.6.1.4.1.1466.115.121.1.37", "Object Class Description");
oids.put("1.3.6.1.4.1.1466.115.121.1.40", "Octet String");
oids.put("1.3.6.1.4.1.1466.115.121.1.38", "OID");
oids.put("1.3.6.1.4.1.1466.115.121.1.39", "Other Mailbox");
oids.put("1.3.6.1.4.1.1466.115.121.1.41", "Postal Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.42", "Protocol Information");
oids.put("1.3.6.1.4.1.1466.115.121.1.43", "Presentation Address");
oids.put("1.3.6.1.4.1.1466.115.121.1.44", "Printable String");
oids.put("1.3.6.1.4.1.1466.115.121.1.58", "Substring Assertion");
oids.put("1.3.6.1.4.1.1466.115.121.1.45", "Subtree Specification");
oids.put("1.3.6.1.4.1.1466.115.121.1.46", "Supplier Information");
oids.put("1.3.6.1.4.1.1466.115.121.1.47", "Supplier Or Consumer");
oids.put("1.3.6.1.4.1.1466.115.121.1.48", "Supplier And Consumer");
oids.put("1.3.6.1.4.1.1466.115.121.1.49", "Supported Algorithm");
oids.put("1.3.6.1.4.1.1466.115.121.1.50", "Telephone Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.51", "Teletex Terminal Identifier");
oids.put("1.3.6.1.4.1.1466.115.121.1.52", "Telex Number");
oids.put("1.3.6.1.4.1.1466.115.121.1.53", "UTC Time");
}
/**
* Utility method to print a syntax subtree...
*
* @param syntaxRoot the root of the syntax tree to print out, e.g. "", "objectClasses"
* @throws NamingException
*/
protected void debugPrint(String syntaxRoot)
throws NamingException
{
System.out.println("---DEBUG PRINT---");
System.out.println("schema root: " + getSchemaRoot());
if (syntaxRoot.length() > 0 && syntaxRoot.startsWith("schema=") == false)
syntaxRoot = "schema=" + syntaxRoot;
tabbedDebugPrint(syntaxRoot, "");
System.out.println("-----------------");
}
protected void tabbedDebugPrint(String syntaxElement, String indent)
throws NamingException
{
System.out.println(indent + syntaxElement);
Attributes entry = getAttributes(syntaxElement);
System.out.println(indent + "--==< " + syntaxElement + ">==--");
if (entry == null)
System.out.println(indent + " ** NULL ENTRY **");
else
{
NamingEnumeration atts = entry.getAll();
while (atts.hasMoreElements())
{
Attribute att = (Attribute) atts.nextElement();
System.out.println(indent + "att " + att.getID());
NamingEnumeration values = att.getAll();
while (values.hasMoreElements())
System.out.println(indent + " " + values.nextElement().toString());
}
}
System.out.println(indent + "-");
ArrayList list = listEntryNames(syntaxElement);
if (list == null)
{
return;
}
for (int i = 0; i < list.size(); i++)
{
String nextLevel = syntaxElement;
if (nextLevel.length() > 0)
nextLevel = "," + nextLevel;
nextLevel = "schema=" + list.get(i) + nextLevel;
tabbedDebugPrint(nextLevel, "\t" + indent);
}
}
public void printRawSchema()
{
if (rawSchemaAttributes == null)
{
System.out.println("NO SCHEMA READ!");
return;
}
try
{
System.out.println("---RAW SCHEMA---");
Enumeration attEnum = rawSchemaAttributes.getAll();
while (attEnum.hasMoreElements())
{
Attribute att = (Attribute) attEnum.nextElement();
String ID = att.getID();
Enumeration vals = att.getAll();
while (vals.hasMoreElements())
System.out.println(ID + " : " + vals.nextElement());
}
}
catch (NamingException e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -