|
1 /* |
|
2 * Copyright (c) 2019, 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.jpackage.internal; |
|
26 |
|
27 import java.io.*; |
|
28 import java.nio.charset.StandardCharsets; |
|
29 import java.nio.file.Files; |
|
30 import java.nio.file.Path; |
|
31 import java.nio.file.StandardCopyOption; |
|
32 import java.text.MessageFormat; |
|
33 import java.util.HashMap; |
|
34 import java.util.List; |
|
35 import java.util.Map; |
|
36 import java.util.Optional; |
|
37 import java.util.stream.Collectors; |
|
38 import java.util.stream.Stream; |
|
39 import static jdk.jpackage.internal.StandardBundlerParam.RESOURCE_DIR; |
|
40 import jdk.jpackage.internal.resources.ResourceLocator; |
|
41 |
|
42 /** |
|
43 * Resource file that may have the default value supplied by jpackage. It can be |
|
44 * overridden by a file from resource directory set with {@code --resource-dir} |
|
45 * jpackage parameter. |
|
46 * |
|
47 * Resource has default name and public name. Default name is the name of a file |
|
48 * in {@code jdk.jpackage.internal.resources} package that provides the default |
|
49 * value of the resource. |
|
50 * |
|
51 * Public name is a path relative to resource directory to a file with custom |
|
52 * value of the resource. |
|
53 * |
|
54 * Use #setPublicName to set the public name. |
|
55 * |
|
56 * If #setPublicName was not called, name of file passed in #saveToFile function |
|
57 * will be used as a public name. |
|
58 * |
|
59 * Use #setExternal to set arbitrary file as a source of resource. If non-null |
|
60 * value was passed in #setExternal call that value will be used as a path to file |
|
61 * to copy in the destination file passed in #saveToFile function call. |
|
62 */ |
|
63 final class OverridableResource { |
|
64 |
|
65 OverridableResource(String defaultName) { |
|
66 this.defaultName = defaultName; |
|
67 } |
|
68 |
|
69 OverridableResource setSubstitutionData(Map<String, String> v) { |
|
70 if (v != null) { |
|
71 // Disconnect `v` |
|
72 substitutionData = new HashMap<>(v); |
|
73 } else { |
|
74 substitutionData = null; |
|
75 } |
|
76 return this; |
|
77 } |
|
78 |
|
79 OverridableResource setCategory(String v) { |
|
80 category = v; |
|
81 return this; |
|
82 } |
|
83 |
|
84 String getCategory() { |
|
85 return category; |
|
86 } |
|
87 |
|
88 OverridableResource setResourceDir(Path v) { |
|
89 resourceDir = v; |
|
90 return this; |
|
91 } |
|
92 |
|
93 OverridableResource setResourceDir(File v) { |
|
94 return setResourceDir(toPath(v)); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Set name of file to look for in resource dir. |
|
99 * |
|
100 * @return this |
|
101 */ |
|
102 OverridableResource setPublicName(Path v) { |
|
103 publicName = v; |
|
104 return this; |
|
105 } |
|
106 |
|
107 OverridableResource setPublicName(String v) { |
|
108 return setPublicName(Path.of(v)); |
|
109 } |
|
110 |
|
111 OverridableResource setExternal(Path v) { |
|
112 externalPath = v; |
|
113 return this; |
|
114 } |
|
115 |
|
116 OverridableResource setExternal(File v) { |
|
117 return setExternal(toPath(v)); |
|
118 } |
|
119 |
|
120 void saveToFile(Path dest) throws IOException { |
|
121 final String printableCategory; |
|
122 if (category != null) { |
|
123 printableCategory = String.format("[%s]", category); |
|
124 } else { |
|
125 printableCategory = ""; |
|
126 } |
|
127 |
|
128 if (externalPath != null && externalPath.toFile().exists()) { |
|
129 Log.verbose(MessageFormat.format(I18N.getString( |
|
130 "message.using-custom-resource-from-file"), |
|
131 printableCategory, |
|
132 externalPath.toAbsolutePath().normalize())); |
|
133 |
|
134 try (InputStream in = Files.newInputStream(externalPath)) { |
|
135 processResourceStream(in, dest); |
|
136 } |
|
137 return; |
|
138 } |
|
139 |
|
140 final Path resourceName = Optional.ofNullable(publicName).orElse( |
|
141 dest.getFileName()); |
|
142 |
|
143 if (resourceDir != null) { |
|
144 final Path customResource = resourceDir.resolve(resourceName); |
|
145 if (customResource.toFile().exists()) { |
|
146 Log.verbose(MessageFormat.format(I18N.getString( |
|
147 "message.using-custom-resource"), printableCategory, |
|
148 resourceDir.normalize().toAbsolutePath().relativize( |
|
149 customResource.normalize().toAbsolutePath()))); |
|
150 |
|
151 try (InputStream in = Files.newInputStream(customResource)) { |
|
152 processResourceStream(in, dest); |
|
153 } |
|
154 return; |
|
155 } |
|
156 } |
|
157 |
|
158 if (defaultName != null) { |
|
159 Log.verbose(MessageFormat.format( |
|
160 I18N.getString("message.using-default-resource"), |
|
161 defaultName, printableCategory, resourceName)); |
|
162 |
|
163 try (InputStream in = readDefault(defaultName)) { |
|
164 processResourceStream(in, dest); |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 void saveToFile(File dest) throws IOException { |
|
170 saveToFile(dest.toPath()); |
|
171 } |
|
172 |
|
173 static InputStream readDefault(String resourceName) { |
|
174 return ResourceLocator.class.getResourceAsStream(resourceName); |
|
175 } |
|
176 |
|
177 static OverridableResource createResource(String defaultName, |
|
178 Map<String, ? super Object> params) { |
|
179 return new OverridableResource(defaultName).setResourceDir( |
|
180 RESOURCE_DIR.fetchFrom(params)); |
|
181 } |
|
182 |
|
183 private static List<String> substitute(Stream<String> lines, |
|
184 Map<String, String> substitutionData) { |
|
185 return lines.map(line -> { |
|
186 String result = line; |
|
187 for (var entry : substitutionData.entrySet()) { |
|
188 result = result.replace(entry.getKey(), Optional.ofNullable( |
|
189 entry.getValue()).orElse("")); |
|
190 } |
|
191 return result; |
|
192 }).collect(Collectors.toList()); |
|
193 } |
|
194 |
|
195 private static Path toPath(File v) { |
|
196 if (v != null) { |
|
197 return v.toPath(); |
|
198 } |
|
199 return null; |
|
200 } |
|
201 |
|
202 private void processResourceStream(InputStream rawResource, Path dest) |
|
203 throws IOException { |
|
204 if (substitutionData == null) { |
|
205 Files.createDirectories(dest.getParent()); |
|
206 Files.copy(rawResource, dest, StandardCopyOption.REPLACE_EXISTING); |
|
207 } else { |
|
208 // Utf8 in and out |
|
209 try (BufferedReader reader = new BufferedReader( |
|
210 new InputStreamReader(rawResource, StandardCharsets.UTF_8))) { |
|
211 Files.createDirectories(dest.getParent()); |
|
212 Files.write(dest, substitute(reader.lines(), substitutionData)); |
|
213 } |
|
214 } |
|
215 } |
|
216 |
|
217 private Map<String, String> substitutionData; |
|
218 private String category; |
|
219 private Path resourceDir; |
|
220 private Path publicName; |
|
221 private Path externalPath; |
|
222 private final String defaultName; |
|
223 } |