1 /*
2 * Copyright (c) 2014, 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
53 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
54 */
55 public final class JrtFileSystemProvider extends FileSystemProvider {
56
57 private volatile FileSystem theFileSystem;
58
59 public JrtFileSystemProvider() {
60 }
61
62 @Override
63 public String getScheme() {
64 return "jrt";
65 }
66
67 /**
68 * Need FilePermission ${java.home}/-", "read" to create or get jrt:/
69 */
70 private void checkPermission() {
71 SecurityManager sm = System.getSecurityManager();
72 if (sm != null) {
73 String home = SystemImages.RUNTIME_HOME;
74 FilePermission perm
75 = new FilePermission(home + File.separator + "-", "read");
76 sm.checkPermission(perm);
77 }
78 }
79
80 private void checkUri(URI uri) {
81 if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
82 throw new IllegalArgumentException("URI does not match this provider");
83 }
84 if (uri.getAuthority() != null) {
85 throw new IllegalArgumentException("Authority component present");
86 }
87 if (uri.getPath() == null) {
88 throw new IllegalArgumentException("Path component is undefined");
89 }
90 if (!uri.getPath().equals("/")) {
91 throw new IllegalArgumentException("Path component should be '/'");
92 }
93 if (uri.getQuery() != null) {
94 throw new IllegalArgumentException("Query component present");
95 }
96 if (uri.getFragment() != null) {
97 throw new IllegalArgumentException("Fragment component present");
98 }
99 }
100
101 @Override
102 public FileSystem newFileSystem(URI uri, Map<String, ?> env)
103 throws IOException {
104 checkPermission();
105 checkUri(uri);
106
107 if (env != null && env.containsKey("java.home")) {
108 return newFileSystem((String)env.get("java.home"), uri, env);
109 } else {
110 return SystemImages.hasModulesImage()
111 ? new JrtFileSystem(this, env)
112 : new JrtExplodedFileSystem(this, env);
113 }
114 }
115
116 private static final String JRT_FS_JAR = "jrt-fs.jar";
117 private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env)
118 throws IOException {
119 Objects.requireNonNull(targetHome);
120 Path jrtfs = FileSystems.getDefault().getPath(targetHome, JRT_FS_JAR);
121 if (Files.notExists(jrtfs)) {
122 throw new IOException(jrtfs.toString() + " not exist");
123 }
124
125 Map<String,?> newEnv = new HashMap<>(env);
126 newEnv.remove("java.home");
127 ClassLoader cl = newJrtFsLoader(jrtfs);
128 try {
129 Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl);
130 return ((FileSystemProvider)c.newInstance()).newFileSystem(uri, newEnv);
131 } catch (ClassNotFoundException |
132 IllegalAccessException |
133 InstantiationException e) {
134 throw new IOException(e);
135 }
136 }
137
138 private static class JrtFsLoader extends URLClassLoader {
139 JrtFsLoader(URL[] urls) {
140 super(urls);
141 }
142
143 @Override
144 protected Class<?> loadClass(String cn, boolean resolve)
145 throws ClassNotFoundException
146 {
147 Class<?> c = findLoadedClass(cn);
148 if (c == null) {
149 URL u = findResource(cn.replace('.', '/') + ".class");
150 if (u != null) {
151 c = findClass(cn);
152 } else {
153 return super.loadClass(cn, resolve);
154 }
155 }
156 if (resolve)
157 resolveClass(c);
158 return c;
159 }
160 }
161
162 private static URLClassLoader newJrtFsLoader(Path jrtfs) {
191 throw new IllegalArgumentException("Query component present");
192 }
193 if (uri.getFragment() != null) {
194 throw new IllegalArgumentException("Fragment component present");
195 }
196 String path = uri.getPath();
197 if (path == null || path.charAt(0) != '/') {
198 throw new IllegalArgumentException("Invalid path component");
199 }
200 return getTheFileSystem().getPath(path);
201 }
202
203 private FileSystem getTheFileSystem() {
204 checkPermission();
205 FileSystem fs = this.theFileSystem;
206 if (fs == null) {
207 synchronized (this) {
208 fs = this.theFileSystem;
209 if (fs == null) {
210 try {
211 if (SystemImages.hasModulesImage()) {
212 this.theFileSystem = fs = new JrtFileSystem(this, null) {
213 @Override
214 public void close() {
215 throw new UnsupportedOperationException();
216 }
217 };
218 } else {
219 this.theFileSystem = fs = new JrtExplodedFileSystem(this, null) {
220 @Override
221 public void close() {
222 throw new UnsupportedOperationException();
223 }
224 };
225 }
226 } catch (IOException ioe) {
227 throw new InternalError(ioe);
228 }
229 }
230 }
231 }
232 return fs;
233 }
234
235 @Override
236 public FileSystem getFileSystem(URI uri) {
237 checkPermission();
238 checkUri(uri);
239 return getTheFileSystem();
240 }
241
242 // Checks that the given file is a JrtPath
243 static final AbstractJrtPath toAbstractJrtPath(Path path) {
244 if (path == null) {
245 throw new NullPointerException();
246 }
247 if (!(path instanceof AbstractJrtPath)) {
248 throw new ProviderMismatchException();
249 }
250 return (AbstractJrtPath) path;
251 }
252
253 @Override
254 public void checkAccess(Path path, AccessMode... modes) throws IOException {
255 toAbstractJrtPath(path).checkAccess(modes);
256 }
257
258 @Override
259 public Path readSymbolicLink(Path link) throws IOException {
260 return toAbstractJrtPath(link).readSymbolicLink();
261 }
262
263 @Override
264 public void copy(Path src, Path target, CopyOption... options)
265 throws IOException {
266 toAbstractJrtPath(src).copy(toAbstractJrtPath(target), options);
267 }
268
269 @Override
270 public void createDirectory(Path path, FileAttribute<?>... attrs)
271 throws IOException {
272 toAbstractJrtPath(path).createDirectory(attrs);
273 }
274
275 @Override
276 public final void delete(Path path) throws IOException {
277 toAbstractJrtPath(path).delete();
278 }
279
280 @Override
281 @SuppressWarnings("unchecked")
282 public <V extends FileAttributeView> V
283 getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
284 return JrtFileAttributeView.get(toAbstractJrtPath(path), type, options);
285 }
286
287 @Override
288 public FileStore getFileStore(Path path) throws IOException {
289 return toAbstractJrtPath(path).getFileStore();
290 }
291
292 @Override
293 public boolean isHidden(Path path) {
294 return toAbstractJrtPath(path).isHidden();
295 }
296
297 @Override
298 public boolean isSameFile(Path path, Path other) throws IOException {
299 return toAbstractJrtPath(path).isSameFile(other);
300 }
301
302 @Override
303 public void move(Path src, Path target, CopyOption... options)
304 throws IOException {
305 toAbstractJrtPath(src).move(toAbstractJrtPath(target), options);
306 }
307
308 @Override
309 public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
310 Set<? extends OpenOption> options,
311 ExecutorService exec,
312 FileAttribute<?>... attrs)
313 throws IOException {
314 throw new UnsupportedOperationException();
315 }
316
317 @Override
318 public SeekableByteChannel newByteChannel(Path path,
319 Set<? extends OpenOption> options,
320 FileAttribute<?>... attrs)
321 throws IOException {
322 return toAbstractJrtPath(path).newByteChannel(options, attrs);
323 }
324
325 @Override
326 public DirectoryStream<Path> newDirectoryStream(
327 Path path, Filter<? super Path> filter) throws IOException {
328 return toAbstractJrtPath(path).newDirectoryStream(filter);
329 }
330
331 @Override
332 public FileChannel newFileChannel(Path path,
333 Set<? extends OpenOption> options,
334 FileAttribute<?>... attrs)
335 throws IOException {
336 return toAbstractJrtPath(path).newFileChannel(options, attrs);
337 }
338
339 @Override
340 public InputStream newInputStream(Path path, OpenOption... options)
341 throws IOException {
342 return toAbstractJrtPath(path).newInputStream(options);
343 }
344
345 @Override
346 public OutputStream newOutputStream(Path path, OpenOption... options)
347 throws IOException {
348 return toAbstractJrtPath(path).newOutputStream(options);
349 }
350
351 @Override
352 @SuppressWarnings("unchecked") // Cast to A
353 public <A extends BasicFileAttributes> A
354 readAttributes(Path path, Class<A> type, LinkOption... options)
355 throws IOException {
356 if (type == BasicFileAttributes.class || type == JrtFileAttributes.class) {
357 return (A) toAbstractJrtPath(path).getAttributes(options);
358 }
359 return null;
360 }
361
362 @Override
363 public Map<String, Object>
364 readAttributes(Path path, String attribute, LinkOption... options)
365 throws IOException {
366 return toAbstractJrtPath(path).readAttributes(attribute, options);
367 }
368
369 @Override
370 public void setAttribute(Path path, String attribute,
371 Object value, LinkOption... options)
372 throws IOException {
373 toAbstractJrtPath(path).setAttribute(attribute, value, options);
374 }
375 }
|
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
53 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
54 */
55 public final class JrtFileSystemProvider extends FileSystemProvider {
56
57 private volatile FileSystem theFileSystem;
58
59 public JrtFileSystemProvider() {
60 }
61
62 @Override
63 public String getScheme() {
64 return "jrt";
65 }
66
67 /**
68 * Need FilePermission ${java.home}/-", "read" to create or get jrt:/
69 */
70 private void checkPermission() {
71 SecurityManager sm = System.getSecurityManager();
72 if (sm != null) {
73 String home = SystemImage.RUNTIME_HOME;
74 FilePermission perm
75 = new FilePermission(home + File.separator + "-", "read");
76 sm.checkPermission(perm);
77 }
78 }
79
80 private void checkUri(URI uri) {
81 if (!uri.getScheme().equalsIgnoreCase(getScheme())) {
82 throw new IllegalArgumentException("URI does not match this provider");
83 }
84 if (uri.getAuthority() != null) {
85 throw new IllegalArgumentException("Authority component present");
86 }
87 if (uri.getPath() == null) {
88 throw new IllegalArgumentException("Path component is undefined");
89 }
90 if (!uri.getPath().equals("/")) {
91 throw new IllegalArgumentException("Path component should be '/'");
92 }
93 if (uri.getQuery() != null) {
94 throw new IllegalArgumentException("Query component present");
95 }
96 if (uri.getFragment() != null) {
97 throw new IllegalArgumentException("Fragment component present");
98 }
99 }
100
101 @Override
102 public FileSystem newFileSystem(URI uri, Map<String, ?> env)
103 throws IOException {
104 checkPermission();
105 checkUri(uri);
106
107 if (env != null && env.containsKey("java.home")) {
108 return newFileSystem((String)env.get("java.home"), uri, env);
109 } else {
110 return new JrtFileSystem(this, env);
111 }
112 }
113
114 private static final String JRT_FS_JAR = "jrt-fs.jar";
115 private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env)
116 throws IOException {
117 Objects.requireNonNull(targetHome);
118 Path jrtfs = FileSystems.getDefault().getPath(targetHome, JRT_FS_JAR);
119 if (Files.notExists(jrtfs)) {
120 throw new IOException(jrtfs.toString() + " not exist");
121 }
122 Map<String,?> newEnv = new HashMap<>(env);
123 newEnv.remove("java.home");
124 ClassLoader cl = newJrtFsLoader(jrtfs);
125 try {
126 Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl);
127 return ((FileSystemProvider)c.newInstance()).newFileSystem(uri, newEnv);
128 } catch (ClassNotFoundException |
129 IllegalAccessException |
130 InstantiationException e) {
131 throw new IOException(e);
132 }
133 }
134
135 private static class JrtFsLoader extends URLClassLoader {
136 JrtFsLoader(URL[] urls) {
137 super(urls);
138 }
139 @Override
140 protected Class<?> loadClass(String cn, boolean resolve)
141 throws ClassNotFoundException
142 {
143 Class<?> c = findLoadedClass(cn);
144 if (c == null) {
145 URL u = findResource(cn.replace('.', '/') + ".class");
146 if (u != null) {
147 c = findClass(cn);
148 } else {
149 return super.loadClass(cn, resolve);
150 }
151 }
152 if (resolve)
153 resolveClass(c);
154 return c;
155 }
156 }
157
158 private static URLClassLoader newJrtFsLoader(Path jrtfs) {
187 throw new IllegalArgumentException("Query component present");
188 }
189 if (uri.getFragment() != null) {
190 throw new IllegalArgumentException("Fragment component present");
191 }
192 String path = uri.getPath();
193 if (path == null || path.charAt(0) != '/') {
194 throw new IllegalArgumentException("Invalid path component");
195 }
196 return getTheFileSystem().getPath(path);
197 }
198
199 private FileSystem getTheFileSystem() {
200 checkPermission();
201 FileSystem fs = this.theFileSystem;
202 if (fs == null) {
203 synchronized (this) {
204 fs = this.theFileSystem;
205 if (fs == null) {
206 try {
207 this.theFileSystem = fs = new JrtFileSystem(this, null);
208 } catch (IOException ioe) {
209 throw new InternalError(ioe);
210 }
211 }
212 }
213 }
214 return fs;
215 }
216
217 @Override
218 public FileSystem getFileSystem(URI uri) {
219 checkPermission();
220 checkUri(uri);
221 return getTheFileSystem();
222 }
223
224 // Checks that the given file is a JrtPath
225 static final JrtPath toJrtPath(Path path) {
226 Objects.requireNonNull(path, "path");
227 if (!(path instanceof JrtPath)) {
228 throw new ProviderMismatchException();
229 }
230 return (JrtPath) path;
231 }
232
233 @Override
234 public void checkAccess(Path path, AccessMode... modes) throws IOException {
235 toJrtPath(path).checkAccess(modes);
236 }
237
238 @Override
239 public Path readSymbolicLink(Path link) throws IOException {
240 return toJrtPath(link).readSymbolicLink();
241 }
242
243 @Override
244 public void copy(Path src, Path target, CopyOption... options)
245 throws IOException {
246 toJrtPath(src).copy(toJrtPath(target), options);
247 }
248
249 @Override
250 public void createDirectory(Path path, FileAttribute<?>... attrs)
251 throws IOException {
252 toJrtPath(path).createDirectory(attrs);
253 }
254
255 @Override
256 public final void delete(Path path) throws IOException {
257 toJrtPath(path).delete();
258 }
259
260 @Override
261 @SuppressWarnings("unchecked")
262 public <V extends FileAttributeView> V
263 getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
264 return JrtFileAttributeView.get(toJrtPath(path), type, options);
265 }
266
267 @Override
268 public FileStore getFileStore(Path path) throws IOException {
269 return toJrtPath(path).getFileStore();
270 }
271
272 @Override
273 public boolean isHidden(Path path) {
274 return toJrtPath(path).isHidden();
275 }
276
277 @Override
278 public boolean isSameFile(Path path, Path other) throws IOException {
279 return toJrtPath(path).isSameFile(other);
280 }
281
282 @Override
283 public void move(Path src, Path target, CopyOption... options)
284 throws IOException {
285 toJrtPath(src).move(toJrtPath(target), options);
286 }
287
288 @Override
289 public AsynchronousFileChannel newAsynchronousFileChannel(Path path,
290 Set<? extends OpenOption> options,
291 ExecutorService exec,
292 FileAttribute<?>... attrs)
293 throws IOException {
294 throw new UnsupportedOperationException();
295 }
296
297 @Override
298 public SeekableByteChannel newByteChannel(Path path,
299 Set<? extends OpenOption> options,
300 FileAttribute<?>... attrs)
301 throws IOException {
302 return toJrtPath(path).newByteChannel(options, attrs);
303 }
304
305 @Override
306 public DirectoryStream<Path> newDirectoryStream(
307 Path path, Filter<? super Path> filter) throws IOException {
308 return toJrtPath(path).newDirectoryStream(filter);
309 }
310
311 @Override
312 public FileChannel newFileChannel(Path path,
313 Set<? extends OpenOption> options,
314 FileAttribute<?>... attrs)
315 throws IOException {
316 return toJrtPath(path).newFileChannel(options, attrs);
317 }
318
319 @Override
320 public InputStream newInputStream(Path path, OpenOption... options)
321 throws IOException {
322 return toJrtPath(path).newInputStream(options);
323 }
324
325 @Override
326 public OutputStream newOutputStream(Path path, OpenOption... options)
327 throws IOException {
328 return toJrtPath(path).newOutputStream(options);
329 }
330
331 @Override
332 @SuppressWarnings("unchecked") // Cast to A
333 public <A extends BasicFileAttributes> A
334 readAttributes(Path path, Class<A> type, LinkOption... options)
335 throws IOException {
336 if (type == BasicFileAttributes.class || type == JrtFileAttributes.class) {
337 return (A) toJrtPath(path).getAttributes(options);
338 }
339 return null;
340 }
341
342 @Override
343 public Map<String, Object>
344 readAttributes(Path path, String attribute, LinkOption... options)
345 throws IOException {
346 return toJrtPath(path).readAttributes(attribute, options);
347 }
348
349 @Override
350 public void setAttribute(Path path, String attribute,
351 Object value, LinkOption... options)
352 throws IOException {
353 toJrtPath(path).setAttribute(attribute, value, options);
354 }
355 }
|