jdk/src/share/classes/javax/naming/spi/NamingManager.java
author yan
Thu, 03 Apr 2014 16:10:40 +0400
changeset 23712 d46a902c1aed
parent 14342 8435a30053c1
child 25752 8df7ddac1cb3
permissions -rw-r--r--
8039041: Tidy warnings cleanup for javax.naming Reviewed-by: alanb, lancea Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14014
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.naming.internal.VersionHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.naming.internal.ResourceManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.naming.internal.FactoryEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class contains methods for creating context objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and objects referred to by location information in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * or directory service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class cannot be instantiated.  It has only static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The mention of URL in the documentation for this class refers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a URL string as defined by RFC 1738 and its related RFCs. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * any string that conforms to the syntax described therein, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * may not always have corresponding support in the java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * class or Web browsers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * NamingManager is safe for concurrent access by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Except as otherwise noted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * a <tt>Name</tt> or environment parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * passed to any method is owned by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The implementation will not modify the object or keep a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to it, although it may keep a reference to a clone or copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class NamingManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Disallow anyone from creating one of these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Made package private so that DirectoryManager can subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    NamingManager() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // should be protected and package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final VersionHelper helper = VersionHelper.getVersionHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
// --------- object factory stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Package-private; used by DirectoryManager and NamingManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static ObjectFactoryBuilder object_factory_builder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The ObjectFactoryBuilder determines the policy used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * trying to load object factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * See getObjectInstance() and class ObjectFactory for a description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * of the default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * setObjectFactoryBuilder() overrides this default policy by installing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * an ObjectFactoryBuilder. Subsequent object factories will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * be loaded and created using the installed builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The builder can only be installed if the executing thread is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * (by the security manager's checkSetFactory() method) to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Once installed, the builder cannot be replaced.
23712
d46a902c1aed 8039041: Tidy warnings cleanup for javax.naming
yan
parents: 14342
diff changeset
    95
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param builder The factory builder to install. If null, no builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *                  is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception SecurityException builder cannot be installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *          for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @exception NamingException builder cannot be installed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *         a non-security-related reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception IllegalStateException If a factory has already been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see ObjectFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see ObjectFactoryBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see java.lang.SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static synchronized void setObjectFactoryBuilder(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ObjectFactoryBuilder builder) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (object_factory_builder != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new IllegalStateException("ObjectFactoryBuilder already set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        object_factory_builder = builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Used for accessing object factory builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static synchronized ObjectFactoryBuilder getObjectFactoryBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return object_factory_builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Retrieves the ObjectFactory for the object identified by a reference,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * using the reference's factory class name and factory codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * to load in the factory's class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param ref The non-null reference to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param factoryName The non-null class name of the factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return The object factory for the object identified by ref; null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * if unable to load the factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static ObjectFactory getObjectFactoryFromReference(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Reference ref, String factoryName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        MalformedURLException {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   142
        Class<?> clas = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Try to use current class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             clas = helper.loadClass(factoryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // ignore and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // All other exceptions are passed up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Not in class path; try to use codebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String codebase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (clas == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                (codebase = ref.getFactoryClassLocation()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                clas = helper.loadClass(factoryName, codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return (clas != null) ? (ObjectFactory) clas.newInstance() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
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
     * Creates an object using the factories specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <tt>Context.OBJECT_FACTORIES</tt> property of the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * or of the provider resource file associated with <tt>nameCtx</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return factory created; null if cannot create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static Object createObjectFromFactories(Object obj, Name name,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   175
            Context nameCtx, Hashtable<?,?> environment) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        FactoryEnumeration factories = ResourceManager.getFactories(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            Context.OBJECT_FACTORIES, environment, nameCtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (factories == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Try each factory until one succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ObjectFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Object answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        while (answer == null && factories.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            factory = (ObjectFactory)factories.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            answer = factory.getObjectInstance(obj, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static String getURLScheme(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int colon_posn = str.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int slash_posn = str.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return str.substring(0, colon_posn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates an instance of an object for the specified object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * and environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * If an object factory builder has been installed, it is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * create a factory for creating the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Otherwise, the following rules are used to create the object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *<ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <li>If <code>refInfo</code> is a <code>Reference</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *    or <code>Referenceable</code> containing a factory class name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *    use the named factory to create the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *    Return <code>refInfo</code> if the factory cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *    Under JDK 1.1, if the factory class must be loaded from a location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *    specified in the reference, a <tt>SecurityManager</tt> must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *    been installed or the factory creation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *    If an exception is encountered while creating the factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *    it is passed up to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <li>If <tt>refInfo</tt> is a <tt>Reference</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *    <tt>Referenceable</tt> with no factory class name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *    and the address or addresses are <tt>StringRefAddr</tt>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *    address type "URL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *    try the URL context factory corresponding to each URL's scheme id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *    to create the object (see <tt>getURLContext()</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *    If that fails, continue to the next step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <li> Use the object factories specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *    the <tt>Context.OBJECT_FACTORIES</tt> property of the environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *    and of the provider resource file associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *    <tt>nameCtx</tt>, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *    The value of this property is a colon-separated list of factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *    class names that are tried in order, and the first one that succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *    in creating an object is the one used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *    If none of the factories can be loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *    return <code>refInfo</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *    If an exception is encountered while creating the object, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *    exception is passed up to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *</ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Service providers that implement the <tt>DirContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * interface should use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <tt>DirectoryManager.getObjectInstance()</tt>, not this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Service providers that implement only the <tt>Context</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * interface should use this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Note that an object factory (an object that implements the ObjectFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * interface) must be public and must have a public constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * accepts no arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * The <code>name</code> and <code>nameCtx</code> parameters may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * optionally be used to specify the name of the object being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <code>name</code> is the name of the object, relative to context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>nameCtx</code>.  This information could be useful to the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * factory or to the object implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *  If there are several possible contexts from which the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *  could be named -- as will often be the case -- it is up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *  the caller to select one.  A good rule of thumb is to select the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * "deepest" context available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * If <code>nameCtx</code> is null, <code>name</code> is relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * to the default initial context.  If no name is being specified, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <code>name</code> parameter should be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param refInfo The possibly null object for which to create an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param name The name of this object relative to <code>nameCtx</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          Specifying a name is optional; if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          omitted, <code>name</code> should be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param nameCtx The context relative to which the <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          parameter is specified.  If null, <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          relative to the default initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param environment The possibly null environment to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          be used in the creation of the object factory and the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return An object created using <code>refInfo</code>; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          <code>refInfo</code> if an object cannot be created using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          the algorithm described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception NamingException if a naming exception was encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *  while attempting to get a URL context, or if one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          factories accessed throws a NamingException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception Exception if one of the factories accessed throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          exception, or if an error was encountered while loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          and instantiating the factory and object classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          A factory should only throw an exception if it does not want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          other factories to be used in an attempt to create an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *  See ObjectFactory.getObjectInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see #getURLContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @see ObjectFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see ObjectFactory#getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public static Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        getObjectInstance(Object refInfo, Name name, Context nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                          Hashtable<?,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        ObjectFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // Use builder if installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        ObjectFactoryBuilder builder = getObjectFactoryBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (builder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // builder must return non-null factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            factory = builder.createObjectFactory(refInfo, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return factory.getObjectInstance(refInfo, name, nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // Use reference if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        Reference ref = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (refInfo instanceof Reference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            ref = (Reference) refInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else if (refInfo instanceof Referenceable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            ref = ((Referenceable)(refInfo)).getReference();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        Object answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            String f = ref.getFactoryClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // if reference identifies a factory, use exclusively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                factory = getObjectFactoryFromReference(ref, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    return factory.getObjectInstance(ref, name, nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                                     environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // No factory found, so return original refInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // Will reach this point if factory class is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // class path and reference does not contain a URL for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                return refInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // if reference has no factory, check for addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // containing URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                answer = processURLAddrs(ref, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (answer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // try using any specified factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        answer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            createObjectFromFactories(refInfo, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return (answer != null) ? answer : refInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Ref has no factory.  For each address of type "URL", try its URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * context factory.  Returns null if unsuccessful in creating and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * invoking a factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static Object processURLAddrs(Reference ref, Name name, Context nameCtx,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   352
                                  Hashtable<?,?> environment)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (int i = 0; i < ref.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            RefAddr addr = ref.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (addr instanceof StringRefAddr &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                addr.getType().equalsIgnoreCase("URL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                String url = (String)addr.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                Object answer = processURL(url, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (answer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private static Object processURL(Object refInfo, Name name,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   371
                                     Context nameCtx, Hashtable<?,?> environment)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Object answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // If refInfo is a URL string, try to use its URL context factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // If no context found, continue to try object factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (refInfo instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            String url = (String)refInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            String scheme = getURLScheme(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                answer = getURLObject(scheme, refInfo, name, nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                      environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                if (answer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // If refInfo is an array of URL strings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // try to find a context factory for any one of its URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // If no context found, continue to try object factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (refInfo instanceof String[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            String[] urls = (String[])refInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for (int i = 0; i <urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                String scheme = getURLScheme(urls[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    answer = getURLObject(scheme, refInfo, name, nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                          environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if (answer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Retrieves a context identified by <code>obj</code>, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Used by ContinuationContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param obj       The object identifying the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param name      The name of the context being returned, relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *                  <code>nameCtx</code>, or null if no name is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *                  specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *                  See the <code>getObjectInstance</code> method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *                  details.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10324
diff changeset
   419
     * @param nameCtx   The context relative to which <code>name</code> is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *                  specified, or null for the default initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *                  See the <code>getObjectInstance</code> method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *                  details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param environment Environment specifying characteristics of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *                  resulting context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return A context identified by <code>obj</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @see #getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    static Context getContext(Object obj, Name name, Context nameCtx,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   430
                              Hashtable<?,?> environment) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        Object answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (obj instanceof Context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // %%% Ignore environment for now.  OK since method not public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return (Context)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            answer = getObjectInstance(obj, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            NamingException ne = new NamingException();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return (answer instanceof Context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            ? (Context)answer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // Used by ContinuationContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    static Resolver getResolver(Object obj, Name name, Context nameCtx,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   455
                                Hashtable<?,?> environment) throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Object answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (obj instanceof Resolver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // %%% Ignore environment for now.  OK since method not public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return (Resolver)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            answer = getObjectInstance(obj, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return (answer instanceof Resolver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            ? (Resolver)answer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /***************** URL Context implementations ***************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Creates a context for the given URL scheme id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * The resulting context is for resolving URLs of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * scheme <code>scheme</code>. The resulting context is not tied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * to a specific URL. It is able to handle arbitrary URLs with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * the specified scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * The class name of the factory that creates the resulting context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * has the naming convention <i>scheme-id</i>URLContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * (e.g. "ftpURLContextFactory" for the "ftp" scheme-id),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * in the package specified as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * The <tt>Context.URL_PKG_PREFIXES</tt> environment property (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * may contain values taken from applet parameters, system properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * or application resource files)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * contains a colon-separated list of package prefixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Each package prefix in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * the property is tried in the order specified to load the factory class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * The default package prefix is "com.sun.jndi.url" (if none of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * specified packages work, this default is tried).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * The complete package name is constructed using the package prefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * concatenated with the scheme id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * For example, if the scheme id is "ldap", and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <tt>Context.URL_PKG_PREFIXES</tt> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * contains "com.widget:com.wiz.jndi",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the naming manager would attempt to load the following classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * until one is successfully instantiated:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <li>com.widget.ldap.ldapURLContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *  <li>com.wiz.jndi.ldap.ldapURLContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *  <li>com.sun.jndi.url.ldap.ldapURLContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * If none of the package prefixes work, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * If a factory is instantiated, it is invoked with the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * parameters to produce the resulting context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <code>factory.getObjectInstance(null, environment);</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * For example, invoking getObjectInstance() as shown above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * on a LDAP URL context factory would return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * context that can resolve LDAP urls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * (e.g. "ldap://ldap.wiz.com/o=wiz,c=us",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * "ldap://ldap.umich.edu/o=umich,c=us", ...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Note that an object factory (an object that implements the ObjectFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * interface) must be public and must have a public constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * accepts no arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param scheme    The non-null scheme-id of the URLs supported by the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param environment The possibly null environment properties to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *           used in the creation of the object factory and the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return A context for resolving URLs with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *         scheme id <code>scheme</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *  <code>null</code> if the factory for creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *         context is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @exception NamingException If a naming exception occurs while creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *          the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see #getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see ObjectFactory#getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public static Context getURLContext(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                        Hashtable<?,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // pass in 'null' to indicate creation of generic context for scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // (i.e. not specific to a URL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            Object answer = getURLObject(scheme, null, null, null, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (answer instanceof Context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                return (Context)answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    private static final String defaultPkgPrefix = "com.sun.jndi.url";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Creates an object for the given URL scheme id using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * the supplied urlInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * If urlInfo is null, the result is a context for resolving URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * with the scheme id 'scheme'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * If urlInfo is a URL, the result is a context named by the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Names passed to this context is assumed to be relative to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * context (i.e. not a URL). For example, if urlInfo is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * "ldap://ldap.wiz.com/o=Wiz,c=us", the resulting context will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * be that pointed to by "o=Wiz,c=us" on the server 'ldap.wiz.com'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Subsequent names that can be passed to this context will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * LDAP names relative to this context (e.g. cn="Barbs Jensen").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * If urlInfo is an array of URLs, the URLs are assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * to be equivalent in terms of the context to which they refer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * The resulting context is like that of the single URL case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * If urlInfo is of any other type, that is handled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * context factory for the URL scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param scheme the URL scheme id for the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param urlInfo information used to create the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @param name name of this object relative to <code>nameCtx</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param nameCtx Context whose provider resource file will be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *          for package prefix values (or null if none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param environment Environment properties for creating the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see javax.naming.InitialContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    private static Object getURLObject(String scheme, Object urlInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                       Name name, Context nameCtx,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   588
                                       Hashtable<?,?> environment)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // e.g. "ftpURLContextFactory"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        ObjectFactory factory = (ObjectFactory)ResourceManager.getFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            Context.URL_PKG_PREFIXES, environment, nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            "." + scheme + "." + scheme + "URLContextFactory", defaultPkgPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (factory == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // Found object factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            return factory.getObjectInstance(urlInfo, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
// ------------ Initial Context Factory Stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private static InitialContextFactoryBuilder initctx_factory_builder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Use this method for accessing initctx_factory_builder while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * inside an unsynchronized method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private static synchronized InitialContextFactoryBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    getInitialContextFactoryBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return initctx_factory_builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Creates an initial context using the specified environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * If an InitialContextFactoryBuilder has been installed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * it is used to create the factory for creating the initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Otherwise, the class specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> environment property is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Note that an initial context factory (an object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * InitialContextFactory interface) must be public and must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * public constructor that accepts no arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param env The possibly null environment properties used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *                  creating the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @return A non-null initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @exception NoInitialContextException If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *          <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *         is not found or names a nonexistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *         class or a class that cannot be instantiated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *          or if the initial context could not be created for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @exception NamingException If some other naming exception was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @see javax.naming.InitialContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @see javax.naming.directory.InitialDirContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public static Context getInitialContext(Hashtable<?,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        InitialContextFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (builder == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            // No factory installed, use property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // Get initial context factory class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            String className = env != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                (String)env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (className == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                NoInitialContextException ne = new NoInitialContextException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    "Need to specify class name in environment or system " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    "property, or as an applet parameter, or in an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    "application resource file:  " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    Context.INITIAL_CONTEXT_FACTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                factory = (InitialContextFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    helper.loadClass(className).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                NoInitialContextException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    new NoInitialContextException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        "Cannot instantiate class: " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            factory = builder.createInitialContextFactory(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return factory.getInitialContext(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Sets the InitialContextFactory builder to be builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * The builder can only be installed if the executing thread is allowed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * the security manager to do so. Once installed, the builder cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * be replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param builder The initial context factory builder to install. If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *                no builder is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @exception SecurityException builder cannot be installed for security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *                  reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @exception NamingException builder cannot be installed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *         a non-security-related reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @exception IllegalStateException If a builder was previous installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @see #hasInitialContextFactoryBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @see java.lang.SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public static synchronized void setInitialContextFactoryBuilder(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        InitialContextFactoryBuilder builder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (initctx_factory_builder != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    "InitialContextFactoryBuilder already set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            initctx_factory_builder = builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Determines whether an initial context factory builder has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return true if an initial context factory builder has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *           been set; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see #setInitialContextFactoryBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public static boolean hasInitialContextFactoryBuilder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return (getInitialContextFactoryBuilder() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
// -----  Continuation Context Stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Constant that holds the name of the environment property into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * which <tt>getContinuationContext()</tt> stores the value of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * <tt>CannotProceedException</tt> parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * This property is inherited by the continuation context, and may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * be used by that context's service provider to inspect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * fields of the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * The value of this constant is "java.naming.spi.CannotProceedException".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @see #getContinuationContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public static final String CPE = "java.naming.spi.CannotProceedException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Creates a context in which to continue a context operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * In performing an operation on a name that spans multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * namespaces, a context from one naming system may need to pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * the operation on to the next naming system.  The context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * implementation does this by first constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <code>CannotProceedException</code> containing information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * pinpointing how far it has proceeded.  It then obtains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * continuation context from JNDI by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * <code>getContinuationContext</code>.  The context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * implementation should then resume the context operation by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * invoking the same operation on the continuation context, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * the remainder of the name that has not yet been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Before making use of the <tt>cpe</tt> parameter, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * updates the environment associated with that object by setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * the value of the property <a href="#CPE"><tt>CPE</tt></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * to <tt>cpe</tt>.  This property will be inherited by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * continuation context, and may be used by that context's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * service provider to inspect the fields of this exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param cpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          The non-null exception that triggered this continuation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @return A non-null Context object for continuing the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @exception NamingException If a naming exception occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   774
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    public static Context getContinuationContext(CannotProceedException cpe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   778
        Hashtable<Object,Object> env = (Hashtable<Object,Object>)cpe.getEnvironment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (env == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   780
            env = new Hashtable<>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            // Make a (shallow) copy of the environment.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   783
            env = (Hashtable<Object,Object>)env.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        env.put(CPE, cpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        ContinuationContext cctx = new ContinuationContext(cpe, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return cctx.getTargetContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
// ------------ State Factory Stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Retrieves the state of an object for binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Service providers that implement the <tt>DirContext</tt> interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * should use <tt>DirectoryManager.getStateToBind()</tt>, not this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * Service providers that implement only the <tt>Context</tt> interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * should use this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * This method uses the specified state factories in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * the <tt>Context.STATE_FACTORIES</tt> property from the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * properties, and from the provider resource file associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <tt>nameCtx</tt>, in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *    The value of this property is a colon-separated list of factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *    class names that are tried in order, and the first one that succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *    in returning the object's state is the one used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * If no object's state can be retrieved in this way, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * object itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *    If an exception is encountered while retrieving the state, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *    exception is passed up to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Note that a state factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * (an object that implements the StateFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * interface) must be public and must have a public constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * accepts no arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * The <code>name</code> and <code>nameCtx</code> parameters may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * optionally be used to specify the name of the object being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * See the description of "Name and Context Parameters" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * {@link ObjectFactory#getObjectInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *          ObjectFactory.getObjectInstance()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * This method may return a <tt>Referenceable</tt> object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * service provider obtaining this object may choose to store it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * directly, or to extract its reference (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <tt>Referenceable.getReference()</tt>) and store that instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @param obj The non-null object for which to get state to bind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @param name The name of this object relative to <code>nameCtx</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *          or null if no name is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @param nameCtx The context relative to which the <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *          parameter is specified, or null if <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *          relative to the default initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *  @param environment The possibly null environment to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *          be used in the creation of the state factory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *  the object's state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @return The non-null object representing <tt>obj</tt>'s state for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *  binding.  It could be the object (<tt>obj</tt>) itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @exception NamingException If one of the factories accessed throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *          exception, or if an error was encountered while loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *          and instantiating the factory and object classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *          A factory should only throw an exception if it does not want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *          other factories to be used in an attempt to create an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *  See <tt>StateFactory.getStateToBind()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @see StateFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @see StateFactory#getStateToBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @see DirectoryManager#getStateToBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    public static Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        getStateToBind(Object obj, Name name, Context nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                       Hashtable<?,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        FactoryEnumeration factories = ResourceManager.getFactories(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            Context.STATE_FACTORIES, environment, nameCtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (factories == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        // Try each factory until one succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        StateFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        Object answer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        while (answer == null && factories.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            factory = (StateFactory)factories.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            answer = factory.getStateToBind(obj, name, nameCtx, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        return (answer != null) ? answer : obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
}