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 import java.util.HashSet; 30 import java.util.Set; 31 32 /** 33 * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead. 34 */ 35 @Deprecated(since="10", forRemoval=true) 36 public class JreUtils { 37 38 public static class Rule { 39 String regex; 40 boolean includeRule; 41 Type type; 42 enum Type {SUFFIX, PREFIX, SUBSTR, REGEX} 43 44 private Rule(String regex, boolean includeRule, Type type) { 45 this.regex = regex; 46 this.type = type; 47 this.includeRule = includeRule; 48 } 49 50 boolean match(String str) { 51 if (type == Type.SUFFIX) { 52 return str.endsWith(regex); 53 } 54 if (type == Type.PREFIX) { 55 return str.startsWith(regex); 56 } 57 if (type == Type.SUBSTR) { 58 return str.contains(regex); 59 } 60 return str.matches(regex); 61 } 62 63 boolean treatAsAccept() {return includeRule;} 64 65 public static Rule suffix(String s) { 66 return new Rule(s, true, Type.SUFFIX); 67 } 68 public static Rule suffixNeg(String s) { 69 return new Rule(s, false, Type.SUFFIX); 70 } 71 static Rule prefix(String s) { 72 return new Rule(s, true, Type.PREFIX); 73 } 74 public static Rule prefixNeg(String s) { 75 return new Rule(s, false, Type.PREFIX); 76 } 77 static Rule substr(String s) { 78 return new Rule(s, true, Type.SUBSTR); 79 } 80 public static Rule substrNeg(String s) { 81 return new Rule(s, false, Type.SUBSTR); 82 } 83 } 84 85 public static boolean shouldExclude(File baseDir, File f, Rule ruleset[]) { 86 if (ruleset == null) { 87 return false; 88 } 89 90 String fname = f.getAbsolutePath().toLowerCase().substring( 91 baseDir.getAbsolutePath().length()); 92 //first rule match defines the answer 93 for (Rule r: ruleset) { 94 if (r.match(fname)) { 95 return !r.treatAsAccept(); 96 } 97 } 98 //default is include 99 return false; 100 } 101 102 public static void walk(File base, File root, Rule ruleset[], Set<File> files) { 103 walk(base, root, ruleset, files, false); 104 } 105 106 public static void walk(File base, File root, Rule ruleset[], Set<File> files, boolean acceptSymlinks) { 107 if (!root.isDirectory()) { 108 if (root.isFile()) { 109 files.add(root); 110 } 111 return; 112 } 113 114 File[] lst = root.listFiles(); 115 if (lst != null) { 116 for (File f : lst) { 117 if ((acceptSymlinks || IOUtils.isNotSymbolicLink(f)) && !shouldExclude(base, f, ruleset)) { 118 if (f.isDirectory()) { 119 walk(base, f, ruleset, files, acceptSymlinks); 120 } else if (f.isFile()) { 121 //add to list 122 files.add(f); 123 } 124 } 125 } 126 } 127 } 128 129 public static RelativeFileSet extractJreAsRelativeFileSet(String root, JreUtils.Rule[] ruleset) { 130 return extractJreAsRelativeFileSet(root, ruleset, false); 131 } 132 133 public static RelativeFileSet extractJreAsRelativeFileSet(String root, JreUtils.Rule[] ruleset, boolean acceptSymlinks) { 134 if (root.isEmpty()) return null; 135 136 File baseDir = new File(root); 137 138 Set<File> lst = new HashSet<>(); 139 140 walk(baseDir, baseDir, ruleset, lst, acceptSymlinks); 141 142 return new RelativeFileSet(baseDir, lst); 143 } 144 145 }