1 /*
   2  * Copyright (c) 2017, 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.
   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 
  25 
  26 package jdk.tools.jaotc.collect;
  27 
  28 import jdk.tools.jaotc.LoadedClass;
  29 
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 import java.util.function.BiConsumer;
  33 
  34 public final class ClassSearch {
  35     private final List<SourceProvider> providers = new ArrayList<>();
  36 
  37     public void addProvider(SourceProvider provider) {
  38         providers.add(provider);
  39     }
  40 
  41     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath) {
  42         return search(search, searchPath, (s, t) -> {
  43             throw new InternalError(s + " : " + t, t);
  44         });
  45     }
  46 
  47     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  48         List<LoadedClass> loaded = new ArrayList<>();
  49 
  50         List<ClassSource> sources = new ArrayList<>();
  51 
  52         for (SearchFor entry : search) {
  53             sources.add(findSource(entry, searchPath, classLoadingErrorsHandler));
  54         }
  55 
  56         for (ClassSource source : sources) {
  57             if (source != null) {
  58                 source.eachClass((name, loader) -> {
  59                     LoadedClass x = loadClass(name, loader, classLoadingErrorsHandler);
  60                     if (x != null) {
  61                         loaded.add(x);
  62                     }
  63                 });
  64             }
  65         }
  66 
  67         return loaded;
  68     }
  69 
  70     private static LoadedClass loadClass(String name, ClassLoader loader, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  71         try {
  72             Class<?> clzz = loader.loadClass(name);
  73             return new LoadedClass(name, clzz);
  74         } catch (Throwable e) {
  75             classLoadingErrorsHandler.accept(name, e);
  76             return null;
  77         }
  78     }
  79 
  80     private ClassSource findSource(SearchFor searchFor, SearchPath searchPath, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  81         ClassSource found = null;
  82 
  83         for (SourceProvider provider : providers) {
  84             if (!searchFor.isUnknown() && !provider.supports(searchFor.getType())) {
  85                 continue;
  86             }
  87             ClassSource source = null;
  88             try {
  89                 source = provider.findSource(searchFor.getName(), searchPath);
  90             } catch (Throwable e) {
  91                 classLoadingErrorsHandler.accept(searchFor.getName(), e);
  92             }
  93             if (source != null) {
  94                 if (found != null) {
  95                     throw new InternalError("Multiple possible sources: " + source + " and: " + found);
  96                 }
  97                 found = source;
  98             }
  99         }
 100 
 101         if (found == null) {
 102             classLoadingErrorsHandler.accept(searchFor.getName(), new InternalError("Failed to find " + searchFor.getType() + " file: " + searchFor.getName()));
 103         }
 104         return found;
 105     }
 106 
 107     public static List<SearchFor> makeList(String type, String argument) {
 108         List<SearchFor> list = new ArrayList<>();
 109         String[] elements = argument.split(":");
 110         for (String element : elements) {
 111             list.add(new SearchFor(element, type));
 112         }
 113         return list;
 114     }
 115 }