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             {
1019                 if (!file.endsWith("module-info.class"))
1020                     files.add(file);
1021 
1022                 return FileVisitResult.CONTINUE;
1023             }
1024         });
1025         return files;
1026     }
1027 
1028     private void install(Manifest mf, File dst, boolean strip)
1029         throws IOException
1030     {
1031         if (mf.classes().size() > 1)
1032             throw new IllegalArgumentException("Multiple module-class"
1033                                                + " directories"
1034                                                + " not yet supported");
1035         if (mf.classes().size() < 1)
1036             throw new IllegalArgumentException("At least one module-class"
1037                                                + " directory required");
1038         File classes = mf.classes().get(0);
1039         final String mn = mf.module();
1040 
1041         File mif = new File(classes, "module-info.class");
1042         File src = null;
1043         if (mif.exists()) {
1044             src = classes;
1045         } else {
1046             src = new File(classes, mn);
1047             mif = new File(src, "module-info.class");


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




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

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