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

Print this page




1143             fc.close();
1144         }
1145     }
1146 
1147     @Override
1148     public void installFromManifests(Collection<Manifest> mfs)
1149         throws ConfigurationException, IOException
1150     {
1151         installFromManifests(mfs, false);
1152     }
1153 
1154     private ModuleId installWhileLocked(InputStream is, boolean verifySignature,
1155                                         boolean strip)
1156         throws ConfigurationException, IOException, SignatureException
1157     {
1158         BufferedInputStream bin = new BufferedInputStream(is);
1159         DataInputStream in = new DataInputStream(bin);
1160         ModuleInfo mi = null;
1161         try (ModuleFile.Reader mr = new ModuleFile.Reader(in)) {
1162             byte[] mib = mr.readStart();
1163             mi = jms.parseModuleInfo(mib);
1164             File md = moduleDictionary.add(mi);

1165             if (verifySignature && mr.hasSignature()) {
1166                 // Verify the module signature
1167                 SignedModule sm = new SignedModule(mr);
1168                 Set<CodeSigner> signers = sm.verifySignature();
1169 
1170                 // Validate the signers
1171                 try {
1172                     SignedModule.validateSigners(signers);
1173                 } catch (CertificateException x) {
1174                     throw new SignatureException(x);
1175                 }
1176 
1177                 // ## TODO: Check policy and determine if signer is trusted
1178                 // ## and what permissions should be granted.
1179                 // ## If there is no policy entry, show signers and prompt
1180                 // ## user to accept before proceeding.
1181 
1182                 // Verify the module header hash and the module info hash
1183                 sm.verifyHashesStart();
1184 


1200 
1201             return mi.id();
1202 
1203         } catch (ConfigurationException | IOException | SignatureException x) {
1204             if (mi != null) {
1205                 try {
1206                     moduleDictionary.remove(mi);
1207                 } catch (IOException y) {
1208                     x.addSuppressed(y);
1209                 }
1210             }
1211             throw x;
1212         }
1213     }
1214 
1215     private ModuleId installFromJarFile(File mf, boolean verifySignature, boolean strip)
1216         throws ConfigurationException, IOException, SignatureException
1217     {
1218         ModuleInfo mi = null;
1219         try (JarFile jf = new JarFile(mf, verifySignature)) {
1220             mi = jf.getModuleInfo();
1221             if (mi == null)
1222                 throw new ConfigurationException(mf + ": not a modular JAR file");
1223 
1224             File md = moduleDictionary.add(mi);

1225             ModuleId mid = mi.id();
1226 
1227             boolean signed = false;
1228 
1229             // copy the jar file to the module library
1230             File classesDir = new File(md, "classes");
1231             try (FileOutputStream fos = new FileOutputStream(classesDir);
1232                  BufferedOutputStream bos = new BufferedOutputStream(fos);
1233                  JarOutputStream jos = new JarOutputStream(bos)) {
1234                 jos.setLevel(0);
1235 
1236                 Enumeration<JarEntry> entries = jf.entries();
1237                 while (entries.hasMoreElements()) {
1238                     JarEntry je = entries.nextElement();
1239                     try (InputStream is = jf.getInputStream(je)) {
1240                         if (je.getName().equals(JarFile.MODULEINFO_NAME)) {
1241                             java.nio.file.Files.copy(is, md.toPath().resolve("info"));
1242                         } else {
1243                             writeJarEntry(is, je, jos);
1244                         }




1143             fc.close();
1144         }
1145     }
1146 
1147     @Override
1148     public void installFromManifests(Collection<Manifest> mfs)
1149         throws ConfigurationException, IOException
1150     {
1151         installFromManifests(mfs, false);
1152     }
1153 
1154     private ModuleId installWhileLocked(InputStream is, boolean verifySignature,
1155                                         boolean strip)
1156         throws ConfigurationException, IOException, SignatureException
1157     {
1158         BufferedInputStream bin = new BufferedInputStream(is);
1159         DataInputStream in = new DataInputStream(bin);
1160         ModuleInfo mi = null;
1161         try (ModuleFile.Reader mr = new ModuleFile.Reader(in)) {
1162             byte[] mib = mr.readStart();
1163             ModuleInfo moduleInfo= jms.parseModuleInfo(mib);
1164             File md = moduleDictionary.add(moduleInfo);
1165             mi = moduleInfo;
1166             if (verifySignature && mr.hasSignature()) {
1167                 // Verify the module signature
1168                 SignedModule sm = new SignedModule(mr);
1169                 Set<CodeSigner> signers = sm.verifySignature();
1170 
1171                 // Validate the signers
1172                 try {
1173                     SignedModule.validateSigners(signers);
1174                 } catch (CertificateException x) {
1175                     throw new SignatureException(x);
1176                 }
1177 
1178                 // ## TODO: Check policy and determine if signer is trusted
1179                 // ## and what permissions should be granted.
1180                 // ## If there is no policy entry, show signers and prompt
1181                 // ## user to accept before proceeding.
1182 
1183                 // Verify the module header hash and the module info hash
1184                 sm.verifyHashesStart();
1185 


1201 
1202             return mi.id();
1203 
1204         } catch (ConfigurationException | IOException | SignatureException x) {
1205             if (mi != null) {
1206                 try {
1207                     moduleDictionary.remove(mi);
1208                 } catch (IOException y) {
1209                     x.addSuppressed(y);
1210                 }
1211             }
1212             throw x;
1213         }
1214     }
1215 
1216     private ModuleId installFromJarFile(File mf, boolean verifySignature, boolean strip)
1217         throws ConfigurationException, IOException, SignatureException
1218     {
1219         ModuleInfo mi = null;
1220         try (JarFile jf = new JarFile(mf, verifySignature)) {
1221             ModuleInfo moduleInfo = jf.getModuleInfo();
1222             if (moduleInfo == null)
1223                 throw new ConfigurationException(mf + ": not a modular JAR file");
1224 
1225             File md = moduleDictionary.add(moduleInfo);
1226             mi = moduleInfo;
1227             ModuleId mid = mi.id();
1228 
1229             boolean signed = false;
1230 
1231             // copy the jar file to the module library
1232             File classesDir = new File(md, "classes");
1233             try (FileOutputStream fos = new FileOutputStream(classesDir);
1234                  BufferedOutputStream bos = new BufferedOutputStream(fos);
1235                  JarOutputStream jos = new JarOutputStream(bos)) {
1236                 jos.setLevel(0);
1237 
1238                 Enumeration<JarEntry> entries = jf.entries();
1239                 while (entries.hasMoreElements()) {
1240                     JarEntry je = entries.nextElement();
1241                     try (InputStream is = jf.getInputStream(je)) {
1242                         if (je.getName().equals(JarFile.MODULEINFO_NAME)) {
1243                             java.nio.file.Files.copy(is, md.toPath().resolve("info"));
1244                         } else {
1245                             writeJarEntry(is, je, jos);
1246                         }