author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 44359 | c6761862ca0b |
child 45004 | ea3137042a61 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 |
||
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
27 |
import java.io.ByteArrayOutputStream; |
36511 | 28 |
import java.io.FileInputStream; |
29 |
import java.io.IOException; |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
30 |
import java.io.PrintWriter; |
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
31 |
import java.io.UncheckedIOException; |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
32 |
import java.lang.module.ModuleDescriptor; |
36511 | 33 |
import java.util.EnumSet; |
38320 | 34 |
import java.util.HashMap; |
36511 | 35 |
import java.util.Map; |
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
36 |
import java.util.Optional; |
38320 | 37 |
import java.util.Properties; |
36511 | 38 |
import java.util.Set; |
38320 | 39 |
import java.util.function.Function; |
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
40 |
import java.util.stream.Collectors; |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
41 |
import jdk.tools.jlink.internal.ModuleSorter; |
36511 | 42 |
import jdk.tools.jlink.internal.Utils; |
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
|
43 |
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
|
44 |
import jdk.tools.jlink.plugin.ResourcePoolBuilder; |
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
45 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
46 |
import jdk.tools.jlink.plugin.ResourcePoolModule; |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
47 |
import jdk.tools.jlink.plugin.Plugin; |
36511 | 48 |
|
49 |
/** |
|
50 |
* This plugin adds/deletes information for 'release' file. |
|
51 |
*/ |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39308
diff
changeset
|
52 |
public final class ReleaseInfoPlugin implements Plugin { |
36511 | 53 |
// option name |
54 |
public static final String NAME = "release-info"; |
|
55 |
public static final String KEYS = "keys"; |
|
38320 | 56 |
private final Map<String, String> release = new HashMap<>(); |
36511 | 57 |
|
58 |
@Override |
|
39129
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
59 |
public Category getType() { |
4b2086305b68
8159593: Plugin Set<Category> getType() should return a Category
sundar
parents:
39042
diff
changeset
|
60 |
return Category.METAINFO_ADDER; |
36511 | 61 |
} |
62 |
||
63 |
@Override |
|
64 |
public String getName() { |
|
65 |
return NAME; |
|
66 |
} |
|
67 |
||
68 |
@Override |
|
69 |
public String getDescription() { |
|
70 |
return PluginsResourceBundle.getDescription(NAME); |
|
71 |
} |
|
72 |
||
73 |
@Override |
|
38320 | 74 |
public Set<State> getState() { |
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
75 |
return EnumSet.of(State.AUTO_ENABLED, State.FUNCTIONAL); |
36511 | 76 |
} |
77 |
||
78 |
@Override |
|
79 |
public boolean hasArguments() { |
|
80 |
return true; |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
84 |
public String getArgumentsDescription() { |
|
85 |
return PluginsResourceBundle.getArgument(NAME); |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
38320 | 89 |
public void configure(Map<String, String> config) { |
90 |
String operation = config.get(NAME); |
|
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
91 |
if (operation == null) { |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
92 |
return; |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
93 |
} |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
94 |
|
38320 | 95 |
switch (operation) { |
96 |
case "add": { |
|
97 |
// leave it to open-ended! source, java_version, java_full_version |
|
98 |
// can be passed via this option like: |
|
99 |
// |
|
100 |
// --release-info add:build_type=fastdebug,source=openjdk,java_version=9 |
|
101 |
// and put whatever value that was passed in command line. |
|
36511 | 102 |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
103 |
config.keySet().stream() |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
104 |
.filter(s -> !NAME.equals(s)) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
105 |
.forEach(s -> release.put(s, config.get(s))); |
38320 | 106 |
} |
107 |
break; |
|
36511 | 108 |
|
38320 | 109 |
case "del": { |
110 |
// --release-info del:keys=openjdk,java_version |
|
39042 | 111 |
Utils.parseList(config.get(KEYS)).stream().forEach((k) -> { |
38320 | 112 |
release.remove(k); |
39042 | 113 |
}); |
38320 | 114 |
} |
115 |
break; |
|
36511 | 116 |
|
38320 | 117 |
default: { |
118 |
// --release-info <file> |
|
119 |
Properties props = new Properties(); |
|
120 |
try (FileInputStream fis = new FileInputStream(operation)) { |
|
121 |
props.load(fis); |
|
122 |
} catch (IOException exp) { |
|
42330
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
39894
diff
changeset
|
123 |
throw new UncheckedIOException(exp); |
36511 | 124 |
} |
38320 | 125 |
props.forEach((k, v) -> release.put(k.toString(), v.toString())); |
36511 | 126 |
} |
38320 | 127 |
break; |
36511 | 128 |
} |
129 |
} |
|
130 |
||
131 |
@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
|
132 |
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { |
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
133 |
in.transformAndCopy(Function.identity(), out); |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
134 |
|
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
135 |
Optional<ResourcePoolModule> javaBase = in.moduleView().findModule("java.base"); |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
136 |
javaBase.ifPresent(mod -> { |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
137 |
// fill release information available from transformed "java.base" module! |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
138 |
ModuleDescriptor desc = mod.descriptor(); |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
139 |
desc.version().ifPresent(s -> release.put("JAVA_VERSION", |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
140 |
quote(parseVersion(s.toString())))); |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
141 |
desc.version().ifPresent(s -> release.put("JAVA_FULL_VERSION", |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
142 |
quote(s.toString()))); |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
42471
diff
changeset
|
143 |
|
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
42471
diff
changeset
|
144 |
release.put("OS_NAME", quote(mod.osName())); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
42471
diff
changeset
|
145 |
release.put("OS_ARCH", quote(mod.osArch())); |
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
146 |
}); |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
147 |
|
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
148 |
// put topological sorted module names separated by space |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
149 |
release.put("MODULES", new ModuleSorter(in.moduleView()) |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
150 |
.sorted().map(ResourcePoolModule::name) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
151 |
.collect(Collectors.joining(" ", "\"", "\""))); |
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
152 |
|
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
153 |
// create a TOP level ResourcePoolEntry for "release" file. |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
154 |
out.add(ResourcePoolEntry.create("/java.base/release", |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
155 |
ResourcePoolEntry.Type.TOP, releaseFileContent())); |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
156 |
return out.build(); |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
157 |
} |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
158 |
|
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
159 |
// Parse version string and return a string that includes only version part |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
160 |
// leaving "pre", "build" information. See also: java.lang.Runtime.Version. |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
161 |
private static String parseVersion(String str) { |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
162 |
return Runtime.Version.parse(str) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
163 |
.version() |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
164 |
.stream() |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
165 |
.map(Object::toString) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
166 |
.collect(Collectors.joining(".")); |
42471
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
167 |
} |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
168 |
|
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
169 |
private static String quote(String str) { |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
170 |
return "\"" + str + "\""; |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
171 |
} |
86b1da05a4b0
8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
42330
diff
changeset
|
172 |
|
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
173 |
private byte[] releaseFileContent() { |
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
174 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
175 |
try (PrintWriter pw = new PrintWriter(baos)) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
176 |
release.entrySet().stream() |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
177 |
.sorted(Map.Entry.comparingByKey()) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
178 |
.forEach(e -> pw.format("%s=%s%n", e.getKey(), e.getValue())); |
39894
cd4f403f9618
8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents:
39834
diff
changeset
|
179 |
} |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
180 |
return baos.toByteArray(); |
36511 | 181 |
} |
182 |
} |