Package com.cloudbees.sdk.boot2

Source Code of com.cloudbees.sdk.boot2.BeesLoader

/*
* Copyright 2010-2013, CloudBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cloudbees.sdk.boot2;

import com.cloudbees.sdk.GAV;
import com.cloudbees.sdk.boot.Launcher;
import com.cloudbees.sdk.extensibility.ExtensionList;
import com.cloudbees.sdk.maven.*;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.ArtifactResolutionException;
import org.sonatype.aether.resolution.DependencyResolutionException;
import org.sonatype.aether.resolution.DependencyResult;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Properties;

/**
* 2nd-level bootstrapping.
*
* <p>
* This code gets run in a classloader that contains Guice+Aether+CloudBees Maven components,
* then it uses Aether to resolve bees-driver:RELEASE, which is the real entry point.
*
* @author Kohsuke Kawaguchi
*/
public class BeesLoader {

    public static String VERSION = loadVersion(BeesLoader.class.getResourceAsStream("version.properties"));

    /*package*/ static final GAV MAIN = new GAV("com.cloudbees.sdk", "bees-driver", "RELEASE");

    public static void main(String[] args) throws Exception {
        reportTime();

        new BeesLoader().run(args);
    }

    private Injector injector;
    private RepositoryService rs;
    private boolean verbose;

    private void run(String[] args) throws Exception {
        if (displayInternalVersion(args)) {
            System.out.println("CloudBees Bootstrap version: " + VERSION);
            System.exit(0);
        }
        verbose = isVerbose(args);

        BootMavenRepositorySystemSessionDecorator.setVerbose(verbose);

        ResolvedDependenciesCache cache = new ResolvedDependenciesCache() {
            /**
             * Lazily loads Aether so that things go blindingly fast if the cache hits.
             */
            private RepositoryService getRepositoryService() {
                if (injector==null)
                    injector = Guice.createInjector(
                            new RepositorySystemModule() {
                                @Override
                                public List<RemoteRepository> getRemoteRepositories(ExtensionList<RemoteRepositoryDecorator> decorators) {
                                    List<RemoteRepository> repos = super.getRemoteRepositories(decorators);

                                    if (OFFLINE_BOOT)
                                        repos.clear()// boot without remote lookup

                                    try {
                                        String bootRepoUrl = new File(Launcher.getInstallationDirectory(), "boot-repo").toURI().toString();
                                        repos.add(new RemoteRepository("bees-boot","default", bootRepoUrl));
                                    } catch (MalformedURLException e) {
                                        throw new AssertionError(e);
                                    }
                                    return repos;
                                }
                            },
                            new AbstractModule() {
                                @Override
                                protected void configure() {
                                    bind(RemoteRepositoryDecorator.class).toInstance(new RemoteRepositoryDecoratorImpl());
                                    bind(MavenRepositorySystemSessionDecorator.class).to(BootMavenRepositorySystemSessionDecorator.class);
                                }
                            });
                if (rs==null) {
                    // code getting to this point means we'll be using Aether to resolve stuff
                    System.out.println("Updating Bees SDK. This may take a while");

                    if (LOCAL_REPOSITORY!=null)
                        injector.getInstance(LocalRepositorySetting.class).set(
                                new LocalRepository(new File(LOCAL_REPOSITORY))
                        );

                    rs = injector.getInstance(RepositoryService.class);
                }
                return rs;
            }

            @Override
            protected File getCacheDir() {
                File dir = Launcher.getLocalRepository();
                dir.mkdirs();
                return dir;
            }

            @Override
            protected DependencyResult forceResolve(GAV gav) throws DependencyResolutionException {
                RepositoryService rs = getRepositoryService();
                return rs.resolveDependencies(gav);
            }

            @Override
            protected File resolveArtifact(Artifact a) throws ArtifactResolutionException {
                return getRepositoryService().resolveArtifact(a).getArtifact().getFile();
            }
        };

        List<File> jars= cache.resolve(getMAIN());

        File tools = findToolsJar();
        if (tools != nulljars.add(tools);

        // we don't let this classloader delegate to the 2nd stage boot classloader
        // in this way, the environment that the driver sees can be fully updated (including Guice, Aether, and Plexus)
        // without updating the SDK itself.
        URLClassLoader loader = new URLClassLoader(toURL(jars), null);
        Class<?> beesClass = loader.loadClass("com.cloudbees.sdk.Bees");

        Thread.currentThread().setContextClassLoader(loader);

        // Set bootstrap current version
        setBootstrapVersion(beesClass);

        Method mainMethod = beesClass.getDeclaredMethod("main", String[].class);
        Object obj = mainMethod.invoke(null, (Object)args);
        if (obj instanceof Integer)
            System.exit((Integer)obj);
    }

    private void setBootstrapVersion(Class<?> beesClass) {
        try {
            Field f = beesClass.getDeclaredField("BOOTSTRAP_VERSION");
            f.set(null, VERSION);
        } catch (Exception e) {
            System.err.println("WARNING: Cannot set BOOTSTRAP_VERSION");
            if (verbose) e.printStackTrace();
        }
    }

    private static boolean isVerbose(String[] args) {
        for (String arg : args) {
            if (arg.equalsIgnoreCase("-v") || arg.equalsIgnoreCase("--verbose"))
                return true;
        }
        return false;
    }

    private static boolean displayInternalVersion(String[] args) {
        for (String arg : args) {
            if (arg.equalsIgnoreCase("--showBootstrapVersion"))
                return true;
        }
        return false;
    }

    /**
     * Get the version number.
     * <p/>
     * To support running this from IDE and elsewhere, work gracefully if the version
     * is not available or not filtered.
     */
    private static String loadVersion(InputStream in) {
        Properties props = new Properties();
        if (in != null) {
            try {
                props.load(in);
            } catch (IOException e) {
                throw new Error(e);
            } finally {
                try {
                    in.close();
                } catch (IOException ignored) {}
            }
        }
        Object v = props.get("version");
        if (v != null)
            try {
                return v.toString();
            } catch (Exception ignored) {}

        return "0";
    }

    private URL[] toURL(List<File> files) throws MalformedURLException {
        URL[] jars = new URL[files.size()];
        int i=0;
        for (File f : files) {
            jars[i++] = f.toURI().toURL();
        }
        return jars;
    }

    private static void reportTime() {
        String profile = System.getProperty("profile");
        if (profile !=null) {
            System.out.println(BeesLoader.class.getName() + ": " + (System.nanoTime() - Long.valueOf(profile)) / 1000000L + "ms");
        }
    }

    private static File findToolsJar() {
        String javaHome = System.getenv("JAVA_HOME");
        // Try to define it
        if (javaHome == null) {
            String[] paths = System.getProperty("sun.boot.library.path").split(",");
            if (paths != null && paths.length > 0) {
                javaHome = paths[0].trim();
            }
        }
        if (javaHome == null)   return null;

        File dir = new File(javaHome);
        File tools = new File(dir, "lib/tools.jar");
        if (tools.exists())     return tools;

        return null;
    }

    private static GAV getMAIN() {
        String version = System.getProperty("bees.driverVersion");
        if (version != null)
            return new GAV("com.cloudbees.sdk", "bees-driver", version);
        else
            return MAIN;
    }

    /**
     * Debug hook to use a different local repository.
     * This is useful for testing fallback boot from boot-repo file repository.
     */
    private static final String LOCAL_REPOSITORY = System.getProperty("bees.localRepository");

    /**
     * Debug hook to avoid looking remote repositories.
     * This is also useful for testing fallback boot from boot-repo file repository.
     */
    private static final boolean OFFLINE_BOOT = Boolean.getBoolean("bees.offlineBoot");

}
TOP

Related Classes of com.cloudbees.sdk.boot2.BeesLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.