1 /*
   2  * Copyright (c) 2014, 2019, 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.internal.module;
  27 
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.module.ModuleDescriptor.Provides;
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 import java.util.Map;
  33 import java.util.Objects;
  34 import java.util.concurrent.ConcurrentHashMap;
  35 import java.util.concurrent.CopyOnWriteArrayList;
  36 
  37 import jdk.internal.loader.ClassLoaderValue;
  38 
  39 /**
  40  * A <em>services catalog</em>. Each {@code ClassLoader} and {@code Layer} has
  41  * an optional {@code ServicesCatalog} for modules that provide services.
  42  *
  43  * @apiNote This class will be replaced once the ServiceLoader is further
  44  *          specified
  45  */
  46 public final class ServicesCatalog {
  47 
  48     /**
  49      * Represents a service provider in the services catalog.
  50      */
  51     public final class ServiceProvider {
  52         private final Module module;
  53         private final String providerName;
  54 
  55         public ServiceProvider(Module module, String providerName) {
  56             this.module = module;
  57             this.providerName = providerName;
  58         }
  59 
  60         public Module module() {
  61             return module;
  62         }
  63 
  64         public String providerName() {
  65             return providerName;
  66         }
  67 
  68         @Override
  69         public int hashCode() {
  70             return Objects.hash(module, providerName);
  71         }
  72 
  73         @Override
  74         public boolean equals(Object ob) {
  75             if (!(ob instanceof ServiceProvider))
  76                 return false;
  77             ServiceProvider that = (ServiceProvider)ob;
  78             return Objects.equals(this.module, that.module)
  79                     && Objects.equals(this.providerName, that.providerName);
  80         }
  81     }
  82 
  83     // service name -> list of providers
  84     private final Map<String, List<ServiceProvider>> map = new ConcurrentHashMap<>();
  85 
  86     private ServicesCatalog() { }
  87 
  88     /**
  89      * Creates a ServicesCatalog that supports concurrent registration
  90      * and lookup
  91      */
  92     public static ServicesCatalog create() {
  93         return new ServicesCatalog();
  94     }
  95 
  96     /**
  97      * Returns the list of service provides for the given service type
  98      * name, creating it if needed.
  99      */
 100     private List<ServiceProvider> providers(String service) {
 101         // avoid computeIfAbsent here
 102         List<ServiceProvider> list = map.get(service);
 103         if (list == null) {
 104             list = new CopyOnWriteArrayList<>();
 105             List<ServiceProvider> prev = map.putIfAbsent(service, list);
 106             if (prev != null)
 107                 list = prev;  // someone else got there
 108         }
 109         return list;
 110     }
 111 
 112     /**
 113      * Registers the providers in the given module in this services catalog.
 114      */
 115     public void register(Module module) {
 116         ModuleDescriptor descriptor = module.getDescriptor();
 117         for (Provides provides : descriptor.provides()) {
 118             String service = provides.service();
 119             List<String> providerNames = provides.providers();
 120             int count = providerNames.size();
 121             if (count == 1) {
 122                 String pn = providerNames.get(0);
 123                 providers(service).add(new ServiceProvider(module, pn));
 124             } else {
 125                 List<ServiceProvider> list = new ArrayList<>(count);
 126                 for (String pn : providerNames) {
 127                     list.add(new ServiceProvider(module, pn));
 128                 }
 129                 providers(service).addAll(list);
 130             }
 131         }
 132     }
 133 
 134     /**
 135      * Add a provider in the given module to this services catalog
 136      *
 137      * @apiNote This method is for use by java.lang.instrument
 138      */
 139     public void addProvider(Module module, Class<?> service, Class<?> impl) {
 140         List<ServiceProvider> list = providers(service.getName());
 141         list.add(new ServiceProvider(module, impl.getName()));
 142     }
 143 
 144     /**
 145      * Returns the (possibly empty) list of service providers that implement
 146      * the given service type.
 147      */
 148     public List<ServiceProvider> findServices(String service) {
 149         return map.getOrDefault(service, List.of());
 150     }
 151 
 152     /**
 153      * Returns the ServicesCatalog for the given class loader or {@code null}
 154      * if there is none.
 155      */
 156     public static ServicesCatalog getServicesCatalogOrNull(ClassLoader loader) {
 157         return CLV.get(loader);
 158     }
 159 
 160     /**
 161      * Returns the ServicesCatalog for the given class loader, creating it if
 162      * needed.
 163      */
 164     public static ServicesCatalog getServicesCatalog(ClassLoader loader) {
 165         // CLV.computeIfAbsent(loader, (cl, clv) -> create());
 166         ServicesCatalog catalog = CLV.get(loader);
 167         if (catalog == null) {
 168             catalog = create();
 169             ServicesCatalog previous = CLV.putIfAbsent(loader, catalog);
 170             if (previous != null) catalog = previous;
 171         }
 172         return catalog;
 173     }
 174 
 175     // the ServicesCatalog registered to a class loader
 176     private static final ClassLoaderValue<ServicesCatalog> CLV = new ClassLoaderValue<>();
 177 }