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