jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ResourcePoolManager.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 41352 f9844bad9052
child 42471 86b1da05a4b0
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    27
import java.lang.module.ModuleDescriptor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.ByteOrder;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    30
import java.util.HashSet;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    31
import java.util.LinkedHashMap;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    32
import java.util.Map;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Objects;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    34
import java.util.Optional;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    35
import java.util.Set;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    36
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    38
import jdk.internal.loader.ResourceHelper;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    39
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    40
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    41
import jdk.tools.jlink.plugin.ResourcePoolEntry;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    42
import jdk.tools.jlink.plugin.ResourcePoolModule;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    43
import jdk.tools.jlink.plugin.ResourcePoolModuleView;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    44
import jdk.tools.jlink.plugin.PluginException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
/**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    47
 * A manager for pool of resources.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    49
public class ResourcePoolManager {
39894
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    50
    // utility to read ModuleDescriptor of the given ResourcePoolModule
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    51
    static ModuleDescriptor readModuleDescriptor(ResourcePoolModule mod) {
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    52
        String p = "/" + mod.name() + "/module-info.class";
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    53
        Optional<ResourcePoolEntry> content = mod.findEntry(p);
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    54
        if (!content.isPresent()) {
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    55
              throw new PluginException("No module-info for " + mod.name()
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    56
                      + " module");
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    57
        }
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    58
        ByteBuffer bb = ByteBuffer.wrap(content.get().contentBytes());
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    59
        return ModuleDescriptor.read(bb);
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    60
    }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    61
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    62
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    63
     * Returns true if a resource has an effective package.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    64
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    65
    public static boolean isNamedPackageResource(String path) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    66
        return (path.endsWith(".class") && !path.endsWith("module-info.class")) ||
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    67
                !ResourceHelper.isSimpleResource(path);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    68
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    69
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    70
    class ResourcePoolModuleImpl implements ResourcePoolModule {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    72
        final Map<String, ResourcePoolEntry> moduleContent = new LinkedHashMap<>();
39894
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
    73
        // lazily initialized
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    74
        private ModuleDescriptor descriptor;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    75
        final String name;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    77
        private ResourcePoolModuleImpl(String name) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    78
            this.name = name;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    79
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    81
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    82
        public String name() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    83
            return name;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    86
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    87
        public Optional<ResourcePoolEntry> findEntry(String path) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    88
            if (!path.startsWith("/")) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    89
                path = "/" + path;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    90
            }
40209
68e8944f4424 8163382: ResourcePoolManager.findEntry has a bug in startsWith call
sundar
parents: 40121
diff changeset
    91
            if (!path.startsWith("/" + name + "/")) {
68e8944f4424 8163382: ResourcePoolManager.findEntry has a bug in startsWith call
sundar
parents: 40121
diff changeset
    92
                path = "/" + name + path; // path already starts with '/'
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    93
            }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    94
            return Optional.ofNullable(moduleContent.get(path));
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    95
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    96
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    97
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
    98
        public ModuleDescriptor descriptor() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    99
            if (descriptor == null) {
39894
cd4f403f9618 8162782: jlink ResourcePool.releaseProperties should be removed
sundar
parents: 39834
diff changeset
   100
                descriptor = readModuleDescriptor(this);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   101
            }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   102
            return descriptor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   106
        public Set<String> packages() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   107
            Set<String> pkgs = new HashSet<>();
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   108
            moduleContent.values().stream()
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   109
                .filter(m -> m.type() == ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   110
                .forEach(res -> {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   111
                    String name = ImageFileCreator.resourceName(res.path());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   112
                    if (isNamedPackageResource(name)) {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   113
                        String pkg = ImageFileCreator.toPackage(name);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   114
                        if (!pkg.isEmpty()) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   115
                            pkgs.add(pkg);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   116
                        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   117
                    }
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40209
diff changeset
   118
                });
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   119
            return pkgs;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   120
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   121
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   122
        @Override
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   123
        public String toString() {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   124
            return name();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   125
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   126
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   127
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   128
        public Stream<ResourcePoolEntry> entries() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   129
            return moduleContent.values().stream();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   130
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   131
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   132
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   133
        public int entryCount() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   134
            return moduleContent.values().size();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   138
    public class ResourcePoolImpl implements ResourcePool {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   139
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   140
        public ResourcePoolModuleView moduleView() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   141
            return ResourcePoolManager.this.moduleView();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   142
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   143
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   144
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   145
        public Stream<ResourcePoolEntry> entries() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   146
            return ResourcePoolManager.this.entries();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   147
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   148
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   149
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   150
        public int entryCount() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   151
            return ResourcePoolManager.this.entryCount();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   152
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   153
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   154
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   155
        public Optional<ResourcePoolEntry> findEntry(String path) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   156
            return ResourcePoolManager.this.findEntry(path);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   157
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   158
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   159
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   160
        public Optional<ResourcePoolEntry> findEntryInContext(String path, ResourcePoolEntry context) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   161
            return ResourcePoolManager.this.findEntryInContext(path, context);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   162
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   163
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   164
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   165
        public boolean contains(ResourcePoolEntry data) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   166
            return ResourcePoolManager.this.contains(data);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   167
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   168
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   169
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   170
        public boolean isEmpty() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   171
            return ResourcePoolManager.this.isEmpty();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   172
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   173
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   174
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   175
        public ByteOrder byteOrder() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   176
            return ResourcePoolManager.this.byteOrder();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   177
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   178
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   179
        public StringTable getStringTable() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   180
            return ResourcePoolManager.this.getStringTable();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   181
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   182
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   183
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   184
    class ResourcePoolBuilderImpl implements ResourcePoolBuilder {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   185
        private boolean built;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   186
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   187
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   188
        public void add(ResourcePoolEntry data) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   189
            if (built) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   190
                throw new IllegalStateException("resource pool already built!");
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   191
            }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   192
            ResourcePoolManager.this.add(data);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   193
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   194
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   195
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   196
        public ResourcePool build() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   197
            built = true;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   198
            return ResourcePoolManager.this.resourcePool();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   199
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   200
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   201
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   202
    class ResourcePoolModuleViewImpl implements ResourcePoolModuleView {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   203
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   204
        public Optional<ResourcePoolModule> findModule(String name) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   205
            return ResourcePoolManager.this.findModule(name);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   206
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   207
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   208
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   209
        public Stream<ResourcePoolModule> modules() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   210
            return ResourcePoolManager.this.modules();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   211
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   212
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   213
        @Override
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   214
        public int moduleCount() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   215
            return ResourcePoolManager.this.moduleCount();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   216
        }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   217
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   218
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   219
    private final Map<String, ResourcePoolEntry> resources = new LinkedHashMap<>();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   220
    private final Map<String, ResourcePoolModule> modules = new LinkedHashMap<>();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   221
    private final ByteOrder order;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   222
    private final StringTable table;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   223
    private final ResourcePool poolImpl;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   224
    private final ResourcePoolBuilder poolBuilderImpl;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   225
    private final ResourcePoolModuleView moduleViewImpl;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   226
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   227
    public ResourcePoolManager() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   228
        this(ByteOrder.nativeOrder());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   229
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   230
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   231
    public ResourcePoolManager(ByteOrder order) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   232
        this(order, new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   238
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   246
    public ResourcePoolManager(ByteOrder order, StringTable table) {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39151
diff changeset
   247
        this.order = Objects.requireNonNull(order);
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 39151
diff changeset
   248
        this.table = Objects.requireNonNull(table);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   249
        this.poolImpl = new ResourcePoolImpl();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   250
        this.poolBuilderImpl = new ResourcePoolBuilderImpl();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   251
        this.moduleViewImpl = new ResourcePoolModuleViewImpl();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   252
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   253
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   254
    public ResourcePool resourcePool() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   255
        return poolImpl;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   256
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   257
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   258
    public ResourcePoolBuilder resourcePoolBuilder() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   259
        return poolBuilderImpl;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   260
    }
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   261
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   262
    public ResourcePoolModuleView moduleView() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   263
        return moduleViewImpl;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   264
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   265
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   266
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   267
     * Add a ResourcePoolEntry.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   268
     *
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   269
     * @param data The ResourcePoolEntry to add.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   270
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   271
    public void add(ResourcePoolEntry data) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   272
        Objects.requireNonNull(data);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   273
        if (resources.get(data.path()) != null) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   274
            throw new PluginException("Resource " + data.path()
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   275
                    + " already present");
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   276
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   277
        String modulename = data.moduleName();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   278
        ResourcePoolModuleImpl m = (ResourcePoolModuleImpl)modules.get(modulename);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   279
        if (m == null) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   280
            m = new ResourcePoolModuleImpl(modulename);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   281
            modules.put(modulename, m);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   282
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   283
        resources.put(data.path(), data);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   284
        m.moduleContent.put(data.path(), data);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   285
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   286
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   287
    /**
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   288
     * Retrieves the module for the provided name.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   289
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   290
     * @param name The module name
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   291
     * @return the module of matching name, if found
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   292
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   293
    public Optional<ResourcePoolModule> findModule(String name) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   294
        Objects.requireNonNull(name);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   295
        return Optional.ofNullable(modules.get(name));
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   296
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   297
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   298
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   299
     * The stream of modules contained in this ResourcePool.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   300
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   301
     * @return The stream of modules.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   302
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   303
    public Stream<ResourcePoolModule> modules() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   304
        return modules.values().stream();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   305
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   306
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   307
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   308
     * Return the number of ResourcePoolModule count in this ResourcePool.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   309
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   310
     * @return the module count.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   311
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   312
    public int moduleCount() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   313
        return modules.size();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   314
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   316
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   317
     * Get all ResourcePoolEntry contained in this ResourcePool instance.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   318
     *
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   319
     * @return The stream of ResourcePoolModuleEntries.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   320
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   321
    public Stream<ResourcePoolEntry> entries() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   322
        return resources.values().stream();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   323
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   324
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   325
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   326
     * Return the number of ResourcePoolEntry count in this ResourcePool.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   327
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   328
     * @return the entry count.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   329
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   330
    public int entryCount() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   331
        return resources.values().size();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   332
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   333
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   334
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   335
     * Get the ResourcePoolEntry for the passed path.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   336
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   337
     * @param path A data path
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   338
     * @return A ResourcePoolEntry instance or null if the data is not found
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   339
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   340
    public Optional<ResourcePoolEntry> findEntry(String path) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   341
        Objects.requireNonNull(path);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   342
        return Optional.ofNullable(resources.get(path));
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   343
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   344
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   345
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   346
     * Get the ResourcePoolEntry for the passed path restricted to supplied context.
39635
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   347
     *
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   348
     * @param path A data path
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   349
     * @param context A context of the search
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   350
     * @return A ResourcePoolEntry instance or null if the data is not found
39635
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   351
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   352
    public Optional<ResourcePoolEntry> findEntryInContext(String path, ResourcePoolEntry context) {
39635
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   353
        Objects.requireNonNull(path);
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   354
        Objects.requireNonNull(context);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   355
        ResourcePoolModule module = modules.get(context.moduleName());
39635
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   356
        Objects.requireNonNull(module);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   357
        Optional<ResourcePoolEntry> entry = module.findEntry(path);
39635
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   358
        // Navigating other modules via requires and exports is problematic
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   359
        // since we cannot construct the runtime model of loaders and layers.
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   360
        return entry;
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   361
     }
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   362
07c4b195280d 8161067: jlink: Enable plugins to use the module pool for class lookup
jlaskey
parents: 39308
diff changeset
   363
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   364
     * Check if the ResourcePool contains the given ResourcePoolEntry.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   365
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   366
     * @param data The module data to check existence for.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   367
     * @return The module data or null if not found.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   368
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   369
    public boolean contains(ResourcePoolEntry data) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   370
        Objects.requireNonNull(data);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   371
        return findEntry(data.path()).isPresent();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   372
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   373
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   374
    /**
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   375
     * Check if the ResourcePool contains some content at all.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   376
     *
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   377
     * @return True, no content, false otherwise.
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   378
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   379
    public boolean isEmpty() {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   380
        return resources.isEmpty();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   383
    /**
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   384
     * The ByteOrder currently in use when generating the jimage file.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   385
     *
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   386
     * @return The ByteOrder.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   387
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   388
    public ByteOrder byteOrder() {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   389
        return order;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   390
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   391
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
    public StringTable getStringTable() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
        return table;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
    /**
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   397
     * A resource that has been compressed.
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   398
     */
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   399
    public static final class CompressedModuleData extends ByteArrayResourcePoolEntry {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   400
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   401
        final long uncompressed_size;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   402
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   403
        private CompressedModuleData(String module, String path,
39151
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents: 38320
diff changeset
   404
                byte[] content, long uncompressed_size) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   405
            super(module, path, ResourcePoolEntry.Type.CLASS_OR_RESOURCE, content);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   406
            this.uncompressed_size = uncompressed_size;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   407
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   408
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   409
        public long getUncompressedSize() {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   410
            return uncompressed_size;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   411
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   412
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   413
        @Override
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   414
        public boolean equals(Object other) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   415
            if (!(other instanceof CompressedModuleData)) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   416
                return false;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   417
            }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   418
            CompressedModuleData f = (CompressedModuleData) other;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   419
            return f.path().equals(path());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   420
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   421
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   422
        @Override
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   423
        public int hashCode() {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   424
            return super.hashCode();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   425
        }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   426
    }
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   427
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   428
    public static CompressedModuleData newCompressedResource(ResourcePoolEntry original,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
            ByteBuffer compressed,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
            String plugin, String pluginConfig, StringTable strings,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
            ByteOrder order) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        Objects.requireNonNull(original);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
        Objects.requireNonNull(compressed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        Objects.requireNonNull(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        boolean isTerminal = !(original instanceof CompressedModuleData);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   437
        long uncompressed_size = original.contentLength();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        if (original instanceof CompressedModuleData) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            CompressedModuleData comp = (CompressedModuleData) original;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
            uncompressed_size = comp.getUncompressedSize();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
        int nameOffset = strings.addString(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        int configOffset = -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
        if (pluginConfig != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
            configOffset = strings.addString(plugin);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
        CompressedResourceHeader rh
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   448
                = new CompressedResourceHeader(compressed.limit(), original.contentLength(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
                        nameOffset, configOffset, isTerminal);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
        // Merge header with content;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        byte[] h = rh.getBytes(order);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        ByteBuffer bb = ByteBuffer.allocate(compressed.limit() + h.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
        bb.order(order);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
        bb.put(h);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
        bb.put(compressed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
        byte[] contentWithHeader = bb.array();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
        CompressedModuleData compressedResource
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39635
diff changeset
   459
                = new CompressedModuleData(original.moduleName(), original.path(),
39151
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents: 38320
diff changeset
   460
                        contentWithHeader, uncompressed_size);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        return compressedResource;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
}