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