jdk/src/java.naming/share/classes/com/sun/jndi/ldap/LdapPoolManager.java
author chegar
Thu, 28 Jul 2016 16:15:51 +0100
changeset 39835 4ecd563fecf6
parent 25859 3317bb8137f4
permissions -rw-r--r--
8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader Reviewed-by: alanb, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20754
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.ldap.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.naming.CommunicationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jndi.ldap.pool.PoolCleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.jndi.ldap.pool.Pool;
39835
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
    42
import jdk.internal.misc.InnocuousThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Contains utilities for managing connection pools of LdapClient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Contains method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * - checking whether attempted connection creation may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * - creating a pooled connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * - closing idle connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * If a timeout period has been configured, then it will automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * close and remove idle connections (those that have not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * used for the duration of the timeout period).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public final class LdapPoolManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final String DEBUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "com.sun.jndi.ldap.connect.pool.debug";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "all".equalsIgnoreCase(getProperty(DEBUG, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final boolean trace = debug ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "fine".equalsIgnoreCase(getProperty(DEBUG, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // ---------- System properties for connection pooling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Authentication mechanisms of connections that may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final String POOL_AUTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "com.sun.jndi.ldap.connect.pool.authentication";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Protocol types of connections that may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final String POOL_PROTOCOL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "com.sun.jndi.ldap.connect.pool.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Maximum number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String MAX_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "com.sun.jndi.ldap.connect.pool.maxsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Preferred number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final String PREF_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        "com.sun.jndi.ldap.connect.pool.prefsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Initial number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final String INIT_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        "com.sun.jndi.ldap.connect.pool.initsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // Milliseconds to wait before closing idle connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final String POOL_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        "com.sun.jndi.ldap.connect.pool.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Properties for DIGEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final String SASL_CALLBACK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        "java.naming.security.sasl.callback";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // --------- Constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int DEFAULT_MAX_POOL_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int DEFAULT_PREF_POOL_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int DEFAULT_INIT_POOL_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int DEFAULT_TIMEOUT = 0;    // no timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final String DEFAULT_AUTH_MECHS = "none simple";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final String DEFAULT_PROTOCOLS = "plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int NONE = 0;    // indices into pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final int SIMPLE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final int DIGEST = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // --------- static fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final long idleTimeout;// ms to wait before closing idle conn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final int maxSize;     // max num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final int prefSize;    // preferred num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final int initSize;    // initial num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static boolean supportPlainProtocol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static boolean supportSslProtocol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // List of pools used for different auth types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final Pool[] pools = new Pool[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        maxSize = getInteger(MAX_POOL_SIZE, DEFAULT_MAX_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        prefSize = getInteger(PREF_POOL_SIZE, DEFAULT_PREF_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        initSize = getInteger(INIT_POOL_SIZE, DEFAULT_INIT_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        idleTimeout = getLong(POOL_TIMEOUT, DEFAULT_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Determine supported authentication mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String str = getProperty(POOL_AUTH, DEFAULT_AUTH_MECHS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        StringTokenizer parser = new StringTokenizer(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int count = parser.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        String mech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i = 0; i < count; i++) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10324
diff changeset
   138
            mech = parser.nextToken().toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (mech.equals("anonymous")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                mech = "none";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            p = findPool(mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (p >= 0 && pools[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                pools[p] = new Pool(initSize, prefSize, maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Determine supported protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        str= getProperty(POOL_PROTOCOL, DEFAULT_PROTOCOLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        parser = new StringTokenizer(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        count = parser.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String proto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            proto = parser.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if ("plain".equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                supportPlainProtocol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else if ("ssl".equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                supportSslProtocol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (idleTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Create cleaner to expire idle connections
39835
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   167
            PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   168
                public Void run() {
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   169
                    Thread t = InnocuousThread.newSystemThread(
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   170
                            "LDAP PoolCleaner",
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   171
                            new PoolCleaner(idleTimeout, pools));
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   172
                    assert t.getContextClassLoader() == null;
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   173
                    t.setDaemon(true);
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   174
                    t.start();
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   175
                    return null;
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   176
                }};
4ecd563fecf6 8156824: com.sun.jndi.ldap.pool.PoolCleaner should clear its context class loader
chegar
parents: 25859
diff changeset
   177
            AccessController.doPrivileged(pa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            showStats(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Cannot instantiate one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private LdapPoolManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Find the index of the pool for the specified mechanism. If not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * one of "none", "simple", "DIGEST-MD5", or "GSSAPI",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param mech mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static int findPool(String mech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if ("none".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else if ("simple".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return SIMPLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else if ("digest-md5".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return DIGEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Determines whether pooling is allowed given information on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * the connection will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Non-configurable rejections:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * - nonstandard socketFactory has been specified: the pool manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *   cannot track input or parameters used by the socket factory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *   thus has no way of determining whether two connection requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *   are equivalent. Maybe in the future it might add a list of allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *   socket factories to be configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * - trace enabled (except when debugging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * - for Digest authentication, if a callback handler has been specified:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *  the pool manager cannot track input collected by the handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *  and thus has no way of determining whether two connection requests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *  equivalent. Maybe in the future it might add a list of allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *  callback handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Configurable tests:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * - Pooling for the requested protocol (plain or ssl) is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * - Pooling for the requested authentication mechanism is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    static boolean isPoolingAllowed(String socketFactory, OutputStream trace,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   229
        String authMech, String protocol, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (trace != null && !debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                // Requesting plain protocol but it is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                || (protocol == null && !supportPlainProtocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // Requesting ssl protocol but it is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                || ("ssl".equalsIgnoreCase(protocol) && !supportSslProtocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            d("Pooling disallowed due to tracing or unsupported pooling of protocol");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // pooling of custom socket factory is possible only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // socket factory interface implements java.util.comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        String COMPARATOR = "java.util.Comparator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean foundSockCmp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if ((socketFactory != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             !socketFactory.equals(LdapCtx.DEFAULT_SSL_FACTORY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   250
                Class<?> socketFactoryClass = Obj.helper.loadClass(socketFactory);
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 10369
diff changeset
   251
                Class<?>[] interfaces = socketFactoryClass.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    if (interfaces[i].getCanonicalName().equals(COMPARATOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        foundSockCmp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                CommunicationException ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    new CommunicationException("Loading the socket factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ce.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (!foundSockCmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Cannot use pooling if authMech is not a supported mechs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Cannot use pooling if authMech contains multiple mechs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int p = findPool(authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (p < 0 || pools[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            d("authmech not found: ", authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        d("using authmech: ", authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        switch (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        case NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        case SIMPLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        case DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Provider won't be able to determine connection identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // if an alternate callback handler is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return (env == null || env.get(SASL_CALLBACK) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Obtains a pooled connection that either already exists or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * newly created using the parameters supplied. If it is newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * created, it needs to go through the authentication checks to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * determine whether an LDAP bind is necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Caller needs to invoke ldapClient.authenticateCalled() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * determine whether ldapClient.authenticate() needs to be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Caller has that responsibility because caller needs to deal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * with the LDAP bind response, which might involve referrals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * response controls, errors, etc. This method is responsible only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * for establishing the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return an LdapClient that is pooled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static LdapClient getLdapClient(String host, int port, String socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int connTimeout, int readTimeout, OutputStream trace, int version,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        String authMech, Control[] ctls, String protocol, String user,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   309
        Object passwd, Hashtable<?,?> env) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Create base identity for LdapClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        ClientId id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Pool pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int p = findPool(authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (p < 0 || (pool=pools[p]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                "Attempting to use pooling for an unsupported mechanism: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        switch (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        case NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            id = new ClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        ctls, trace, socketFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        case SIMPLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // Add identity information used in simple authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            id = new SimpleClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ctls, trace, socketFactory, user, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        case DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // Add user/passwd/realm/authzid/qop/strength/maxbuf/mutual/policy*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            id = new DigestClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                ctls, trace, socketFactory, user, passwd, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return (LdapClient) pool.getPooledConnection(id, connTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            new LdapClientFactory(host, port, socketFactory, connTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                readTimeout, trace));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public static void showStats(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        out.println("***** start *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        out.println("idle timeout: " + idleTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        out.println("maximum pool size: " + maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        out.println("preferred pool size: " + prefSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        out.println("initial pool size: " + initSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        out.println("protocol types: " + (supportPlainProtocol ? "plain " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            (supportSslProtocol ? "ssl" : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        out.println("authentication types: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            (pools[NONE] != null ? "none " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            (pools[SIMPLE] != null ? "simple " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            (pools[DIGEST] != null ? "DIGEST-MD5 " : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (pools[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    (i == NONE ? "anonymous pools" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        i == SIMPLE ? "simple auth pools" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        i == DIGEST ? "digest pools" : "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                pools[i].showStats(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        out.println("***** end *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Closes idle connections idle since specified time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param threshold Close connections idle since this time, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * specified in milliseconds since "the epoch".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see java.util.Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public static void expire(long threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (pools[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                pools[i].expire(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private static void d(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            System.err.println("LdapPoolManager: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private static void d(String msg, String o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            System.err.println("LdapPoolManager: " + msg + o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private static final String getProperty(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        final String defVal) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   400
        return AccessController.doPrivileged(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   401
            new PrivilegedAction<String>() {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   402
            public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    return System.getProperty(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return defVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private static final int getInteger(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        final int defVal) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   414
        Integer val = AccessController.doPrivileged(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   415
            new PrivilegedAction<Integer>() {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   416
            public Integer run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    return Integer.getInteger(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                } catch (SecurityException e) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   420
                    return defVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return val.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private static final long getLong(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        final long defVal) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   429
        Long val = AccessController.doPrivileged(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   430
            new PrivilegedAction<Long>() {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   431
            public Long run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    return Long.getLong(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                } catch (SecurityException e) {
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
   435
                    return defVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return val.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
}