jdk/src/share/classes/com/sun/jndi/ldap/LdapCtx.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 23010 6dadb192ad81
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
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1999, 2013, 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: 2605
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: 2605
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: 2605
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2605
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2605
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 javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.naming.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.ldap.LdapName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.ldap.Rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    36
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import com.sun.jndi.toolkit.ctx.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.jndi.toolkit.dir.HierMemDirCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.jndi.toolkit.dir.SearchFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.jndi.ldap.ext.StartTlsResponseImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The LDAP context implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Implementation is not thread-safe. Caller must sync as per JNDI spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Members that are used directly or indirectly by internal worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * (Connection, EventQueue, NamingEventNotifier) must be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Connection - calls LdapClient.processUnsolicited(), which in turn calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   LdapCtx.convertControls() and LdapCtx.fireUnsolicited().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   convertControls() - no sync; reads envprops and 'this'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   fireUnsolicited() - sync on eventSupport for all references to 'unsolicited'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      (even those in other methods);  don't sync on LdapCtx in case caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      is already sync'ing on it - this would prevent Unsol events from firing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      and the Connection thread to block (thus preventing any other data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      from being read from the connection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      References to 'eventSupport' need not be sync'ed because these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *      methods can only be called after eventSupport has been set first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      (via addNamingListener()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * EventQueue - no direct or indirect calls to LdapCtx
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * NamingEventNotifier - calls newInstance() to get instance for run() to use;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      no sync needed for methods invoked on new instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * LdapAttribute links to LdapCtx in order to process getAttributeDefinition()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * and getAttributeSyntaxDefinition() calls. It invokes LdapCtx.getSchema(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * which uses schemaTrees (a Hashtable - already sync). Potential conflict
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * of duplicating construction of tree for same subschemasubentry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * but no inconsistency problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * NamingEnumerations link to LdapCtx for the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * 1. increment/decrement enum count so that ctx doesn't close the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *    underlying connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * 2. LdapClient handle to get next batch of results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * 3. Sets LdapCtx's response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * 4. Process return code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * 5. For narrowing response controls (using ctx's factories)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Since processing of NamingEnumeration by client is treated the same as method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * invocation on LdapCtx, caller is responsible for locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
final public class LdapCtx extends ComponentDirContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    implements EventDirContext, LdapContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Used to store arguments to the search method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    final static class SearchArgs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Name name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SearchControls cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String[] reqAttrs; // those attributes originally requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        SearchArgs(Name name, String filter, SearchControls cons, String[] ra) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.cons = cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.reqAttrs = ra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final boolean HARD_CLOSE = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static final boolean SOFT_CLOSE = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // -----------------  Constants  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final int DEFAULT_PORT = 389;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static final int DEFAULT_SSL_PORT = 636;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static final String DEFAULT_HOST = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final boolean DEFAULT_DELETE_RDN = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static final boolean DEFAULT_TYPES_ONLY = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final int DEFAULT_DEREF_ALIASES = 3; // always deref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int DEFAULT_LDAP_VERSION = LdapClient.LDAP_VERSION3_VERSION2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int DEFAULT_BATCH_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static final int DEFAULT_REFERRAL_MODE = LdapClient.LDAP_REF_IGNORE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final char DEFAULT_REF_SEPARATOR = '#';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Used by LdapPoolManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static final String DEFAULT_SSL_FACTORY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        "javax.net.ssl.SSLSocketFactory";       // use Sun's SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final int DEFAULT_REFERRAL_LIMIT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final String STARTTLS_REQ_OID = "1.3.6.1.4.1.1466.20037";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // schema operational and user attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static final String[] SCHEMA_ATTRIBUTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        { "objectClasses", "attributeTypes", "matchingRules", "ldapSyntaxes" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // --------------- Environment property names ----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // LDAP protocol version: "2", "3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static final String VERSION = "java.naming.ldap.version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // Binary-valued attributes. Space separated string of attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private static final String BINARY_ATTRIBUTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                        "java.naming.ldap.attributes.binary";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Delete old RDN during modifyDN: "true", "false"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static final String DELETE_RDN = "java.naming.ldap.deleteRDN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // De-reference aliases: "never", "searching", "finding", "always"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final String DEREF_ALIASES = "java.naming.ldap.derefAliases";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // Return only attribute types (no values)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final String TYPES_ONLY = "java.naming.ldap.typesOnly";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Separator character for encoding Reference's RefAddrs; default is '#'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static final String REF_SEPARATOR = "java.naming.ldap.ref.separator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // Socket factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final String SOCKET_FACTORY = "java.naming.ldap.factory.socket";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // Bind Controls (used by LdapReferralException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static final String BIND_CONTROLS = "java.naming.ldap.control.connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static final String REFERRAL_LIMIT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        "java.naming.ldap.referral.limit";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // trace BER (java.io.OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final String TRACE_BER = "com.sun.jndi.ldap.trace.ber";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Get around Netscape Schema Bugs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final String NETSCAPE_SCHEMA_BUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        "com.sun.jndi.ldap.netscape.schemaBugs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final String OLD_NETSCAPE_SCHEMA_BUG =
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10369
diff changeset
   179
        "com.sun.naming.netscape.schemaBugs";   // for backward compatibility
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // Timeout for socket connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final String CONNECT_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        "com.sun.jndi.ldap.connect.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     // Timeout for reading responses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final String READ_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        "com.sun.jndi.ldap.read.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // Environment property for connection pooling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static final String ENABLE_POOL = "com.sun.jndi.ldap.connect.pool";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // Environment property for the domain name (derived from this context's DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static final String DOMAIN_NAME = "com.sun.jndi.ldap.domainname";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   195
    // Block until the first search reply is received
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   196
    private static final String WAIT_FOR_REPLY =
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   197
        "com.sun.jndi.ldap.search.waitForReply";
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   198
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   199
    // Size of the queue of unprocessed search replies
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   200
    private static final String REPLY_QUEUE_SIZE =
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   201
        "com.sun.jndi.ldap.search.replyQueueSize";
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   202
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // ----------------- Fields that don't change -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static final NameParser parser = new LdapNameParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // controls that Provider needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private static final ControlFactory myResponseControlFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        new DefaultResponseControlFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static final Control manageReferralControl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        new ManageReferralControl(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static final HierMemDirCtx EMPTY_SCHEMA = new HierMemDirCtx();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        EMPTY_SCHEMA.setReadOnly(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            new SchemaViolationException("Cannot update schema object"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // ------------ Package private instance variables ----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // Cannot be private; used by enums
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // ------- Inherited by derived context instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    int port_number;                    // port number of server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    String hostname = null;             // host name of server (no brackets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                        //   for IPv6 literals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    LdapClient clnt = null;             // connection handle
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   227
    Hashtable<String, java.lang.Object> envprops = null; // environment properties of context
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    int handleReferrals = DEFAULT_REFERRAL_MODE; // how referral is handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    boolean hasLdapsScheme = false;     // true if the context was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        //  using an LDAPS URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // ------- Not inherited by derived context instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    String currentDN;                   // DN of this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    Name currentParsedDN;               // DN of this context
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   236
    Vector<Control> respCtls = null;    // Response controls read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    Control[] reqCtls = null;           // Controls to be sent with each request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // ------------- Private instance variables ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // ------- Inherited by derived context instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private OutputStream trace = null;  // output stream for BER debug output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private boolean netscapeSchemaBug = false;       // workaround
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private Control[] bindCtls = null;  // Controls to be sent with LDAP "bind"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private int referralHopLimit = DEFAULT_REFERRAL_LIMIT;  // max referral
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   248
    private Hashtable<String, DirContext> schemaTrees = null; // schema root of this context
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private int batchSize = DEFAULT_BATCH_SIZE;      // batch size for search results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private boolean deleteRDN = DEFAULT_DELETE_RDN;  // delete the old RDN when modifying DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private boolean typesOnly = DEFAULT_TYPES_ONLY;  // return attribute types (no values)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private int derefAliases = DEFAULT_DEREF_ALIASES;// de-reference alias entries during searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private char addrEncodingSeparator = DEFAULT_REF_SEPARATOR;  // encoding RefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   255
    private Hashtable<String, Boolean> binaryAttrs = null; // attr values returned as byte[]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private int connectTimeout = -1;         // no timeout value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private int readTimeout = -1;            // no timeout value
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   258
    private boolean waitForReply = true;     // wait for search response
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   259
    private int replyQueueSize  = -1;        // unlimited queue size
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private boolean useSsl = false;          // true if SSL protocol is active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private boolean useDefaultPortNumber = false; // no port number was supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // ------- Not inherited by derived context instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // True if this context was created by another LdapCtx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private boolean parentIsLdapCtx = false; // see composeName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private int hopCount = 1;                // current referral hop count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private String url = null;               // URL of context; see getURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private EventSupport eventSupport;       // Event support helper for this ctx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private boolean unsolicited = false;     // if there unsolicited listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private boolean sharable = true;         // can share connection with other ctx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // -------------- Constructors  -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   276
    @SuppressWarnings("unchecked")
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   277
    public LdapCtx(String dn, String host, int port_number,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   278
            Hashtable<?,?> props,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            boolean useSsl) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this.useSsl = this.hasLdapsScheme = useSsl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (props != null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   284
            envprops = (Hashtable<String, java.lang.Object>) props.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // SSL env prop overrides the useSsl argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if ("ssl".equals(envprops.get(Context.SECURITY_PROTOCOL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                this.useSsl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // %%% These are only examined when the context is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // %%% because they are only for debugging or workaround purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            trace = (OutputStream)envprops.get(TRACE_BER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (props.get(NETSCAPE_SCHEMA_BUG) != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                props.get(OLD_NETSCAPE_SCHEMA_BUG) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                netscapeSchemaBug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        currentDN = (dn != null) ? dn : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        currentParsedDN = parser.parse(currentDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        hostname = (host != null && host.length() > 0) ? host : DEFAULT_HOST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (hostname.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            hostname = hostname.substring(1, hostname.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (port_number > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            this.port_number = port_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            this.port_number = this.useSsl ? DEFAULT_SSL_PORT : DEFAULT_PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            this.useDefaultPortNumber = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   316
        schemaTrees = new Hashtable<>(11, 0.75f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        initEnv();
2605
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   318
        try {
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   319
            connect(false);
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   320
        } catch (NamingException e) {
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   321
            try {
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   322
                close();
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   323
            } catch (Exception e2) {
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   324
                // Nothing
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   325
            }
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   326
            throw e;
e8d0473c25e8 6717680: LdapCtx does not close the connection if initialization fails
weijun
parents: 2
diff changeset
   327
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    LdapCtx(LdapCtx existing, String newDN) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        useSsl = existing.useSsl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        hasLdapsScheme = existing.hasLdapsScheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        useDefaultPortNumber = existing.useDefaultPortNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        hostname = existing.hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        port_number = existing.port_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        currentDN = newDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (existing.currentDN == currentDN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            currentParsedDN = existing.currentParsedDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            currentParsedDN = parser.parse(currentDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        envprops = existing.envprops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        schemaTrees = existing.schemaTrees;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        clnt = existing.clnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        clnt.incRefCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        parentIsLdapCtx = ((newDN == null || newDN.equals(existing.currentDN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                           ? existing.parentIsLdapCtx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                           : true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // inherit these debugging/workaround flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        trace = existing.trace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        netscapeSchemaBug = existing.netscapeSchemaBug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        initEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public LdapContext newInstance(Control[] reqCtls) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        LdapContext clone = new LdapCtx(this, currentDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // Connection controls are inherited from environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // Set clone's request controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // setRequestControls() will clone reqCtls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        clone.setRequestControls(reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // --------------- Namespace Updates ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    // -- bind/rebind/unbind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // -- rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    // -- createSubcontext/destroySubcontext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    protected void c_bind(Name name, Object obj, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        c_bind(name, obj, null, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * attrs == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *      if obj is DirContext, attrs = obj.getAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * if attrs == null && obj == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *      disallow (cannot determine objectclass to use)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * if obj == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *      just create entry using attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *      objAttrs = create attributes for representing obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *      attrs += objAttrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *      create entry using attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    protected void c_bind(Name name, Object obj, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                          Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        Attributes inputAttrs = attrs; // Attributes supplied by caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                if (attrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        "cannot bind null object with no attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                attrs = Obj.determineBindAttrs(addrEncodingSeparator, obj, attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    false, name, this, envprops); // not cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            String newDN = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            attrs = addRdnAttributes(newDN, attrs, inputAttrs != attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            LdapEntry entry = new LdapEntry(newDN, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            LdapResult answer = clnt.add(entry, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    refCtx.bind(name, obj, inputAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    protected void c_rebind(Name name, Object obj, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        c_rebind(name, obj, null, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * attrs == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *    if obj is DirContext, attrs = obj.getAttributes().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * if attrs == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *    leave any existing attributes alone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *    (set attrs = {objectclass=top} if object doesn't exist)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *    replace all existing attributes with attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * if obj == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *      just create entry using attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *      objAttrs = create attributes for representing obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *      attrs += objAttrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *      create entry using attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    protected void c_rebind(Name name, Object obj, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        Continuation cont) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        Attributes inputAttrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Attributes origAttrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            // Check if name is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                origAttrs = c_getAttributes(name, null, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            } catch (NameNotFoundException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            // Name not bound, just add it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (origAttrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                c_bind(name, obj, attrs, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // there's an object there already, need to figure out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            // what to do about its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (attrs == null && obj instanceof DirContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                attrs = ((DirContext)obj).getAttributes("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            Attributes keepAttrs = (Attributes)origAttrs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (attrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // we're not changing any attrs, leave old attributes alone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                // Remove Java-related object classes from objectclass attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                Attribute origObjectClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    origAttrs.get(Obj.JAVA_ATTRIBUTES[Obj.OBJECT_CLASS]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                if (origObjectClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    // clone so that keepAttrs is not affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    origObjectClass = (Attribute)origObjectClass.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    for (int i = 0; i < Obj.JAVA_OBJECT_CLASSES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        origObjectClass.remove(Obj.JAVA_OBJECT_CLASSES_LOWER[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        origObjectClass.remove(Obj.JAVA_OBJECT_CLASSES[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    // update;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    origAttrs.put(origObjectClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                // remove all Java-related attributes except objectclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                for (int i = 1; i < Obj.JAVA_ATTRIBUTES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    origAttrs.remove(Obj.JAVA_ATTRIBUTES[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                attrs = origAttrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                attrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    Obj.determineBindAttrs(addrEncodingSeparator, obj, attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        inputAttrs != attrs, name, this, envprops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            String newDN = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            // remove entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            LdapResult answer = clnt.delete(newDN, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            Exception addEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                attrs = addRdnAttributes(newDN, attrs, inputAttrs != attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                // add it back using updated attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                LdapEntry entry = new LdapEntry(newDN, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                answer = clnt.add(entry, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (answer.resControls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    respCtls = appendVector(respCtls, answer.resControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   563
            } catch (NamingException | IOException ae) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                addEx = ae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if ((addEx != null && !(addEx instanceof LdapReferralException)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                // Attempt to restore old entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                LdapResult answer2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    clnt.add(new LdapEntry(newDN, keepAttrs), reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (answer2.resControls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    respCtls = appendVector(respCtls, answer2.resControls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (addEx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            // Rethrow exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (addEx instanceof NamingException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                throw (NamingException)addEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            } else if (addEx instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                throw (IOException)addEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    refCtx.rebind(name, obj, inputAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    protected void c_unbind(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            String fname = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            LdapResult answer = clnt.delete(fname, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            adjustDeleteStatus(fname, answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    refCtx.unbind(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    protected void c_rename(Name oldName, Name newName, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        Name oldParsed, newParsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        Name oldParent, newParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        String newRDN = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        String newSuperior = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        // assert (oldName instanceOf CompositeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        cont.setError(this, oldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            // permit oldName to be empty (for processing referral contexts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (oldName.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                oldParent = parser.parse("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                oldParsed = parser.parse(oldName.get(0)); // extract DN & parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                oldParent = oldParsed.getPrefix(oldParsed.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (newName instanceof CompositeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                newParsed = parser.parse(newName.get(0)); // extract DN & parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                newParsed = newName; // CompoundName/LdapName is already parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            newParent = newParsed.getPrefix(newParsed.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if(!oldParent.equals(newParent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                if (!clnt.isLdapv3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                  "LDAPv2 doesn't support changing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                  "the parent as a result of a rename");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    newSuperior = fullyQualifiedName(newParent.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            newRDN = newParsed.get(newParsed.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            LdapResult answer = clnt.moddn(fullyQualifiedName(oldName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                    newRDN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                    deleteRDN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                    newSuperior,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                    reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                processReturnCode(answer, oldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            // Record the new RDN (for use after the referral is followed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            e.setNewRdn(newRDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            // Cannot continue when a referral has been received and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // newSuperior name was supplied (because the newSuperior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            // relative to a naming context BEFORE the referral is followed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (newSuperior != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                PartialResultException pre = new PartialResultException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    "Cannot continue referral processing when newSuperior is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    "nonempty: " + newSuperior);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                pre.setRootCause(cont.fillInException(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                throw cont.fillInException(pre);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    refCtx.rename(oldName, newName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    protected Context c_createSubcontext(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        return c_createSubcontext(name, null, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    protected DirContext c_createSubcontext(Name name, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                            Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        Attributes inputAttrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (attrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                  // add structural objectclass; name needs to have "cn"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                  Attribute oc = new BasicAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                      Obj.JAVA_ATTRIBUTES[Obj.OBJECT_CLASS],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                      Obj.JAVA_OBJECT_CLASSES[Obj.STRUCTURAL]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                  oc.add("top");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                  attrs = new BasicAttributes(true); // case ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                  attrs.put(oc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            String newDN = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            attrs = addRdnAttributes(newDN, attrs, inputAttrs != attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            LdapEntry entry = new LdapEntry(newDN, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            LdapResult answer = clnt.add(entry, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            // creation successful, get back live object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            return new LdapCtx(this, newDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    return refCtx.createSubcontext(name, inputAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    protected void c_destroySubcontext(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            String fname = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            LdapResult answer = clnt.delete(fname, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            adjustDeleteStatus(fname, answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    refCtx.destroySubcontext(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Adds attributes from RDN to attrs if not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Note that if attrs already contains an attribute by the same name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * or if the distinguished name is empty, then leave attrs unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @param dn The non-null DN of the entry to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @param attrs The non-null attributes of entry to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @param directUpdate Whether attrs can be updated directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @returns Non-null attributes with attributes from the RDN added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    private static Attributes addRdnAttributes(String dn, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        boolean directUpdate) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            // Handle the empty name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if (dn.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // Parse string name into list of RDNs
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   922
            List<Rdn> rdnList = (new LdapName(dn)).getRdns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // Get leaf RDN
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   925
            Rdn rdn = rdnList.get(rdnList.size() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            Attributes nameAttrs = rdn.toAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            // Add attributes of RDN to attrs if not already there
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   929
            NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            Attribute nameAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            while (enum_.hasMore()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   932
                nameAttr = enum_.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                // If attrs already has the attribute, don't change or add to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                if (attrs.get(nameAttr.getID()) ==  null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                     * When attrs.isCaseIgnored() is false, attrs.get() will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                     * return null when the case mis-matches for otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                     * equal attrIDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                     * As the attrIDs' case is irrelevant for LDAP, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                     * the case of attrIDs even when attrs.isCaseIgnored() is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                     * false. This is done by explicitly comparing the elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                     * the enumeration of IDs with their case ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    if (!attrs.isCaseIgnored() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                            containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    if (!directUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        attrs = (Attributes)attrs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        directUpdate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    attrs.put(nameAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   963
    private static boolean containsIgnoreCase(NamingEnumeration<String> enumStr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                                String str) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        String strEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        while (enumStr.hasMore()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   968
             strEntry = enumStr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
             if (strEntry.equalsIgnoreCase(str)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private void adjustDeleteStatus(String fname, LdapResult answer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if (answer.status == LdapClient.LDAP_NO_SUCH_OBJECT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            answer.matchedDN != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                // %%% RL: are there any implications for referrals?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                Name orig = parser.parse(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                Name matched = parser.parse(answer.matchedDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                if ((orig.size() - matched.size()) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    answer.status = LdapClient.LDAP_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            } catch (NamingException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Append the the second Vector onto the first Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * (v2 must be non-null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   995
    private static <T> Vector<T> appendVector(Vector<T> v1, Vector<T> v2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        if (v1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            v1 = v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            for (int i = 0; i < v2.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                v1.addElement(v2.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return v1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    // ------------- Lookups and Browsing -------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    // lookup/lookupLink
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    // list/listBindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    protected Object c_lookupLink(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return c_lookup(name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    protected Object c_lookup(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        Object obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        Attributes attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            SearchControls cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            cons.setSearchScope(SearchControls.OBJECT_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            cons.setReturningAttributes(null); // ask for all attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            cons.setReturningObjFlag(true); // need values to construct obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            LdapResult answer = doSearchOnce(name, "(objectClass=*)", cons, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            // should get back 1 SearchResponse and 1 SearchResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            if (answer.entries == null || answer.entries.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                // found it but got no attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                attrs = new BasicAttributes(LdapClient.caseIgnore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1040
                LdapEntry entry = answer.entries.elementAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                attrs = entry.attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1043
                Vector<Control> entryCtls = entry.respCtls; // retrieve entry controls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                if (entryCtls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    appendVector(respCtls, entryCtls); // concatenate controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (attrs.get(Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                // serialized object or object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                obj = Obj.decodeObject(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                obj = new LdapCtx(this, fullyQualifiedName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    return refCtx.lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            return DirectoryManager.getObjectInstance(obj, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                this, envprops, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            NamingException e2 = new NamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    "problem generating object using object factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1099
    protected NamingEnumeration<NameClassPair> c_list(Name name, Continuation cont)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        SearchControls cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        String[] classAttrs = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        classAttrs[0] = Obj.JAVA_ATTRIBUTES[Obj.OBJECT_CLASS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        classAttrs[1] = Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        cons.setReturningAttributes(classAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // set this flag to override the typesOnly flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        cons.setReturningObjFlag(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        LdapResult answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            answer = doSearch(name, "(objectClass=*)", cons, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            // list result may contain continuation references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            if ((answer.status != LdapClient.LDAP_SUCCESS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                (answer.referrals != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            return new LdapNamingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    return refCtx.list(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        } catch (LimitExceededException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            LdapNamingEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                new LdapNamingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            res.setNamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    (LimitExceededException)cont.fillInException(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        } catch (PartialResultException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            LdapNamingEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                new LdapNamingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            res.setNamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    (PartialResultException)cont.fillInException(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1172
    protected NamingEnumeration<Binding> c_listBindings(Name name, Continuation cont)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        SearchControls cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        cons.setReturningAttributes(null); // ask for all attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        cons.setReturningObjFlag(true); // need values to construct obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        LdapResult answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            answer = doSearch(name, "(objectClass=*)", cons, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            // listBindings result may contain continuation references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if ((answer.status != LdapClient.LDAP_SUCCESS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                (answer.referrals != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            return new LdapBindingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            while (true) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1200
                @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    return refCtx.listBindings(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        } catch (LimitExceededException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            LdapBindingEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                new LdapBindingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1222
            res.setNamingException(cont.fillInException(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        } catch (PartialResultException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            LdapBindingEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                new LdapBindingEnumeration(this, answer, name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1229
            res.setNamingException(cont.fillInException(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    // --------------- Name-related Methods -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    // -- getNameParser/getNameInNamespace/composeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    protected NameParser c_getNameParser(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        // ignore name, always return same parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        return parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    public String getNameInNamespace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        return currentDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    public Name composeName(Name name, Name prefix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        Name result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // Handle compound names.  A pair of LdapNames is an easy case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if ((name instanceof LdapName) && (prefix instanceof LdapName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            result = (Name)(prefix.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            result.addAll(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            return new CompositeName().add(result.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        if (!(name instanceof CompositeName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            name = new CompositeName().add(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        if (!(prefix instanceof CompositeName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            prefix = new CompositeName().add(prefix.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        int prefixLast = prefix.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        if (name.isEmpty() || prefix.isEmpty() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                name.get(0).equals("") || prefix.get(prefixLast).equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            return super.composeName(name, prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        result = (Name)(prefix.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        result.addAll(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        if (parentIsLdapCtx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            String ldapComp = concatNames(result.get(prefixLast + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                          result.get(prefixLast));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            result.remove(prefixLast + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            result.remove(prefixLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            result.add(prefixLast, ldapComp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    private String fullyQualifiedName(Name rel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        return rel.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                ? currentDN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                : fullyQualifiedName(rel.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    private String fullyQualifiedName(String rel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        return (concatNames(rel, currentDN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    // used by LdapSearchEnumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    private static String concatNames(String lesser, String greater) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if (lesser == null || lesser.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            return greater;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        } else if (greater == null || greater.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return lesser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            return (lesser + "," + greater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
   // --------------- Reading and Updating Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
   // getAttributes/modifyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    protected Attributes c_getAttributes(Name name, String[] attrIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        SearchControls cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        cons.setSearchScope(SearchControls.OBJECT_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        cons.setReturningAttributes(attrIds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            LdapResult answer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                doSearchOnce(name, "(objectClass=*)", cons, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            if (answer.entries == null || answer.entries.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                return new BasicAttributes(LdapClient.caseIgnore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            // get attributes from result
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1337
            LdapEntry entry = answer.entries.elementAt(0);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1338
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1339
            Vector<Control> entryCtls = entry.respCtls; // retrieve entry controls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (entryCtls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                appendVector(respCtls, entryCtls); // concatenate controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            // do this so attributes can find their schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            setParents(entry.attributes, (Name) name.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            return (entry.attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    return refCtx.getAttributes(name, attrIds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    protected void c_modifyAttributes(Name name, int mod_op, Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            if (attrs == null || attrs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                return; // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            String newDN = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            int jmod_op = convertToLdapModCode(mod_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            // construct mod list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            int[] jmods = new int[attrs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            Attribute[] jattrs = new Attribute[attrs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1398
            NamingEnumeration<? extends Attribute> ae = attrs.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            for(int i = 0; i < jmods.length && ae.hasMore(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                jmods[i] = jmod_op;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1401
                jattrs[i] = ae.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            LdapResult answer = clnt.modify(newDN, jmods, jattrs, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                    refCtx.modifyAttributes(name, mod_op, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    protected void c_modifyAttributes(Name name, ModificationItem[] mods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            if (mods == null || mods.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                return; // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            String newDN = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            // construct mod list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            int[] jmods = new int[mods.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            Attribute[] jattrs = new Attribute[mods.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            ModificationItem mod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            for (int i = 0; i < jmods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                mod = mods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                jmods[i] = convertToLdapModCode(mod.getModificationOp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                jattrs[i] = mod.getAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            LdapResult answer = clnt.modify(newDN, jmods, jattrs, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                    refCtx.modifyAttributes(name, mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    private static int convertToLdapModCode(int mod_op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        switch (mod_op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        case DirContext.ADD_ATTRIBUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            return(LdapClient.ADD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        case DirContext.REPLACE_ATTRIBUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            return (LdapClient.REPLACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        case DirContext.REMOVE_ATTRIBUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            return (LdapClient.DELETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            throw new IllegalArgumentException("Invalid modification code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
   // ------------------- Schema -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    protected DirContext c_getSchema(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            return getSchemaTree(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    protected DirContext c_getSchemaClassDefinition(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                                    Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            // retrieve the objectClass attribute from LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            Attribute objectClassAttr = c_getAttributes(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                new String[]{"objectclass"}, cont).get("objectclass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (objectClassAttr == null || objectClassAttr.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                return EMPTY_SCHEMA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            // retrieve the root of the ObjectClass schema tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            Context ocSchema = (Context) c_getSchema(name, cont).lookup(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                LdapSchemaParser.OBJECTCLASS_DEFINITION_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            // create a context to hold the schema objects representing the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            // classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            HierMemDirCtx objectClassCtx = new HierMemDirCtx();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            DirContext objectClassDef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            String objectClassName;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1565
            for (Enumeration<?> objectClasses = objectClassAttr.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                objectClasses.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                objectClassName = (String)objectClasses.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                // %%% Should we fail if not found, or just continue?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                objectClassDef = (DirContext)ocSchema.lookup(objectClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                objectClassCtx.bind(objectClassName, objectClassDef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            // Make context read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            objectClassCtx.setReadOnly(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                new SchemaViolationException("Cannot update schema object"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            return (DirContext)objectClassCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * getSchemaTree first looks to see if we have already built a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * schema tree for the given entry. If not, it builds a new one and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * stores it in our private hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    private DirContext getSchemaTree(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        String subschemasubentry = getSchemaEntry(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1591
        DirContext schemaTree = schemaTrees.get(subschemasubentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        if(schemaTree==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            if(debug){System.err.println("LdapCtx: building new schema tree " + this);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            schemaTree = buildSchemaTree(subschemasubentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            schemaTrees.put(subschemasubentry, schemaTree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        return schemaTree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * buildSchemaTree builds the schema tree corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * given subschemasubentree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    private DirContext buildSchemaTree(String subschemasubentry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        // get the schema entry itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        // DO ask for return object here because we need it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        // create context. Since asking for all attrs, we won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        // be transmitting any specific attrIDs (like Java-specific ones).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        SearchControls constraints = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            SearchControls(SearchControls.OBJECT_SCOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                0, 0, /* count and time limits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                SCHEMA_ATTRIBUTES /* return schema attrs */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                true /* return obj */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                false /*deref link */ );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        Name sse = (new CompositeName()).add(subschemasubentry);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1621
        NamingEnumeration<SearchResult> results =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            searchAux(sse, "(objectClass=subschema)", constraints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            false, true, new Continuation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if(!results.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            throw new OperationNotSupportedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                "Cannot get read subschemasubentry: " + subschemasubentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1629
        SearchResult result = results.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        results.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        Object obj = result.getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        if(!(obj instanceof LdapCtx)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            throw new NamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                "Cannot get schema object as DirContext: " + subschemasubentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        return LdapSchemaCtx.createSchemaTree(envprops, subschemasubentry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            (LdapCtx)obj /* schema entry */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            result.getAttributes() /* schema attributes */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            netscapeSchemaBug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * getSchemaEntree returns the DN of the subschemasubentree for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * given entree. It first looks to see if the given entry has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * a subschema different from that of the root DIT (by looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * a "subschemasubentry" attribute). If it doesn't find one, it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * the one for the root of the DIT (by looking for the root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * "subschemasubentry" attribute).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * This function is called regardless of the server's version, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * an administrator may have setup the server to support client schema
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23010
diff changeset
  1654
     * queries. If this function tries a search on a v2 server that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * doesn't support schema, one of these two things will happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * 1) It will get an exception when querying the root DSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * 2) It will not find a subschemasubentry on the root DSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * If either of these things occur and the server is not v3, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * throw OperationNotSupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * the relative flag tells whether the given name is relative to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    private String getSchemaEntry(Name name, boolean relative)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        // Asks for operational attribute "subschemasubentry"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        SearchControls constraints = new SearchControls(SearchControls.OBJECT_SCOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            0, 0, /* count and time limits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            new String[]{"subschemasubentry"} /* attr to return */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            false /* returning obj */,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            false /* deref link */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1674
        NamingEnumeration<SearchResult> results;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            results = searchAux(name, "objectclass=*", constraints, relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                true, new Continuation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        } catch (NamingException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            if (!clnt.isLdapv3 && currentDN.length() == 0 && name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                // we got an error looking for a root entry on an ldapv2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                // server. The server must not support schema.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                throw new OperationNotSupportedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                    "Cannot get schema information from server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        if (!results.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                "Requesting schema of nonexistent entry: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1695
        SearchResult result = results.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        results.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        Attribute schemaEntryAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            result.getAttributes().get("subschemasubentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        //System.err.println("schema entry attrs: " + schemaEntryAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        if (schemaEntryAttr == null || schemaEntryAttr.size() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            if (currentDN.length() == 0 && name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                // the server doesn't have a subschemasubentry in its root DSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                // therefore, it doesn't support schema.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                throw new OperationNotSupportedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    "Cannot read subschemasubentry of root DSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                return getSchemaEntry(new CompositeName(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        return (String)(schemaEntryAttr.get()); // return schema entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    // package-private; used by search enum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    // Set attributes to point to this context in case some one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    // asked for their schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    void setParents(Attributes attrs, Name name) throws NamingException {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1720
        NamingEnumeration<? extends Attribute> ae = attrs.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        while(ae.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            ((LdapAttribute) ae.next()).setParent(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * Returns the URL associated with this context; used by LdapAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * after deserialization to get pointer to this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    String getURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            url = LdapURL.toUrlString(hostname, port_number, currentDN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                hasLdapsScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
   // --------------------- Searches -----------------------------
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1740
    protected NamingEnumeration<SearchResult> c_search(Name name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                                         Attributes matchingAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        return c_search(name, matchingAttributes, null, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1747
    protected NamingEnumeration<SearchResult> c_search(Name name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                                         Attributes matchingAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                                         String[] attributesToReturn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        SearchControls cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        cons.setReturningAttributes(attributesToReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        String filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            filter = SearchFilter.format(matchingAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        return c_search(name, filter, cons, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1764
    protected NamingEnumeration<SearchResult> c_search(Name name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                                         String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                                         SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            throws NamingException {
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  1769
        return searchAux(name, filter, cloneSearchControls(cons), true,
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  1770
                 waitForReply, cont);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1773
    protected NamingEnumeration<SearchResult> c_search(Name name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                                         String filterExpr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                                         Object[] filterArgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                                         SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        String strfilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            strfilter = SearchFilter.format(filterExpr, filterArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        return c_search(name, strfilter, cons, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        // Used by NamingNotifier
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1790
    NamingEnumeration<SearchResult> searchAux(Name name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        boolean relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        boolean waitForReply, Continuation cont) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        LdapResult answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        String[] tokens = new String[2];    // stores ldap compare op. values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        String[] reqAttrs;                  // remember what was asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            cons = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        reqAttrs = cons.getReturningAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        // if objects are requested then request the Java attributes too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        // so that the objects can be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        if (cons.getReturningObjFlag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            if (reqAttrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                // check for presence of "*" (user attributes wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                boolean hasWildcard = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                for (int i = reqAttrs.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                    if (reqAttrs[i].equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                        hasWildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                if (! hasWildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    String[] totalAttrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                        new String[reqAttrs.length +Obj.JAVA_ATTRIBUTES.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    System.arraycopy(reqAttrs, 0, totalAttrs, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                        reqAttrs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    System.arraycopy(Obj.JAVA_ATTRIBUTES, 0, totalAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                        reqAttrs.length, Obj.JAVA_ATTRIBUTES.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                    cons.setReturningAttributes(totalAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        LdapCtx.SearchArgs args =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            new LdapCtx.SearchArgs(name, filter, cons, reqAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            // see if this can be done as a compare, otherwise do a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            if (searchToCompare(filter, cons, tokens)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                //System.err.println("compare triggered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                answer = compare(name, tokens[0], tokens[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                if (! (answer.compareToSearchResult(fullyQualifiedName(name)))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                answer = doSearch(name, filter, cons, relative, waitForReply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                // search result may contain referrals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                processReturnCode(answer, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            return new LdapSearchEnumeration(this, answer,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1849
                                             fullyQualifiedName(name),
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1850
                                             args, cont);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1859
                @SuppressWarnings("unchecked")
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1860
                LdapReferralContext refCtx = (LdapReferralContext)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  1861
                        e.getReferralContext(envprops, bindCtls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                    return refCtx.search(name, filter, cons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        } catch (LimitExceededException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            LdapSearchEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                new LdapSearchEnumeration(this, answer, fullyQualifiedName(name),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                                          args, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            res.setNamingException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        } catch (PartialResultException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            LdapSearchEnumeration res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                new LdapSearchEnumeration(this, answer, fullyQualifiedName(name),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                                          args, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            res.setNamingException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            throw cont.fillInException(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    LdapResult getSearchReply(LdapClient eClnt, LdapResult res)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        // ensureOpen() won't work here because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        // session was associated with previous connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        // %%% RL: we can actually allow the enumeration to continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        // using the old handle but other weird things might happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        // when we hit a referral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        if (clnt != eClnt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            throw new CommunicationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                "Context's connection changed; unable to continue enumeration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            return eClnt.getSearchReply(batchSize, res, binaryAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            throw e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    // Perform a search. Expect 1 SearchResultEntry and the SearchResultDone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    private LdapResult doSearchOnce(Name name, String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        SearchControls cons, boolean relative) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        int savedBatchSize = batchSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        batchSize = 2; // 2 protocol elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        LdapResult answer = doSearch(name, filter, cons, relative, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        batchSize = savedBatchSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    private LdapResult doSearch(Name name, String filter, SearchControls cons,
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  1940
        boolean relative, boolean waitForReply) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                switch (cons.getSearchScope()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                case SearchControls.OBJECT_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                    scope = LdapClient.SCOPE_BASE_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                case SearchControls.ONELEVEL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                    scope = LdapClient.SCOPE_ONE_LEVEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                case SearchControls.SUBTREE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                    scope = LdapClient.SCOPE_SUBTREE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                // If cons.getReturningObjFlag() then caller should already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                // have make sure to request the appropriate attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                String[] retattrs = cons.getReturningAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                if (retattrs != null && retattrs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                    // Ldap treats null and empty array the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    // need to replace with single element array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    retattrs = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                    retattrs[0] = "1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                String nm = (relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                             ? fullyQualifiedName(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                             : (name.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                                ? ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                                : name.get(0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                // JNDI unit is milliseconds, LDAP unit is seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                // Zero means no limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                int msecLimit = cons.getTimeLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                int secLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                if (msecLimit > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    secLimit = (msecLimit / 1000) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                LdapResult answer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                    clnt.search(nm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                        scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                        derefAliases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                        (int)cons.getCountLimit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                        secLimit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                        cons.getReturningObjFlag() ? false : typesOnly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                        retattrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                        filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                        batchSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                        reqCtls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                        binaryAttrs,
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  1996
                        waitForReply,
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  1997
                        replyQueueSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                throw e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * Certain simple JNDI searches are automatically converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * LDAP compare operations by the LDAP service provider. A search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * is converted to a compare iff:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *    - the scope is set to OBJECT_SCOPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *    - the filter string contains a simple assertion: "<type>=<value>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *    - the returning attributes list is present but empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23010
diff changeset
  2019
    // returns true if a search can be carried out as a compare, and sets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    // tokens[0] and tokens[1] to the type and value respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    // e.g. filter "cn=Jon Ruiz" becomes, type "cn" and value "Jon Ruiz"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    // This function uses the documents JNDI Compare example as a model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    // for when to turn a search into a compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    private static boolean searchToCompare(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                                    String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                                    SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                                    String tokens[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        // if scope is not object-scope, it's really a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        if (cons.getSearchScope() != SearchControls.OBJECT_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        // if attributes are to be returned, it's really a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        String[] attrs = cons.getReturningAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        if (attrs == null || attrs.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        // if the filter not a simple assertion, it's really a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        if (! filterToAssertion(filter, tokens)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        // it can be converted to a compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    // If the supplied filter is a simple assertion i.e. "<type>=<value>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    // (enclosing parentheses are permitted) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    // filterToAssertion will return true and pass the type and value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    // the first and second elements of tokens respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    // precondition: tokens[] must be initialized and be at least of size 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    private static boolean filterToAssertion(String filter, String tokens[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        // find the left and right half of the assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        StringTokenizer assertionTokenizer = new StringTokenizer(filter, "=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        if (assertionTokenizer.countTokens() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        tokens[0] = assertionTokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        tokens[1] = assertionTokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        // make sure the value does not contain a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        if (tokens[1].indexOf('*') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        // test for enclosing parenthesis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        boolean hasParens = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        int len = tokens[1].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        if ((tokens[0].charAt(0) == '(') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            (tokens[1].charAt(len - 1) == ')')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            hasParens = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        } else if ((tokens[0].charAt(0) == '(') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            (tokens[1].charAt(len - 1) == ')')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            return false; // unbalanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23010
diff changeset
  2086
        // make sure the left and right half are not expressions themselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        StringTokenizer illegalCharsTokenizer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            new StringTokenizer(tokens[0], "()&|!=~><*", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        if (illegalCharsTokenizer.countTokens() != (hasParens ? 2 : 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        illegalCharsTokenizer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            new StringTokenizer(tokens[1], "()&|!=~><*", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        if (illegalCharsTokenizer.countTokens() != (hasParens ? 2 : 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        // strip off enclosing parenthesis, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        if (hasParens) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            tokens[0] = tokens[0].substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            tokens[1] = tokens[1].substring(0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    private LdapResult compare(Name name, String type, String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        String nm = fullyQualifiedName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        LdapResult answer = clnt.compare(nm, type, value, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    private static SearchControls cloneSearchControls(SearchControls cons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        if (cons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        String[] retAttrs = cons.getReturningAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        if (retAttrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            String[] attrs = new String[retAttrs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            System.arraycopy(retAttrs, 0, attrs, 0, retAttrs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            retAttrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        return new SearchControls(cons.getSearchScope(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                                  cons.getCountLimit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                                  cons.getTimeLimit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                                  retAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                                  cons.getReturningObjFlag(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                                  cons.getDerefLinkFlag());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
   // -------------- Environment Properties ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * Override with noncloning version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2145
    protected Hashtable<String, Object> p_getEnvironment() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        return envprops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2149
    @SuppressWarnings("unchecked") // clone()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2150
    public Hashtable<String, Object> getEnvironment() throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        return (envprops == null
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2152
                ? new Hashtable<String, Object>(5, 0.75f)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2153
                : (Hashtable<String, Object>)envprops.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2156
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    public Object removeFromEnvironment(String propName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        // not there; just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        if (envprops == null || envprops.get(propName) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2164
        switch (propName) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2165
            case REF_SEPARATOR:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2166
                addrEncodingSeparator = DEFAULT_REF_SEPARATOR;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2167
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2168
            case TYPES_ONLY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2169
                typesOnly = DEFAULT_TYPES_ONLY;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2170
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2171
            case DELETE_RDN:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2172
                deleteRDN = DEFAULT_DELETE_RDN;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2173
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2174
            case DEREF_ALIASES:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2175
                derefAliases = DEFAULT_DEREF_ALIASES;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2176
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2177
            case Context.BATCHSIZE:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2178
                batchSize = DEFAULT_BATCH_SIZE;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2179
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2180
            case REFERRAL_LIMIT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2181
                referralHopLimit = DEFAULT_REFERRAL_LIMIT;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2182
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2183
            case Context.REFERRAL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2184
                setReferralMode(null, true);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2185
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2186
            case BINARY_ATTRIBUTES:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2187
                setBinaryAttributes(null);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2188
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2189
            case CONNECT_TIMEOUT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2190
                connectTimeout = -1;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2191
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2192
            case READ_TIMEOUT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2193
                readTimeout = -1;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2194
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2195
            case WAIT_FOR_REPLY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2196
                waitForReply = true;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2197
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2198
            case REPLY_QUEUE_SIZE:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2199
                replyQueueSize = -1;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2200
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2201
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2202
            // The following properties affect the connection
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2203
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2204
            case Context.SECURITY_PROTOCOL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2205
                closeConnection(SOFT_CLOSE);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2206
                // De-activate SSL and reset the context's url and port number
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2207
                if (useSsl && !hasLdapsScheme) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2208
                    useSsl = false;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2209
                    url = null;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2210
                    if (useDefaultPortNumber) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2211
                        port_number = DEFAULT_PORT;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2212
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2214
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2215
            case VERSION:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2216
            case SOCKET_FACTORY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2217
                closeConnection(SOFT_CLOSE);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2218
                break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2219
            case Context.SECURITY_AUTHENTICATION:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2220
            case Context.SECURITY_PRINCIPAL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2221
            case Context.SECURITY_CREDENTIALS:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2222
                sharable = false;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2223
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        // Update environment; reconnection will use new props
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2227
        envprops = (Hashtable<String, Object>)envprops.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        return envprops.remove(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2231
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    public Object addToEnvironment(String propName, Object propVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            // If adding null, call remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            if (propVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                return removeFromEnvironment(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2239
            switch (propName) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2240
                case REF_SEPARATOR:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2241
                    setRefSeparator((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2242
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2243
                case TYPES_ONLY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2244
                    setTypesOnly((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2245
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2246
                case DELETE_RDN:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2247
                    setDeleteRDN((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2248
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2249
                case DEREF_ALIASES:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2250
                    setDerefAliases((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2251
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2252
                case Context.BATCHSIZE:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2253
                    setBatchSize((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2254
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2255
                case REFERRAL_LIMIT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2256
                    setReferralLimit((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2257
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2258
                case Context.REFERRAL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2259
                    setReferralMode((String)propVal, true);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2260
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2261
                case BINARY_ATTRIBUTES:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2262
                    setBinaryAttributes((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2263
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2264
                case CONNECT_TIMEOUT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2265
                    setConnectTimeout((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2266
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2267
                case READ_TIMEOUT:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2268
                    setReadTimeout((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2269
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2270
                case WAIT_FOR_REPLY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2271
                    setWaitForReply((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2272
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2273
                case REPLY_QUEUE_SIZE:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2274
                    setReplyQueueSize((String)propVal);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2275
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2276
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2277
            // The following properties affect the connection
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2278
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2279
                case Context.SECURITY_PROTOCOL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2280
                    closeConnection(SOFT_CLOSE);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2281
                    // Activate SSL and reset the context's url and port number
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2282
                    if ("ssl".equals(propVal)) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2283
                        useSsl = true;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2284
                        url = null;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2285
                        if (useDefaultPortNumber) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2286
                            port_number = DEFAULT_SSL_PORT;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2287
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                    }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2289
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2290
                case VERSION:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2291
                case SOCKET_FACTORY:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2292
                    closeConnection(SOFT_CLOSE);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2293
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2294
                case Context.SECURITY_AUTHENTICATION:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2295
                case Context.SECURITY_PRINCIPAL:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2296
                case Context.SECURITY_CREDENTIALS:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2297
                    sharable = false;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2298
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            // Update environment; reconnection will use new props
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            envprops = (envprops == null
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2303
                ? new Hashtable<String, Object>(5, 0.75f)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2304
                : (Hashtable<String, Object>)envprops.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            return envprops.put(propName, propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * Sets the URL that created the context in the java.naming.provider.url
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    void setProviderUrl(String providerUrl) { // called by LdapCtxFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        if (envprops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            envprops.put(Context.PROVIDER_URL, providerUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * Sets the domain name for the context in the com.sun.jndi.ldap.domainname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * Used for hostname verification by Start TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
    void setDomainName(String domainName) { // called by LdapCtxFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        if (envprops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            envprops.put(DOMAIN_NAME, domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
    private void initEnv() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        if (envprops == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            // Make sure that referrals are to their default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            setReferralMode(null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        // Set batch size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        setBatchSize((String)envprops.get(Context.BATCHSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        // Set separator used for encoding RefAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        setRefSeparator((String)envprops.get(REF_SEPARATOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        // Set whether RDN is removed when renaming object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        setDeleteRDN((String)envprops.get(DELETE_RDN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        // Set whether types are returned only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        setTypesOnly((String)envprops.get(TYPES_ONLY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        // Set how aliases are dereferenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        setDerefAliases((String)envprops.get(DEREF_ALIASES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        // Set the limit on referral chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        setReferralLimit((String)envprops.get(REFERRAL_LIMIT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        setBinaryAttributes((String)envprops.get(BINARY_ATTRIBUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        bindCtls = cloneControls((Control[]) envprops.get(BIND_CONTROLS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        // set referral handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        setReferralMode((String)envprops.get(Context.REFERRAL), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        // Set the connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        setConnectTimeout((String)envprops.get(CONNECT_TIMEOUT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        // Set the read timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        setReadTimeout((String)envprops.get(READ_TIMEOUT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2367
        // Set the flag that controls whether to block until the first reply
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2368
        // is received
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2369
        setWaitForReply((String)envprops.get(WAIT_FOR_REPLY));
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2370
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2371
        // Set the size of the queue of unprocessed search replies
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2372
        setReplyQueueSize((String)envprops.get(REPLY_QUEUE_SIZE));
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2373
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        // When connection is created, it will use these and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        // properties from the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    private void setDeleteRDN(String deleteRDNProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        if ((deleteRDNProp != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            (deleteRDNProp.equalsIgnoreCase("false"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            deleteRDN = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            deleteRDN = DEFAULT_DELETE_RDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    private void setTypesOnly(String typesOnlyProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        if ((typesOnlyProp != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            (typesOnlyProp.equalsIgnoreCase("true"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            typesOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            typesOnly = DEFAULT_TYPES_ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * Sets the batch size of this context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
    private void setBatchSize(String batchSizeProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        // set batchsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        if (batchSizeProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            batchSize = Integer.parseInt(batchSizeProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            batchSize = DEFAULT_BATCH_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * Sets the referral mode of this context to 'follow', 'throw' or 'ignore'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * If referral mode is 'ignore' then activate the manageReferral control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    private void setReferralMode(String ref, boolean update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        // First determine the referral mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        if (ref != null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2415
            switch (ref) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2416
                case "follow":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2417
                    handleReferrals = LdapClient.LDAP_REF_FOLLOW;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2418
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2419
                case "throw":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2420
                    handleReferrals = LdapClient.LDAP_REF_THROW;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2421
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2422
                case "ignore":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2423
                    handleReferrals = LdapClient.LDAP_REF_IGNORE;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2424
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2425
                default:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2426
                    throw new IllegalArgumentException(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2427
                        "Illegal value for " + Context.REFERRAL + " property.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            handleReferrals = DEFAULT_REFERRAL_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            // If ignoring referrals, add manageReferralControl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            reqCtls = addControl(reqCtls, manageReferralControl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        } else if (update) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            // If we're update an existing context, remove the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            reqCtls = removeControl(reqCtls, manageReferralControl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        } // else, leave alone; need not update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    /**
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 23010
diff changeset
  2446
     * Set whether aliases are dereferenced during resolution and searches.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    private void setDerefAliases(String deref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        if (deref != null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2450
            switch (deref) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2451
                case "never":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2452
                    derefAliases = 0; // never de-reference aliases
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2453
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2454
                case "searching":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2455
                    derefAliases = 1; // de-reference aliases during searching
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2456
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2457
                case "finding":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2458
                    derefAliases = 2; // de-reference during name resolution
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2459
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2460
                case "always":
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2461
                    derefAliases = 3; // always de-reference aliases
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2462
                    break;
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2463
                default:
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2464
                    throw new IllegalArgumentException("Illegal value for " +
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2465
                        DEREF_ALIASES + " property.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            derefAliases = DEFAULT_DEREF_ALIASES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
    private void setRefSeparator(String sepStr) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
        if (sepStr != null && sepStr.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            addrEncodingSeparator = sepStr.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            addrEncodingSeparator = DEFAULT_REF_SEPARATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * Sets the limit on referral chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    private void setReferralLimit(String referralLimitProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        // set referral limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        if (referralLimitProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            referralHopLimit = Integer.parseInt(referralLimitProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            // a zero setting indicates no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            if (referralHopLimit == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                referralHopLimit = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            referralHopLimit = DEFAULT_REFERRAL_LIMIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
    // For counting referral hops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
    void setHopCount(int hopCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
        this.hopCount = hopCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * Sets the connect timeout value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    private void setConnectTimeout(String connectTimeoutProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        if (connectTimeoutProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            connectTimeout = Integer.parseInt(connectTimeoutProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
            connectTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
    /**
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2513
     * Sets the size of the queue of unprocessed search replies
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2514
     */
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2515
    private void setReplyQueueSize(String replyQueueSizeProp) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2516
        if (replyQueueSizeProp != null) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2517
           replyQueueSize = Integer.parseInt(replyQueueSizeProp);
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2518
            // disallow an empty queue
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2519
            if (replyQueueSize <= 0) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2520
                replyQueueSize = -1;    // unlimited
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2521
            }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2522
        } else {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2523
            replyQueueSize = -1;        // unlimited
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2524
        }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2525
    }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2526
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2527
    /**
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2528
     * Sets the flag that controls whether to block until the first search
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2529
     * reply is received
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2530
     */
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2531
    private void setWaitForReply(String waitForReplyProp) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2532
        if (waitForReplyProp != null &&
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2533
            (waitForReplyProp.equalsIgnoreCase("false"))) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2534
            waitForReply = false;
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2535
        } else {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2536
            waitForReply = true;
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2537
        }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2538
    }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2539
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
  2540
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * Sets the read timeout value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    private void setReadTimeout(String readTimeoutProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        if (readTimeoutProp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
           readTimeout = Integer.parseInt(readTimeoutProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            readTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * Extract URLs from a string. The format of the string is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     *     <urlstring > ::= "Referral:" <ldapurls>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     *     <ldapurls>   ::= <separator> <ldapurl> | <ldapurls>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     *     <separator>  ::= ASCII linefeed character (0x0a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     *     <ldapurl>    ::= LDAP URL format (RFC 1959)
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2558
     *
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2559
     * Returns a Vector of single-String Vectors.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2561
    private static Vector<Vector<String>> extractURLs(String refString) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        int separator = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        int urlCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        // count the number of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        while ((separator = refString.indexOf('\n', separator)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            separator++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            urlCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2572
        Vector<Vector<String>> referrals = new Vector<>(urlCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        int iURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        separator = refString.indexOf('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        iURL = separator + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        while ((separator = refString.indexOf('\n', iURL)) >= 0) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2579
            Vector<String> referral = new Vector<>(1);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2580
            referral.addElement(refString.substring(iURL, separator));
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2581
            referrals.addElement(referral);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            iURL = separator + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2584
        Vector<String> referral = new Vector<>(1);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2585
        referral.addElement(refString.substring(iURL));
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2586
        referrals.addElement(referral);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        return referrals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     * Argument is a space-separated list of attribute IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     * Converts attribute IDs to lowercase before adding to built-in list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    private void setBinaryAttributes(String attrIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        if (attrIds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            binaryAttrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2599
            binaryAttrs = new Hashtable<>(11, 0.75f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            StringTokenizer tokens =
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
  2601
                new StringTokenizer(attrIds.toLowerCase(Locale.ENGLISH), " ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            while (tokens.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                binaryAttrs.put(tokens.nextToken(), Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
   // ----------------- Connection  ---------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            // ignore failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    synchronized public void close() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
            System.err.println("LdapCtx: close() called " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
            (new Throwable()).printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        // Event (normal and unsolicited)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        if (eventSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            eventSupport.cleanup(); // idempotent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            removeUnsolicited();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        // Enumerations that are keeping the connection alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        if (enumCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                System.err.println("LdapCtx: close deferred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            closeRequested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        closeConnection(SOFT_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
// %%%: RL: There is no need to set these to null, as they're just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
// variables whose contents and references will automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
// be cleaned up when they're no longer referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
// Also, setting these to null creates problems for the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
// schema-related methods, which need these to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        schemaTrees = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        envprops = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2651
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
    public void reconnect(Control[] connCtls) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        // Update environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        envprops = (envprops == null
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2655
                ? new Hashtable<String, Object>(5, 0.75f)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2656
                : (Hashtable<String, Object>)envprops.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        if (connCtls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            envprops.remove(BIND_CONTROLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            bindCtls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            envprops.put(BIND_CONTROLS, bindCtls = cloneControls(connCtls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        sharable = false;  // can't share with existing contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        ensureOpen();      // open or reauthenticated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    private void ensureOpen() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        ensureOpen(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
    private void ensureOpen(boolean startTLS) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            if (clnt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                    System.err.println("LdapCtx: Reconnecting " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                // reset the cache before a new connection is established
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2682
                schemaTrees = new Hashtable<>(11, 0.75f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                connect(startTLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            } else if (!sharable || startTLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                synchronized (clnt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                    if (!clnt.isLdapv3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                        || clnt.referenceCount > 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                        || clnt.usingSaslStreams()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                        closeConnection(SOFT_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                // reset the cache before a new connection is established
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2695
                schemaTrees = new Hashtable<>(11, 0.75f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                connect(startTLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            sharable = true;   // connection is now either new or single-use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                               // OK for others to start sharing again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
    private void connect(boolean startTLS) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        if (debug) { System.err.println("LdapCtx: Connecting " + this); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        String user = null;             // authenticating user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        Object passwd = null;           // password for authenticating user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        String secProtocol = null;      // security protocol (e.g. "ssl")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        String socketFactory = null;    // socket factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        String authMechanism = null;    // authentication mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        String ver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
        int ldapVersion;                // LDAP protocol version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        boolean usePool = false;        // enable connection pooling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        if (envprops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            user = (String)envprops.get(Context.SECURITY_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            passwd = envprops.get(Context.SECURITY_CREDENTIALS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            ver = (String)envprops.get(VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            secProtocol =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
               useSsl ? "ssl" : (String)envprops.get(Context.SECURITY_PROTOCOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            socketFactory = (String)envprops.get(SOCKET_FACTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            authMechanism =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                (String)envprops.get(Context.SECURITY_AUTHENTICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            usePool = "true".equalsIgnoreCase((String)envprops.get(ENABLE_POOL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        if (socketFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            socketFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                "ssl".equals(secProtocol) ? DEFAULT_SSL_FACTORY : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        if (authMechanism == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
            authMechanism = (user == null) ? "none" : "simple";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            boolean initial = (clnt == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            if (initial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                ldapVersion = (ver != null) ? Integer.parseInt(ver) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                    DEFAULT_LDAP_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                clnt = LdapClient.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                    usePool, // Whether to use connection pooling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                    // Required for LdapClient constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                    hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                    port_number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                    socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    connectTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                    readTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                    trace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                    // Required for basic client identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    ldapVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                    authMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                    bindCtls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                    secProtocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                    // Required for simple client identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                    user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                    passwd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                    // Required for SASL client identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                    envprops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                 * Pooled connections are preauthenticated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                 * newly created ones are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                if (clnt.authenticateCalled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
            } else if (sharable && startTLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                return; // no authentication required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                // reauthenticating over existing connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                // only v3 supports this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                ldapVersion = LdapClient.LDAP_VERSION3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            LdapResult answer = clnt.authenticate(initial,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                user, passwd, ldapVersion, authMechanism, bindCtls, envprops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            respCtls = answer.resControls; // retrieve (bind) response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                if (initial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    closeConnection(HARD_CLOSE);  // hard close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                processReturnCode(answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            String referral;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            LdapURL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            NamingException saved_ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            // Process the referrals sequentially (top level) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            // recursively (per referral)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                if ((referral = e.getNextReferral()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                    // No more referrals to follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                    if (saved_ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                        throw (NamingException)(saved_ex.fillInStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                        // No saved exception, something must have gone wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                        throw new NamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                        "Internal error processing referral during connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                // Use host/port number from referral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                url = new LdapURL(referral);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                hostname = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                if ((hostname != null) && (hostname.charAt(0) == '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                    hostname = hostname.substring(1, hostname.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                port_number = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                // Try to connect again using new host/port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                    connect(startTLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                } catch (NamingException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                    saved_ex = ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                    continue; // follow another referral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    private void closeConnection(boolean hardclose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        removeUnsolicited();            // idempotent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        if (clnt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                System.err.println("LdapCtx: calling clnt.close() " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
            clnt.close(reqCtls, hardclose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            clnt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
    // Used by Enum classes to track whether it still needs context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
    private int enumCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
    private boolean closeRequested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
    synchronized void incEnumCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        ++enumCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        if (debug) System.err.println("LdapCtx: " + this + " enum inc: " + enumCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
    synchronized void decEnumCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        --enumCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        if (debug) System.err.println("LdapCtx: " + this + " enum dec: " + enumCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        if (enumCount == 0 && closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                // ignore failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
   // ------------ Return code and Error messages  -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
    protected void processReturnCode(LdapResult answer) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        processReturnCode(answer, null, this, null, envprops, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    void processReturnCode(LdapResult answer, Name remainName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
    throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        processReturnCode(answer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                          (new CompositeName()).add(currentDN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                          this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                          remainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                          envprops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                          fullyQualifiedName(remainName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
    protected void processReturnCode(LdapResult res, Name resolvedName,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2897
        Object resolvedObj, Name remainName, Hashtable<?,?> envprops, String fullDN)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
    throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        String msg = LdapClient.getErrorMessage(res.status, res.errorMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
        NamingException e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        LdapReferralException r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        switch (res.status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        case LdapClient.LDAP_SUCCESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            // handle Search continuation references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            if (res.referrals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                msg = "Unprocessed Continuation Reference(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                    e = new PartialResultException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                // handle multiple sets of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                int contRefCount = res.referrals.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                LdapReferralException head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                LdapReferralException ptr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                msg = "Continuation Reference";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                // make a chain of LdapReferralExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                for (int i = 0; i < contRefCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                    r = new LdapReferralException(resolvedName, resolvedObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                        remainName, msg, envprops, fullDN, handleReferrals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                        reqCtls);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2931
                    r.setReferralInfo(res.referrals.elementAt(i), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                    if (hopCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                        r.setHopCount(hopCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                    if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                        head = ptr = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                        ptr.nextReferralEx = r; // append ex. to end of chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                        ptr = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                res.referrals = null;  // reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                if (res.refEx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                    res.refEx = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                    ptr = res.refEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                    while (ptr.nextReferralEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                        ptr = ptr.nextReferralEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                    ptr.nextReferralEx = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                // check the hop limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                if (hopCount > referralHopLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                    NamingException lee =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                        new LimitExceededException("Referral limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                    lee.setRootCause(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                    throw lee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        case LdapClient.LDAP_REFERRAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
            if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                e = new PartialResultException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            r = new LdapReferralException(resolvedName, resolvedObj, remainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                msg, envprops, fullDN, handleReferrals, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
            // only one set of URLs is present
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  2978
            r.setReferralInfo(res.referrals.elementAt(0), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            if (hopCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                r.setHopCount(hopCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            // check the hop limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            if (hopCount > referralHopLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                NamingException lee =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                    new LimitExceededException("Referral limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                lee.setRootCause(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                e = lee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                e = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
         * Handle SLAPD-style referrals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
         * Referrals received during name resolution should be followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
         * until one succeeds - the target entry is located. An exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
         * is thrown now to handle these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
         * Referrals received during a search operation point to unexplored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
         * parts of the directory and each should be followed. An exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
         * is thrown later (during results enumeration) to handle these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        case LdapClient.LDAP_PARTIAL_RESULTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
            if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                e = new PartialResultException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            // extract SLAPD-style referrals from errorMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
            if ((res.errorMessage != null) && (!res.errorMessage.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                res.referrals = extractURLs(res.errorMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                e = new PartialResultException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            // build exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            r = new LdapReferralException(resolvedName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                resolvedObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                remainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                envprops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                fullDN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                handleReferrals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
                reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
            if (hopCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                r.setHopCount(hopCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
             * %%%
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
             * SLAPD-style referrals received during name resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
             * cannot be distinguished from those received during a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
             * search operation. Since both must be handled differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
             * the following rule is applied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
             *     If 1 referral and 0 entries is received then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
             *     assume name resolution has not yet completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
             */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3046
            if (((res.entries == null) || (res.entries.isEmpty())) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                (res.referrals.size() == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3049
                r.setReferralInfo(res.referrals, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                // check the hop limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                if (hopCount > referralHopLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                    NamingException lee =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
                        new LimitExceededException("Referral limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                    lee.setRootCause(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
                    e = lee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                    e = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                r.setReferralInfo(res.referrals, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                res.refEx = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        case LdapClient.LDAP_INVALID_DN_SYNTAX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
        case LdapClient.LDAP_NAMING_VIOLATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            if (remainName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
                e = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
                    InvalidNameException(remainName.toString() + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
                e = new InvalidNameException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
            e = mapErrorCode(res.status, res.errorMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        e.setResolvedName(resolvedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        e.setResolvedObj(resolvedObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        e.setRemainingName(remainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * Maps an LDAP error code to an appropriate NamingException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     * %%% public; used by controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
     * @param errorCode numeric LDAP error code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
     * @param errorMessage textual description of the LDAP error. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
     * @return A NamingException or null if the error code indicates success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
    public static NamingException mapErrorCode(int errorCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        String errorMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        if (errorCode == LdapClient.LDAP_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        NamingException e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        String message = LdapClient.getErrorMessage(errorCode, errorMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        switch (errorCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
        case LdapClient.LDAP_ALIAS_DEREFERENCING_PROBLEM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
        case LdapClient.LDAP_ALIAS_PROBLEM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        case LdapClient.LDAP_ATTRIBUTE_OR_VALUE_EXISTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
            e = new AttributeInUseException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
        case LdapClient.LDAP_AUTH_METHOD_NOT_SUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        case LdapClient.LDAP_CONFIDENTIALITY_REQUIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        case LdapClient.LDAP_STRONG_AUTH_REQUIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        case LdapClient.LDAP_INAPPROPRIATE_AUTHENTICATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            e = new AuthenticationNotSupportedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
        case LdapClient.LDAP_ENTRY_ALREADY_EXISTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            e = new NameAlreadyBoundException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
        case LdapClient.LDAP_INVALID_CREDENTIALS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        case LdapClient.LDAP_SASL_BIND_IN_PROGRESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
            e = new AuthenticationException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        case LdapClient.LDAP_INAPPROPRIATE_MATCHING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
            e = new InvalidSearchFilterException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        case LdapClient.LDAP_INSUFFICIENT_ACCESS_RIGHTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            e = new NoPermissionException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        case LdapClient.LDAP_INVALID_ATTRIBUTE_SYNTAX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        case LdapClient.LDAP_CONSTRAINT_VIOLATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            e =  new InvalidAttributeValueException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        case LdapClient.LDAP_LOOP_DETECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        case LdapClient.LDAP_NO_SUCH_ATTRIBUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
            e = new NoSuchAttributeException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        case LdapClient.LDAP_NO_SUCH_OBJECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            e = new NameNotFoundException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        case LdapClient.LDAP_OBJECT_CLASS_MODS_PROHIBITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        case LdapClient.LDAP_OBJECT_CLASS_VIOLATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        case LdapClient.LDAP_NOT_ALLOWED_ON_RDN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            e = new SchemaViolationException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        case LdapClient.LDAP_NOT_ALLOWED_ON_NON_LEAF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
            e = new ContextNotEmptyException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
        case LdapClient.LDAP_OPERATIONS_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            // %%% need new exception ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        case LdapClient.LDAP_OTHER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        case LdapClient.LDAP_PROTOCOL_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            e = new CommunicationException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
        case LdapClient.LDAP_SIZE_LIMIT_EXCEEDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            e = new SizeLimitExceededException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        case LdapClient.LDAP_TIME_LIMIT_EXCEEDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            e = new TimeLimitExceededException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
        case LdapClient.LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
            e = new OperationNotSupportedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        case LdapClient.LDAP_UNAVAILABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        case LdapClient.LDAP_BUSY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            e = new ServiceUnavailableException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        case LdapClient.LDAP_UNDEFINED_ATTRIBUTE_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
            e = new InvalidAttributeIdentifierException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
        case LdapClient.LDAP_UNWILLING_TO_PERFORM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
            e = new OperationNotSupportedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
        case LdapClient.LDAP_COMPARE_FALSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        case LdapClient.LDAP_COMPARE_TRUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        case LdapClient.LDAP_IS_LEAF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
            // these are really not exceptions and this code probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            // never gets executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
        case LdapClient.LDAP_ADMIN_LIMIT_EXCEEDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
            e = new LimitExceededException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        case LdapClient.LDAP_REFERRAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
        case LdapClient.LDAP_PARTIAL_RESULTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        case LdapClient.LDAP_INVALID_DN_SYNTAX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        case LdapClient.LDAP_NAMING_VIOLATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
            e = new InvalidNameException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            e = new NamingException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
    // ----------------- Extensions and Controls -------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
    public ExtendedResponse extendedOperation(ExtendedRequest request)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        boolean startTLS = (request.getID().equals(STARTTLS_REQ_OID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        ensureOpen(startTLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            LdapResult answer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                clnt.extendedOp(request.getID(), request.getEncodedValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
                                reqCtls, startTLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            respCtls = answer.resControls; // retrieve response controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
            if (answer.status != LdapClient.LDAP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                processReturnCode(answer, new CompositeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
            // %%% verify request.getID() == answer.extensionId
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            int len = (answer.extensionValue == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                        0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                        answer.extensionValue.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
            ExtendedResponse er =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                request.createExtendedResponse(answer.extensionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                    answer.extensionValue, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            if (er instanceof StartTlsResponseImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
                // Pass the connection handle to StartTlsResponseImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
                String domainName = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
                    (envprops != null ? envprops.get(DOMAIN_NAME) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                ((StartTlsResponseImpl)er).setConnection(clnt.conn, domainName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
            return er;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        } catch (LdapReferralException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            if (handleReferrals == LdapClient.LDAP_REF_THROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
            // process the referrals sequentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
                LdapReferralContext refCtx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                    (LdapReferralContext)e.getReferralContext(envprops, bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                // repeat the original operation at the new context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                    return refCtx.extendedOperation(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                } catch (LdapReferralException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                    e = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                    // Make sure we close referral context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                    refCtx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
            NamingException e2 = new CommunicationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            e2.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            throw e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
    public void setRequestControls(Control[] reqCtls) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
            this.reqCtls = addControl(reqCtls, manageReferralControl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
            this.reqCtls = cloneControls(reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
    public Control[] getRequestControls() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
        return cloneControls(reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    public Control[] getConnectControls() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
        return cloneControls(bindCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
    public Control[] getResponseControls() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        return (respCtls != null)? convertControls(respCtls) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     * Narrow controls using own default factory and ControlFactory.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3335
     * @param ctls A non-null Vector<Control>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3337
    Control[] convertControls(Vector<Control> ctls) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
        int count = ctls.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
        Control[] controls = new Control[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
            // Try own factory first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            controls[i] = myResponseControlFactory.getControlInstance(
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3349
                ctls.elementAt(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            // Try assigned factories if own produced null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
            if (controls[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                controls[i] = ControlFactory.getControlInstance(
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3354
                ctls.elementAt(i), this, envprops);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        return controls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
    private static Control[] addControl(Control[] prevCtls, Control addition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
        if (prevCtls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            return new Control[]{addition};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        // Find it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        int found = findControl(prevCtls, addition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        if (found != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
            return prevCtls;  // no need to do it again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        Control[] newCtls = new Control[prevCtls.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        System.arraycopy(prevCtls, 0, newCtls, 0, prevCtls.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        newCtls[prevCtls.length] = addition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        return newCtls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    private static int findControl(Control[] ctls, Control target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        for (int i = 0; i < ctls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            if (ctls[i] == target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
    private static Control[] removeControl(Control[] prevCtls, Control target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
        if (prevCtls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
        // Find it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        int found = findControl(prevCtls, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
        if (found == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            return prevCtls;  // not there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
        // Remove it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        Control[] newCtls = new Control[prevCtls.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
        System.arraycopy(prevCtls, 0, newCtls, 0, found);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        System.arraycopy(prevCtls, found+1, newCtls, found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
            prevCtls.length-found-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        return newCtls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
    private static Control[] cloneControls(Control[] ctls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
        if (ctls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        Control[] copiedCtls = new Control[ctls.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        System.arraycopy(ctls, 0, copiedCtls, 0, ctls.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        return copiedCtls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    // -------------------- Events ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
     * Access to eventSupport need not be synchronized even though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * Connection thread can access it asynchronously. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     * impossible for a race condition to occur because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * eventSupport.addNamingListener() must have been called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * the Connection thread can call back to this ctx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
    public void addNamingListener(Name nm, int scope, NamingListener l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            addNamingListener(getTargetName(nm), scope, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    public void addNamingListener(String nm, int scope, NamingListener l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
            if (eventSupport == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                eventSupport = new EventSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
            eventSupport.addNamingListener(getTargetName(new CompositeName(nm)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                scope, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
            // If first time asking for unsol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            if (l instanceof UnsolicitedNotificationListener && !unsolicited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                addUnsolicited();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
    public void removeNamingListener(NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        if (eventSupport == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
            return; // no activity before, so just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        eventSupport.removeNamingListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        // If removing an Unsol listener and it is the last one, let clnt know
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        if (l instanceof UnsolicitedNotificationListener &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            !eventSupport.hasUnsolicited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            removeUnsolicited();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
    public void addNamingListener(String nm, String filter, SearchControls ctls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            if (eventSupport == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                eventSupport = new EventSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
            eventSupport.addNamingListener(getTargetName(new CompositeName(nm)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                filter, cloneSearchControls(ctls), l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
            // If first time asking for unsol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
            if (l instanceof UnsolicitedNotificationListener && !unsolicited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                addUnsolicited();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
    public void addNamingListener(Name nm, String filter, SearchControls ctls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
        NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
            addNamingListener(getTargetName(nm), filter, ctls, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
    public void addNamingListener(Name nm, String filter, Object[] filterArgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
        SearchControls ctls, NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
            addNamingListener(getTargetName(nm), filter, filterArgs, ctls, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
    public void addNamingListener(String nm, String filterExpr, Object[] filterArgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
        SearchControls ctls, NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
        String strfilter = SearchFilter.format(filterExpr, filterArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
        addNamingListener(getTargetName(new CompositeName(nm)), strfilter, ctls, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
    public boolean targetMustExist() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     * Retrieves the target name for which the listener is registering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     * If nm is a CompositeName, use its first and only component. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     * cannot have more than one components because a target be outside of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
     * this namespace. If nm is not a CompositeName, then treat it as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
     * compound name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     * @param nm The non-null target name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
    private static String getTargetName(Name nm) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
        if (nm instanceof CompositeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
            if (nm.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                    "Target cannot span multiple namespaces: " + nm);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
  3499
            } else if (nm.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                return nm.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            // treat as compound name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
            return nm.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
    // ------------------ Unsolicited Notification ---------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
    // package private methods for handling unsolicited notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
     * Registers this context with the underlying LdapClient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
     * When the underlying LdapClient receives an unsolicited notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
     * it will invoke LdapCtx.fireUnsolicited() so that this context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
     * can (using EventSupport) notified any registered listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
     * This method is called by EventSupport when an unsolicited listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     * first registers with this context (should be called just once).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     * @see #removeUnsolicited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * @see #fireUnsolicited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    private void addUnsolicited() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
            System.out.println("LdapCtx.addUnsolicited: " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
        // addNamingListener must have created EventSupport already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        synchronized (eventSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            clnt.addUnsolicited(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
            unsolicited = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     * Removes this context from registering interest in unsolicited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * notifications from the underlying LdapClient. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * under any one of the following conditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * <li>All unsolicited listeners have been removed. (see removingNamingListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * <li>This context is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * <li>This context's underlying LdapClient changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     * After this method has been called, this context will not pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * on any events related to unsolicited notifications to EventSupport and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     * and its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    private void removeUnsolicited() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            System.out.println("LdapCtx.removeUnsolicited: " + unsolicited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        if (eventSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
        // addNamingListener must have created EventSupport already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
        synchronized(eventSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            if (unsolicited && clnt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                clnt.removeUnsolicited(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            unsolicited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
     * Uses EventSupport to fire an event related to an unsolicited notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
     * Called by LdapClient when LdapClient receives an unsolicited notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    void fireUnsolicited(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
            System.out.println("LdapCtx.fireUnsolicited: " + obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
        // addNamingListener must have created EventSupport already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
        synchronized(eventSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
            if (unsolicited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
                eventSupport.fireUnsolicited(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
                if (obj instanceof NamingException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                    unsolicited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                    // No need to notify clnt because clnt is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                    // only one that can fire a NamingException to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
                    // unsol listeners and it will handle its own cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
}