jdk/src/java.base/share/classes/sun/security/jca/ProviderList.java
author valeriep
Fri, 26 Jun 2015 21:34:34 +0000
changeset 31270 e6470b24700d
parent 30374 2abaf49910ea
child 32649 2ee9017c7597
permissions -rw-r--r--
7191662: JCE providers should be located via ServiceLoader Summary: Enhanced to use ServiceLoader and switched provider to Provider.Service model. Reviewed-by: mullan, alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * List of Providers. Used to represent the provider preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The system starts out with a ProviderList that only has the classNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * of the Providers. Providers are loaded on demand only when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * For compatibility reasons, Providers that could not be loaded are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and internally presented as the instance EMPTY_PROVIDER. However, those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * objects cannot be presented to applications. Call the convert() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to force all Providers to be loaded and to obtain a ProviderList with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * invalid entries removed. All this is handled by the Security class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Note that all indices used by this class are 0-based per general Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * convention. These must be converted to the 1-based indices used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Security class externally when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Instances of this class are immutable. This eliminates the need for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * cloning and synchronization in consumers. The add() and remove() style
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * methods are static in order to avoid confusion about the immutability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class ProviderList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    final static sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        sun.security.util.Debug.getInstance("jca", "ProviderList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static ProviderConfig[] PC0 = new ProviderConfig[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static Provider[] P0 = new Provider[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // constant for an ProviderList with no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final ProviderList EMPTY = new ProviderList(PC0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // dummy provider object to use during initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // used to avoid explicit null checks in various places
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final Provider EMPTY_PROVIDER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        new Provider("##Empty##", 1.0d, "initialization in progress") {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    72
            private static final long serialVersionUID = 1151354171352296389L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // override getService() to return null slightly faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            public Service getService(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // construct a ProviderList from the security properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // (static provider configuration in the java.security file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static ProviderList fromSecurityProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // doPrivileged() because of Security.getProperty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        new PrivilegedAction<ProviderList>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            public ProviderList run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return new ProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static ProviderList add(ProviderList providerList, Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return insertAt(providerList, p, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static ProviderList insertAt(ProviderList providerList, Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (providerList.getProvider(p.getName()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return providerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   100
        List<ProviderConfig> list = new ArrayList<>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                    (Arrays.asList(providerList.configs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int n = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if ((position < 0) || (position > n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            position = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        list.add(position, new ProviderConfig(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return new ProviderList(list.toArray(PC0), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static ProviderList remove(ProviderList providerList, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // make sure provider exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (providerList.getProvider(name) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return providerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // copy all except matching to new list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ProviderConfig[] configs = new ProviderConfig[providerList.size() - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (ProviderConfig config : providerList.configs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (config.getProvider().getName().equals(name) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                configs[j++] = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return new ProviderList(configs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // Create a new ProviderList from the specified Providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // This method is for use by SunJSSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static ProviderList newList(Provider ... providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        ProviderConfig[] configs = new ProviderConfig[providers.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            configs[i] = new ProviderConfig(providers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return new ProviderList(configs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // configuration of the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private final ProviderConfig[] configs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // flag indicating whether all configs have been loaded successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private volatile boolean allLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // List returned by providers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private final List<Provider> userList = new AbstractList<Provider>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public Provider get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return getProvider(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Create a new ProviderList from an array of configs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private ProviderList(ProviderConfig[] configs, boolean allLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.configs = configs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.allLoaded = allLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Return a new ProviderList parsed from the java.security Properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private ProviderList() {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   164
        List<ProviderConfig> configList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for (int i = 1; true; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            String entry = Security.getProperty("security.provider." + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            entry = entry.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (entry.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                System.err.println("invalid entry for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                   "security.provider." + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int k = entry.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ProviderConfig config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (k == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                config = new ProviderConfig(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   181
                String provName = entry.substring(0, k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                String argument = entry.substring(k + 1).trim();
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   183
                config = new ProviderConfig(provName, argument);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // Get rid of duplicate providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (configList.contains(config) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                configList.add(config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        configs = configList.toArray(PC0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            debug.println("provider configuration: " + configList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Construct a special ProviderList for JAR verification. It consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * of the providers specified via jarClassNames, which must be on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * bootclasspath and cannot be in signed JAR files. This is to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * possible recursion and deadlock during verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   203
    ProviderList getJarList(String[] jarProvNames) {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   204
        List<ProviderConfig> newConfigs = new ArrayList<>();
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   205
        for (String provName : jarProvNames) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   206
            ProviderConfig newConfig = new ProviderConfig(provName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            for (ProviderConfig config : configs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // if the equivalent object is present in this provider list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // use the old object rather than the new object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // this ensures that when the provider is loaded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // new thread local list, it will also become available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                // in this provider list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (config.equals(newConfig)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    newConfig = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            newConfigs.add(newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ProviderConfig[] configArray = newConfigs.toArray(PC0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return new ProviderList(configArray, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Return the Provider at the specified index. Returns EMPTY_PROVIDER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * if the provider could not be loaded at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    Provider getProvider(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Provider p = configs[index].getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return (p != null) ? p : EMPTY_PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Return an unmodifiable List of all Providers in this List. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * individual Providers are loaded on demand. Elements that could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * be initialized are replaced with EMPTY_PROVIDER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public List<Provider> providers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return userList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private ProviderConfig getProviderConfig(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int index = getIndex(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return (index != -1) ? configs[index] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // return the Provider with the specified name or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Provider getProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ProviderConfig config = getProviderConfig(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return (config == null) ? null : config.getProvider();
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 the index at which the provider with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * installed or -1 if it is not present in this ProviderList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public int getIndex(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            Provider p = getProvider(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (p.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // attempt to load all Providers not already loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private int loadAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (allLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            debug.println("Loading all providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            Provider p = configs[i].getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (n == configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            allLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Try to load all Providers and return the ProviderList. If one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * more Providers could not be loaded, a new ProviderList with those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * entries removed is returned. Otherwise, the method returns this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    ProviderList removeInvalid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int n = loadAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (n == configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        ProviderConfig[] newConfigs = new ProviderConfig[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for (int i = 0, j = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            ProviderConfig config = configs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (config.isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                newConfigs[j++] = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return new ProviderList(newConfigs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // return the providers as an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public Provider[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return providers().toArray(P0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // return a String representation of this ProviderList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return Arrays.asList(configs).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Return a Service describing an implementation of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * algorithm from the Provider with the highest precedence that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * supports that algorithm. Return null if no Provider supports this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Service getService(String type, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        for (int i = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            Provider p = getProvider(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            Service s = p.getService(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Return a List containing all the Services describing implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * of the specified algorithms in precedence order. If no implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * exists, this method returns an empty List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * The elements of this list are determined lazily on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * The List returned is NOT thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public List<Service> getServices(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return new ServiceList(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * This method exists for compatibility with JCE only. It will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * once JCE has been changed to use the replacement method.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   356
     * @deprecated use {@code getServices(List<ServiceId>)} instead
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public List<Service> getServices(String type, List<String> algorithms) {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   360
        List<ServiceId> ids = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (String alg : algorithms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            ids.add(new ServiceId(type, alg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return getServices(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public List<Service> getServices(List<ServiceId> ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return new ServiceList(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Inner class for a List of Services. Custom List implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * order to delay Provider initialization and lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Not thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private final class ServiceList extends AbstractList<Service> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // type and algorithm for simple lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // avoid allocating/traversing the ServiceId list for these lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // list of ids for parallel lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // if ids is non-null, type and algorithm are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        private final List<ServiceId> ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // first service we have found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // it is stored in a separate variable so that we can avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // allocating the services list if we do not need the second service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // this is the case if we don't failover (failovers are typically rare)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // list of the services we have found so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private List<Service> services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // index into config[] of the next provider we need to query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        private int providerIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        ServiceList(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            this.ids = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        ServiceList(List<ServiceId> ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            this.type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            this.algorithm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            this.ids = ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        private void addService(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (firstService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                firstService = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (services == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    services = new ArrayList<Service>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    services.add(firstService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                services.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        private Service tryGet(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if ((index == 0) && (firstService != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    return firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                } else if ((services != null) && (services.size() > index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    return services.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (providerIndex >= configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                // check all algorithms in this provider before moving on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                Provider p = getProvider(providerIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    // simple lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    Service s = p.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        addService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    // parallel lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    for (ServiceId id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        Service s = p.getService(id.type, id.algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            addService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        public Service get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            Service s = tryGet(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (services != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                n = services.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                n = (firstService != null) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            while (tryGet(n) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // override isEmpty() and iterator() to not call size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // this avoids loading + checking all Providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return (tryGet(0) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        public Iterator<Service> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return new Iterator<Service>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    return tryGet(index) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                public Service next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    Service s = tryGet(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
}