src/share/classes/org/openjdk/jigsaw/SimpleLibrary.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.lang.module.*;
  29 import java.io.*;
  30 import java.net.URI;
  31 import java.nio.file.*;

  32 import java.security.*;
  33 import java.security.cert.*;
  34 import java.util.*;
  35 import java.util.jar.*;
  36 import java.util.zip.*;
  37 
  38 import static java.nio.file.StandardCopyOption.*;
  39 
  40 /**
  41  * A simple module library which stores data directly in the filesystem
  42  *
  43  * @see Library
  44  */
  45 
  46 // ## TODO: Move remaining parent-searching logic upward into Library class
  47 
  48 // On-disk library layout
  49 //
  50 //   $LIB/%jigsaw-library
  51 //        com.foo.bar/1.2.3/info (= module-info.class)


 874         throws IOException
 875     {
 876         Object o = findContent(mid);
 877         if (o == null)
 878             return null;
 879         if (o instanceof ZipFile) {
 880             ZipFile zf = (ZipFile)o;
 881             ZipEntry ze = zf.getEntry(path);
 882             if (ze == null)
 883                 return null;
 884             return URI.create("jar:"
 885                               + new File(zf.getName()).toURI().toString()
 886                               + "!/" + path);
 887         }
 888         if (o instanceof File) {
 889             File f = new File((File)o, path);
 890             if (!f.exists())
 891                 return null;
 892             return f.toURI();
 893         }
 894         assert false;
 895         return null;
 896     }
 897 
 898     public byte[] readLocalClass(ModuleId mid, String className)
 899         throws IOException
 900     {
 901         return loadContent(mid, className.replace('.', '/') + ".class");
 902     }
 903 
 904     public List<String> listLocalClasses(ModuleId mid, boolean all)
 905         throws IOException
 906     {
 907         File md = findModuleDir(mid);
 908         if (md == null)
 909             return null;
 910         Index ix = Index.load(md);
 911         int os = all ? ix.otherClasses().size() : 0;
 912         ArrayList<String> cns
 913             = new ArrayList<String>(ix.publicClasses().size() + os);
 914         cns.addAll(ix.publicClasses());


 992             try (JarFile jf = new JarFile(classes);
 993                 FileOutputStream out = new FileOutputStream(pf))
 994             {
 995                 Pack200.Packer packer = Pack200.newPacker();
 996                 Map<String,String> p = packer.properties();
 997                 p.put("com.sun.java.util.jar.pack.strip.debug", Pack200.Packer.TRUE);
 998                 packer.pack(jf, out);
 999             }
1000 
1001             try (OutputStream out = new FileOutputStream(classes);
1002                  JarOutputStream jos = new JarOutputStream(out))
1003             {
1004                 Pack200.Unpacker unpacker = Pack200.newUnpacker();
1005                 unpacker.unpack(pf, jos);
1006             } finally {
1007                 pf.delete();
1008            }
1009         }
1010     }
1011 















1012     private void install(Manifest mf, File dst, boolean strip)
1013         throws IOException
1014     {
1015         if (mf.classes().size() > 1)
1016             throw new IllegalArgumentException("Multiple module-class"
1017                                                + " directories"
1018                                                + " not yet supported");
1019         if (mf.classes().size() < 1)
1020             throw new IllegalArgumentException("At least one module-class"
1021                                                + " directory required");
1022         File classes = mf.classes().get(0);
1023         final String mn = mf.module();
1024 
1025         File mif = new File(classes, "module-info.class");
1026         File src = null;
1027         if (mif.exists()) {
1028             src = classes;
1029         } else {
1030             src = new File(classes, mn);
1031             mif = new File(src, "module-info.class");


1059 
1060         if (false) {
1061 
1062             // ## Retained for now in case we later want to add an option
1063             // ## to install into a tree rather than a zip file
1064 
1065             // Copy class files and build index
1066             final Index ix = new Index(mdst);
1067             Files.copyTree(src, cldst, new Files.Filter<File>() {
1068                     public boolean accept(File f) throws IOException {
1069                         if (f.isDirectory())
1070                             return true;
1071                         if (f.getName().endsWith(".class")) {
1072                             return addToIndex(ClassInfo.read(f), ix);
1073                         } else {
1074                             return true;
1075                         }
1076                     }});
1077             ix.store();
1078         } else {






1079             try (FileOutputStream fos = new FileOutputStream(new File(mdst, "classes"));
1080                  JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(fos)))
1081             {
1082                 // Copy class files and build index
1083                 final Index ix = new Index(mdst);
1084                 Files.storeTree(src, jos, isDeflated(), new Files.Filter<File>() {
1085                         public boolean accept(File f) throws IOException {
1086                             if (f.isDirectory())
1087                                 return true;
1088                             if (f.getName().endsWith(".class")) {
1089                                 return addToIndex(ClassInfo.read(f), ix);
1090                             } else {
1091                                 return true;
1092                             }
1093                         }});




1094                 ix.store();
1095                 copyModuleInfo(dst, mi, bs);
1096             }
1097             if (strip)
1098                 strip(mdst);
1099         }
1100 
1101     }
1102 
1103     private void install(Collection<Manifest> mfs, File dst, boolean strip)
1104         throws IOException
1105     {
1106         for (Manifest mf : mfs)
1107             install(mf, dst, strip);
1108     }
1109 
1110     public void installFromManifests(Collection<Manifest> mfs, boolean strip)
1111         throws ConfigurationException, IOException
1112     {
1113         install(mfs, root, strip);
1114         configure(null);
1115     }
1116 




  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.lang.module.*;
  29 import java.io.*;
  30 import java.net.URI;
  31 import java.nio.file.*;
  32 import java.nio.file.attribute.BasicFileAttributes;
  33 import java.security.*;
  34 import java.security.cert.*;
  35 import java.util.*;
  36 import java.util.jar.*;
  37 import java.util.zip.*;
  38 
  39 import static java.nio.file.StandardCopyOption.*;
  40 
  41 /**
  42  * A simple module library which stores data directly in the filesystem
  43  *
  44  * @see Library
  45  */
  46 
  47 // ## TODO: Move remaining parent-searching logic upward into Library class
  48 
  49 // On-disk library layout
  50 //
  51 //   $LIB/%jigsaw-library
  52 //        com.foo.bar/1.2.3/info (= module-info.class)


 875         throws IOException
 876     {
 877         Object o = findContent(mid);
 878         if (o == null)
 879             return null;
 880         if (o instanceof ZipFile) {
 881             ZipFile zf = (ZipFile)o;
 882             ZipEntry ze = zf.getEntry(path);
 883             if (ze == null)
 884                 return null;
 885             return URI.create("jar:"
 886                               + new File(zf.getName()).toURI().toString()
 887                               + "!/" + path);
 888         }
 889         if (o instanceof File) {
 890             File f = new File((File)o, path);
 891             if (!f.exists())
 892                 return null;
 893             return f.toURI();
 894         }

 895         return null;
 896     }
 897 
 898     public byte[] readLocalClass(ModuleId mid, String className)
 899         throws IOException
 900     {
 901         return loadContent(mid, className.replace('.', '/') + ".class");
 902     }
 903 
 904     public List<String> listLocalClasses(ModuleId mid, boolean all)
 905         throws IOException
 906     {
 907         File md = findModuleDir(mid);
 908         if (md == null)
 909             return null;
 910         Index ix = Index.load(md);
 911         int os = all ? ix.otherClasses().size() : 0;
 912         ArrayList<String> cns
 913             = new ArrayList<String>(ix.publicClasses().size() + os);
 914         cns.addAll(ix.publicClasses());


 992             try (JarFile jf = new JarFile(classes);
 993                 FileOutputStream out = new FileOutputStream(pf))
 994             {
 995                 Pack200.Packer packer = Pack200.newPacker();
 996                 Map<String,String> p = packer.properties();
 997                 p.put("com.sun.java.util.jar.pack.strip.debug", Pack200.Packer.TRUE);
 998                 packer.pack(jf, out);
 999             }
1000 
1001             try (OutputStream out = new FileOutputStream(classes);
1002                  JarOutputStream jos = new JarOutputStream(out))
1003             {
1004                 Pack200.Unpacker unpacker = Pack200.newUnpacker();
1005                 unpacker.unpack(pf, jos);
1006             } finally {
1007                 pf.delete();
1008            }
1009         }
1010     }
1011 
1012     private List<Path> listFiles(Path dir) throws IOException {
1013         final List<Path> files = new ArrayList<>();
1014         java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
1015             @Override
1016             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
1017                     throws IOException {
1018                 if (!file.endsWith("module-info.class")) {
1019                     files.add(file);
1020                 }
1021                 return FileVisitResult.CONTINUE;
1022             }
1023         });
1024         return files;
1025     }
1026 
1027     private void install(Manifest mf, File dst, boolean strip)
1028         throws IOException
1029     {
1030         if (mf.classes().size() > 1)
1031             throw new IllegalArgumentException("Multiple module-class"
1032                                                + " directories"
1033                                                + " not yet supported");
1034         if (mf.classes().size() < 1)
1035             throw new IllegalArgumentException("At least one module-class"
1036                                                + " directory required");
1037         File classes = mf.classes().get(0);
1038         final String mn = mf.module();
1039 
1040         File mif = new File(classes, "module-info.class");
1041         File src = null;
1042         if (mif.exists()) {
1043             src = classes;
1044         } else {
1045             src = new File(classes, mn);
1046             mif = new File(src, "module-info.class");


1074 
1075         if (false) {
1076 
1077             // ## Retained for now in case we later want to add an option
1078             // ## to install into a tree rather than a zip file
1079 
1080             // Copy class files and build index
1081             final Index ix = new Index(mdst);
1082             Files.copyTree(src, cldst, new Files.Filter<File>() {
1083                     public boolean accept(File f) throws IOException {
1084                         if (f.isDirectory())
1085                             return true;
1086                         if (f.getName().endsWith(".class")) {
1087                             return addToIndex(ClassInfo.read(f), ix);
1088                         } else {
1089                             return true;
1090                         }
1091                     }});
1092             ix.store();
1093         } else {
1094             // Copy class/resource files and build index
1095             Index ix = new Index(mdst);
1096             Path srcPath = src.toPath();
1097             List<Path> files = listFiles(srcPath);
1098 
1099             if (!files.isEmpty()) {
1100                 try (FileOutputStream fos = new FileOutputStream(new File(mdst, "classes"));
1101                      JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(fos)))
1102                 {
1103                     boolean deflate = isDeflated();
1104                     for (Path path : files) {
1105                         File file = path.toFile();
1106                         String jp = Files.convertSeparator(srcPath.relativize(path).toString());
1107                         try (OutputStream out = Files.newOutputStream(jos, deflate, jp)) {
1108                             java.nio.file.Files.copy(path, out);




1109                         }
1110                         if (file.getName().endsWith(".class"))
1111                             addToIndex(ClassInfo.read(file), ix);
1112                     }
1113                 }
1114             }
1115             ix.store();
1116             copyModuleInfo(dst, mi, bs);

1117             if (strip)
1118                 strip(mdst);
1119         }
1120 
1121     }
1122 
1123     private void install(Collection<Manifest> mfs, File dst, boolean strip)
1124         throws IOException
1125     {
1126         for (Manifest mf : mfs)
1127             install(mf, dst, strip);
1128     }
1129 
1130     public void installFromManifests(Collection<Manifest> mfs, boolean strip)
1131         throws ConfigurationException, IOException
1132     {
1133         install(mfs, root, strip);
1134         configure(null);
1135     }
1136