LdapAttribute.java revision 0
0N/A/*
2362N/A * Copyright 1999-2002 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.ldap;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Vector;
0N/Aimport javax.naming.*;
0N/Aimport javax.naming.directory.*;
0N/A
0N/A/**
0N/A * This subclass is used by LDAP to implement the schema calls.
0N/A * Basically, it keeps track of which context it is an attribute of
0N/A * so it can get the schema for that cotnext.
0N/A *
0N/A * @author Jon Ruiz
0N/A */
0N/Afinal class LdapAttribute extends BasicAttribute {
0N/A
0N/A static final long serialVersionUID = -4288716561020779584L;
0N/A
0N/A private transient DirContext baseCtx = null;
0N/A private Name rdn = new CompositeName();
0N/A
0N/A // these two are used to reconstruct the baseCtx if this attribute has
0N/A // been serialized (
0N/A private String baseCtxURL;
0N/A private Hashtable baseCtxEnv;
0N/A
0N/A public Object clone() {
0N/A LdapAttribute attr = new LdapAttribute(this.attrID, baseCtx, rdn);
0N/A attr.values = (Vector)values.clone();
0N/A return attr;
0N/A }
0N/A
0N/A /**
0N/A * Adds a new value to this attribute.
0N/A *
0N/A * @param attrVal The value to be added. If null, a null value is added to
0N/A * the attribute.
0N/A * @return true Always returns true.
0N/A */
0N/A public boolean add(Object attrVal) {
0N/A // LDAP attributes don't contain duplicate values so there's no need
0N/A // to check if the value already exists before adding it.
0N/A values.addElement(attrVal);
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new instance of an attribute.
0N/A *
0N/A * @param id The attribute's id. It cannot be null.
0N/A */
0N/A LdapAttribute(String id) {
0N/A super(id);
0N/A }
0N/A
/**
* Constructs a new instance of an attribute.
*
* @param id The attribute's id. It cannot be null.
* @param baseCtx the baseCtx object of this attribute
* @param rdn the RDN of the entry (relative to baseCtx)
*/
private LdapAttribute(String id, DirContext baseCtx, Name rdn) {
super(id);
this.baseCtx = baseCtx;
this.rdn = rdn;
}
/**
* Sets the baseCtx and rdn used to find the attribute's schema
* Used by LdapCtx.setParents().
*/
void setParent(DirContext baseCtx, Name rdn) {
this.baseCtx = baseCtx;
this.rdn = rdn;
}
/**
* returns the ctx this attribute came from. This call allows
* LDAPAttribute to be serializable. 'baseCtx' is transient so if
* it is null, the `baseCtxURL` is used to reconstruct the context
* to which calls are made.
*/
private DirContext getBaseCtx() throws NamingException {
if(baseCtx == null) {
if (baseCtxEnv == null) {
baseCtxEnv = new Hashtable(3);
}
baseCtxEnv.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
baseCtxEnv.put(Context.PROVIDER_URL,baseCtxURL);
baseCtx = (new InitialDirContext(baseCtxEnv));
}
return baseCtx;
}
/**
* This is called when the object is serialized. It is
* overridden so that the appropriate class variables can be set
* to re-construct the baseCtx when deserialized. Setting these
* variables is costly, so it is only done if the object
* is actually serialized.
*/
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
// setup internal state
this.setBaseCtxInfo();
// let the ObjectOutpurStream do the real work of serialization
out.defaultWriteObject();
}
/**
* sets the information needed to reconstruct the baseCtx if
* we are serialized. This must be called _before_ the object is
* serialized!!!
*/
private void setBaseCtxInfo() {
Hashtable realEnv = null;
Hashtable secureEnv = null;
if (baseCtx != null) {
realEnv = ((LdapCtx)baseCtx).envprops;
this.baseCtxURL = ((LdapCtx)baseCtx).getURL();
}
if(realEnv != null && realEnv.size() > 0 ) {
// remove any security credentials - otherwise the serialized form
// would store them in the clear
Enumeration keys = realEnv.keys();
while(keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (key.indexOf("security") != -1 ) {
//if we need to remove props, we must do it to a clone
//of the environment. cloning is expensive, so we only do
//it if we have to.
if(secureEnv == null) {
secureEnv = (Hashtable)realEnv.clone();
}
secureEnv.remove(key);
}
}
}
// set baseCtxEnv depending on whether we removed props or not
this.baseCtxEnv = (secureEnv == null ? realEnv : secureEnv);
}
/**
* Retrieves the syntax definition associated with this attribute.
* @return This attribute's syntax definition.
*/
public DirContext getAttributeSyntaxDefinition() throws NamingException {
// get the syntax id from the attribute def
DirContext schema = getBaseCtx().getSchema(rdn);
DirContext attrDef = (DirContext)schema.lookup(
LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
Attribute syntaxAttr = attrDef.getAttributes("").get("SYNTAX");
if(syntaxAttr == null || syntaxAttr.size() == 0) {
throw new NameNotFoundException(
getID() + "does not have a syntax associated with it");
}
String syntaxName = (String)syntaxAttr.get();
// look in the schema tree for the syntax definition
return (DirContext)schema.lookup(
LdapSchemaParser.SYNTAX_DEFINITION_NAME + "/" + syntaxName);
}
/**
* Retrieves this attribute's schema definition.
*
* @return This attribute's schema definition.
*/
public DirContext getAttributeDefinition() throws NamingException {
DirContext schema = getBaseCtx().getSchema(rdn);
return (DirContext)schema.lookup(
LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
}
}