author | redestad |
Fri, 17 Feb 2017 18:12:55 +0100 | |
changeset 43837 | 51a9f82cff03 |
parent 42330 | bd999576b78d |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package jdk.tools.jlink.internal.plugins; |
|
26 |
||
27 |
import java.io.File; |
|
28 |
import java.io.IOException; |
|
42330
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
39834
diff
changeset
|
29 |
import java.io.UncheckedIOException; |
38871
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
30 |
import java.nio.file.FileSystem; |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
31 |
import java.nio.file.Files; |
38871
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
32 |
import java.nio.file.PathMatcher; |
36511 | 33 |
import java.util.ArrayList; |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
34 |
import java.util.HashMap; |
36511 | 35 |
import java.util.List; |
36 |
import java.util.Map; |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
37 |
import java.util.function.ToIntFunction; |
36511 | 38 |
import jdk.tools.jlink.plugin.PluginException; |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
39 |
import jdk.tools.jlink.plugin.ResourcePool; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
40 |
import jdk.tools.jlink.plugin.ResourcePoolBuilder; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
41 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
42 |
import jdk.tools.jlink.plugin.Plugin; |
36511 | 43 |
import jdk.tools.jlink.internal.Utils; |
44 |
||
45 |
/** |
|
46 |
* |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
47 |
* Order Resources plugin |
36511 | 48 |
*/ |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
49 |
public final class OrderResourcesPlugin implements Plugin { |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
50 |
public static final String NAME = "order-resources"; |
38871
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
51 |
private static final FileSystem JRT_FILE_SYSTEM = Utils.jrtFileSystem(); |
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
52 |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
53 |
private final List<ToIntFunction<String>> filters; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
54 |
private final Map<String, Integer> orderedPaths; |
36511 | 55 |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
56 |
public OrderResourcesPlugin() { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
57 |
this.filters = new ArrayList<>(); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
58 |
this.orderedPaths = new HashMap<>(); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
59 |
} |
36511 | 60 |
|
61 |
@Override |
|
62 |
public String getName() { |
|
63 |
return NAME; |
|
64 |
} |
|
65 |
||
66 |
static class SortWrapper { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
67 |
private final ResourcePoolEntry resource; |
36511 | 68 |
private final int ordinal; |
69 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
70 |
SortWrapper(ResourcePoolEntry resource, int ordinal) { |
36511 | 71 |
this.resource = resource; |
72 |
this.ordinal = ordinal; |
|
73 |
} |
|
74 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
75 |
ResourcePoolEntry getResource() { |
36511 | 76 |
return resource; |
77 |
} |
|
78 |
||
79 |
String getPath() { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
80 |
return resource.path(); |
36511 | 81 |
} |
82 |
||
83 |
int getOrdinal() { |
|
84 |
return ordinal; |
|
85 |
} |
|
86 |
} |
|
87 |
||
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
88 |
private String stripModule(String path) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
89 |
if (path.startsWith("/")) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
90 |
int index = path.indexOf('/', 1); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
91 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
92 |
if (index != -1) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
93 |
return path.substring(index + 1); |
36511 | 94 |
} |
95 |
} |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
96 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
97 |
return path; |
36511 | 98 |
} |
99 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
100 |
private int getOrdinal(ResourcePoolEntry resource) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
101 |
String path = resource.path(); |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
102 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
103 |
Integer value = orderedPaths.get(stripModule(path)); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
104 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
105 |
if (value != null) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
106 |
return value; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
107 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
108 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
109 |
for (ToIntFunction<String> function : filters) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
110 |
int ordinal = function.applyAsInt(path); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
111 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
112 |
if (ordinal != Integer.MAX_VALUE) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
113 |
return ordinal; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
114 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
115 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
116 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
117 |
return Integer.MAX_VALUE; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
118 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
119 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
120 |
private static int compare(SortWrapper wrapper1, SortWrapper wrapper2) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
121 |
int compare = wrapper1.getOrdinal() - wrapper2.getOrdinal(); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
122 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
123 |
if (compare != 0) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
124 |
return compare; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
125 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
126 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
127 |
return wrapper1.getPath().compareTo(wrapper2.getPath()); |
36511 | 128 |
} |
129 |
||
130 |
@Override |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
131 |
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { |
38320 | 132 |
in.entries() |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
133 |
.filter(resource -> resource.type() |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
134 |
.equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
135 |
.map((resource) -> new SortWrapper(resource, getOrdinal(resource))) |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
136 |
.sorted(OrderResourcesPlugin::compare) |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
137 |
.forEach((wrapper) -> out.add(wrapper.getResource())); |
38320 | 138 |
in.entries() |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
139 |
.filter(other -> !other.type() |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
140 |
.equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
141 |
.forEach((other) -> out.add(other)); |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
142 |
|
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
143 |
return out.build(); |
36511 | 144 |
} |
145 |
||
146 |
@Override |
|
39129
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
147 |
public Category getType() { |
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
148 |
return Category.SORTER; |
36511 | 149 |
} |
150 |
||
151 |
@Override |
|
152 |
public String getDescription() { |
|
153 |
return PluginsResourceBundle.getDescription(NAME); |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public boolean hasArguments() { |
|
158 |
return true; |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public String getArgumentsDescription() { |
|
163 |
return PluginsResourceBundle.getArgument(NAME); |
|
164 |
} |
|
165 |
||
166 |
@Override |
|
167 |
public void configure(Map<String, String> config) { |
|
39042 | 168 |
List<String> patterns = Utils.parseList(config.get(NAME)); |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
169 |
int ordinal = 0; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
170 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
171 |
for (String pattern : patterns) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
172 |
if (pattern.startsWith("@")) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
173 |
File file = new File(pattern.substring(1)); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
174 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
175 |
if (file.exists()) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
176 |
List<String> lines; |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
177 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
178 |
try { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
179 |
lines = Files.readAllLines(file.toPath()); |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
180 |
} catch (IOException ex) { |
42330
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
39834
diff
changeset
|
181 |
throw new UncheckedIOException(ex); |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
182 |
} |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
183 |
|
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
184 |
for (String line : lines) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
185 |
if (!line.startsWith("#")) { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
186 |
orderedPaths.put(line + ".class", ordinal++); |
36511 | 187 |
} |
188 |
} |
|
189 |
} |
|
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
190 |
} else { |
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
191 |
final int result = ordinal++; |
38871
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
192 |
final PathMatcher matcher = Utils.getPathMatcher(JRT_FILE_SYSTEM, pattern); |
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
193 |
ToIntFunction<String> function = (path)-> matcher.matches(JRT_FILE_SYSTEM.getPath(path)) ? result : Integer.MAX_VALUE; |
37888
c34e01b87292
8155237: jlink plugin to order resources should take a class list as input
jlaskey
parents:
36511
diff
changeset
|
194 |
filters.add(function); |
38871
ec08bf1979d4
8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents:
38320
diff
changeset
|
195 |
} |
36511 | 196 |
} |
197 |
} |
|
198 |
} |