1 /*
   2  * Copyright (c) 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.tools.jaotc.collect;
  25 
  26 import jdk.tools.jaotc.LoadedClass;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 public final class ClassSearch {
  32     private final List<SourceProvider> providers = new ArrayList<>();
  33 
  34     public void addProvider(SourceProvider provider) {
  35         providers.add(provider);
  36     }
  37 
  38     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath) {
  39         List<LoadedClass> loaded = new ArrayList<>();
  40 
  41         List<ClassSource> sources = new ArrayList<>();
  42 
  43         for (SearchFor entry : search) {
  44             sources.add(findSource(entry, searchPath));
  45         }
  46 
  47         for (ClassSource source : sources) {
  48             source.eachClass((name, loader) -> loaded.add(loadClass(name, loader)));
  49         }
  50 
  51         return loaded;
  52     }
  53 
  54     private static LoadedClass loadClass(String name, ClassLoader loader) {
  55         try {
  56             Class<?> clzz = loader.loadClass(name);
  57             return new LoadedClass(name, clzz);
  58         } catch (ClassNotFoundException e) {
  59             throw new InternalError("Failed to load with: " + loader, e);
  60         }
  61     }
  62 
  63     private ClassSource findSource(SearchFor searchFor, SearchPath searchPath) {
  64         ClassSource found = null;
  65 
  66         for (SourceProvider provider : providers) {
  67             if (!searchFor.isUnknown() && !provider.supports(searchFor.getType())) {
  68                 continue;
  69             }
  70 
  71             ClassSource source = provider.findSource(searchFor.getName(), searchPath);
  72             if (source != null) {
  73                 if (found != null) {
  74                     throw new InternalError("Multiple possible sources: " + source + " and: " + found);
  75                 }
  76                 found = source;
  77             }
  78         }
  79 
  80         if (found == null) {
  81             throw new InternalError("Failed to find " + searchFor.getType() + " file: " + searchFor.getName());
  82         }
  83         return found;
  84     }
  85 
  86     public static List<SearchFor> makeList(String type, String argument) {
  87         List<SearchFor> list = new ArrayList<>();
  88         String[] elements = argument.split(":");
  89         for (String element : elements) {
  90             list.add(new SearchFor(element, type));
  91         }
  92         return list;
  93     }
  94 }