jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/plugin/ModuleEntry.java
changeset 39934 9c84ee88dd3a
parent 39933 c0dd0f198453
parent 39922 e613affb88d1
child 39935 6016bd47edc9
equal deleted inserted replaced
39933:c0dd0f198453 39934:9c84ee88dd3a
     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.ModuleEntryFactory;
       
    35 
       
    36 /**
       
    37  * A ModuleEntry 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 ModuleEntry 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 other files (shared lib, launchers, config, ...):/{module name}/
       
    45  * {@literal bin|conf|native}/{dir1}/.../{dirN}/{file name}</li>
       
    46  * </ul>
       
    47  */
       
    48 public interface ModuleEntry {
       
    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>OTHER: Other kind of file.</ul>
       
    58      * </li>
       
    59      */
       
    60     public enum Type {
       
    61         CLASS_OR_RESOURCE,
       
    62         CONFIG,
       
    63         NATIVE_CMD,
       
    64         NATIVE_LIB,
       
    65         OTHER
       
    66     }
       
    67     /**
       
    68      * The ModuleEntry module name.
       
    69      *
       
    70      * @return The module name.
       
    71      */
       
    72     public String getModule();
       
    73 
       
    74     /**
       
    75      * The ModuleEntry path.
       
    76      *
       
    77      * @return The module path.
       
    78      */
       
    79     public String getPath();
       
    80 
       
    81     /**
       
    82      * The ModuleEntry's type.
       
    83      *
       
    84      * @return The data type.
       
    85      */
       
    86     public Type getType();
       
    87 
       
    88     /**
       
    89      * The ModuleEntry content as an array of bytes.
       
    90      *
       
    91      * @return An Array of bytes.
       
    92      */
       
    93     public default byte[] getBytes() {
       
    94         try (InputStream is = stream()) {
       
    95             return is.readAllBytes();
       
    96         } catch (IOException ex) {
       
    97             throw new UncheckedIOException(ex);
       
    98         }
       
    99     }
       
   100 
       
   101     /**
       
   102      * The ModuleEntry content length.
       
   103      *
       
   104      * @return The length.
       
   105      */
       
   106     public long getLength();
       
   107 
       
   108     /**
       
   109      * The ModuleEntry stream.
       
   110      *
       
   111      * @return The module data stream.
       
   112      */
       
   113     public InputStream stream();
       
   114 
       
   115     /**
       
   116      * Write the content of this ModuleEntry to stream.
       
   117      *
       
   118      * @param out the output stream
       
   119      */
       
   120     public default void write(OutputStream out) {
       
   121         try {
       
   122             out.write(getBytes());
       
   123         } catch (IOException ex) {
       
   124             throw new UncheckedIOException(ex);
       
   125         }
       
   126     }
       
   127 
       
   128     /**
       
   129      * Create a ModuleEntry with new content but other information
       
   130      * copied from this ModuleEntry.
       
   131      *
       
   132      * @param content The new resource content.
       
   133      * @return A new ModuleEntry.
       
   134      */
       
   135     public default ModuleEntry create(byte[] content) {
       
   136         return ModuleEntryFactory.create(this, content);
       
   137     }
       
   138 
       
   139     /**
       
   140      * Create a ModuleEntry with new content but other information
       
   141      * copied from this ModuleEntry.
       
   142      *
       
   143      * @param file The new resource content.
       
   144      * @return A new ModuleEntry.
       
   145      */
       
   146     public default ModuleEntry create(Path file) {
       
   147         return ModuleEntryFactory.create(this, file);
       
   148     }
       
   149 
       
   150     /**
       
   151      * Create a ModuleEntry for a resource of the given type.
       
   152      *
       
   153      * @param path The resource path.
       
   154      * @param type The ModuleEntry type.
       
   155      * @param content The resource content.
       
   156      * @return A new ModuleEntry.
       
   157      */
       
   158     public static ModuleEntry create(String path,
       
   159             ModuleEntry.Type type, byte[] content) {
       
   160         return ModuleEntryFactory.create(path, type, content);
       
   161     }
       
   162 
       
   163     /**
       
   164      * Create a ModuleEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}.
       
   165      *
       
   166      * @param path The resource path.
       
   167      * @param content The resource content.
       
   168      * @return A new ModuleEntry.
       
   169      */
       
   170     public static ModuleEntry create(String path, byte[] content) {
       
   171         return create(path, Type.CLASS_OR_RESOURCE, content);
       
   172     }
       
   173 
       
   174     /**
       
   175      * Create a ModuleEntry for a resource of the given type.
       
   176      *
       
   177      * @param path The resource path.
       
   178      * @param type The ModuleEntry type.
       
   179      * @param file The resource file.
       
   180      * @return A new ModuleEntry.
       
   181      */
       
   182     public static ModuleEntry create(String path,
       
   183             ModuleEntry.Type type, Path file) {
       
   184         return ModuleEntryFactory.create(path, type, file);
       
   185     }
       
   186 
       
   187     /**
       
   188      * Create a ModuleEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}.
       
   189      *
       
   190      * @param path The resource path.
       
   191      * @param file The resource file.
       
   192      * @return A new ModuleEntry.
       
   193      */
       
   194     public static ModuleEntry create(String path, Path file) {
       
   195         return create(path, Type.CLASS_OR_RESOURCE, file);
       
   196     }
       
   197 }