SunAttachAPIResolver.java 3.99 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.annotation.Nullable
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceRegistration
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.deserfw.impl.attach;

import com.adobe.cq.deserfw.api.AttachAPIResolver;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.felix.scr.annotations.Deactivate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SunAttachAPIResolver
implements AttachAPIResolver {
    private static final String ATTACH_VM_CLASSNAME = "com.sun.tools.attach.VirtualMachine";
    private static final Logger log = LoggerFactory.getLogger(AttachAPIResolver.class);
    private URLClassLoader loader = null;

    @Nullable
    @Override
    public Class<?> getAttachAPIClass() {
        if (this.loader != null) {
            this.teardown();
        }
        try {
            return ClassLoader.getSystemClassLoader().loadClass("com.sun.tools.attach.VirtualMachine");
        }
        catch (ClassNotFoundException cnfe) {
            log.debug("Sun Attach API not found. Trying to load tools.jar");
            try {
                Iterator<File> i$ = this.getPossibleToolsJars().iterator();
                if (i$.hasNext()) {
                    File f = i$.next();
                    try {
                        this.loader = new URLClassLoader(new URL[]{f.toURI().toURL()}, ClassLoader.getSystemClassLoader());
                        Class result = this.loader.loadClass("com.sun.tools.attach.VirtualMachine");
                        log.info("Loaded {} from {}", (Object)result.getName(), (Object)f.getAbsolutePath());
                        return result;
                    }
                    catch (Throwable t) {
                        this.teardown();
                        log.debug("Unable to load {}. See nested exception", (Object)f, (Object)t);
                        throw cnfe;
                    }
                }
            }
            catch (ClassNotFoundException cnfe2) {
                log.info("Sun Attach API not found.");
            }
            return null;
        }
    }

    @Deactivate
    public void teardown() {
        if (this.loader != null) {
            try {
                this.loader.close();
                this.loader = null;
            }
            catch (IOException e) {
                log.warn("Unable to tear down tools loader", (Throwable)e);
            }
        }
    }

    public List<File> getPossibleToolsJars() {
        ArrayList<File> jars = new ArrayList<File>();
        String homePath = System.getProperty("java.home");
        if (homePath == null || homePath.length() == 0) {
            return jars;
        }
        File home = new File(homePath);
        File fromJRE = new File(home, "lib/tools.jar");
        this.addTools(fromJRE, jars);
        if ("jre".equalsIgnoreCase(home.getName())) {
            File fromJDK = home.getParentFile();
            this.addTools(fromJDK, jars);
        }
        return jars;
    }

    private void addTools(File parent, List<File> jars) {
        File tools = new File(parent, "lib/tools.jar");
        if (tools.exists() && tools.canRead()) {
            jars.add(tools);
        }
    }

    public static ServiceRegistration registerService(BundleContext ctx) {
        Hashtable<String, Object> props = new Hashtable<String, Object>();
        props.put("service.ranking", 100);
        props.put("service.description", "AttachAPIResolver for Sun JVMs");
        return ctx.registerService(AttachAPIResolver.class.getName(), (Object)new SunAttachAPIResolver(), props);
    }
}