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 ModuleType {     // ## Should be 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         ModuleType(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 ModuleType fromFileNameExtension(String extension) {
  80             Objects.requireNonNull(extension, "Extension is null");
  81             for (ModuleType 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 ModuleMetaData { // ## Should be ModuleFileMetaData
  97 
  98         private final ModuleType type;
  99         
 100         /**
 101          * The type of the module.
 102          */
 103         public ModuleType getType() {
 104             return type;
 105         }
 106         
 107         private final long csize;
 108 
 109         /**
 110          * The module's download size, in bytes.
 111          */
 112         public long getDownloadSize() { return csize; }
 113 
 114         private final long usize;
 115 
 116         /**
 117          * The module's installed size, in bytes.
 118          *
 119          * <p> The number of bytes required to install a module may be less
 120          * than the value returned by this method, but it will never be
 121          * greater. </p>
 122          */
 123         public long getInstallSize() { return usize; }
 124 
 125         ModuleMetaData(ModuleType t, long cs, long us) {
 126             type = t;
 127             csize = cs;
 128             usize = us;
 129         }
 130 
 131     }
 132 
 133     /**
 134      * Fetch the meta data for a given module. Such meta data will consist of
 135      * of the module type and size information.
 136      *
 137      * @param   mid
 138      *          The {@linkplain java.lang.module.ModuleId id} of the
 139      *          requested module
 140      *
 141      * @throws  IllegalArgumentException
 142      *          If the named module is not present in this repository
 143      */
 144     public abstract ModuleMetaData fetchMetaData(ModuleId mid) throws IOException;
 145 
 146     /**
 147      * Fetch the bytes for a given module.
 148      * 
 149      * @param   mid
 150      *          The {@linkplain java.lang.module.ModuleId id} of the
 151      *          requested module
 152      * 
 153      * @throws  IllegalArgumentException
 154      *          If the named module is not present in this repository
 155      * @throws  IOException 
 156      *          If there is an error fetching the module.
 157      */
 158     public abstract InputStream fetch(ModuleId mid) throws IOException;
 159 
 160 }