1 /*
   2  * Copyright (c) 2012, 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.  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.tools.packager;
  27 
  28 import java.io.File;
  29 
  30 import java.util.Collection;
  31 import java.util.LinkedHashSet;
  32 import java.util.Set;
  33 
  34 public class RelativeFileSet {
  35 
  36     public enum Type {
  37         UNKNOWN, jnlp, jar, nativelib, icon, license, data
  38     }
  39 
  40     private Type type = Type.UNKNOWN;
  41     private String mode;
  42     private String os;
  43     private String arch;
  44 
  45     private File basedir;
  46     private Set<String> files = new LinkedHashSet<>();
  47 
  48     public RelativeFileSet(RelativeFileSet copy) {
  49         type = copy.type;
  50         mode = copy.mode;
  51         os = copy.os;
  52         arch = copy.arch;
  53         basedir = copy.basedir;
  54         files = new LinkedHashSet<>(copy.files);
  55     }
  56 
  57     public RelativeFileSet(File base, Collection<File> files) {
  58         basedir = base;
  59         String baseAbsolute = basedir.getAbsolutePath();
  60         for (File f: files) {
  61             String absolute = f.getAbsolutePath();
  62             if (!absolute.startsWith(baseAbsolute)) {
  63                 throw new RuntimeException("File " + f.getAbsolutePath() +
  64                         " does not belong to " + baseAbsolute);
  65             }
  66             if (!absolute.equals(baseAbsolute)) { //possible in javapackager case
  67                 this.files.add(absolute.substring(baseAbsolute.length()+1));
  68             }
  69         }
  70     }
  71 
  72     public void upshift() {
  73         String root = basedir.getName();
  74         basedir = basedir.getParentFile();
  75         Set<String> newFiles = new LinkedHashSet<>();
  76         for (String s : files) {
  77             newFiles.add(root + File.separator + s);
  78         }
  79         files = newFiles;
  80     }
  81 
  82     public RelativeFileSet(File base, Set<File> files) {
  83         this(base, (Collection<File>) files);
  84     }
  85 
  86     public boolean contains(String[] requiredFiles) {
  87         boolean result = true;
  88 
  89         for(String fname: requiredFiles) {
  90             if (!files.contains(fname)) {
  91                 Log.debug("  Runtime does not contain [" + fname + "]");
  92                 result = false;
  93             }
  94         }
  95 
  96         return result;
  97     }
  98 
  99     public boolean contains(String requiredFile) {
 100         if (files.contains(requiredFile)) {
 101             return true;
 102         } else {
 103             Log.debug("  Runtime does not contain [" + requiredFile + "]");
 104             return false;
 105         }
 106     }
 107 
 108     public File getBaseDirectory() {
 109         return basedir;
 110     }
 111 
 112     public Set<String> getIncludedFiles() {
 113         return files;
 114     }
 115 
 116     public void dump() {
 117         Log.verbose("\n=========\nBasedir: " + basedir + "\n");
 118         for (String fname : files) {
 119             Log.verbose("  " + fname);
 120         }
 121         Log.verbose("\n========");
 122     }
 123 
 124     public Type getType() {
 125         return type;
 126     }
 127 
 128     public void setType(Type type) {
 129         this.type = type;
 130     }
 131 
 132     public String getMode() {
 133         return mode;
 134     }
 135 
 136     public void setMode(String mode) {
 137         this.mode = mode;
 138     }
 139 
 140     public String getOs() {
 141         return os;
 142     }
 143 
 144     public void setOs(String os) {
 145         this.os = os;
 146     }
 147 
 148     public String getArch() {
 149         return arch;
 150     }
 151 
 152     public void setArch(String arch) {
 153         this.arch = arch;
 154     }
 155 
 156     @Override
 157     public String toString() {
 158         return "RelativeFileSet{basedir:" + basedir + ", files:" + files + "}";
 159     }
 160 
 161 }