1 /*
   2  * Copyright (c) 2016, 2019, 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.jpackage.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 final class ModuleManager {
  36     private final List<String> folders = new ArrayList<String>();
  37 
  38     enum SearchType {UnnamedJar, ModularJar, Jmod, ExplodedModule}
  39 
  40     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     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     List<ModFile> getModules() {
  66         return getModules(EnumSet.of(SearchType.UnnamedJar,
  67                 SearchType.ModularJar, SearchType.Jmod,
  68                 SearchType.ExplodedModule));
  69     }
  70 
  71     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 };
  86         if (lfolder.isDirectory()) {
  87             files = lfolder.listFiles();
  88         }
  89 
  90         if (files != null) {
  91             for (File file : files) {
  92                 ModFile modFile = new ModFile(file);
  93 
  94                 switch (modFile.getModType()) {
  95                     case Unknown:
  96                         break;
  97                     case UnnamedJar:
  98                         if (Search.contains(SearchType.UnnamedJar)) {
  99                             result.add(modFile);
 100                         }
 101                         break;
 102                     case ModularJar:
 103                         if (Search.contains(SearchType.ModularJar)) {
 104                             result.add(modFile);
 105                         }
 106                         break;
 107                     case Jmod:
 108                         if (Search.contains(SearchType.Jmod)) {
 109                             result.add(modFile);
 110                         }
 111                         break;
 112                     case ExplodedModule:
 113                         if (Search.contains(SearchType.ExplodedModule)) {
 114                             result.add(modFile);
 115                         }
 116                         break;
 117                 }
 118             }
 119         }
 120         return result;
 121     }
 122 }