1 /*
   2  * Copyright (c) 2014, 2020, 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.Arrays;
  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<>(32);
  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      * Adds service providers for the given service type.
  98      */
  99     private void addProviders(String service, ServiceProvider ... providers) {
 100         List<ServiceProvider> list = map.get(service);
 101         if (list == null) {
 102             list = new CopyOnWriteArrayList<>(providers);
 103             List<ServiceProvider> prev = map.putIfAbsent(service, list);
 104             if (prev != null) {
 105                 // someone else got there
 106                 prev.addAll(list);
 107             }
 108         } else {
 109             if (providers.length == 1) {
 110                 list.add(providers[0]);
 111             } else {
 112                 list.addAll(Arrays.asList(providers));
 113             }
 114         }
 115     }
 116 
 117     /**
 118      * Registers the providers in the given module in this services catalog.
 119      */
 120     public void register(Module module) {
 121         ModuleDescriptor descriptor = module.getDescriptor();
 122         for (Provides provides : descriptor.provides()) {
 123             String service = provides.service();
 124             List<String> providerNames = provides.providers();
 125             int count = providerNames.size();
 126             ServiceProvider[] providers = new ServiceProvider[count];
 127             for (int i = 0; i < count; i++) {
 128                 providers[i] = new ServiceProvider(module, providerNames.get(i));
 129             }
 130             addProviders(service, providers);
 131         }
 132     }
 133 
 134     /**
 135      * Adds 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         addProviders(service.getName(), new ServiceProvider(module, impl.getName()));
 141     }
 142 
 143     /**
 144      * Returns the (possibly empty) list of service providers that implement
 145      * the given service type.
 146      */
 147     public List<ServiceProvider> findServices(String service) {
 148         return map.getOrDefault(service, List.of());
 149     }
 150 
 151     /**
 152      * Returns the ServicesCatalog for the given class loader or {@code null}
 153      * if there is none.
 154      */
 155     public static ServicesCatalog getServicesCatalogOrNull(ClassLoader loader) {
 156         return CLV.get(loader);
 157     }
 158 
 159     /**
 160      * Returns the ServicesCatalog for the given class loader, creating it if
 161      * needed.
 162      */
 163     public static ServicesCatalog getServicesCatalog(ClassLoader loader) {
 164         // CLV.computeIfAbsent(loader, (cl, clv) -> create());
 165         ServicesCatalog catalog = CLV.get(loader);
 166         if (catalog == null) {
 167             catalog = create();
 168             ServicesCatalog previous = CLV.putIfAbsent(loader, catalog);
 169             if (previous != null) catalog = previous;
 170         }
 171         return catalog;
 172     }
 173 
 174     static void setServicesCatalog(ClassLoader loader, ServicesCatalog catalog) {
 175         ServicesCatalog previous = CLV.putIfAbsent(loader, catalog);
 176         if (previous != null) {
 177             throw new RuntimeException("unexpected: " + previous);
 178         }
 179     }
 180 
 181     // the ServicesCatalog registered to a class loader
 182     private static final ClassLoaderValue<ServicesCatalog> CLV = new ClassLoaderValue<>();
 183 }