1 /*
   2  * Copyright (c) 2016, 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.plugin;
  26 
  27 import java.io.ByteArrayInputStream;
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.OutputStream;
  31 import java.io.UncheckedIOException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import jdk.tools.jlink.internal.ResourcePoolEntryFactory;
  35 
  36 /**
  37  * A ResourcePoolEntry is the elementary unit of data inside an image. It is
  38  * generally a file. e.g.: a java class file, a resource file, a shared library.
  39  * <br>
  40  * A ResourcePoolEntry is identified by a path of the form:
  41  * <ul>
  42  * <li>For jimage content: /{module name}/{package1}/.../{packageN}/{file
  43  * name}</li>
  44  * <li>For top-level files:/{module name}/{file name}</li>
  45  * <li>For other files (shared lib, launchers, config, ...):/{module name}/
  46  * {@literal bin|conf|native}/{dir1}/.../{dirN}/{file name}</li>
  47  * </ul>
  48  */
  49 public interface ResourcePoolEntry {
  50 
  51     /**
  52      * Type of module data.
  53      * <li>
  54      * <ul>CLASS_OR_RESOURCE: A java class or resource file.</ul>
  55      * <ul>CONFIG: A configuration file.</ul>
  56      * <ul>NATIVE_CMD: A native process launcher.</ul>
  57      * <ul>NATIVE_LIB: A native library.</ul>
  58      * <ul>TOP: A top-level file in the jdk run-time image directory.</ul>
  59      * <ul>OTHER: Other kind of file.</ul>
  60      * </li>
  61      */
  62     public enum Type {
  63         CLASS_OR_RESOURCE,
  64         CONFIG,
  65         NATIVE_CMD,
  66         NATIVE_LIB,
  67         TOP,
  68         OTHER
  69     }
  70 
  71     /**
  72      * The module name of this ResourcePoolEntry.
  73      *
  74      * @return The module name.
  75      */
  76     public String moduleName();
  77 
  78     /**
  79      * The path of this ResourcePoolEntry.
  80      *
  81      * @return The module path.
  82      */
  83     public String path();
  84 
  85     /**
  86      * The ResourcePoolEntry's type.
  87      *
  88      * @return The data type.
  89      */
  90     public Type type();
  91 
  92     /**
  93      * The ResourcePoolEntry content length.
  94      *
  95      * @return The content length.
  96      */
  97     public long contentLength();
  98 
  99     /**
 100      * The ResourcePoolEntry content as an InputStream.
 101      *
 102      * @return The resource content as an InputStream.
 103      */
 104     public InputStream content();
 105 
 106     /**
 107      * The ResourcePoolEntry content as an array of bytes.
 108      *
 109      * @return An Array of bytes.
 110      */
 111     public default byte[] contentBytes() {
 112         try (InputStream is = content()) {
 113             return is.readAllBytes();
 114         } catch (IOException ex) {
 115             throw new UncheckedIOException(ex);
 116         }
 117     }
 118 
 119     /**
 120      * Write the content of this ResourcePoolEntry to an OutputStream.
 121      *
 122      * @param out the output stream
 123      */
 124     public default void write(OutputStream out) {
 125         try {
 126             out.write(contentBytes());
 127         } catch (IOException ex) {
 128             throw new UncheckedIOException(ex);
 129         }
 130     }
 131 
 132     /**
 133      * Create a ResourcePoolEntry with new content but other information
 134      * copied from this ResourcePoolEntry.
 135      *
 136      * @param content The new resource content.
 137      * @return A new ResourcePoolEntry.
 138      */
 139     public default ResourcePoolEntry copyWithContent(byte[] content) {
 140         return ResourcePoolEntryFactory.create(this, content);
 141     }
 142 
 143     /**
 144      * Create a ResourcePoolEntry with new content but other information
 145      * copied from this ResourcePoolEntry.
 146      *
 147      * @param file The new resource content.
 148      * @return A new ResourcePoolEntry.
 149      */
 150     public default ResourcePoolEntry copyWithContent(Path file) {
 151         return ResourcePoolEntryFactory.create(this, file);
 152     }
 153 
 154     /**
 155      * Create a ResourcePoolEntry for a resource of the given type.
 156      *
 157      * @param path The resource path.
 158      * @param type The ResourcePoolEntry type.
 159      * @param content The resource content.
 160      * @return A new ResourcePoolEntry.
 161      */
 162     public static ResourcePoolEntry create(String path,
 163             ResourcePoolEntry.Type type, byte[] content) {
 164         return ResourcePoolEntryFactory.create(path, type, content);
 165     }
 166 
 167     /**
 168      * Create a ResourcePoolEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}.
 169      *
 170      * @param path The resource path.
 171      * @param content The resource content.
 172      * @return A new ResourcePoolEntry.
 173      */
 174     public static ResourcePoolEntry create(String path, byte[] content) {
 175         return create(path, Type.CLASS_OR_RESOURCE, content);
 176     }
 177 
 178     /**
 179      * Create a ResourcePoolEntry for a resource of the given type.
 180      *
 181      * @param path The resource path.
 182      * @param type The ResourcePoolEntry type.
 183      * @param file The resource file.
 184      * @return A new ResourcePoolEntry.
 185      */
 186     public static ResourcePoolEntry create(String path,
 187             ResourcePoolEntry.Type type, Path file) {
 188         return ResourcePoolEntryFactory.create(path, type, file);
 189     }
 190 
 191     /**
 192      * Create a ResourcePoolEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}.
 193      *
 194      * @param path The resource path.
 195      * @param file The resource file.
 196      * @return A new ResourcePoolEntry.
 197      */
 198     public static ResourcePoolEntry create(String path, Path file) {
 199         return create(path, Type.CLASS_OR_RESOURCE, file);
 200     }
 201 }