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             SERVICE;
  51         }
  52 
  53         private final String name;
  54         private final EntryType type;
  55         private final Archive archive;
  56         private final String path;
  57 
  58         /**
  59          * Constructs an entry of the given archive
  60          * @param archive archive
  61          * @param path
  62          * @param name an entry name that does not contain the module name
  63          * @param type
  64          */
  65         public Entry(Archive archive, String path, String name, EntryType type) {
  66             this.archive = Objects.requireNonNull(archive);
  67             this.path = Objects.requireNonNull(path);
  68             this.name = Objects.requireNonNull(name);
  69             this.type = Objects.requireNonNull(type);
  70         }
  71 
  72         public final Archive archive() {
  73             return archive;
  74         }
  75 
  76         public final EntryType type() {
  77             return type;
  78         }
  79 
  80         /**
  81          * Returns the name of this entry.
  82          */
  83         public final String name() {
  84             return name;
  85         }
  86 
  87         /**
  88          * Returns the name representing a ResourcePoolEntry in the form of:
  89          *    /$MODULE/$ENTRY_NAME
  90          */
  91         public final String getResourcePoolEntryName() {
  92             return "/" + archive.moduleName() + "/" + name;
  93         }
  94 
  95         @Override
  96         public String toString() {
  97             return "type " + type.name() + " path " + path;
  98         }
  99 
 100         /*
 101          * Returns the number of uncompressed bytes for this entry.
 102          */
 103         public abstract long size();
 104 
 105         public abstract InputStream stream() throws IOException;
 106     }
 107 
 108     /*
 109      * The module name.
 110      */
 111     String moduleName();
 112 
 113     /*
 114      * Returns the path to this module's content
 115      */
 116     Path getPath();
 117 
 118     /*
 119      * Stream of Entry.
 120      * The stream of entries needs to be closed after use
 121      * since it might cover lazy I/O based resources.
 122      * So callers need to use a try-with-resources block.
 123      */
 124     Stream<Entry> entries();
 125 
 126     /*
 127      * Open the archive
 128      */
 129     void open() throws IOException;
 130 
 131     /*
 132      * Close the archive
 133      */
 134     void close() throws IOException;
 135 }