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