23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package jdk.packager.internal; |
26 package jdk.packager.internal; |
27 |
27 |
28 |
|
29 import java.io.ByteArrayOutputStream; |
28 import java.io.ByteArrayOutputStream; |
30 import java.io.File; |
29 import java.io.File; |
31 import java.io.FileInputStream; |
30 import java.io.FileInputStream; |
|
31 import java.io.BufferedInputStream; |
32 import java.io.IOException; |
32 import java.io.IOException; |
33 import java.io.InputStream; |
33 import java.io.InputStream; |
34 import java.nio.file.StandardCopyOption; |
34 import java.nio.file.StandardCopyOption; |
35 import java.nio.file.Files; |
35 import java.nio.file.Files; |
36 import java.text.MessageFormat; |
36 import java.text.MessageFormat; |
37 import java.util.Map; |
37 import java.util.Map; |
38 import java.util.ResourceBundle; |
38 import java.util.ResourceBundle; |
39 |
|
40 |
39 |
41 public abstract class AbstractBundler implements Bundler { |
40 public abstract class AbstractBundler implements Bundler { |
42 |
41 |
43 private static final ResourceBundle I18N = ResourceBundle.getBundle( |
42 private static final ResourceBundle I18N = ResourceBundle.getBundle( |
44 "jdk.packager.internal.resources.AbstractBundler"); |
43 "jdk.packager.internal.resources.AbstractBundler"); |
57 // we use it for classpath lookup and there / are not platform specific |
56 // we use it for classpath lookup and there / are not platform specific |
58 public final static String BUNDLER_PREFIX = "package/"; |
57 public final static String BUNDLER_PREFIX = "package/"; |
59 |
58 |
60 protected Class baseResourceLoader = null; |
59 protected Class baseResourceLoader = null; |
61 |
60 |
62 protected void fetchResource( |
61 protected void fetchResource(String publicName, String category, |
63 String publicName, String category, |
|
64 String defaultName, File result, boolean verbose, File publicRoot) |
62 String defaultName, File result, boolean verbose, File publicRoot) |
65 throws IOException { |
63 throws IOException { |
66 InputStream is = streamResource(publicName, category, |
64 InputStream is = streamResource(publicName, category, |
67 defaultName, verbose, publicRoot); |
65 defaultName, verbose, publicRoot); |
68 if (is != null) { |
66 if (is != null) { |
69 Files.copy(is, result.toPath(), |
67 try { |
70 StandardCopyOption.REPLACE_EXISTING); |
68 Files.copy(is, result.toPath(), |
|
69 StandardCopyOption.REPLACE_EXISTING); |
|
70 } finally { |
|
71 is.close(); |
|
72 } |
71 } else { |
73 } else { |
72 if (verbose) { |
74 if (verbose) { |
73 Log.info(MessageFormat.format(I18N.getString( |
75 Log.info(MessageFormat.format(I18N.getString( |
74 "message.using-default-resource"), |
76 "message.using-default-resource"), |
75 category == null ? "" : "[" + category + "] ", |
77 category == null ? "" : "[" + category + "] ", |
76 publicName)); |
78 publicName)); |
77 } |
79 } |
78 } |
80 } |
79 } |
81 } |
80 |
82 |
81 protected void fetchResource( |
83 protected void fetchResource(String publicName, String category, |
82 String publicName, String category, |
|
83 File defaultFile, File result, boolean verbose, File publicRoot) |
84 File defaultFile, File result, boolean verbose, File publicRoot) |
84 throws IOException { |
85 throws IOException { |
85 InputStream is = streamResource(publicName, category, |
86 InputStream is = streamResource(publicName, category, |
86 null, verbose, publicRoot); |
87 null, verbose, publicRoot); |
87 if (is != null) { |
88 if (is != null) { |
88 Files.copy(is, result.toPath()); |
89 try { |
|
90 Files.copy(is, result.toPath()); |
|
91 } finally { |
|
92 is.close(); |
|
93 } |
89 } else { |
94 } else { |
90 IOUtils.copyFile(defaultFile, result); |
95 IOUtils.copyFile(defaultFile, result); |
91 if (verbose) { |
96 if (verbose) { |
92 Log.info(MessageFormat.format(I18N.getString( |
97 Log.info(MessageFormat.format(I18N.getString( |
93 "message.using-custom-resource-from-file"), |
98 "message.using-custom-resource-from-file"), |
104 InputStream is = null; |
109 InputStream is = null; |
105 if (publicName != null) { |
110 if (publicName != null) { |
106 if (publicRoot != null) { |
111 if (publicRoot != null) { |
107 File publicResource = new File(publicRoot, publicName); |
112 File publicResource = new File(publicRoot, publicName); |
108 if (publicResource.exists() && publicResource.isFile()) { |
113 if (publicResource.exists() && publicResource.isFile()) { |
109 is = new FileInputStream(publicResource); |
114 is = new BufferedInputStream( |
|
115 new FileInputStream(publicResource)); |
110 } |
116 } |
111 } else { |
117 } else { |
112 is = baseResourceLoader.getClassLoader().getResourceAsStream( |
118 is = baseResourceLoader.getClassLoader().getResourceAsStream( |
113 publicName); |
119 publicName); |
114 } |
120 } |
115 custom = (is != null); |
121 custom = (is != null); |
116 } |
122 } |
117 if (is == null && defaultName != null) { |
123 if (is == null && defaultName != null) { |
118 is = baseResourceLoader.getResourceAsStream(defaultName); |
124 is = baseResourceLoader.getResourceAsStream(defaultName); |
119 } |
125 } |
120 String msg = null; |
|
121 if (custom) { |
|
122 msg = MessageFormat.format(I18N.getString( |
|
123 "message.using-custom-resource-from-classpath"), |
|
124 category == null ? "" : "[" + category + "] ", publicName); |
|
125 } else if (is != null) { |
|
126 msg = MessageFormat.format(I18N.getString( |
|
127 "message.using-default-resource-from-classpath"), |
|
128 category == null ? "" : "[" + category + "] ", publicName); |
|
129 } |
|
130 if (verbose && is != null) { |
126 if (verbose && is != null) { |
|
127 String msg = null; |
|
128 if (custom) { |
|
129 msg = MessageFormat.format(I18N.getString( |
|
130 "message.using-custom-resource-from-classpath"), |
|
131 category == null ? |
|
132 "" : "[" + category + "] ", publicName); |
|
133 } else { |
|
134 msg = MessageFormat.format(I18N.getString( |
|
135 "message.using-default-resource-from-classpath"), |
|
136 category == null ? |
|
137 "" : "[" + category + "] ", publicName); |
|
138 } |
131 Log.info(msg); |
139 Log.info(msg); |
132 } |
140 } |
133 return is; |
141 return is; |
134 } |
142 } |
135 |
143 |