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