8161055: Remove plugin ordering by isAfter, isBefore

Reviewed-by: mchung, jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2016-07-08 17:11:51 +05:30
parent f1e53d48e9
commit 09a43a8546
5 changed files with 1 additions and 602 deletions

View File

@ -93,8 +93,7 @@ public final class ImagePluginConfiguration {
List<Plugin> orderedPlugins = new ArrayList<>();
plugins.entrySet().stream().forEach((entry) -> {
// Sort according to plugin constraints
orderedPlugins.addAll(PluginOrderingGraph.sort(entry.getValue()));
orderedPlugins.addAll(entry.getValue());
});
Plugin lastSorter = null;
for (Plugin plugin : orderedPlugins) {

View File

@ -1,150 +0,0 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.tools.jlink.internal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jdk.tools.jlink.plugin.Plugin;
import jdk.tools.jlink.plugin.PluginException;
public class PluginOrderingGraph {
static class Node {
Plugin plugin;
Set<Node> nexts = new HashSet<>();
Set<Node> previous = new HashSet<>();
@Override
public String toString() {
return plugin.getName();
}
}
public static List<Plugin> sort(List<Plugin> plugs) {
List<Plugin> plugins = new ArrayList<>();
plugins.addAll(plugs);
// for each plugin creates its graph.
Map<Plugin, Node> graphs = buildGraphs(plugins);
// At this points all individual graphs are connected
// Check for cycles.
for (Node n : graphs.values()) {
checkCycle(n);
}
List<Plugin> orderedPlugins = new ArrayList<>();
// Retrieve the current roots, add them to the result, remove them from
while (!plugins.isEmpty()) {
// Build the current set of graphs from the list of input plugins
Map<Plugin, Node> currentGraphs = buildGraphs(plugins);
// Retrieve the root nodes (no previous)
List<Node> roots = getRoots(currentGraphs);
for (Node n : roots) {
// add them to the ordered list.
orderedPlugins.add(n.plugin);
// remove them from the input list.
plugins.remove(n.plugin);
}
}
return orderedPlugins;
}
// Create a grapth according to list of plugins.
private static Map<Plugin, Node> buildGraphs(List<Plugin> plugins) {
Map<String, Node> nodeStore = new HashMap<>();
for (Plugin p : plugins) {
Node newNode = new Node();
newNode.plugin = p;
nodeStore.put(p.getName(), newNode);
}
// for each plugin creates its graph.
Map<Plugin, Node> graphs = new LinkedHashMap<>();
for (Plugin p : plugins) {
Node node = nodeStore.get(p.getName());
for (String after : p.isAfter()) {
Node previous = nodeStore.get(after);
if (previous == null) {
continue;
}
node.previous.add(previous);
previous.nexts.add(node);
}
for (String before : p.isBefore()) {
Node next = nodeStore.get(before);
if (next == null) {
continue;
}
node.nexts.add(next);
next.previous.add(node);
}
graphs.put(p, node);
}
return graphs;
}
private static List<Node> getRoots(Map<Plugin, Node> graphs) {
List<Node> ret = new ArrayList<>();
for (Node n : graphs.values()) {
if (n.previous.isEmpty()) {
ret.add(n);
}
}
return ret;
}
private static void checkCycle(Node root) {
for (Node next : root.nexts) {
Set<Node> path = new LinkedHashSet<>();
path.add(root);
checkCycle(next, root, path);
}
}
private static void checkCycle(Node current, Node root, Set<Node> path) {
path.add(current);
if (current == root) {
StringBuilder builder = new StringBuilder();
for (Node p : path) {
builder.append(p.plugin.getName()).append(">");
}
builder.append(root);
throw new PluginException("Cycle detected for " + builder.toString()
+ ". Please fix Plugin ordering (before, after constraints).");
}
for (Node n : current.nexts) {
checkCycle(n, root, path);
}
}
}

View File

@ -106,24 +106,6 @@ public interface Plugin {
return EnumSet.of(State.FUNCTIONAL);
}
/**
* The set of plugin names that must be located, within the stack of plugins,
* before this plugin.
* @return The set of names. By default this set is empty.
*/
public default Set<String> isBefore() {
return Collections.emptySet();
}
/**
* The set of plugin names that must be located, within the stack of plugins,
* after this plugin.
* @return The set of names. By default this set is empty.
*/
public default Set<String> isAfter() {
return Collections.emptySet();
}
/**
* The plugin name.
* @return The name.

View File

@ -105,60 +105,6 @@ public class IntegrationTest {
}
}
public static class MyPlugin1 implements Plugin {
Integer index;
Set<String> after;
Set<String> before;
private MyPlugin1(Integer index, Set<String> after, Set<String> before) {
this.index = index;
this.after = after;
this.before = before;
}
@Override
public Set<String> isAfter() {
return after;
}
@Override
public Set<String> isBefore() {
return before;
}
@Override
public String getName() {
return NAME + index;
}
@Override
public void visit(ModulePool in, ModulePool out) {
System.err.println(NAME + index);
ordered.add(index);
in.transformAndCopy((file) -> {
return file;
}, out);
}
@Override
public String getDescription() {
return null;
}
@Override
public String getOption() {
return null;
}
static final String NAME = "myprovider";
static final String INDEX = "INDEX";
@Override
public void configure(Map<String, String> config) {
throw new UnsupportedOperationException("Shouldn't be called");
}
}
public static void main(String[] args) throws Exception {
Helper helper = Helper.newHelper();
@ -168,8 +114,6 @@ public class IntegrationTest {
}
apitest();
test();
testOrder();
testCycleOrder();
}
private static void apitest() throws Exception {
@ -262,109 +206,4 @@ public class IntegrationTest {
}
}
private static void testOrder() throws Exception {
Jlink jlink = new Jlink();
Path output = Paths.get("integrationout2");
List<Path> modulePaths = new ArrayList<>();
File jmods
= JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk")));
modulePaths.add(jmods.toPath());
Set<String> mods = new HashSet<>();
mods.add("java.management");
Set<String> limits = new HashSet<>();
limits.add("java.management");
JlinkConfiguration config = new Jlink.JlinkConfiguration(output,
modulePaths, mods, limits, null);
List<Plugin> lst = new ArrayList<>();
// Order is Plug1>Plug2>Plug3
// Plug1
// TRANSFORMER 3, must be after 2.
{
Set<String> after = new HashSet<>();
after.add(MyPlugin1.NAME+"2");
lst.add(new MyPlugin1(3, after, Collections.emptySet()));
}
// TRANSFORMER 2, must be after 1.
{
Set<String> after = new HashSet<>();
after.add(MyPlugin1.NAME+"1");
lst.add(new MyPlugin1(2, after, Collections.emptySet()));
}
// TRANSFORMER 1
{
Set<String> before = new HashSet<>();
before.add(MyPlugin1.NAME+"2");
lst.add(new MyPlugin1(1, Collections.emptySet(), before));
}
// Image builder
DefaultImageBuilder builder = new DefaultImageBuilder(output);
PluginsConfiguration plugins
= new Jlink.PluginsConfiguration(lst, builder, null);
jlink.build(config, plugins);
if (ordered.isEmpty()) {
throw new AssertionError("Plugins not called");
}
List<Integer> clone = new ArrayList<>();
clone.addAll(ordered);
Collections.sort(clone);
if (!clone.equals(ordered)) {
throw new AssertionError("Ordered is not properly sorted" + ordered);
}
}
private static void testCycleOrder() throws Exception {
Jlink jlink = new Jlink();
Path output = Paths.get("integrationout3");
List<Path> modulePaths = new ArrayList<>();
File jmods
= JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk")));
modulePaths.add(jmods.toPath());
Set<String> mods = new HashSet<>();
mods.add("java.management");
Set<String> limits = new HashSet<>();
limits.add("java.management");
JlinkConfiguration config = new Jlink.JlinkConfiguration(output,
modulePaths, mods, limits, null);
List<Plugin> lst = new ArrayList<>();
// packager 1
{
Set<String> before = new HashSet<>();
before.add(MyPlugin1.NAME+"2");
lst.add(new MyPlugin1(1, Collections.emptySet(), before));
}
// packager 2
{
Set<String> before = new HashSet<>();
before.add(MyPlugin1.NAME+"1");
lst.add(new MyPlugin1(2, Collections.emptySet(), before));
}
// Image builder
DefaultImageBuilder builder = new DefaultImageBuilder(output);
PluginsConfiguration plugins
= new Jlink.PluginsConfiguration(lst, builder, null);
boolean failed = false;
try {
jlink.build(config, plugins);
failed = true;
} catch (Exception ex) {
// XXX OK
}
if (failed) {
throw new AssertionError("Should have failed");
}
}
}

View File

@ -1,271 +0,0 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Test order of plugins
* @author Jean-Francois Denise
* @library ../../lib
* @modules java.base/jdk.internal.jimage
* jdk.jdeps/com.sun.tools.classfile
* jdk.jlink/jdk.tools.jlink.internal
* jdk.jlink/jdk.tools.jmod
* jdk.jlink/jdk.tools.jimage
* jdk.compiler
* @build tests.*
* @run main/othervm PluginOrderTest
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jdk.tools.jlink.internal.PluginOrderingGraph;
import jdk.tools.jlink.plugin.Plugin;
import jdk.tools.jlink.plugin.Plugin.Category;
import jdk.tools.jlink.plugin.ModulePool;
public class PluginOrderTest {
public static void main(String[] args) throws Exception {
validGraph0();
validGraph1();
boolean failed = false;
try {
withCycles0();
failed = true;
} catch (Exception ex) {
//ok
System.err.println(ex.getMessage());
}
if (failed) {
throw new Exception("Should have failed");
}
try {
withCycles1();
failed = true;
} catch (Exception ex) {
//ok
System.err.println(ex.getMessage());
}
if (failed) {
throw new Exception("Should have failed");
}
try {
withCycles2();
failed = true;
} catch (Exception ex) {
//ok
System.err.println(ex.getMessage());
}
if (failed) {
throw new Exception("Should have failed");
}
}
private static void validGraph0() throws Exception {
Set<String> set = new HashSet<>();
set.add("plug2");
List<Plugin> plugins = new ArrayList<>();
plugins.add(new Plug("plug2", Collections.emptySet(), Collections.emptySet(),
Category.TRANSFORMER));
plugins.add(new Plug("plug1", set, Collections.emptySet(), Category.TRANSFORMER));
List<Plugin> ordered = PluginOrderingGraph.sort(plugins);
if (ordered.get(0) != plugins.get(1) || ordered.get(1) != plugins.get(0)) {
throw new Exception("Invalid sorting");
}
}
private static void validGraph1() {
Set<String> lst1 = new HashSet<>();
lst1.add("plug2");
lst1.add("plug3");
Plugin p1 = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst3 = new HashSet<>();
lst3.add("plug4");
lst3.add("plug6");
Plugin p3 = new Plug("plug3", lst3, Collections.emptySet(), Category.TRANSFORMER);
Plugin p4 = new Plug("plug4", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst5 = new HashSet<>();
lst5.add("plug3");
lst5.add("plug1");
lst5.add("plug2");
lst5.add("plug6");
Plugin p5 = new Plug("plug5", lst5, Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst6 = new HashSet<>();
lst6.add("plug4");
lst6.add("plug2");
Plugin p6 = new Plug("plug6", lst6, Collections.emptySet(), Category.TRANSFORMER);
Plugin p7 = new Plug("plug7", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Plugin p8 = new Plug("plug8", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
List<Plugin> plugins = new ArrayList<>();
plugins.add(p1);
plugins.add(p2);
plugins.add(p3);
plugins.add(p4);
plugins.add(p5);
plugins.add(p6);
plugins.add(p7);
plugins.add(p8);
PluginOrderingGraph.sort(plugins);
}
private static void withCycles0() throws Exception {
Set<String> set2 = new HashSet<>();
set2.add("plug1");
List<Plugin> plugins = new ArrayList<>();
plugins.add(new Plug("plug2", set2, Collections.emptySet(),
Category.TRANSFORMER));
Set<String> set1 = new HashSet<>();
set1.add("plug2");
plugins.add(new Plug("plug1", set1, Collections.emptySet(), Category.TRANSFORMER));
PluginOrderingGraph.sort(plugins);
}
private static void withCycles2() {
Set<String> lst1 = new HashSet<>();
lst1.add("plug2");
lst1.add("plug3");
Plugin p1 = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst3 = new HashSet<>();
lst3.add("plug4");
lst3.add("plug6");
Plugin p3 = new Plug("plug3", lst3, Collections.emptySet(), Category.TRANSFORMER);
Plugin p4 = new Plug("plug4", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst5 = new HashSet<>();
lst5.add("plug3");
lst5.add("plug1");
lst5.add("plug2");
Plugin p5 = new Plug("plug5", lst5, Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst6 = new HashSet<>();
lst6.add("plug4");
lst6.add("plug1");
Plugin p6 = new Plug("plug6", lst6, Collections.emptySet(), Category.TRANSFORMER);
Plugin p7 = new Plug("plug7", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Plugin p8 = new Plug("plug8", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
List<Plugin> plugins = new ArrayList<>();
plugins.add(p1);
plugins.add(p2);
plugins.add(p3);
plugins.add(p4);
plugins.add(p5);
plugins.add(p6);
plugins.add(p7);
plugins.add(p8);
PluginOrderingGraph.sort(plugins);
}
private static void withCycles1() {
Set<String> lst1 = new HashSet<>();
lst1.add("plug2");
lst1.add("plug3");
Plugin p = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
Set<String> lst3 = new HashSet<>();
lst3.add("plug2");
Set<String> lst4 = new HashSet<>();
lst4.add("plug1");
Plugin p3 = new Plug("plug3", lst4, lst3, Category.TRANSFORMER);
List<Plugin> plugins = new ArrayList<>();
plugins.add(p);
plugins.add(p2);
plugins.add(p3);
PluginOrderingGraph.sort(plugins);
}
private static class Plug implements Plugin {
private final Set<String> isBefore;
private final Set<String> isAfter;
private final Category category;
private final String name;
private Plug(String name, Set<String> isBefore, Set<String> isAfter, Category category) {
this.name = name;
this.isBefore = isBefore;
this.isAfter = isAfter;
this.category = category;
}
@Override
public Set<String> isAfter() {
return isAfter;
}
@Override
public Set<String> isBefore() {
return isBefore;
}
@Override
public String toString() {
return name;
}
@Override
public void visit(ModulePool in, ModulePool out) {
}
@Override
public Category getType() {
return category;
}
@Override
public String getName() {
return name;
}
}
}