1 #// Usage: jjs -fx jrtfsviewer.js
   2 
   3 /*
   4  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 
  28 /*
  29  * @implNote This script needs to maintain JDK 8 source compatibility.
  30  *
  31  * It is used internally in the JDK to implement jimage/jrtfs access,
  32  * but also compiled and delivered as part of the jrtfs.jar to support access
  33  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
  34  */
  35 function usage() {
  36     print("Usage:");
  37     print("jdk9+: jjs -fx jrtfsviewer.js");
  38     print("jdk8+: jjs -fx -cp <path-of jrt-fs.jar> jrtfsviewer.js");
  39     exit(1);
  40 }
  41 
  42 if (! $OPTIONS._fx) {
  43     usage();
  44 }
  45 
  46 // shows the jrt file system as a JavaFX tree view.
  47 
  48 // Using JavaFX from Nashorn. See also:
  49 // http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
  50 
  51 // Java classes used
  52 var FileSystems = Java.type("java.nio.file.FileSystems");
  53 var Files = Java.type("java.nio.file.Files");
  54 var System = Java.type("java.lang.System");
  55 var URI = Java.type("java.net.URI");
  56 
  57 // JavaFX classes used
  58 var StackPane = Java.type("javafx.scene.layout.StackPane");
  59 var Scene     = Java.type("javafx.scene.Scene");
  60 var TreeItem  = Java.type("javafx.scene.control.TreeItem");
  61 var TreeView  = Java.type("javafx.scene.control.TreeView");
  62 
  63 // Create a javafx TreeItem to view nio Path
  64 function treeItemForPath(path) {
  65     var item = new TreeItem(path.toString());
  66   
  67     if (Files.isDirectory(path)) {
  68         var stream = Files.newDirectoryStream(path);
  69         try {
  70             var itr = stream.iterator();
  71             while (itr.hasNext()) {
  72                 var childPath = itr.next();
  73                 if (Files.isDirectory(childPath)) {
  74                     var subitem = treeItemForPath(childPath);
  75                 } else {
  76                     var subitem = new TreeItem(childPath.toString());
  77                 }
  78                 item.children.add(subitem);
  79             }
  80         } finally {
  81             stream.close();
  82         }
  83     }
  84     return item;
  85 }
  86 
  87 function getJrtFileSystem() {
  88     var isJdk9 = System.getProperty("java.version").startsWith("1.9.0");
  89     var uri = URI.create("jrt:/");
  90 
  91     if (isJdk9) {
  92         return FileSystems.getFileSystem(uri);
  93     } else {
  94         // pass jrt-fs.jar in -classpath but running on jdk8+
  95         var cls;
  96         try {
  97             cls = Java.type("jdk.internal.jrtfs.JrtFileSystem").class;
  98         } catch (e) {
  99             print(e);
 100             print("did you miss specifying jrt-fs.jar with -cp option?");
 101             usage();
 102         }
 103         return FileSystems.newFileSystem(uri, null, cls.classLoader);
 104     }
 105 }
 106 
 107 // JavaFX start method
 108 function start(stage) {
 109     var jrtfs = getJrtFileSystem();
 110     var root = jrtfs.getPath('/');
 111     stage.title = "jrt fs viewer";
 112     var rootItem = treeItemForPath(root);
 113     var tree = new TreeView(rootItem);
 114     var root = new StackPane();
 115     root.children.add(tree);
 116     stage.scene = new Scene(root, 300, 450);
 117     stage.show();
 118 }