1 /*
   2  * Copyright (c) 2011, 2013, 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 com.oracle.ipack.packer;
  27 
  28 import com.oracle.ipack.resources.CodeResources;
  29 import com.oracle.ipack.resources.ResourceRules;
  30 import com.oracle.ipack.util.DataCopier;
  31 import com.oracle.ipack.util.HashingOutputStream;
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.util.List;
  35 import java.util.zip.ZipEntry;
  36 import java.util.zip.ZipOutputStream;
  37 
  38 final class ResourcePacker {
  39     private final ZipOutputStream zipStream;
  40     private final File baseDir;
  41     private final String appPath;
  42 
  43     private final DataCopier dataCopier;
  44     private final ResourceRules resourceRules;
  45     private final HashingOutputStream dataStream;
  46 
  47     private byte[] codeResourcesHash;
  48     private byte[] infoPlistHash;
  49 
  50     ResourcePacker(final ZipOutputStream zipStream,
  51                    final File baseDir,
  52                    final String appPath,
  53                    final String appName) {
  54         this.zipStream = zipStream;
  55         this.baseDir = baseDir;
  56         this.appPath = appPath;
  57 
  58         dataCopier = new DataCopier();
  59         dataStream = new HashingOutputStream(zipStream);
  60 
  61         resourceRules = new ResourceRules();
  62         resourceRules.addExclude(appName, -1);
  63         resourceRules.addExclude("_CodeSignature", -1);
  64         resourceRules.addExclude("Info.plist", 10);
  65         resourceRules.addExclude("ResourceRules.plist", 100);
  66     }
  67 
  68     void execute() throws IOException {
  69         final CodeResources codeResources = new CodeResources(resourceRules);
  70 
  71         storeResourceFiles(codeResources);
  72         codeResourcesHash = storeCodeResources(codeResources);
  73 
  74         final String infoPlistName = appPath + "Info.plist";
  75         infoPlistHash =
  76                 storeFileEntry(infoPlistName, new File(baseDir, infoPlistName));
  77     }
  78 
  79     byte[] getCodeResourcesHash() {
  80         return codeResourcesHash;
  81     }
  82 
  83     byte[] getInfoPlistHash() {
  84         return infoPlistHash;
  85     }
  86 
  87     private void storeResourceFiles(final CodeResources codeResources)
  88             throws IOException {
  89         final List<String> resources =
  90                 resourceRules.collectResources(
  91                         new File(baseDir, appPath));
  92 
  93         for (final String resourceName: resources) {
  94             final String fullResourceName =
  95                     appPath + resourceName;
  96             if (resourceName.endsWith("/")) {
  97                 storeDirEntry(fullResourceName);
  98                 continue;
  99             }
 100 
 101             final byte[] resourceHash =
 102                     storeFileEntry(fullResourceName,
 103                                    new File(baseDir, fullResourceName));
 104             codeResources.addHashedResource(resourceName, resourceHash);
 105         }
 106     }
 107 
 108     private byte[] storeCodeResources(final CodeResources codeResources)
 109             throws IOException {
 110         storeDirEntry(appPath + "_CodeSignature/");
 111 
 112         final String codeResourcesName =
 113                 appPath + "_CodeSignature/CodeResources";
 114 
 115         System.out.println("Adding " + codeResourcesName);
 116         zipStream.putNextEntry(new ZipEntry(codeResourcesName));
 117         try {
 118             codeResources.write(dataStream);
 119         } finally {
 120             dataStream.flush();
 121             zipStream.closeEntry();
 122         }
 123 
 124         return dataStream.calculateHash();
 125     }
 126 
 127     private void storeDirEntry(final String entryName) throws IOException {
 128         zipStream.putNextEntry(new ZipEntry(entryName));
 129         zipStream.closeEntry();
 130     }
 131 
 132     private byte[] storeFileEntry(final String entryName,
 133                                   final File file) throws IOException {
 134         System.out.println("Adding " + entryName);
 135         zipStream.putNextEntry(new ZipEntry(entryName));
 136         try {
 137             dataCopier.copyFile(dataStream, file);
 138         } finally {
 139             dataStream.flush();
 140             zipStream.closeEntry();
 141         }
 142 
 143         return dataStream.calculateHash();
 144     }
 145 }