1 /* 2 * Copyright (c) 2016, 2018, 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 jdk.jpackager.internal; 27 28 import java.io.File; 29 import java.nio.file.Path; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.EnumSet; 33 import java.util.List; 34 35 public final class ModuleManager { 36 private final List<String> folders = new ArrayList<String>(); 37 38 public enum SearchType {UnnamedJar, ModularJar, Jmod, ExplodedModule} 39 40 public ModuleManager(String folders) { 41 super(); 42 String lfolders = folders.replaceAll("^\"|\"$", ""); 43 List<Path> paths = new ArrayList<Path>(); 44 45 for (String folder : 46 Arrays.asList(lfolders.split(File.pathSeparator))) { 47 File file = new File(folder); 48 paths.add(file.toPath()); 49 } 50 51 initialize(paths); 52 } 53 54 public ModuleManager(List<Path> Paths) { 55 super(); 56 initialize(Paths); 57 } 58 59 private void initialize(List<Path> Paths) { 60 for (Path path : Paths) { 61 folders.add(path.toString().replaceAll("^\"|\"$", "")); 62 } 63 } 64 65 public List<ModFile> getModules() { 66 return getModules(EnumSet.of(SearchType.UnnamedJar, 67 SearchType.ModularJar, SearchType.Jmod, 68 SearchType.ExplodedModule)); 69 } 70 71 public List<ModFile> getModules(EnumSet<SearchType> Search) { 72 List<ModFile> result = new ArrayList<ModFile>(); 73 74 for (String folder : folders) { 75 result.addAll(getAllModulesInDirectory(folder, Search)); 76 } 77 78 return result; 79 } 80 81 private static List<ModFile> getAllModulesInDirectory(String Folder, 82 EnumSet<SearchType> Search) { 83 List<ModFile> result = new ArrayList<ModFile>(); 84 File lfolder = new File(Folder); 85 File[] files = lfolder.listFiles(); 86 87 for (File file : files) { 88 ModFile modFile = new ModFile(file); 89 90 switch (modFile.getModType()) { 91 case Unknown: 92 break; 93 case UnnamedJar: 94 if (Search.contains(SearchType.UnnamedJar)) { 95 result.add(modFile); 96 } 97 break; 98 case ModularJar: 99 if (Search.contains(SearchType.ModularJar)) { 100 result.add(modFile); 101 } 102 break; 103 case Jmod: 104 if (Search.contains(SearchType.Jmod)) { 105 result.add(modFile); 106 } 107 break; 108 case ExplodedModule: 109 if (Search.contains(SearchType.ExplodedModule)) { 110 result.add(modFile); 111 } 112 break; 113 } 114 } 115 116 return result; 117 } 118 }