1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.tools.jlink.internal;
  26 
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.nio.file.Path;
  30 import java.util.Objects;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * An Archive of all content, classes, resources, configuration files, and
  35  * other, for a module.
  36  */
  37 public interface Archive {
  38 
  39     /**
  40      * Entry is contained in an Archive
  41      */
  42     public abstract class Entry {
  43 
  44         public static enum EntryType {
  45             MODULE_NAME,
  46             CLASS_OR_RESOURCE,
  47             NATIVE_LIB,
  48             NATIVE_CMD,
  49             CONFIG,
  50             OTHER_FILES,
  51             SERVICE;
  52         }
  53 
  54         private final String name;
  55         private final EntryType type;
  56         private final Archive archive;
  57         private final String path;
  58 
  59         public Entry(Archive archive, String path, String name, EntryType type) {
  60             this.archive = Objects.requireNonNull(archive);
  61             this.path = Objects.requireNonNull(path);
  62             this.name = Objects.requireNonNull(name);
  63             this.type = Objects.requireNonNull(type);
  64         }
  65 
  66         public final Archive archive() {
  67             return archive;
  68         }
  69 
  70         public final EntryType type() {
  71             return type;
  72         }
  73 
  74         /*
  75          * Returns the name of this entry.
  76          */
  77         public final String name() {
  78             return name;
  79         }
  80 
  81         public final String getResourceName() {
  82             assert !name.startsWith("/");
  83             return "/" + archive.moduleName() + "/" + name;
  84         }
  85 
  86         @Override
  87         public String toString() {
  88             return "type " + type.name() + " path " + path;
  89         }
  90 
  91         /*
  92          * Returns the number of uncompressed bytes for this entry.
  93          */
  94         public abstract long size();
  95 
  96         public abstract InputStream stream() throws IOException;
  97     }
  98 
  99     /*
 100      * The module name.
 101      */
 102     String moduleName();
 103 
 104     /*
 105      * Returns the path to this module's content
 106      */
 107     Path getPath();
 108 
 109     /*
 110      * Stream of Entry.
 111      * The stream of entries needs to be closed after use
 112      * since it might cover lazy I/O based resources.
 113      * So callers need to use a try-with-resources block.
 114      */
 115     Stream<Entry> entries();
 116 
 117     /*
 118      * Open the archive
 119      */
 120     void open() throws IOException;
 121 
 122     /*
 123      * Close the archive
 124      */
 125     void close() throws IOException;
 126 }