jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/DirArchive.java
author jlaskey
Tue, 28 Jun 2016 16:07:23 -0300
changeset 39308 f6517755057f
parent 36511 9d0388c6b336
child 41352 f9844bad9052
permissions -rw-r--r--
8160459: jlink minor code clean up Reviewed-by: mchung
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.function.Consumer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * An Archive backed by a directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
public class DirArchive implements Archive {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
     * A File located in a Directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    private class FileEntry extends Archive.Entry {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        private final long size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        private final Path path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        FileEntry(Path path, String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            super(DirArchive.this, getPathName(path), name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
                    Archive.Entry.EntryType.CLASS_OR_RESOURCE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            this.path = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
                size = Files.size(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
                throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
         * Returns the number of bytes of this file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        public long size() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            return size;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        public InputStream stream() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            InputStream stream = Files.newInputStream(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            open.add(stream);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            return stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    private static final String MODULE_INFO = "module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    private final Path dirPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    private final String moduleName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    private final List<InputStream> open = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private final int chop;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    private final Consumer<String> log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    private static final Consumer<String> noopConsumer = (String t) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    public DirArchive(Path dirPath) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        this(dirPath, noopConsumer);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    public DirArchive(Path dirPath, Consumer<String> log) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        Objects.requireNonNull(dirPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        if (!Files.isDirectory(dirPath)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            throw new IllegalArgumentException("Not a directory");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        chop = dirPath.toString().length() + 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        this.moduleName = Objects.requireNonNull(dirPath.getFileName()).toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        this.dirPath = dirPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        this.log = log;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    public String moduleName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        return moduleName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    public Path getPath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        return dirPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    public Stream<Entry> entries() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        try {
39308
f6517755057f 8160459: jlink minor code clean up
jlaskey
parents: 36511
diff changeset
   116
            return Files.walk(dirPath).map(this::toEntry).filter(n -> n != null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            throw new RuntimeException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
    private Archive.Entry toEntry(Path p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        if (Files.isDirectory(p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        String name = getPathName(p).substring(chop);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        if (name.startsWith("_")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        log.accept(moduleName + "/" + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        if (name.equals(MODULE_INFO)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            name = moduleName + "/" + MODULE_INFO;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        return new FileEntry(p, name);
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
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    public void close() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        IOException e = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        for (InputStream stream : open) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                stream.close();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                if (e == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                    e = ex;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                    e.addSuppressed(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        if (e != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            throw e;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    public void open() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        // NOOP
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    private static String getPathName(Path path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        return path.toString().replace(File.separatorChar, '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
}