jdk/src/share/classes/sun/security/jgss/ProviderList.java
author weijun
Tue, 07 May 2013 12:30:36 +0800
changeset 17435 ec797e955dca
parent 5506 202f599c92aa
child 24969 afa6934dd8e8
permissions -rw-r--r--
8010192: Enable native JGSS provider on Mac Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
     2
 * Copyright (c) 2000, 2009, 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: 2942
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: 2942
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: 2942
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
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.jgss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.jgss.wrapper.NativeGSSFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.jgss.wrapper.SunNativeProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class stores the list of providers that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * GSS-Implementation is configured to use. The GSSManagerImpl class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * queries this class whenever it needs a mechanism's factory.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This class stores an ordered list of pairs of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <provider, oid>. When it attempts to instantiate a mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * defined by oid o, it steps through the list looking for an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * with oid=o, or with oid=null. (An entry with oid=null matches all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * mechanisms.) When it finds such an entry, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * provider is approached for the mechanism's factory class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * At instantiation time this list in initialized to contain those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * system wide providers that contain a property of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * "GssApiMechanism.x.y.z..." where "x.y.z..." is a numeric object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * identifier with numbers x, y, z, etc. Such a property is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * to map to that provider's implementation of the MechanismFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * interface for the mechanism x.y.z...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * As and when a MechanismFactory is instantiated, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * cached for future use. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * An application can cause more providers to be added by means of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the addProviderAtFront and addProviderAtEnd methods on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * GSSManager which get delegated to this class. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * addProviderAtFront method can also cause a change in the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * of the providers without adding any new providers, by causing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * provider to move up in a list. The method addProviderAtEnd can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * only add providers at the end of the list if they are not already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * in the list. The rationale is that an application will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * addProviderAtFront when it wants a provider to be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * preference over the default ones. And it will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * addProviderAtEnd when it wants a provider to be used in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * the system ones don't suffice.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * If a mechanism's factory is being obtained from a provider as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * result of encountering a entryof the form <provider, oid> where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * oid is non-null, then the assumption is that the application added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * this entry and it wants this mechanism to be obtained from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * provider. Thus is the provider does not actually contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * requested mechanism, an exception will be thrown. However, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * entry were of the form <provider, null>, then it is viewed more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * liberally and is simply skipped over if the provider does not claim to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * support the requested mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public final class ProviderList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final String PROV_PROP_PREFIX = "GssApiMechanism.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final int PROV_PROP_PREFIX_LEN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        PROV_PROP_PREFIX.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String SPI_MECH_FACTORY_TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        = "sun.security.jgss.spi.MechanismFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // Undocumented property?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final String DEFAULT_MECH_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        "sun.security.jgss.mechanism";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final Oid DEFAULT_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * Set the default mechanism. Kerberos v5 is the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * mechanism unless it is overridden by a system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * with a valid OID value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Oid defOid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String defaultOidStr = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            (new GetPropertyAction(DEFAULT_MECH_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (defaultOidStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            defOid = GSSUtil.createOid(defaultOidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        DEFAULT_MECH_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            (defOid == null ? GSSUtil.GSS_KRB5_MECH_OID : defOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private ArrayList<PreferencesEntry> preferences =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        new ArrayList<PreferencesEntry>(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private HashMap<PreferencesEntry, MechanismFactory> factories =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        new HashMap<PreferencesEntry, MechanismFactory>(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private HashSet<Oid> mechs = new HashSet<Oid>(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   124
    final private GSSCaller caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   126
    public ProviderList(GSSCaller caller, boolean useNative) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.caller = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Provider[] provList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (useNative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            provList = new Provider[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            provList[0] = new SunNativeProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            provList = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int i = 0; i < provList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Provider prov = provList[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                addProviderAtEnd(prov, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // Move on to the next provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                GSSUtil.debug("Error in adding provider " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                              prov.getName() + ": " + ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } // End of for loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Determines if the given provider property represents a GSS-API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Oid to MechanismFactory mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return true if this is a GSS-API property, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private boolean isMechFactoryProperty(String prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return (prop.startsWith(PROV_PROP_PREFIX) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                prop.regionMatches(true, 0, // Try ignoring case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                   PROV_PROP_PREFIX, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                   PROV_PROP_PREFIX_LEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private Oid getOidFromMechFactoryProperty(String prop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String oidPart = prop.substring(PROV_PROP_PREFIX_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return new Oid(oidPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // So the existing code do not have to be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    synchronized public MechanismFactory getMechFactory(Oid mechOid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (mechOid == null) mechOid = ProviderList.DEFAULT_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return getMechFactory(mechOid, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Obtains a MechanismFactory for a given mechanism. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * specified provider is not null, then the impl from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * provider is used. Otherwise, the most preferred impl based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * on the configured preferences is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param mechOid the oid of the desired mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return a MechanismFactory for the desired mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws GSSException when the specified provider does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * support the desired mechanism, or when no provider supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the desired mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    synchronized public MechanismFactory getMechFactory(Oid mechOid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                        Provider p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (mechOid == null) mechOid = ProviderList.DEFAULT_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // Iterate thru all preferences to find right provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            PreferencesEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            Iterator<PreferencesEntry> list = preferences.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            while (list.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                entry = list.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (entry.impliesMechanism(mechOid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    MechanismFactory retVal = getMechFactory(entry, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if (retVal != null) return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } // end of while loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new GSSExceptionImpl(GSSException.BAD_MECH, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // Use the impl from the specified provider; return null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // the mech is unsupported by the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            PreferencesEntry entry = new PreferencesEntry(p, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return getMechFactory(entry, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Helper routine that uses a preferences entry to obtain an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * implementation of a MechanismFactory from it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param e the preferences entry that contains the provider and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * either a null of an explicit oid that matched the oid of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * desired mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param mechOid the oid of the desired mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws GSSException If the application explicitly requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * this entry's provider to be used for the desired mechanism but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * some problem is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private MechanismFactory getMechFactory(PreferencesEntry e, Oid mechOid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Provider p = e.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * See if a MechanismFactory was previously instantiated for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * this provider and mechanism combination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        PreferencesEntry searchEntry = new PreferencesEntry(p, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        MechanismFactory retVal = factories.get(searchEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (retVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             * Apparently not. Now try to instantiate this class from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             * the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            String prop = PROV_PROP_PREFIX + mechOid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            String className = p.getProperty(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (className != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                retVal = getMechFactoryImpl(p, className, mechOid, caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                factories.put(searchEntry, retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                 * This provider does not support this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                 * If the application explicitly requested that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                 * this provider be used for this mechanism, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                 * throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (e.getOid() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    throw new GSSExceptionImpl(GSSException.BAD_MECH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                         "Provider " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                         " does not support mechanism " + mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Helper routine to obtain a MechanismFactory implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * from the same class loader as the provider of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param p the provider whose classloader must be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * instantiating the desired MechanismFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @ param className the name of the MechanismFactory class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws GSSException If some error occurs when trying to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * instantiate this MechanismFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private static MechanismFactory getMechFactoryImpl(Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                                       String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                                       Oid mechOid,
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   274
                                                       GSSCaller caller)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            Class<?> baseClass = Class.forName(SPI_MECH_FACTORY_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
             * Load the implementation class with the same class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             * that was used to load the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
             * In order to get the class loader of a class, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             * caller's class loader must be the same as or an ancestor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             * the class loader being returned. Otherwise, the caller must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
             * have "getClassLoader" permission, or a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            ClassLoader cl = p.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            Class<?> implClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                implClass = cl.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                implClass = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (baseClass.isAssignableFrom(implClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                java.lang.reflect.Constructor<?> c =
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   301
                                implClass.getConstructor(GSSCaller.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                MechanismFactory mf = (MechanismFactory) (c.newInstance(caller));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (mf instanceof NativeGSSFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    ((NativeGSSFactory) mf).setMech(mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return mf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                throw createGSSException(p, className, "is not a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                         SPI_MECH_FACTORY_TYPE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw createGSSException(p, className, "cannot be created", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // Only used by getMechFactoryImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static GSSException createGSSException(Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                   String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                   String trailingMsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                   Exception cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String errClassInfo = className + " configured by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            p.getName() + " for GSS-API Mechanism Factory ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return new GSSExceptionImpl(GSSException.BAD_MECH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                    errClassInfo + trailingMsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public Oid[] getMechs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return mechs.toArray(new Oid[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    synchronized public void addProviderAtFront(Provider p, Oid mechOid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        PreferencesEntry newEntry = new PreferencesEntry(p, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        PreferencesEntry oldEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        boolean foundSomeMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Iterator<PreferencesEntry> list = preferences.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        while (list.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            oldEntry = list.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (newEntry.implies(oldEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                list.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (mechOid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            foundSomeMech = addAllMechsFromProvider(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            String oidStr = mechOid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (p.getProperty(PROV_PROP_PREFIX + oidStr) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw new GSSExceptionImpl(GSSException.BAD_MECH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                           "Provider " + p.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                           + " does not support "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                           + oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            mechs.add(mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            foundSomeMech = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (foundSomeMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            preferences.add(0, newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    synchronized public void addProviderAtEnd(Provider p, Oid mechOid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        PreferencesEntry newEntry = new PreferencesEntry(p, mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        PreferencesEntry oldEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        boolean foundSomeMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        Iterator<PreferencesEntry> list = preferences.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        while (list.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            oldEntry = list.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (oldEntry.implies(newEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // System.out.println("addProviderAtEnd: No it is not redundant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (mechOid == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            foundSomeMech = addAllMechsFromProvider(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            String oidStr = mechOid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (p.getProperty(PROV_PROP_PREFIX + oidStr) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                throw new GSSExceptionImpl(GSSException.BAD_MECH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                       "Provider " + p.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                       + " does not support "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                       + oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            mechs.add(mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            foundSomeMech = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (foundSomeMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            preferences.add(newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Helper routine to go through all properties contined in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * provider and add its mechanisms to the list of supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * mechanisms. If no default mechanism has been assinged so far,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * it sets the default MechanismFactory and Oid as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param p the provider to query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return true if there is at least one mechanism that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * provider contributed, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private boolean addAllMechsFromProvider(Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        String prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        boolean retVal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // Get all props for this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        Enumeration<Object> props = p.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // See if there are any GSS prop's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        while (props.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            prop = (String) props.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (isMechFactoryProperty(prop)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                // Ok! This is a GSS provider!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    Oid mechOid = getOidFromMechFactoryProperty(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    mechs.add(mechOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    retVal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    // Skip to next property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    GSSUtil.debug("Ignore the invalid property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                  prop + " from provider " + p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } // Processed GSS property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } // while loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Stores a provider and a mechanism oid indicating that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * provider should be used for the mechanism. If the mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Oid is null, then it indicates that this preference holds for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * any mechanism.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * The ProviderList maintains an ordered list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * PreferencesEntry's and iterates thru them as it tries to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * instantiate MechanismFactory's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private static final class PreferencesEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        private Provider p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        private Oid oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        PreferencesEntry(Provider p, Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            this.oid = oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (!(other instanceof PreferencesEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            PreferencesEntry that = (PreferencesEntry)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (this.p.getName().equals(that.p.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (this.oid != null && that.oid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return this.oid.equals(that.oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    return (this.oid == null && that.oid == 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
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            result = 37 * result + p.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (oid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                result = 37 * result + oid.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * Determines if a preference implies another. A preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * implies another if the latter is subsumed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * former. e.g., <Provider1, null> implies <Provider1, OidX>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * because the null in the former indicates that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * be used for all mechanisms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        boolean implies(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (other instanceof PreferencesEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                PreferencesEntry temp = (PreferencesEntry) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                return (equals(temp) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        p.getName().equals(temp.p.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        oid == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Oid getOid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * Determines if this entry is applicable to the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * mechanism. The entry is applicable to the desired mech if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * it contains the same oid or if it contains a null oid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         * indicating that it is applicable to all mechs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         * @param mechOid the desired mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * @return true if the provider in this entry should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * queried for this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        boolean impliesMechanism(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return (this.oid == null || this.oid.equals(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // For debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            StringBuffer buf = new StringBuffer("<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            buf.append(p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            buf.append(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            buf.append(">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
}