src/java.naming/share/classes/com/sun/naming/internal/FactoryEnumeration.java
author jlaskey
Thu, 14 Nov 2019 12:39:56 -0400
branchJDK-8193209-branch
changeset 59087 effb66aab08b
parent 47216 71c04702a3d5
permissions -rw-r--r--
[mq]: other
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 com.sun.naming.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  * The FactoryEnumeration is used for returning factory instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// no need to implement Enumeration since this is only for internal use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public final class FactoryEnumeration {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    40
    // List<NamedWeakReference<Class | Object>>
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    41
    private List<NamedWeakReference<Object>> factories;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int posn = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Records the input list and uses it directly to satisfy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * hasMore()/next() requests. An alternative would have been to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * an enumeration/iterator from the list, but we want to update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * list so we keep the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * original list. The list initially contains Class objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * As each element is used, the Class object is replaced by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * instance of the Class itself; eventually, the list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * only a list of factory instances and no more updates are required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <p> Both Class objects and factories are wrapped in weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * references so as not to prevent GC of the class loader.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * weak reference is tagged with the factory's class name so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * class can be reloaded if the reference is cleared.
22329
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    59
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param factories A non-null list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param loader    The class loader of the list's contents
22329
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    62
     *
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    63
     * This internal method is used with Thread Context Class Loader (TCCL),
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    64
     * please don't expose this method as public.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    66
    FactoryEnumeration(List<NamedWeakReference<Object>> factories,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    67
                       ClassLoader loader) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.factories = factories;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public Object next() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        synchronized (factories) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    75
            NamedWeakReference<Object> ref = factories.get(posn++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Object answer = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if ((answer != null) && !(answer instanceof Class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            String className = ref.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (answer == null) {   // reload class if weak ref cleared
22329
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    85
                    Class<?> cls = Class.forName(className, true, loader);
4cd45450d07c 8025758: Enhance Naming management
xuelei
parents: 10324
diff changeset
    86
                    answer = cls;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // Instantiate Class to get factory
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 25859
diff changeset
    89
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 25859
diff changeset
    90
                Object tmp = ((Class) answer).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 25859
diff changeset
    91
                answer = tmp;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    92
                ref = new NamedWeakReference<>(answer, className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                factories.set(posn-1, ref);  // replace Class object or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    new NamingException("No longer able to load " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                NamingException ne =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    new NamingException("Cannot instantiate " + answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                NamingException ne = new NamingException("Cannot access " + answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public boolean hasMore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        synchronized (factories) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return posn < factories.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}