src/jdk.jlink/share/classes/jdk/tools/jlink/internal/PluginRepository.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.ServiceLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import jdk.tools.jlink.plugin.Plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * Plugin Providers repository. Plugin Providers are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * retrieved thanks to the ServiceLoader mechanism.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
public final class PluginRepository {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    private PluginRepository() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    private static final Map<String, Plugin> registeredPlugins = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
     * Retrieves the plugin associated to the passed name. If multiple providers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     * exist for the same name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
     * then an exception is thrown.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
     * @param name The plugin provider name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
     * @param pluginsLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
     * @return A provider or null if not found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    public static Plugin getPlugin(String name,
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42330
diff changeset
    58
            ModuleLayer pluginsLayer) {
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 36511
diff changeset
    59
        return getPlugin(Plugin.class, name, pluginsLayer);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     * Build plugin for the passed name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
     * @param config Optional config.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
     * @param name Non null name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     * @param pluginsLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     * @return A plugin or null if no plugin found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static Plugin newPlugin(Map<String, String> config, String name,
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42330
diff changeset
    71
            ModuleLayer pluginsLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Objects.requireNonNull(pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Plugin plugin = getPlugin(name, pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        if (plugin != null) {
42330
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    76
            try {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    77
                plugin.configure(config);
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    78
            } catch (IllegalArgumentException e) {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    79
                if (JlinkTask.DEBUG) {
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    80
                    System.err.println("Plugin " + plugin.getName() + " threw exception with config: " + config);
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    81
                    e.printStackTrace();
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    82
                }
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    83
                throw e;
bd999576b78d 8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents: 39321
diff changeset
    84
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        return plugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     * Explicit registration of a plugin in the repository. Used by unit tests
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     * @param plugin The plugin to register.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public synchronized static void registerPlugin(Plugin plugin) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        Objects.requireNonNull(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        registeredPlugins.put(plugin.getName(), plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * Explicit unregistration of a plugin in the repository. Used by unit
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * tests
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     * @param name Plugin name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    public synchronized static void unregisterPlugin(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        registeredPlugins.remove(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42330
diff changeset
   109
    public static List<Plugin> getPlugins(ModuleLayer pluginsLayer) {
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 36511
diff changeset
   110
        return getPlugins(Plugin.class, pluginsLayer);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    private static <T extends Plugin> T getPlugin(Class<T> clazz, String name,
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42330
diff changeset
   114
            ModuleLayer pluginsLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        Objects.requireNonNull(pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        T provider = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        List<T> javaProviders = getPlugins(clazz, pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        for(T factory : javaProviders) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            if (factory.getName().equals(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                if (provider != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                    throw new PluginException("Multiple plugin "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                            + "for the name " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                provider = factory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        return provider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    /**
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 36511
diff changeset
   133
     * The plugins accessible in the current context.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     * @param pluginsLayer
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 36511
diff changeset
   136
     * @return The list of plugins.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     */
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42330
diff changeset
   138
    private static <T extends Plugin> List<T> getPlugins(Class<T> clazz, ModuleLayer pluginsLayer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        Objects.requireNonNull(pluginsLayer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        List<T> factories = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            Iterator<T> providers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    = ServiceLoader.load(pluginsLayer, clazz).iterator();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            while (providers.hasNext()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                factories.add(providers.next());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            registeredPlugins.values().stream().forEach((fact) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                if (clazz.isInstance(fact)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                    T trans = (T) fact;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                    factories.add(trans);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            return factories;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        } catch (Exception ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            throw new PluginException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
}