jdk/src/share/classes/com/sun/jndi/ldap/LdapPoolManager.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10324 e28265130e4f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2005, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.ldap.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.CommunicationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jndi.ldap.pool.PoolCleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jndi.ldap.pool.Pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Contains utilities for managing connection pools of LdapClient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Contains method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * - checking whether attempted connection creation may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * - creating a pooled connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * - closing idle connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If a timeout period has been configured, then it will automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * close and remove idle connections (those that have not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * used for the duration of the timeout period).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class LdapPoolManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final String DEBUG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "com.sun.jndi.ldap.connect.pool.debug";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "all".equalsIgnoreCase(getProperty(DEBUG, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final boolean trace = debug ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "fine".equalsIgnoreCase(getProperty(DEBUG, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // ---------- System properties for connection pooling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Authentication mechanisms of connections that may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final String POOL_AUTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "com.sun.jndi.ldap.connect.pool.authentication";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Protocol types of connections that may be pooled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final String POOL_PROTOCOL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "com.sun.jndi.ldap.connect.pool.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Maximum number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final String MAX_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "com.sun.jndi.ldap.connect.pool.maxsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Preferred number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final String PREF_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        "com.sun.jndi.ldap.connect.pool.prefsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Initial number of identical connections per pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final String INIT_POOL_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        "com.sun.jndi.ldap.connect.pool.initsize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Milliseconds to wait before closing idle connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final String POOL_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        "com.sun.jndi.ldap.connect.pool.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // Properties for DIGEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String SASL_CALLBACK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        "java.naming.security.sasl.callback";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // --------- Constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int DEFAULT_MAX_POOL_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final int DEFAULT_PREF_POOL_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int DEFAULT_INIT_POOL_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int DEFAULT_TIMEOUT = 0;    // no timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final String DEFAULT_AUTH_MECHS = "none simple";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final String DEFAULT_PROTOCOLS = "plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int NONE = 0;    // indices into pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int SIMPLE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int DIGEST = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // --------- static fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final long idleTimeout;// ms to wait before closing idle conn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final int maxSize;     // max num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int prefSize;    // preferred num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final int initSize;    // initial num of identical conns/pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static boolean supportPlainProtocol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static boolean supportSslProtocol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // List of pools used for different auth types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final Pool[] pools = new Pool[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        maxSize = getInteger(MAX_POOL_SIZE, DEFAULT_MAX_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        prefSize = getInteger(PREF_POOL_SIZE, DEFAULT_PREF_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        initSize = getInteger(INIT_POOL_SIZE, DEFAULT_INIT_POOL_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        idleTimeout = getLong(POOL_TIMEOUT, DEFAULT_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Determine supported authentication mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String str = getProperty(POOL_AUTH, DEFAULT_AUTH_MECHS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        StringTokenizer parser = new StringTokenizer(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int count = parser.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String mech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            mech = parser.nextToken().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (mech.equals("anonymous")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                mech = "none";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            p = findPool(mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (p >= 0 && pools[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                pools[p] = new Pool(initSize, prefSize, maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Determine supported protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        str= getProperty(POOL_PROTOCOL, DEFAULT_PROTOCOLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        parser = new StringTokenizer(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        count = parser.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        String proto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            proto = parser.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if ("plain".equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                supportPlainProtocol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } else if ("ssl".equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                supportSslProtocol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (idleTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Create cleaner to expire idle connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            new PoolCleaner(idleTimeout, pools).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            showStats(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // Cannot instantiate one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private LdapPoolManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Find the index of the pool for the specified mechanism. If not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * one of "none", "simple", "DIGEST-MD5", or "GSSAPI",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param mech mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static int findPool(String mech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if ("none".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else if ("simple".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return SIMPLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if ("digest-md5".equalsIgnoreCase(mech)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return DIGEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Determines whether pooling is allowed given information on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the connection will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Non-configurable rejections:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * - nonstandard socketFactory has been specified: the pool manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *   cannot track input or parameters used by the socket factory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *   thus has no way of determining whether two connection requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *   are equivalent. Maybe in the future it might add a list of allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *   socket factories to be configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * - trace enabled (except when debugging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * - for Digest authentication, if a callback handler has been specified:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *  the pool manager cannot track input collected by the handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *  and thus has no way of determining whether two connection requests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *  equivalent. Maybe in the future it might add a list of allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *  callback handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Configurable tests:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * - Pooling for the requested protocol (plain or ssl) is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * - Pooling for the requested authentication mechanism is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static boolean isPoolingAllowed(String socketFactory, OutputStream trace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String authMech, String protocol, Hashtable env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (trace != null && !debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // Requesting plain protocol but it is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                || (protocol == null && !supportPlainProtocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                // Requesting ssl protocol but it is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                || ("ssl".equalsIgnoreCase(protocol) && !supportSslProtocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            d("Pooling disallowed due to tracing or unsupported pooling of protocol");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // pooling of custom socket factory is possible only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // socket factory interface implements java.util.comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String COMPARATOR = "java.util.Comparator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        boolean foundSockCmp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((socketFactory != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             !socketFactory.equals(LdapCtx.DEFAULT_SSL_FACTORY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                Class socketFactoryClass = Obj.helper.loadClass(socketFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                Class[] interfaces = socketFactoryClass.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (interfaces[i].getCanonicalName().equals(COMPARATOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        foundSockCmp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                CommunicationException ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    new CommunicationException("Loading the socket factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                ce.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (!foundSockCmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Cannot use pooling if authMech is not a supported mechs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // Cannot use pooling if authMech contains multiple mechs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int p = findPool(authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (p < 0 || pools[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            d("authmech not found: ", authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        d("using authmech: ", authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        switch (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        case NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        case SIMPLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        case DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // Provider won't be able to determine connection identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // if an alternate callback handler is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return (env == null || env.get(SASL_CALLBACK) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Obtains a pooled connection that either already exists or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * newly created using the parameters supplied. If it is newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * created, it needs to go through the authentication checks to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * determine whether an LDAP bind is necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Caller needs to invoke ldapClient.authenticateCalled() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * determine whether ldapClient.authenticate() needs to be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Caller has that responsibility because caller needs to deal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * with the LDAP bind response, which might involve referrals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * response controls, errors, etc. This method is responsible only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * for establishing the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return an LdapClient that is pooled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static LdapClient getLdapClient(String host, int port, String socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int connTimeout, int readTimeout, OutputStream trace, int version,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        String authMech, Control[] ctls, String protocol, String user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Object passwd, Hashtable env) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Create base identity for LdapClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        ClientId id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Pool pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int p = findPool(authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (p < 0 || (pool=pools[p]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                "Attempting to use pooling for an unsupported mechanism: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                authMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        switch (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            id = new ClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        ctls, trace, socketFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        case SIMPLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // Add identity information used in simple authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            id = new SimpleClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                ctls, trace, socketFactory, user, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        case DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // Add user/passwd/realm/authzid/qop/strength/maxbuf/mutual/policy*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            id = new DigestClientId(version, host, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ctls, trace, socketFactory, user, passwd, env);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return (LdapClient) pool.getPooledConnection(id, connTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            new LdapClientFactory(host, port, socketFactory, connTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                readTimeout, trace));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public static void showStats(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        out.println("***** start *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        out.println("idle timeout: " + idleTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        out.println("maximum pool size: " + maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        out.println("preferred pool size: " + prefSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        out.println("initial pool size: " + initSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        out.println("protocol types: " + (supportPlainProtocol ? "plain " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            (supportSslProtocol ? "ssl" : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        out.println("authentication types: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            (pools[NONE] != null ? "none " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            (pools[SIMPLE] != null ? "simple " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            (pools[DIGEST] != null ? "DIGEST-MD5 " : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (pools[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    (i == NONE ? "anonymous pools" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        i == SIMPLE ? "simple auth pools" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        i == DIGEST ? "digest pools" : "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                pools[i].showStats(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        out.println("***** end *****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Closes idle connections idle since specified time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param threshold Close connections idle since this time, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * specified in milliseconds since "the epoch".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see java.util.Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public static void expire(long threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (pools[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                pools[i].expire(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private static void d(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            System.err.println("LdapPoolManager: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private static void d(String msg, String o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            System.err.println("LdapPoolManager: " + msg + o);
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 final String getProperty(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        final String defVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return (String) AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    return System.getProperty(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    return defVal;
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private static final int getInteger(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        final int defVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Integer val = (Integer) AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return Integer.getInteger(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    return new Integer(defVal);
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
        return val.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private static final long getLong(final String propName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        final long defVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        Long val = (Long) AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    return Long.getLong(propName, defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    return new Long(defVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return val.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}