Search in sources :

Example 1 with JavaSpecificationVersion

use of io.jenkins.lib.versionnumber.JavaSpecificationVersion in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method cleanUpClassInfoCache.

private static void cleanUpClassInfoCache(Class<?> clazz) {
    JavaSpecificationVersion current = JavaSpecificationVersion.forCurrentJVM();
    if (current.isNewerThan(new JavaSpecificationVersion("1.8")) && current.isOlderThan(new JavaSpecificationVersion("16"))) {
        try {
            // TODO Work around JDK-8231454.
            Class<?> classInfoC = Class.forName("com.sun.beans.introspect.ClassInfo");
            Field cacheF = classInfoC.getDeclaredField("CACHE");
            try {
                cacheF.setAccessible(true);
            } catch (RuntimeException e) {
                /*
                     * Not running with "--add-opens java.desktop/com.sun.beans.introspect=ALL-UNNAMED".
                     * Until core adds this to its --add-opens configuration, and until that core
                     * change is widely adopted, avoid unnecessary log spam and return early.
                     */
                if (LOGGER.isLoggable(Level.FINER)) {
                    LOGGER.log(Level.FINER, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
                }
                return;
            }
            Object cache = cacheF.get(null);
            Class<?> cacheC = Class.forName("com.sun.beans.util.Cache");
            if (LOGGER.isLoggable(Level.FINER)) {
                LOGGER.log(Level.FINER, "Cleaning up " + clazz.getName() + " from ClassInfo#CACHE.");
            }
            Method removeM = cacheC.getMethod("remove", Object.class);
            removeM.invoke(cache, clazz);
        } catch (ReflectiveOperationException e) {
            /*
                 * Should never happen, but if it does, ensure the failure is isolated to this
                 * method and does not prevent other cleanup logic from executing.
                 */
            LOGGER.log(Level.WARNING, "Failed to clean up " + clazz.getName() + " from ClassInfo#CACHE. A metaspace leak may have occurred.", e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) JavaSpecificationVersion(io.jenkins.lib.versionnumber.JavaSpecificationVersion)

Example 2 with JavaSpecificationVersion

use of io.jenkins.lib.versionnumber.JavaSpecificationVersion in project plugin-compat-tester by jenkinsci.

the class PluginCompatTester method populateSplits.

/**
 * Use JENKINS-47634 to load metadata from jenkins-core.jar if available.
 */
private void populateSplits(File war) throws IOException {
    System.out.println("Checking " + war + " for plugin split metadata…");
    System.out.println("Checking jdk version as splits may depend on a jdk version");
    // From Java 9 onwards there is a standard for versions see JEP-223
    JavaSpecificationVersion jdkVersion = new JavaSpecificationVersion(config.getTestJavaVersion());
    try (JarFile jf = new JarFile(war, false)) {
        Enumeration<JarEntry> warEntries = jf.entries();
        while (warEntries.hasMoreElements()) {
            JarEntry coreJar = warEntries.nextElement();
            if (coreJar.getName().matches("WEB-INF/lib/jenkins-core-.+[.]jar")) {
                try (InputStream is = jf.getInputStream(coreJar);
                    JarInputStream jis = new JarInputStream(is, false)) {
                    JarEntry entry;
                    int found = 0;
                    while ((entry = jis.getNextJarEntry()) != null) {
                        if (entry.getName().equals("jenkins/split-plugins.txt")) {
                            splits = configLines(jis).collect(Collectors.toList());
                            // Since https://github.com/jenkinsci/jenkins/pull/3865 splits can depend on jdk version
                            // So make sure we are not applying splits not intended for our JDK
                            splits = removeSplitsBasedOnJDK(splits, jdkVersion);
                            System.out.println("found splits: " + splits);
                            found++;
                        } else if (entry.getName().equals("jenkins/split-plugin-cycles.txt")) {
                            splitCycles = configLines(jis).collect(Collectors.toSet());
                            System.out.println("found split cycles: " + splitCycles);
                            found++;
                        }
                    }
                    if (found == 0) {
                        System.out.println("None found, falling back to hard-coded historical values.");
                        splits = HISTORICAL_SPLITS;
                        splitCycles = HISTORICAL_SPLIT_CYCLES;
                    } else if (found != 2) {
                        throw new IOException("unexpected amount of metadata");
                    }
                }
                return;
            }
        }
    }
    throw new IOException("no jenkins-core-*.jar found in " + war);
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JavaSpecificationVersion(io.jenkins.lib.versionnumber.JavaSpecificationVersion)

Aggregations

JavaSpecificationVersion (io.jenkins.lib.versionnumber.JavaSpecificationVersion)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 JarInputStream (java.util.jar.JarInputStream)1