src/share/classes/org/openjdk/jigsaw/RepositoryCatalog.java

Print this page




  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.*;
  31 
  32 import static org.openjdk.jigsaw.Repository.ModuleType;
  33 import static org.openjdk.jigsaw.FileConstants.ModuleFile.HashType;
  34 
  35 
  36 /**
  37  * <p> A {@linkplain Repository module repository's} catalog </p>
  38  */
  39 
  40 public abstract class RepositoryCatalog {
  41 
  42     // ## Elements in this class are public only to enable unit tests
  43 
  44     private static final JigsawModuleSystem jms
  45         = JigsawModuleSystem.instance();
  46 
  47     public abstract void gatherDeclaringModuleIds(Set<ModuleId> mids)
  48         throws IOException;
  49     
  50     public abstract void gatherModuleIds(String moduleName, Set<ModuleId> mids)
  51         throws IOException;
  52 





  53     public abstract byte[] readModuleInfoBytes(ModuleId mid)
  54         throws IOException;
  55 
  56     static class Entry {
  57 
  58         final ModuleType type;

  59         final byte[] mibs;
  60         final long csize;
  61         final long usize;
  62         final HashType hashType;
  63         final byte[] hash;
  64 
  65         Entry(ModuleType t, byte[] m, long cs, long us, HashType ht, byte[] h) {

  66             type = t;

  67             mibs = m;
  68             csize = cs;
  69             usize = us;
  70             hashType = ht;
  71             hash = h;
  72         }
  73 
  74     }
  75 
  76     abstract void add(Entry e);
  77 
  78     public void add(ModuleType t, byte[] mibs, long cs, long us,
  79                     HashType hashType, byte[] hash)
  80     {
  81         add(new Entry(t, mibs, cs, us, hashType, hash));
  82     }
  83 
  84     public abstract boolean remove(ModuleId mid);
  85 
  86     abstract Entry get(ModuleId mid);
  87 
  88 
  89     /**
  90      * <p> A {@linkplain RepositoryCatalog repository catalog} which can be
  91      * stored to, and then loaded from, a byte stream </p>
  92      */
  93     public static class StreamedRepositoryCatalog
  94         extends RepositoryCatalog
  95     {
  96 
  97         static final int MAJOR_VERSION = 0;
  98         static final int MINOR_VERSION = 0;
  99 
 100         private Map<ModuleId,Entry> modules = new HashMap<>();
 101         private Map<ModuleId,ModuleId> moduleForViewId= new HashMap<>();
 102 
 103         @Override
 104         public void gatherDeclaringModuleIds(Set<ModuleId> mids) {
 105             mids.addAll(modules.keySet());
 106         }
 107         
 108         @Override
 109         public void gatherModuleIds(String moduleName, Set<ModuleId> mids) {








 110             for (ModuleId mid : moduleForViewId.keySet()) {
 111                 if (moduleName == null || mid.name().equals(moduleName))


 112                     mids.add(mid);
 113             }
 114         }

 115 
 116         @Override
 117         public byte[] readModuleInfoBytes(ModuleId mid) {
 118             Entry e = modules.get(moduleForViewId.get(mid));
 119             return (e != null) ? e.mibs : null;
 120         }
 121 
 122         @Override
 123         void add(Entry e) {
 124             ModuleInfo mi = jms.parseModuleInfo(e.mibs); // ## Need fast path
 125             modules.put(mi.id(), e);
 126             for (ModuleView mv : mi.views()) {
 127                 moduleForViewId.put(mv.id(), mi.id());
 128                 for (ModuleId alias : mv.aliases()) {
 129                     moduleForViewId.put(alias, mi.id());
 130                 }
 131             }
 132         }
 133 
 134         @Override


 167         */
 168 
 169         private StreamedRepositoryCatalog() { }
 170 
 171         private FileHeader fileHeader() {
 172             return (new FileHeader()
 173                     .type(FileConstants.Type.STREAM_CATALOG)
 174                     .majorVersion(MAJOR_VERSION)
 175                     .minorVersion(MINOR_VERSION));
 176         }
 177 
 178         public void store(OutputStream os) throws IOException {
 179             OutputStream bos = new BufferedOutputStream(os);
 180             try (DataOutputStream out = new DataOutputStream(bos)) {
 181                 fileHeader().write(out);
 182                 out.writeInt(modules.size());
 183                 for (Map.Entry<ModuleId,Entry> me : modules.entrySet()) {
 184                     out.writeUTF(me.getKey().toString()); // ## Redundant
 185                     Entry e = me.getValue();
 186                     out.writeUTF(e.type.getFileNameExtension());


 187                     out.writeLong(e.csize);
 188                     out.writeLong(e.usize);
 189                     out.writeShort(e.hashType.value());
 190                     out.writeShort(e.hash.length);
 191                     out.write(e.hash);
 192                     out.writeShort(e.mibs.length);
 193                     out.write(e.mibs);
 194                 }
 195                 out.writeInt(moduleForViewId.size());
 196                 for (Map.Entry<ModuleId,ModuleId> me : moduleForViewId.entrySet()) {
 197                     out.writeUTF(me.getKey().toString());
 198                     out.writeUTF(me.getValue().toString());
 199                 }
 200             }
 201         }
 202 
 203         public StreamedRepositoryCatalog loadStream(InputStream is)
 204             throws IOException
 205         {
 206             BufferedInputStream bis = new BufferedInputStream(is);
 207             DataInputStream in = new DataInputStream(bis);
 208             FileHeader fh = fileHeader();
 209             fh.read(in);
 210             int nms = in.readInt();
 211             for (int i = 0; i < nms; i++) {
 212                 ModuleId mid = jms.parseModuleId(in.readUTF());
 213                 ModuleType t = ModuleType.fromFileNameExtension(in.readUTF());


 214                 long cs = in.readLong();
 215                 long us = in.readLong();
 216                 HashType ht = HashType.valueOf(in.readShort());
 217                 int nb = in.readShort();
 218                 byte[] hash = new byte[nb];
 219                 in.readFully(hash);
 220                 nb = in.readShort();
 221                 byte[] mibs = new byte[nb];
 222                 in.readFully(mibs);
 223                 modules.put(mid, new Entry(t, mibs, cs, us, ht, hash));
 224             }
 225             int nmids = in.readInt();
 226             for (int i = 0; i < nmids; i++) {
 227                 ModuleId id = jms.parseModuleId(in.readUTF());
 228                 ModuleId mid = jms.parseModuleId(in.readUTF());
 229                 moduleForViewId.put(id, mid);
 230             }
 231             return this;
 232         }
 233 
 234     }
 235 
 236     public static StreamedRepositoryCatalog load(InputStream in)
 237         throws IOException
 238     {
 239         StreamedRepositoryCatalog src = new StreamedRepositoryCatalog();
 240         if (in != null) {
 241             try {
 242                 src.loadStream(in);
 243             } finally {


  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.*;
  31 
  32 import static org.openjdk.jigsaw.Repository.ModuleFileType;
  33 import static org.openjdk.jigsaw.FileConstants.ModuleFile.HashType;
  34 
  35 
  36 /**
  37  * <p> A {@linkplain Repository module repository's} catalog </p>
  38  */
  39 
  40 public abstract class RepositoryCatalog {
  41 
  42     // ## Elements in this class are public only to enable unit tests
  43 
  44     private static final JigsawModuleSystem jms
  45         = JigsawModuleSystem.instance();
  46 
  47     public abstract void gatherDeclaringModuleIds(Set<ModuleId> mids)
  48         throws IOException;
  49 
  50     public abstract void gatherModuleIds(String moduleName, Set<ModuleId> mids)
  51         throws IOException;
  52 
  53     public abstract void gatherModuleIds(String moduleName,
  54                                          ModuleArchitecture modArch,
  55                                          Set<ModuleId> mids)
  56         throws IOException;
  57 
  58     public abstract byte[] readModuleInfoBytes(ModuleId mid)
  59         throws IOException;
  60 
  61     static class Entry {
  62 
  63         final ModuleFileType type;
  64         final ModuleArchitecture modArch;
  65         final byte[] mibs;
  66         final long csize;
  67         final long usize;
  68         final HashType hashType;
  69         final byte[] hash;
  70 
  71         Entry(ModuleFileType t, ModuleArchitecture modArch,
  72               byte[] m, long cs, long us, HashType ht, byte[] h) {
  73             type = t;
  74             this.modArch = modArch;
  75             mibs = m;
  76             csize = cs;
  77             usize = us;
  78             hashType = ht;
  79             hash = h;
  80         }
  81 
  82     }
  83 
  84     abstract void add(Entry e);
  85 
  86     public void add(ModuleFileType t, ModuleArchitecture modArch, byte[] mibs,
  87                     long cs, long us, HashType hashType, byte[] hash)
  88     {
  89         add(new Entry(t, modArch, mibs, cs, us, hashType, hash));
  90     }
  91 
  92     public abstract boolean remove(ModuleId mid);
  93 
  94     abstract Entry get(ModuleId mid);
  95 
  96 
  97     /**
  98      * <p> A {@linkplain RepositoryCatalog repository catalog} which can be
  99      * stored to, and then loaded from, a byte stream </p>
 100      */
 101     public static class StreamedRepositoryCatalog
 102         extends RepositoryCatalog
 103     {
 104 
 105         static final int MAJOR_VERSION = 0;
 106         static final int MINOR_VERSION = 0;
 107 
 108         private Map<ModuleId,Entry> modules = new HashMap<>();
 109         private Map<ModuleId,ModuleId> moduleForViewId= new HashMap<>();
 110 
 111         @Override
 112         public void gatherDeclaringModuleIds(Set<ModuleId> mids) {
 113             mids.addAll(modules.keySet());
 114         }
 115 
 116         @Override
 117         public void gatherModuleIds(String moduleName, Set<ModuleId> mids) {
 118             gatherModuleIds(moduleName, ModuleArchitecture.ANY, mids);
 119         }
 120 
 121         @Override
 122         public void gatherModuleIds(String moduleName,
 123                                     ModuleArchitecture modArch,
 124                                     Set<ModuleId> mids)
 125         {
 126             for (ModuleId mid : moduleForViewId.keySet()) {
 127                 if (moduleName == null || mid.name().equals(moduleName)) {
 128                     Entry entry = get(mid);
 129                     if (entry.modArch.equals(modArch))
 130                         mids.add(mid);
 131                 }
 132             }
 133         }
 134 
 135         @Override
 136         public byte[] readModuleInfoBytes(ModuleId mid) {
 137             Entry e = modules.get(moduleForViewId.get(mid));
 138             return (e != null) ? e.mibs : null;
 139         }
 140 
 141         @Override
 142         void add(Entry e) {
 143             ModuleInfo mi = jms.parseModuleInfo(e.mibs); // ## Need fast path
 144             modules.put(mi.id(), e);
 145             for (ModuleView mv : mi.views()) {
 146                 moduleForViewId.put(mv.id(), mi.id());
 147                 for (ModuleId alias : mv.aliases()) {
 148                     moduleForViewId.put(alias, mi.id());
 149                 }
 150             }
 151         }
 152 
 153         @Override


 186         */
 187 
 188         private StreamedRepositoryCatalog() { }
 189 
 190         private FileHeader fileHeader() {
 191             return (new FileHeader()
 192                     .type(FileConstants.Type.STREAM_CATALOG)
 193                     .majorVersion(MAJOR_VERSION)
 194                     .minorVersion(MINOR_VERSION));
 195         }
 196 
 197         public void store(OutputStream os) throws IOException {
 198             OutputStream bos = new BufferedOutputStream(os);
 199             try (DataOutputStream out = new DataOutputStream(bos)) {
 200                 fileHeader().write(out);
 201                 out.writeInt(modules.size());
 202                 for (Map.Entry<ModuleId,Entry> me : modules.entrySet()) {
 203                     out.writeUTF(me.getKey().toString()); // ## Redundant
 204                     Entry e = me.getValue();
 205                     out.writeUTF(e.type.getFileNameExtension());
 206                     out.writeUTF(e.modArch.os());
 207                     out.writeUTF(e.modArch.arch());
 208                     out.writeLong(e.csize);
 209                     out.writeLong(e.usize);
 210                     out.writeShort(e.hashType.value());
 211                     out.writeShort(e.hash.length);
 212                     out.write(e.hash);
 213                     out.writeShort(e.mibs.length);
 214                     out.write(e.mibs);
 215                 }
 216                 out.writeInt(moduleForViewId.size());
 217                 for (Map.Entry<ModuleId,ModuleId> me : moduleForViewId.entrySet()) {
 218                     out.writeUTF(me.getKey().toString());
 219                     out.writeUTF(me.getValue().toString());
 220                 }
 221             }
 222         }
 223 
 224         public StreamedRepositoryCatalog loadStream(InputStream is)
 225             throws IOException
 226         {
 227             BufferedInputStream bis = new BufferedInputStream(is);
 228             DataInputStream in = new DataInputStream(bis);
 229             FileHeader fh = fileHeader();
 230             fh.read(in);
 231             int nms = in.readInt();
 232             for (int i = 0; i < nms; i++) {
 233                 ModuleId mid = jms.parseModuleId(in.readUTF());
 234                 ModuleFileType t = ModuleFileType.fromFileNameExtension(in.readUTF());
 235                 ModuleArchitecture modArch = ModuleArchitecture.create(in.readUTF(),
 236                                                                        in.readUTF());
 237                 long cs = in.readLong();
 238                 long us = in.readLong();
 239                 HashType ht = HashType.valueOf(in.readShort());
 240                 int nb = in.readShort();
 241                 byte[] hash = new byte[nb];
 242                 in.readFully(hash);
 243                 nb = in.readShort();
 244                 byte[] mibs = new byte[nb];
 245                 in.readFully(mibs);
 246                 modules.put(mid, new Entry(t, modArch, mibs, cs, us, ht, hash));
 247             }
 248             int nmids = in.readInt();
 249             for (int i = 0; i < nmids; i++) {
 250                 ModuleId id = jms.parseModuleId(in.readUTF());
 251                 ModuleId mid = jms.parseModuleId(in.readUTF());
 252                 moduleForViewId.put(id, mid);
 253             }
 254             return this;
 255         }
 256 
 257     }
 258 
 259     public static StreamedRepositoryCatalog load(InputStream in)
 260         throws IOException
 261     {
 262         StreamedRepositoryCatalog src = new StreamedRepositoryCatalog();
 263         if (in != null) {
 264             try {
 265                 src.loadStream(in);
 266             } finally {