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