Search in sources :

Example 1 with Node

use of jdk.internal.jimage.ImageReader.Node in project Bytecoder by mirkosertic.

the class ExplodedImage method initNodes.

// initialize file system Nodes
private void initNodes() throws IOException {
    // same package prefix may exist in mutliple modules. This Map
    // is filled by walking "jdk modules" directory recursively!
    Map<String, List<String>> packageToModules = new HashMap<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(explodedModulesDir)) {
        for (Path module : stream) {
            if (Files.isDirectory(module)) {
                String moduleName = module.getFileName().toString();
                // make sure "/modules/<moduleName>" is created
                findModulesNode(MODULES + moduleName);
                Files.walk(module).filter(Files::isDirectory).forEach((p) -> {
                    p = module.relativize(p);
                    String pkgName = slashesToDots(p.toString());
                    // skip META-INFO and empty strings
                    if (!pkgName.isEmpty() && !pkgName.startsWith("META-INF")) {
                        List<String> moduleNames = packageToModules.get(pkgName);
                        if (moduleNames == null) {
                            moduleNames = new ArrayList<>();
                            packageToModules.put(pkgName, moduleNames);
                        }
                        moduleNames.add(moduleName);
                    }
                });
            }
        }
    }
    // create "/modules" directory
    // "nodes" map contains only /modules/<foo> nodes only so far and so add all as children of /modules
    PathNode modulesDir = new PathNode("/modules", new ArrayList<>(nodes.values()));
    nodes.put(modulesDir.getName(), modulesDir);
    // create children under "/packages"
    List<Node> packagesChildren = new ArrayList<>(packageToModules.size());
    for (Map.Entry<String, List<String>> entry : packageToModules.entrySet()) {
        String pkgName = entry.getKey();
        List<String> moduleNameList = entry.getValue();
        List<Node> moduleLinkNodes = new ArrayList<>(moduleNameList.size());
        for (String moduleName : moduleNameList) {
            Node moduleNode = findModulesNode(MODULES + moduleName);
            PathNode linkNode = new PathNode(PACKAGES + pkgName + "/" + moduleName, moduleNode);
            nodes.put(linkNode.getName(), linkNode);
            moduleLinkNodes.add(linkNode);
        }
        PathNode pkgDir = new PathNode(PACKAGES + pkgName, moduleLinkNodes);
        nodes.put(pkgDir.getName(), pkgDir);
        packagesChildren.add(pkgDir);
    }
    // "/packages" dir
    PathNode packagesDir = new PathNode("/packages", packagesChildren);
    nodes.put(packagesDir.getName(), packagesDir);
    // finally "/" dir!
    List<Node> rootChildren = new ArrayList<>();
    rootChildren.add(packagesDir);
    rootChildren.add(modulesDir);
    PathNode root = new PathNode("/", rootChildren);
    nodes.put(root.getName(), root);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Node(jdk.internal.jimage.ImageReader.Node) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Node

use of jdk.internal.jimage.ImageReader.Node in project Bytecoder by mirkosertic.

the class JrtFileSystem method checkNode.

Node checkNode(JrtPath path) throws IOException {
    ensureOpen();
    String p = path.getResolvedPath();
    Node node = lookup(p);
    if (node == null) {
        node = lookupSymbolic(p);
        if (node == null) {
            throw new NoSuchFileException(p);
        }
    }
    return node;
}
Also used : Node(jdk.internal.jimage.ImageReader.Node) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 3 with Node

use of jdk.internal.jimage.ImageReader.Node in project Bytecoder by mirkosertic.

the class SystemImage method open.

static SystemImage open() throws IOException {
    if (modulesImageExists) {
        // open a .jimage and build directory structure
        final ImageReader image = ImageReader.open(moduleImageFile);
        image.getRootDirectory();
        return new SystemImage() {

            @Override
            Node findNode(String path) throws IOException {
                return image.findNode(path);
            }

            @Override
            byte[] getResource(Node node) throws IOException {
                return image.getResource(node);
            }

            @Override
            void close() throws IOException {
                image.close();
            }
        };
    }
    if (Files.notExists(explodedModulesDir))
        throw new FileSystemNotFoundException(explodedModulesDir.toString());
    return new ExplodedImage(explodedModulesDir);
}
Also used : FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) Node(jdk.internal.jimage.ImageReader.Node) ImageReader(jdk.internal.jimage.ImageReader)

Example 4 with Node

use of jdk.internal.jimage.ImageReader.Node in project Bytecoder by mirkosertic.

the class ExplodedImage method findNode.

// find Node for the given Path
@Override
public synchronized Node findNode(String str) {
    Node node = findModulesNode(str);
    if (node != null) {
        return node;
    }
    // For example /packages/java.lang/java.base/java/lang/
    if (str.startsWith(PACKAGES)) {
        // pkgEndIdx marks end of <package> part
        int pkgEndIdx = str.indexOf('/', PACKAGES_LEN);
        if (pkgEndIdx != -1) {
            // modEndIdx marks end of <module> part
            int modEndIdx = str.indexOf('/', pkgEndIdx + 1);
            if (modEndIdx != -1) {
                // make sure we have such module link!
                // ie., /packages/<package>/<module> is valid
                Node linkNode = nodes.get(str.substring(0, modEndIdx));
                if (linkNode == null || !linkNode.isLink()) {
                    return null;
                }
                // map to "/modules/zyz" path and return that node
                // For example, "/modules/java.base/java/lang" for
                // "/packages/java.lang/java.base/java/lang".
                String mod = MODULES + str.substring(pkgEndIdx + 1);
                return findModulesNode(mod);
            }
        }
    }
    return null;
}
Also used : Node(jdk.internal.jimage.ImageReader.Node)

Example 5 with Node

use of jdk.internal.jimage.ImageReader.Node in project Bytecoder by mirkosertic.

the class JrtFileSystem method lookupSymbolic.

private Node lookupSymbolic(String path) {
    int i = 1;
    while (i < path.length()) {
        i = path.indexOf('/', i);
        if (i == -1) {
            break;
        }
        String prefix = path.substring(0, i);
        Node node = lookup(prefix);
        if (node == null) {
            break;
        }
        if (node.isLink()) {
            Node link = node.resolveLink(true);
            // resolved symbolic path concatenated to the rest of the path
            String resPath = link.getName() + path.substring(i);
            node = lookup(resPath);
            return node != null ? node : lookupSymbolic(resPath);
        }
        i++;
    }
    return null;
}
Also used : Node(jdk.internal.jimage.ImageReader.Node)

Aggregations

Node (jdk.internal.jimage.ImageReader.Node)5 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ImageReader (jdk.internal.jimage.ImageReader)1