0N/A/*
2362N/A * Copyright (c) 1999, 2002, Oracle and/or its affiliates. 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle 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,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * 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
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 * @param baseCtx the baseCtx object of this attribute
0N/A * @param rdn the RDN of the entry (relative to baseCtx)
0N/A */
0N/A private LdapAttribute(String id, DirContext baseCtx, Name rdn) {
0N/A super(id);
0N/A this.baseCtx = baseCtx;
0N/A this.rdn = rdn;
0N/A }
0N/A
0N/A /**
0N/A * Sets the baseCtx and rdn used to find the attribute's schema
0N/A * Used by LdapCtx.setParents().
0N/A */
0N/A void setParent(DirContext baseCtx, Name rdn) {
0N/A this.baseCtx = baseCtx;
0N/A this.rdn = rdn;
0N/A }
0N/A
0N/A /**
0N/A * returns the ctx this attribute came from. This call allows
0N/A * LDAPAttribute to be serializable. 'baseCtx' is transient so if
0N/A * it is null, the `baseCtxURL` is used to reconstruct the context
0N/A * to which calls are made.
0N/A */
0N/A private DirContext getBaseCtx() throws NamingException {
0N/A if(baseCtx == null) {
0N/A if (baseCtxEnv == null) {
0N/A baseCtxEnv = new Hashtable(3);
0N/A }
0N/A baseCtxEnv.put(Context.INITIAL_CONTEXT_FACTORY,
0N/A "com.sun.jndi.ldap.LdapCtxFactory");
0N/A baseCtxEnv.put(Context.PROVIDER_URL,baseCtxURL);
0N/A baseCtx = (new InitialDirContext(baseCtxEnv));
0N/A }
0N/A return baseCtx;
0N/A }
0N/A
0N/A /**
0N/A * This is called when the object is serialized. It is
0N/A * overridden so that the appropriate class variables can be set
0N/A * to re-construct the baseCtx when deserialized. Setting these
0N/A * variables is costly, so it is only done if the object
0N/A * is actually serialized.
0N/A */
0N/A private void writeObject(java.io.ObjectOutputStream out)
0N/A throws IOException {
0N/A
0N/A // setup internal state
0N/A this.setBaseCtxInfo();
0N/A
0N/A // let the ObjectOutpurStream do the real work of serialization
0N/A out.defaultWriteObject();
0N/A }
0N/A
0N/A /**
0N/A * sets the information needed to reconstruct the baseCtx if
0N/A * we are serialized. This must be called _before_ the object is
0N/A * serialized!!!
0N/A */
0N/A private void setBaseCtxInfo() {
0N/A Hashtable realEnv = null;
0N/A Hashtable secureEnv = null;
0N/A
0N/A if (baseCtx != null) {
0N/A realEnv = ((LdapCtx)baseCtx).envprops;
0N/A this.baseCtxURL = ((LdapCtx)baseCtx).getURL();
0N/A }
0N/A
0N/A if(realEnv != null && realEnv.size() > 0 ) {
0N/A // remove any security credentials - otherwise the serialized form
0N/A // would store them in the clear
0N/A Enumeration keys = realEnv.keys();
0N/A while(keys.hasMoreElements()) {
0N/A String key = (String)keys.nextElement();
0N/A if (key.indexOf("security") != -1 ) {
0N/A
0N/A //if we need to remove props, we must do it to a clone
0N/A //of the environment. cloning is expensive, so we only do
0N/A //it if we have to.
0N/A if(secureEnv == null) {
0N/A secureEnv = (Hashtable)realEnv.clone();
0N/A }
0N/A secureEnv.remove(key);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // set baseCtxEnv depending on whether we removed props or not
0N/A this.baseCtxEnv = (secureEnv == null ? realEnv : secureEnv);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the syntax definition associated with this attribute.
0N/A * @return This attribute's syntax definition.
0N/A */
0N/A public DirContext getAttributeSyntaxDefinition() throws NamingException {
0N/A // get the syntax id from the attribute def
0N/A DirContext schema = getBaseCtx().getSchema(rdn);
0N/A DirContext attrDef = (DirContext)schema.lookup(
0N/A LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
0N/A
0N/A Attribute syntaxAttr = attrDef.getAttributes("").get("SYNTAX");
0N/A
0N/A if(syntaxAttr == null || syntaxAttr.size() == 0) {
0N/A throw new NameNotFoundException(
0N/A getID() + "does not have a syntax associated with it");
0N/A }
0N/A
0N/A String syntaxName = (String)syntaxAttr.get();
0N/A
0N/A // look in the schema tree for the syntax definition
0N/A return (DirContext)schema.lookup(
0N/A LdapSchemaParser.SYNTAX_DEFINITION_NAME + "/" + syntaxName);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves this attribute's schema definition.
0N/A *
0N/A * @return This attribute's schema definition.
0N/A */
0N/A public DirContext getAttributeDefinition() throws NamingException {
0N/A DirContext schema = getBaseCtx().getSchema(rdn);
0N/A
0N/A return (DirContext)schema.lookup(
0N/A LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
0N/A }
0N/A}