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