1 /*
   2  * Copyright (c) 2010, 2012 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 
  26 package org.openjdk.jigsaw;
  27 
  28 import java.io.*;
  29 import java.lang.module.*;
  30 import java.util.Objects;
  31 
  32 
  33 /**
  34  * <p> A collection of module-info files together with associated module files,
  35  * suitable for download and installation. </p>
  36  */
  37 
  38 public abstract class Repository
  39     extends LocatableCatalog
  40 {
  41 
  42     /**
  43      * <p> The type of a module file <p>
  44      */
  45     public static enum ModuleFileType {
  46         /**
  47          * A module type that is a java module file
  48          */
  49         JMOD("jmod"),
  50 
  51         /**
  52          * A module type that is a modular jar file
  53          */
  54         JAR("jar");
  55 
  56         private final String extension;
  57 
  58         ModuleFileType(String suffix) {
  59             this.extension = suffix;
  60         }
  61 
  62         public String getFileNameExtension() {
  63             return extension;
  64         }
  65 
  66         public String getFileNameSuffix() {
  67             return "." + getFileNameExtension();
  68         }
  69 
  70         /**
  71          * Get the module type from the file name extension.
  72          *
  73          * @param extension the file name extension.
  74          * @return the module type.
  75          * @throws IllegalArgumentException if {@code extension}
  76          *         has no corresponding module type.
  77          * @throws NullPointerException if {@code extension} is null
  78          */
  79         public static ModuleFileType fromFileNameExtension(String extension) {
  80             Objects.requireNonNull(extension, "Extension is null");
  81             for (ModuleFileType type: values()) {
  82                 if (type.extension.equals(extension)) {
  83                     return type;
  84                 }
  85             }
  86 
  87             throw new IllegalArgumentException(
  88                     "No module type for the file name extension " + extension);
  89         }
  90 
  91     }
  92 
  93     /**
  94      * <p> Size and type information about a yet-to-be-installed module </p>
  95      */
  96     public static class ModuleFileMetaData {
  97 
  98         private final ModuleFileType type;
  99 
 100         /**
 101          * The type of the module-file..
 102          */
 103         public ModuleFileType getType() {
 104             return type;
 105         }
 106 
 107         private ModuleArchitecture modArch;
 108 
 109         /**
 110          * The Architecture of a module-file
 111          */
 112         public ModuleArchitecture architecture() {
 113             return modArch;
 114         }
 115 
 116         private final long csize;
 117 
 118         /**
 119          * The module's download size, in bytes.
 120          */
 121         public long getDownloadSize() { return csize; }
 122 
 123         private final long usize;
 124 
 125         /**
 126          * The module's installed size, in bytes.
 127          *
 128          * <p> The number of bytes required to install a module may be less
 129          * than the value returned by this method, but it will never be
 130          * greater. </p>
 131          */
 132         public long getInstallSize() { return usize; }
 133 
 134         ModuleFileMetaData(ModuleFileType t, ModuleArchitecture ma,
 135                            long cs, long us) {
 136             type = t;
 137             modArch = ma;
 138             csize = cs;
 139             usize = us;
 140         }
 141 
 142     }
 143 
 144     /**
 145      * Fetch the meta data for a given module. Such meta data will consist of
 146      * of the module type and size information.
 147      *
 148      * @param   mid
 149      *          The {@linkplain java.lang.module.ModuleId id} of the
 150      *          requested module
 151      *
 152      * @throws  IllegalArgumentException
 153      *          If the named module is not present in this repository
 154      */
 155     public abstract ModuleFileMetaData fetchMetaData(ModuleId mid)
 156         throws IOException;
 157 
 158     /**
 159      * Fetch the bytes for a given module.
 160      *
 161      * @param   mid
 162      *          The {@linkplain java.lang.module.ModuleId id} of the
 163      *          requested module
 164      *
 165      * @throws  IllegalArgumentException
 166      *          If the named module is not present in this repository
 167      * @throws  IOException
 168      *          If there is an error fetching the module.
 169      */
 170     public abstract InputStream fetch(ModuleId mid)
 171         throws IOException;
 172 
 173 }