jdk/src/java.base/share/classes/sun/security/jca/ProviderConfig.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 25859 3317bb8137f4
child 31270 e6470b24700d
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22041
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 2449
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: 2449
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: 2449
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2449
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2449
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 sun.security.jca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.PropertyExpander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Class representing a configured provider. Encapsulates configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * (className plus optional argument), the provider loading logic, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the loaded Provider object itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
final class ProviderConfig {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final static sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        sun.security.util.Debug.getInstance("jca", "ProviderConfig");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // classname of the SunPKCS11-Solaris provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final String P11_SOL_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        "sun.security.pkcs11.SunPKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // config file argument of the SunPKCS11-Solaris provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final String P11_SOL_ARG  =
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    54
        "${java.home}/conf/security/sunpkcs11-solaris.cfg";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // maximum number of times to try loading a provider before giving up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static int MAX_LOAD_TRIES = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // parameters for the Provider(String) constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // use by doLoadProvider()
22041
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 5506
diff changeset
    61
    private final static Class<?>[] CL_STRING = { String.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // name of the provider class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // argument to the provider constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // empty string indicates no-arg constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final String argument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // number of times we have already tried to load this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int tries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Provider object, if loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private volatile Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // flag indicating if we are currently trying to load the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // used to detect recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean isLoading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    ProviderConfig(String className, String argument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (className.equals(P11_SOL_NAME) && argument.equals(P11_SOL_ARG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            checkSunPKCS11Solaris();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.argument = expand(argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    ProviderConfig(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this(className, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    ProviderConfig(Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.className = provider.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.argument = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // check if we should try to load the SunPKCS11-Solaris provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // avoid if not available (pre Solaris 10) to reduce startup time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // or if disabled via system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private void checkSunPKCS11Solaris() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Boolean o = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                new PrivilegedAction<Boolean>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                File file = new File("/usr/lib/libpkcs11.so");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (file.exists() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if ("false".equalsIgnoreCase(System.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        ("sun.security.pkcs11.enable-solaris"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (o == Boolean.FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            tries = MAX_LOAD_TRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private boolean hasArgument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return argument.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // should we try to load this provider?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private boolean shouldLoad() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return (tries < MAX_LOAD_TRIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // do not try to load this provider again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private void disableLoad() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        tries = MAX_LOAD_TRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    boolean isLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return (provider != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (obj instanceof ProviderConfig == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ProviderConfig other = (ProviderConfig)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return this.className.equals(other.className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            && this.argument.equals(other.argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return className.hashCode() + argument.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (hasArgument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return className + "('" + argument + "')";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Get the provider object. Loads the provider if it is not already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   166
    synchronized Provider getProvider() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // volatile variable load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Provider p = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (shouldLoad() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   175
        if (isLoading) {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   176
            // because this method is synchronized, this can only
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   177
            // happen if there is recursion.
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   178
            if (debug != null) {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   179
                debug.println("Recursion loading provider: " + this);
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   180
                new Exception("Call trace").printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   182
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   184
        try {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   185
            isLoading = true;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   186
            tries++;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   187
            p = doLoadProvider();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   188
        } finally {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   189
            isLoading = false;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   190
        }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents: 2
diff changeset
   191
        provider = p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Load and instantiate the Provider described by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * NOTE use of doPrivileged().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return null if the Provider could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws ProviderException if executing the Provider's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * throws a ProviderException. All other Exceptions are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private Provider doLoadProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return AccessController.doPrivileged(new PrivilegedAction<Provider>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            public Provider run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    debug.println("Loading provider: " + ProviderConfig.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    Class<?> provClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        provClass = cl.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        provClass = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    if (hasArgument() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        obj = provClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        Constructor<?> cons = provClass.getConstructor(CL_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        obj = cons.newInstance(argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    if (obj instanceof Provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                            debug.println("Loaded provider " + obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        return (Provider)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            debug.println(className + " is not a provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        disableLoad();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    Throwable t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    if (e instanceof InvocationTargetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        t = ((InvocationTargetException)e).getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        t = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        debug.println("Error loading provider " + ProviderConfig.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    // provider indicates fatal error, pass through exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    if (t instanceof ProviderException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        throw (ProviderException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    // provider indicates that loading should not be retried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (t instanceof UnsupportedOperationException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        disableLoad();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    return null;
25408
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   258
                } catch (ExceptionInInitializerError err) {
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   259
                    // no sufficient permission to initialize provider class
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   260
                    if (debug != null) {
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   261
                        debug.println("Error loading provider " + ProviderConfig.this);
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   262
                        err.printStackTrace();
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   263
                    }
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   264
                    disableLoad();
27563093d2d2 8043406: Change default policy for JCE providers to run with as few privileges as possible
valeriep
parents: 22041
diff changeset
   265
                    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Perform property expansion of the provider value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * NOTE use of doPrivileged().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private static String expand(final String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // shortcut if value does not contain any properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (value.contains("${") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    return PropertyExpander.expand(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}