jdk/src/share/classes/com/sun/naming/internal/ResourceManager.java
author jjg
Wed, 10 Aug 2011 13:44:58 -0700
changeset 10324 e28265130e4f
parent 5506 202f599c92aa
child 14417 4615017b9cab
permissions -rw-r--r--
7072353: JNDI libraries do not build with javac -Xlint:all -Werror Reviewed-by: xuelei Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4040
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: 4040
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: 4040
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4040
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4040
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.naming.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.WeakReference;
4040
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
    31
import java.lang.reflect.Method;
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
    32
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * The ResourceManager class facilitates the reading of JNDI resource files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class ResourceManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Name of provider resource files (without the package-name prefix.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final String PROVIDER_RESOURCE_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            "jndiprovider.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Name of application resource files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final String APP_RESOURCE_FILE_NAME = "jndi.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Name of properties file in <java.home>/lib.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The standard JNDI properties that specify colon-separated lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String[] listProperties = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Context.OBJECT_FACTORIES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Context.URL_PKG_PREFIXES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Context.STATE_FACTORIES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // The following shouldn't create a runtime dependence on ldap package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        javax.naming.ldap.LdapContext.CONTROL_FACTORIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final VersionHelper helper =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            VersionHelper.getVersionHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * A cache of the properties that have been constructed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the ResourceManager.  A Hashtable from a provider resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * file is keyed on a class in the resource file's package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * One from application resource files is keyed on the thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * context class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    90
    // WeakHashMap<Class | ClassLoader, Hashtable>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    91
    private static final WeakHashMap<Object, Hashtable<? super String, Object>>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    92
            propertiesCache = new WeakHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * A cache of factory objects (ObjectFactory, StateFactory, ControlFactory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * A two-level cache keyed first on context class loader and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * on propValue.  Value is a list of class or factory objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * weakly referenced so as not to prevent GC of the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Used in getFactories().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   102
    private static final
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   103
        WeakHashMap<ClassLoader, Map<String, List<NamedWeakReference<Object>>>>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   104
            factoryCache = new WeakHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * A cache of URL factory objects (ObjectFactory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * A two-level cache keyed first on context class loader and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * on classSuffix+propValue.  Value is the factory itself (weakly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * referenced so as not to prevent GC of the class loader) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * NO_FACTORY if a previous search revealed no factory.  Used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * getFactory().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   115
    private static final
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   116
        WeakHashMap<ClassLoader, Map<String, WeakReference<Object>>>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   117
            urlFactoryCache = new WeakHashMap<>(11);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   118
    private static final WeakReference<Object> NO_FACTORY =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   119
            new WeakReference<>(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
4040
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   121
    /**
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   122
     * A class to allow JNDI properties be specified as applet parameters
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   123
     * without creating a static dependency on java.applet.
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   124
     */
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   125
    private static class AppletParameter {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   126
        private static final Class<?> clazz = getClass("java.applet.Applet");
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   127
        private static final Method getMethod =
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   128
            getMethod(clazz, "getParameter", String.class);
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   129
        private static Class<?> getClass(String name) {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   130
            try {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   131
                return Class.forName(name, true, null);
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   132
            } catch (ClassNotFoundException e) {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   133
                return null;
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   134
            }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   135
        }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   136
        private static Method getMethod(Class<?> clazz,
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   137
                                        String name,
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   138
                                        Class<?>... paramTypes)
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   139
        {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   140
            if (clazz != null) {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   141
                try {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   142
                    return clazz.getMethod(name, paramTypes);
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   143
                } catch (NoSuchMethodException e) {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   144
                    throw new AssertionError(e);
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   145
                }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   146
            } else {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   147
                return null;
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   148
            }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   149
        }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   150
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   151
        /**
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   152
         * Returns the value of the applet's named parameter.
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   153
         */
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   154
        static Object get(Object applet, String name) {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   155
            // if clazz is null then applet cannot be an Applet.
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   156
            if (clazz == null || !clazz.isInstance(applet))
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   157
                throw new ClassCastException(applet.getClass().getName());
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   158
            try {
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   159
                return getMethod.invoke(applet, name);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   160
            } catch (InvocationTargetException |
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   161
                     IllegalAccessException e) {
4040
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   162
                throw new AssertionError(e);
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   163
            }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   164
        }
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   165
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // There should be no instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private ResourceManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // ---------- Public methods ----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Given the environment parameter passed to the initial context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * constructor, returns the full environment for that initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * context (never null).  This is based on the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * parameter, the applet parameters (where appropriate), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * system properties, and all application resource files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p> This method will modify <tt>env</tt> and save
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * a reference to it.  The caller may no longer modify it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param env       environment passed to initial context constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                  Null indicates an empty environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws NamingException if an error occurs while reading a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *          resource file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   190
    @SuppressWarnings("unchecked")
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   191
    public static Hashtable<?, ?> getInitialEnvironment(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   192
            Hashtable<?, ?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String[] props = VersionHelper.PROPS;   // system/applet properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (env == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   197
            env = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
4040
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   199
        Object applet = env.get(Context.APPLET);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // Merge property values from env param, applet params, and system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // properties.  The first value wins:  there's no concatenation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // colon-separated lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Read system properties by first trying System.getProperties(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // and then trying System.getProperty() if that fails.  The former
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // is more efficient due to fewer permission checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        String[] jndiSysProps = helper.getJndiProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < props.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            Object val = env.get(props[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (applet != null) {
4040
c5fb14a5bd89 6888552: Allow JNDI to be used when java.applet is not present
alanb
parents: 2
diff changeset
   213
                    val = AppletParameter.get(applet, props[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    // Read system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    val = (jndiSysProps != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        ? jndiSysProps[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        : helper.getJndiProperty(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (val != null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   222
                    ((Hashtable<String, Object>)env).put(props[i], val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Merge the above with the values read from all application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // resource files.  Colon-separated lists are concatenated.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   229
        mergeTables((Hashtable<Object, Object>)env, getApplicationResources());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      * Retrieves the property from the environment, or from the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
      * resource file associated with the given context.  The environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      * may in turn contain values that come from applet parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      * system properties, or application resource files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
      * If <tt>concat</tt> is true and both the environment and the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
      * resource file contain the property, the two values are concatenated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      * (with a ':' separator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      * Returns null if no value is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      * @param propName The non-null property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      * @param env      The possibly null environment properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      * @param ctx      The possibly null context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
      * @param concat   True if multiple values should be concatenated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
      * @return the property value, or null is there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
      * @throws NamingException if an error occurs while reading the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
      * resource file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
      */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   253
    public static String getProperty(String propName, Hashtable<?,?> env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        Context ctx, boolean concat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String val1 = (env != null) ? (String)env.get(propName) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if ((ctx == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            ((val1 != null) && !concat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return val1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        String val2 = (String)getProviderResource(ctx).get(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (val1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return val2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else if ((val2 == null) || !concat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return val1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return (val1 + ":" + val2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Retrieves an enumeration of factory classes/object specified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The property is gotten from the environment and the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * resource file associated with the given context and concantenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * See getProperty(). The resulting property value is a list of class names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * This method then loads each class using the current thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * class loader and keeps them in a list. Any class that cannot be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * is ignored. The resulting list is then cached in a two-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * hash table, keyed first by the context class loader and then by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * the property's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * The next time threads of the same context class loader call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * method, they can use the cached list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * After obtaining the list either from the cache or by creating one from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * the property value, this method then creates and returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * FactoryEnumeration using the list. As the FactoryEnumeration is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * traversed, the cached Class object in the list is instantiated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * replaced by an instance of the factory object itself.  Both class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * objects and factories are wrapped in weak references so as not to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * prevent GC of the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Note that multiple threads can be accessing the same cached list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * via FactoryEnumeration, which locks the list during each next().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * The size of the list will not change,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * but a cached Class object might be replaced by an instantiated factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param propName  The non-null property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param env       The possibly null environment properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param ctx       The possibly null context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return An enumeration of factory classes/objects; null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @exception NamingException If encounter problem while reading the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * property file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see javax.naming.spi.NamingManager#getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see javax.naming.spi.NamingManager#getStateToBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @see javax.naming.spi.DirectoryManager#getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @see javax.naming.spi.DirectoryManager#getStateToBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see javax.naming.ldap.ControlFactory#getControlInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   314
    public static FactoryEnumeration getFactories(String propName,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   315
        Hashtable<?,?> env, Context ctx) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        String facProp = getProperty(propName, env, ctx, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (facProp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return null;  // no classes specified; return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // Cache is based on context class loader and property val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ClassLoader loader = helper.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   324
        Map<String, List<NamedWeakReference<Object>>> perLoaderCache = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        synchronized (factoryCache) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   326
            perLoaderCache = factoryCache.get(loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (perLoaderCache == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   328
                perLoaderCache = new HashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                factoryCache.put(loader, perLoaderCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        synchronized (perLoaderCache) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   334
            List<NamedWeakReference<Object>> factories =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   335
                    perLoaderCache.get(facProp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (factories != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // Cached list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                return factories.size() == 0 ? null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    : new FactoryEnumeration(factories, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // Populate list with classes named in facProp; skipping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // those that we cannot load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                StringTokenizer parser = new StringTokenizer(facProp, ":");
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   344
                factories = new ArrayList<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                while (parser.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        // System.out.println("loading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        String className = parser.nextToken();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   349
                        Class<?> c = helper.loadClass(className, loader);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   350
                        factories.add(new NamedWeakReference<Object>(c, className));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        // ignore ClassNotFoundException, IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                // System.out.println("adding to cache: " + factories);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                perLoaderCache.put(facProp, factories);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return new FactoryEnumeration(factories, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Retrieves a factory from a list of packages specified in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * The property is gotten from the environment and the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * resource file associated with the given context and concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * classSuffix is added to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * See getProperty(). The resulting property value is a list of package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * prefixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * This method then constructs a list of class names by concatenating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * each package prefix with classSuffix and attempts to load and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * instantiate the class until one succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Any class that cannot be loaded is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * The resulting object is then cached in a two-level hash table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * keyed first by the context class loader and then by the property's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * value and classSuffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * The next time threads of the same context class loader call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * method, they use the cached factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If no factory can be loaded, NO_FACTORY is recorded in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * so that next time it'll return quickly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param propName  The non-null property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param env       The possibly null environment properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param ctx       The possibly null context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param classSuffix The non-null class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *                  (e.g. ".ldap.ldapURLContextFactory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param defaultPkgPrefix The non-null default package prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *        (e.g., "com.sun.jndi.url").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return An factory object; null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @exception NamingException If encounter problem while reading the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * property file, or problem instantiating the factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @see javax.naming.spi.NamingManager#getURLContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see javax.naming.spi.NamingManager#getURLObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   398
    public static Object getFactory(String propName, Hashtable<?,?> env,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   399
            Context ctx, String classSuffix, String defaultPkgPrefix)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   400
            throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // Merge property with provider property and supplied default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        String facProp = getProperty(propName, env, ctx, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (facProp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            facProp += (":" + defaultPkgPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            facProp = defaultPkgPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Cache factory based on context class loader, class name, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // property val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        ClassLoader loader = helper.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        String key = classSuffix + " " + facProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   414
        Map<String, WeakReference<Object>> perLoaderCache = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        synchronized (urlFactoryCache) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   416
            perLoaderCache = urlFactoryCache.get(loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (perLoaderCache == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   418
                perLoaderCache = new HashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                urlFactoryCache.put(loader, perLoaderCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        synchronized (perLoaderCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            Object factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   426
            WeakReference<Object> factoryRef = perLoaderCache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (factoryRef == NO_FACTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else if (factoryRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                factory = factoryRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (factory != null) {  // check if weak ref has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // Not cached; find first factory and cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            StringTokenizer parser = new StringTokenizer(facProp, ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            while (factory == null && parser.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                className = parser.nextToken() + classSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // System.out.println("loading " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    factory = helper.loadClass(className, loader).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        new NamingException("Cannot instantiate " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        new NamingException("Cannot access " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    // ignore ClassNotFoundException, IllegalArgumentException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    // etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // Cache it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            perLoaderCache.put(key, (factory != null)
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   462
                                        ? new WeakReference<>(factory)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                        : NO_FACTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // ---------- Private methods ----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns the properties contained in the provider resource file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * of an object's package.  Returns an empty hash table if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * object is null or the resource file cannot be found.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * results are cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @throws NamingException if an error occurs while reading the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   479
    private static Hashtable<? super String, Object>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   480
        getProviderResource(Object obj)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (obj == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   484
            return (new Hashtable<>(1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        synchronized (propertiesCache) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   487
            Class<?> c = obj.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   489
            Hashtable<? super String, Object> props =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   490
                    propertiesCache.get(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            InputStream istream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (istream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    ((Properties)props).load(istream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    NamingException ne = new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            "Error reading provider resource file for " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            propertiesCache.put(c, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns the Hashtable (never null) that results from merging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * all application resource files available to this thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * context class loader.  The properties file in <java.home>/lib
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * is also merged in.  The results are cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * SECURITY NOTES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * 1.  JNDI needs permission to read the application resource files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * 2.  Any class will be able to use JNDI to view the contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * the application resource files in its own classpath.  Give
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * careful consideration to this before storing sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * information there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @throws NamingException if an error occurs while reading a resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *  file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   531
    private static Hashtable<? super String, Object> getApplicationResources()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   532
            throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        ClassLoader cl = helper.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        synchronized (propertiesCache) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   537
            Hashtable<? super String, Object> result = propertiesCache.get(cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   543
                NamingEnumeration<InputStream> resources =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    helper.getResources(cl, APP_RESOURCE_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                while (resources.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    Properties props = new Properties();
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   547
                    props.load(resources.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        result = props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        mergeTables(result, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // Merge in properties from file in <java.home>/lib.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                InputStream istream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if (istream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    props.load(istream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        result = props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        mergeTables(result, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                NamingException ne = new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        "Error reading application resource file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (result == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   577
                result = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            propertiesCache.put(cl, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Merge the properties from one hash table into another.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * property in props2 that is not in props1 is added to props1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * For each property in both hash tables that is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * standard JNDI properties that specify colon-separated lists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * the values are concatenated and stored in props1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   591
    private static void mergeTables(Hashtable<? super String, Object> props1,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   592
                                    Hashtable<? super String, Object> props2) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   593
        for (Object key : props2.keySet()) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   594
            String prop = (String)key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            Object val1 = props1.get(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (val1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                props1.put(prop, props2.get(prop));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            } else if (isListProperty(prop)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                String val2 = (String)props2.get(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                props1.put(prop, ((String)val1) + ":" + val2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Is a property one of the standard JNDI properties that specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * colon-separated lists?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private static boolean isListProperty(String prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        prop = prop.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        for (int i = 0; i < listProperties.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (prop == listProperties[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
}