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