1 /*
   2  * Copyright (c) 2014, 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 jdk.internal.jimage;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.UncheckedIOException;
  31 import java.nio.file.FileAlreadyExistsException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.util.function.Consumer;
  35 import jdk.internal.jimage.Archive.Entry;
  36 
  37 /**
  38  * A Consumer suitable for processing non resources Archive Entry and writing it to the
  39  * appropriate location.
  40  */
  41 class ExternalFilesWriter implements Consumer<Entry> {
  42     private final Path root;
  43 
  44     ExternalFilesWriter(Path root) {
  45         this.root = root;
  46     }
  47 
  48     @Override
  49     public void accept(Entry entry) {
  50         String name = entry.path();
  51         try {
  52             String filename = entry.path();
  53             try (InputStream in = entry.stream()) {
  54                 switch (entry.type()) {
  55                     case NATIVE_LIB:
  56                         writeEntry(in, destFile(nativeDir(filename), filename));
  57                         break;
  58                     case NATIVE_CMD:
  59                         Path path = destFile("bin", filename);
  60                         writeEntry(in, path);
  61                         path.toFile().setExecutable(true);
  62                         break;
  63                     case CONFIG:
  64                         writeEntry(in, destFile("conf", filename));
  65                         break;
  66                     case MODULE_NAME:
  67                         // skip
  68                         break;
  69                     case SERVICE:
  70                         //throw new UnsupportedOperationException(name + " in " + zipfile.toString()); //TODO
  71                         throw new UnsupportedOperationException(name + " in " + name);
  72                     default:
  73                         //throw new InternalError("unexpected entry: " + name + " " + zipfile.toString()); //TODO
  74                         throw new InternalError("unexpected entry: " + name + " " + name);
  75                 }
  76             }
  77         } catch (FileAlreadyExistsException x) {
  78             System.err.println("File already exists (skipped) " + name);
  79         } catch (IOException x) {
  80             throw new UncheckedIOException(x);
  81         }
  82     }
  83 
  84     private Path destFile(String dir, String filename) {
  85         return root.resolve(dir).resolve(filename);
  86     }
  87 
  88     private void writeEntry(InputStream in, Path dstFile) throws IOException {
  89         Files.createDirectories(dstFile.getParent());
  90         Files.copy(in, dstFile);
  91     }
  92 
  93     private static String nativeDir(String filename) {
  94         if (System.getProperty("os.name").startsWith("Windows")) {
  95             if (filename.endsWith(".dll") || filename.endsWith(".diz")
  96                 || filename.endsWith(".pdb") || filename.endsWith(".map")) {
  97                 return "bin";
  98             } else {
  99                 return "lib";
 100             }
 101         } else {
 102             return "lib";
 103         }
 104     }
 105 }