|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.grape |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | +import org.apache.groovy.plugin.GroovyRunner |
| 23 | +import org.apache.groovy.plugin.GroovyRunnerRegistry |
| 24 | +import org.codehaus.groovy.reflection.CachedClass |
| 25 | +import org.codehaus.groovy.reflection.ClassInfo |
| 26 | +import org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner |
| 27 | +import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl |
| 28 | + |
| 29 | +import java.util.jar.JarFile |
| 30 | +import java.util.zip.ZipEntry |
| 31 | +import java.util.zip.ZipException |
| 32 | +import java.util.zip.ZipFile |
| 33 | + |
| 34 | +/** |
| 35 | + * Utility methods shared between GrapeIvy and GrapeMaven implementations. |
| 36 | + */ |
| 37 | +@CompileStatic |
| 38 | +class GrapeUtil { |
| 39 | + |
| 40 | + private static final String METAINF_PREFIX = 'META-INF/services/' |
| 41 | + private static final String RUNNER_PROVIDER_CONFIG = GroovyRunner.name |
| 42 | + private static final boolean DEBUG_GRAPE = Boolean.getBoolean('groovy.grape.debug') |
| 43 | + |
| 44 | + /** |
| 45 | + * Adds a URI to a classloader's classpath via reflection. |
| 46 | + */ |
| 47 | + static void addURL(ClassLoader loader, URI uri) { |
| 48 | + // Dynamic invocation needed as addURL is not part of ClassLoader interface |
| 49 | + loader.metaClass.invokeMethod(loader, 'addURL', uri.toURL()) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Processes and registers category methods (extension modules) from a JAR file. |
| 54 | + * |
| 55 | + * @param loader the classloader to register methods with |
| 56 | + * @param file the JAR file to process |
| 57 | + */ |
| 58 | + static void processExtensionMethods(ClassLoader loader, File file) { |
| 59 | + // register extension methods if jar |
| 60 | + if (file.getName().toLowerCase().endsWith('.jar')) { |
| 61 | + def mcRegistry = GroovySystem.metaClassRegistry |
| 62 | + if (mcRegistry instanceof MetaClassRegistryImpl) { |
| 63 | + try (JarFile jar = new JarFile(file)) { |
| 64 | + ZipEntry entry = jar.getEntry(ExtensionModuleScanner.MODULE_META_INF_FILE) |
| 65 | + if (!entry) { |
| 66 | + entry = jar.getEntry(ExtensionModuleScanner.LEGACY_MODULE_META_INF_FILE) |
| 67 | + } |
| 68 | + if (entry) { |
| 69 | + Properties props = new Properties() |
| 70 | + |
| 71 | + try (InputStream is = jar.getInputStream(entry)) { |
| 72 | + props.load(is) |
| 73 | + } |
| 74 | + |
| 75 | + Map<CachedClass, List<MetaMethod>> metaMethods = [:] |
| 76 | + mcRegistry.registerExtensionModuleFromProperties(props, loader, metaMethods) |
| 77 | + // add old methods to the map |
| 78 | + metaMethods.each { CachedClass c, List<MetaMethod> methods -> |
| 79 | + // GROOVY-5543: if a module was loaded using grab, there are chances that subclasses |
| 80 | + // have their own ClassInfo, and we must change them as well! |
| 81 | + Set<CachedClass> classesToBeUpdated = [c].toSet() |
| 82 | + ClassInfo.onAllClassInfo { ClassInfo info -> |
| 83 | + if (c.getTheClass().isAssignableFrom(info.getCachedClass().getTheClass())) { |
| 84 | + classesToBeUpdated << info.getCachedClass() |
| 85 | + } |
| 86 | + } |
| 87 | + classesToBeUpdated*.addNewMopMethods(methods) |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (ZipException e) { |
| 91 | + throw new RuntimeException("Grape could not load jar '$file'", e) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Searches the given File for known service provider configuration files to process. |
| 99 | + * |
| 100 | + * @param loader used to locate service provider files |
| 101 | + * @param f ZipFile in which to search for services |
| 102 | + * @return a collection of service provider files that were found |
| 103 | + */ |
| 104 | + static Collection<String> processMetaInfServices(ClassLoader loader, File f) { |
| 105 | + List<String> services = [] |
| 106 | + try (ZipFile zf = new ZipFile(f)) { |
| 107 | + // TODO: remove in a future release (replaced by GroovyRunnerRegistry) |
| 108 | + String providerConfig = 'org.codehaus.groovy.plugins.Runners' |
| 109 | + ZipEntry pluginRunners = zf.getEntry(METAINF_PREFIX + providerConfig) |
| 110 | + if (pluginRunners != null) { |
| 111 | + services.add(providerConfig) |
| 112 | + |
| 113 | + try (InputStream is = zf.getInputStream(pluginRunners)) { |
| 114 | + processRunners(is, f.getName(), loader) |
| 115 | + } |
| 116 | + } |
| 117 | + // GroovyRunners are loaded per ClassLoader using a ServiceLoader so here |
| 118 | + // it only needs to be indicated that the service provider file was found |
| 119 | + if (zf.getEntry(METAINF_PREFIX + RUNNER_PROVIDER_CONFIG) != null) { |
| 120 | + services.add(RUNNER_PROVIDER_CONFIG) |
| 121 | + } |
| 122 | + } catch (ZipException ignore) { |
| 123 | + // ignore files we can't process, e.g. non-jar/zip artifacts |
| 124 | + if (DEBUG_GRAPE) { |
| 125 | + System.err.println "Grape could not process file '$f' for service provider configuration: ${ignore.message}" |
| 126 | + } |
| 127 | + } |
| 128 | + services |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Processes and registers Groovy runner implementations from a service provider file. |
| 133 | + * |
| 134 | + * @param is the input stream containing runner class names |
| 135 | + * @param name the name to register the runners under |
| 136 | + * @param loader the classloader to load runner classes from |
| 137 | + */ |
| 138 | + static void processRunners(InputStream is, String name, ClassLoader loader) { |
| 139 | + GroovyRunnerRegistry registry = GroovyRunnerRegistry.instance |
| 140 | + is.getText().readLines()*.trim().each { String line -> |
| 141 | + if (!line.isEmpty() && line[0] != '#') { |
| 142 | + try { |
| 143 | + registry[name] = (GroovyRunner) loader.loadClass(line).getDeclaredConstructor().newInstance() |
| 144 | + } catch (Exception e) { |
| 145 | + throw new IllegalStateException("Error registering runner class '$line'", e) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + static boolean checkForRunner(Collection<String> services) { |
| 152 | + services.contains(RUNNER_PROVIDER_CONFIG) |
| 153 | + } |
| 154 | + |
| 155 | + static void registryLoad(ClassLoader classLoader) { |
| 156 | + GroovyRunnerRegistry.instance.load(classLoader) |
| 157 | + } |
| 158 | +} |
0 commit comments