jdk/src/share/classes/com/sun/jndi/ldap/LdapAttribute.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 23882 7dbf42ed83ef
permissions -rw-r--r--
8054158: Fix typos in JNDI-related packages Reviewed-by: rriggs, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * This subclass is used by LDAP to implement the schema calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  * Basically, it keeps track of which context it is an attribute of
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23882
diff changeset
    37
  * so it can get the schema for that context.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * @author Jon Ruiz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
final class LdapAttribute extends BasicAttribute {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final long serialVersionUID = -4288716561020779584L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private transient DirContext baseCtx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Name rdn = new CompositeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // these two are used to reconstruct the baseCtx if this attribute has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // been serialized (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private String baseCtxURL;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    51
    private Hashtable<String, ? super String> baseCtxEnv;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    53
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        LdapAttribute attr = new LdapAttribute(this.attrID, baseCtx, rdn);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    56
        attr.values = (Vector<Object>)values.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      * Adds a new value to this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      * @param attrVal The value to be added. If null, a null value is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      *                the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      * @return true Always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public boolean add(Object attrVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // LDAP attributes don't contain duplicate values so there's no need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // to check if the value already exists before adding it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        values.addElement(attrVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      * Constructs a new instance of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      * @param id The attribute's id. It cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    LdapAttribute(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        super(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      * Constructs a new instance of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      * @param id The attribute's id. It cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      * @param baseCtx  the baseCtx object of this attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      * @param rdn      the RDN of the entry (relative to baseCtx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private LdapAttribute(String id, DirContext baseCtx, Name rdn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.baseCtx = baseCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.rdn = rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      * Sets the baseCtx and rdn used to find the attribute's schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      * Used by LdapCtx.setParents().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void setParent(DirContext baseCtx, Name rdn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.baseCtx = baseCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.rdn = rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * returns the ctx this attribute came from. This call allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * LDAPAttribute to be serializable. 'baseCtx' is transient so if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * it is null, the `baseCtxURL` is used to reconstruct the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * to which calls are made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private DirContext getBaseCtx() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if(baseCtx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (baseCtxEnv == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   114
                baseCtxEnv = new Hashtable<String, String>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            baseCtxEnv.put(Context.INITIAL_CONTEXT_FACTORY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                             "com.sun.jndi.ldap.LdapCtxFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            baseCtxEnv.put(Context.PROVIDER_URL,baseCtxURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            baseCtx = (new InitialDirContext(baseCtxEnv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return baseCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * This is called when the object is serialized. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * overridden so that the appropriate class variables can be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * to re-construct the baseCtx when deserialized. Setting these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * variables is costly, so it is only done if the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * is actually serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private void writeObject(java.io.ObjectOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // setup internal state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.setBaseCtxInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23882
diff changeset
   137
        // let the ObjectOutputStream do the real work of serialization
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        out.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * sets the information needed to reconstruct the baseCtx if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * we are serialized. This must be called _before_ the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * serialized!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   146
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private void setBaseCtxInfo() {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   148
        Hashtable<String, Object> realEnv = null;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   149
        Hashtable<String, Object> secureEnv = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (baseCtx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            realEnv = ((LdapCtx)baseCtx).envprops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            this.baseCtxURL = ((LdapCtx)baseCtx).getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if(realEnv != null && realEnv.size() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // remove any security credentials - otherwise the serialized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // would store them in the clear
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   159
            for (String key : realEnv.keySet()){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (key.indexOf("security") != -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    //if we need to remove props, we must do it to a clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    //of the environment. cloning is expensive, so we only do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    //it if we have to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if(secureEnv == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   166
                        secureEnv = (Hashtable<String, Object>)realEnv.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    secureEnv.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // set baseCtxEnv depending on whether we removed props or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.baseCtxEnv = (secureEnv == null ? realEnv : secureEnv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      * Retrieves the syntax definition associated with this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      * @return This attribute's syntax definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public DirContext getAttributeSyntaxDefinition() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // get the syntax id from the attribute def
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DirContext schema = getBaseCtx().getSchema(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        DirContext attrDef = (DirContext)schema.lookup(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Attribute syntaxAttr = attrDef.getAttributes("").get("SYNTAX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if(syntaxAttr == null || syntaxAttr.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new NameNotFoundException(
23882
7dbf42ed83ef 8009637: Some error messages are missing a space
igerasim
parents: 10324
diff changeset
   191
                getID() + " does not have a syntax associated with it");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        String syntaxName = (String)syntaxAttr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // look in the schema tree for the syntax definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return (DirContext)schema.lookup(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            LdapSchemaParser.SYNTAX_DEFINITION_NAME + "/" + syntaxName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
      * Retrieves this attribute's schema definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      * @return This attribute's schema definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public DirContext getAttributeDefinition() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        DirContext schema = getBaseCtx().getSchema(rdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return (DirContext)schema.lookup(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}