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.Closeable;
 28 import java.io.IOException;
 29 import java.io.InputStream;
 30 import java.nio.file.Path;
 31 import java.util.Objects;
 32 import java.util.stream.Stream;
 33 
 34 /**
 35  * An Archive of all content, classes, resources, configuration files, and
 36  * other, for a module.
 37  */
 38 public interface Archive extends Closeable {
 39 
 40     /**
 41      * Entry is contained in an Archive
 42      */
 43     public abstract class Entry {
 44 
 45         public static enum EntryType {
 46             MODULE_NAME,
 47             CLASS_OR_RESOURCE,
 48             CONFIG,
 49             NATIVE_LIB,
 50             NATIVE_CMD,
 51             HEADER_FILE,
 52             LEGAL_NOTICE,
 53             MAN_PAGE,
 54             SERVICE;
 55         }
 56 
 57         private final String name;
 58         private final EntryType type;
 59         private final Archive archive;
 60         private final String path;
 61 
 62         /**
 63          * Constructs an entry of the given archive.
 64          *
 65          * @param archive the archive in which this entry exists.
 66          * @param path the complete path of the entry, including the module.
 67          * @param name an entry name relative to its containing module.
 68          * @param type the entry type.
 69          */
 70         public Entry(Archive archive, String path, String name, EntryType type) {
 71             this.archive = Objects.requireNonNull(archive);
 72             this.path = Objects.requireNonNull(path);
 73             this.name = Objects.requireNonNull(name);
 74             this.type = Objects.requireNonNull(type);
 75         }
 76 
 77         public final EntryType type() {
 78             return type;
 79         }
 80 
 81         /**
 82          * Returns the name of this entry.
 83          */
 84         public final String name() {
 85             return name;
 86         }
 87 
 88         /**
 89          * Returns the name representing a ResourcePoolEntry in the form of:
 90          *    /$MODULE/$ENTRY_NAME
 91          */
 92         public final String getResourcePoolEntryName() {
 93             return "/" + archive.moduleName() + "/" + name;
 94         }
 95 
 96         @Override
 97         public String toString() {
 98             return "type " + type.name() + " path " + path;
 99         }
100 
101         /*
102          * Returns the number of uncompressed bytes for this entry.
103          */
104         public abstract long size();
105 
106         public abstract InputStream stream() throws IOException;
107     }
108 
109     /*
110      * The module name.
111      */
112     String moduleName();
113 
114     /*
115      * Returns the path to this module's content
116      */
117     Path getPath();
118 
119     /*
120      * Stream of Entry.
121      * The stream of entries needs to be closed after use
122      * since it might cover lazy I/O based resources.
123      * So callers need to use a try-with-resources block.
124      */
125     Stream<Entry> entries();
126 
127     /*
128      * Open the archive
129      */
130     void open() throws IOException;
131 
132     /*
133      * Close the archive
134      */
135     @Override
136     void close() throws IOException;
137 }