src/jdk.packager/share/classes/jdk/packager/internal/ModuleManager.java
branchJDK-8200758-branch
changeset 56821 565d54ca1f41
child 56869 41e17fe9fbeb
equal deleted inserted replaced
56820:b763f6492df9 56821:565d54ca1f41
       
     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.packager.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 
       
    36 public final class ModuleManager {
       
    37     private final List<String> folders = new ArrayList();
       
    38 
       
    39     public enum SearchType {UnnamedJar, ModularJar, Jmod, ExplodedModule}
       
    40 
       
    41     public ModuleManager(String folders) {
       
    42         super();
       
    43         String lfolders = folders.replaceAll("^\"|\"$", "");
       
    44         List<Path> paths = new ArrayList();
       
    45 
       
    46         for (String folder : 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<Module> getModules() {
       
    66         return getModules(EnumSet.of(SearchType.UnnamedJar,
       
    67                 SearchType.ModularJar, SearchType.Jmod, SearchType.ExplodedModule));
       
    68     }
       
    69 
       
    70     public List<Module> getModules(EnumSet<SearchType> Search) {
       
    71         List<Module> result = new ArrayList();
       
    72 
       
    73         for (String folder : folders) {
       
    74             result.addAll(getAllModulesInDirectory(folder, Search));
       
    75         }
       
    76 
       
    77         return result;
       
    78     }
       
    79 
       
    80     private static List<Module> getAllModulesInDirectory(String Folder,
       
    81             EnumSet<SearchType> Search) {
       
    82         List<Module> result = new ArrayList();
       
    83         File lfolder = new File(Folder);
       
    84         File[] files = lfolder.listFiles();
       
    85 
       
    86         for (File file : files) {
       
    87             Module module = new Module(file);
       
    88 
       
    89             switch (module.getModuleType()) {
       
    90                 case Unknown:
       
    91                     break;
       
    92                 case UnnamedJar:
       
    93                     if (Search.contains(SearchType.UnnamedJar)) {
       
    94                         result.add(module);
       
    95                     }
       
    96                     break;
       
    97                 case ModularJar:
       
    98                     if (Search.contains(SearchType.ModularJar)) {
       
    99                         result.add(module);
       
   100                     }
       
   101                     break;
       
   102                 case Jmod:
       
   103                     if (Search.contains(SearchType.Jmod)) {
       
   104                         result.add(module);
       
   105                     }
       
   106                     break;
       
   107                 case ExplodedModule:
       
   108                     if (Search.contains(SearchType.ExplodedModule)) {
       
   109                         result.add(module);
       
   110                     }
       
   111                     break;
       
   112             }
       
   113         }
       
   114 
       
   115         return result;
       
   116     }
       
   117 }