1 /*
   2  * Copyright (c) 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.testlibrary;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.nio.file.Path;
  32 import java.util.ArrayList;
  33 import java.util.Enumeration;
  34 import java.util.List;
  35 import java.util.jar.JarEntry;
  36 import java.util.jar.JarFile;
  37 import java.util.jar.JarOutputStream;
  38 import java.util.jar.Manifest;
  39 
  40 /**
  41  * Common library for various test jar file utility functions.
  42  */
  43 public final class JarUtils {
  44 
  45 
  46     /**
  47      * Create jar file with specified files. If a specified file does not exist,
  48      * a new jar entry will be created with the file name itself as the content.
  49      */
  50     public static void createJar(String dest, Path filesLocation,
  51                                  String... fileNames) throws IOException {
  52         try (JarOutputStream jos = new JarOutputStream(
  53                 new FileOutputStream(dest), new Manifest())) {
  54             for (String fileName : fileNames) {
  55                 System.out.println(String.format("Adding %s to %s",
  56                         fileName, dest));
  57 
  58                 // add an archive entry, and write a file
  59                 jos.putNextEntry(new JarEntry(fileName));
  60                 File file;
  61                 if (filesLocation != null) {
  62                     file = filesLocation.resolve(fileName).toFile();
  63                 } else {
  64                     file = new File(fileName);
  65                 }
  66                 try (FileInputStream fis = new FileInputStream(file)) {
  67                     Utils.transferBetweenStreams(fis, jos);
  68                 } catch (FileNotFoundException e) {
  69                     jos.write(fileName.getBytes());
  70                 }
  71             }
  72         }
  73         System.out.println();
  74     }
  75 
  76     /**
  77      * Create jar file with specified files from current directory.
  78      */
  79     public static void createJar(String dest, String... files)
  80             throws IOException {
  81         createJar(dest, null, files);
  82     }
  83 
  84     /**
  85      * Add or remove specified files to existing jar file. If a specified file
  86      * to be updated or added does not exist, the jar entry will be created
  87      * with the file name itself as the content.
  88      *
  89      * @param src the original jar file name
  90      * @param dest the new jar file name
  91      * @param files the files to update. The list is broken into 2 groups
  92      *              by a "-" string. The files before in the 1st group will
  93      *              be either updated or added. The files in the 2nd group
  94      *              will be removed. If no "-" exists, all files belong to
  95      *              the 1st group.
  96      */
  97     public static void updateJar(String src, String dest, String... files)
  98             throws IOException {
  99         try (JarOutputStream jos = new JarOutputStream(
 100                 new FileOutputStream(dest))) {
 101 
 102             // copy each old entry into destination unless the entry name
 103             // is in the updated list
 104             List<String> updatedFiles = new ArrayList<>();
 105             try (JarFile srcJarFile = new JarFile(src)) {
 106                 Enumeration<JarEntry> entries = srcJarFile.entries();
 107                 while (entries.hasMoreElements()) {
 108                     JarEntry entry = entries.nextElement();
 109                     String name = entry.getName();
 110                     boolean found = false;
 111                     boolean update = true;
 112                     for (String file : files) {
 113                         if (file.equals("-")) {
 114                             update = false;
 115                         } else if (name.equals(file)) {
 116                             updatedFiles.add(file);
 117                             found = true;
 118                             break;
 119                         }
 120                     }
 121 
 122                     if (found) {
 123                         if (update) {
 124                         System.out.println(String.format("Updating %s with %s",
 125                                 dest, name));
 126                         jos.putNextEntry(new JarEntry(name));
 127                         try (FileInputStream fis = new FileInputStream(name)) {
 128                             Utils.transferBetweenStreams(fis, jos);
 129                             } catch (FileNotFoundException e) {
 130                                 jos.write(name.getBytes());
 131                             }
 132                         } else {
 133                             System.out.println(String.format("Removing %s from %s",
 134                                     name, dest));
 135                         }
 136                     } else {
 137                         System.out.println(String.format("Copying %s to %s",
 138                                 name, dest));
 139                         jos.putNextEntry(entry);
 140                         Utils.transferBetweenStreams(srcJarFile.
 141                                 getInputStream(entry), jos);
 142                     }
 143                 }
 144             }
 145 
 146             // append new files
 147             for (String file : files) {
 148                 if (file.equals("-")) {
 149                     break;
 150                 }
 151                 if (!updatedFiles.contains(file)) {
 152                     System.out.println(String.format("Adding %s with %s",
 153                             dest, file));
 154                     jos.putNextEntry(new JarEntry(file));
 155                     try (FileInputStream fis = new FileInputStream(file)) {
 156                         Utils.transferBetweenStreams(fis, jos);
 157                     } catch (FileNotFoundException e) {
 158                         jos.write(file.getBytes());
 159                     }
 160                 }
 161             }
 162         }
 163         System.out.println();
 164     }