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