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 var Collections = Java.type("java.util.Collections");
  57 
  58 // JavaFX classes used
  59 var StackPane = Java.type("javafx.scene.layout.StackPane");
  60 var Scene     = Java.type("javafx.scene.Scene");
  61 var TreeItem  = Java.type("javafx.scene.control.TreeItem");
  62 var TreeView  = Java.type("javafx.scene.control.TreeView");
  63 
  64 // Create a javafx TreeItem to view nio Path
  65 function treeItemForPath(path) {
  66     var item = new TreeItem(path.toString());
  67   
  68     if (Files.isDirectory(path)) {
  69         var stream = Files.newDirectoryStream(path);
  70         try {
  71             var itr = stream.iterator();
  72             while (itr.hasNext()) {
  73                 var childPath = itr.next();
  74                 if (Files.isDirectory(childPath)) {
  75                     var subitem = treeItemForPath(childPath);
  76                 } else {
  77                     var subitem = new TreeItem(childPath.toString());
  78                 }
  79                 item.children.add(subitem);
  80             }
  81         } finally {
  82             stream.close();
  83         }
  84     }
  85     return item;
  86 }
  87 
  88 function getJrtFileSystem() {
  89     var isJdk9 = System.getProperty("java.version").startsWith("1.9.0");
  90     var uri = URI.create("jrt:/");
  91 
  92     if (isJdk9) {
  93         return FileSystems.getFileSystem(uri);
  94     } else {
  95         // pass jrt-fs.jar in -classpath but running on jdk8+
  96         var cls;
  97         try {
  98             cls = Java.type("jdk.internal.jrtfs.JrtFileSystem").class;
  99         } catch (e) {
 100             print(e);
 101             print("did you miss specifying jrt-fs.jar with -cp option?");
 102             usage();
 103         }
 104         return FileSystems.newFileSystem(uri, Collections.emptyMap(), cls.classLoader);
 105     }
 106 }
 107 
 108 // JavaFX start method
 109 function start(stage) {
 110     var jrtfs = getJrtFileSystem();
 111     var root = jrtfs.getPath('/');
 112     stage.title = "jrt fs viewer";
 113     var rootItem = treeItemForPath(root);
 114     var tree = new TreeView(rootItem);
 115     var root = new StackPane();
 116     root.children.add(tree);
 117     stage.scene = new Scene(root, 300, 450);
 118     stage.show();
 119 }