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