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