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