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