Example usage for org.springframework.boot.loader.archive JarFileArchive JarFileArchive

List of usage examples for org.springframework.boot.loader.archive JarFileArchive JarFileArchive

Introduction

In this page you can find the example usage for org.springframework.boot.loader.archive JarFileArchive JarFileArchive.

Prototype

public JarFileArchive(JarFile jarFile) 

Source Link

Usage

From source file:org.springframework.cloud.dataflow.app.launcher.ModuleLauncher.java

public void launchAggregatedModules(List<ModuleLaunchRequest> moduleLaunchRequests,
        Map<String, String> aggregateArgs) {
    try {/*from ww w  .  ja  v  a 2 s. c o m*/
        List<String> mainClassNames = new ArrayList<>();
        LinkedHashSet<URL> jarURLs = new LinkedHashSet<>();
        List<String> seenArchives = new ArrayList<>();
        final List<String[]> arguments = new ArrayList<>();
        final ClassLoader classLoader;
        if (!(aggregateArgs.containsKey(EXCLUDE_DEPENDENCIES_ARG)
                || aggregateArgs.containsKey(INCLUDE_DEPENDENCIES_ARG))) {
            for (ModuleLaunchRequest moduleLaunchRequest : moduleLaunchRequests) {
                Resource resource = resolveModule(moduleLaunchRequest.getModule());
                JarFileArchive jarFileArchive = new JarFileArchive(resource.getFile());
                jarURLs.add(jarFileArchive.getUrl());
                for (Archive archive : jarFileArchive.getNestedArchives(ArchiveMatchingEntryFilter.FILTER)) {
                    // avoid duplication based on unique JAR names
                    String urlAsString = archive.getUrl().toString();
                    String jarNameWithExtension = urlAsString.substring(0, urlAsString.lastIndexOf("!/"));
                    String jarNameWithoutExtension = jarNameWithExtension
                            .substring(jarNameWithExtension.lastIndexOf("/") + 1);
                    if (!seenArchives.contains(jarNameWithoutExtension)) {
                        seenArchives.add(jarNameWithoutExtension);
                        jarURLs.add(archive.getUrl());
                    }
                }
                mainClassNames.add(jarFileArchive.getMainClass());
                arguments.add(toArgArray(moduleLaunchRequest.getArguments()));
            }
            classLoader = ClassloaderUtils.createModuleClassloader(jarURLs.toArray(new URL[jarURLs.size()]));
        } else {
            // First, resolve modules and extract main classes - while slightly less efficient than just
            // doing the same processing after resolution, this ensures that module artifacts are processed
            // correctly for extracting their main class names. It is not possible in the general case to
            // identify, after resolution, whether a resource represents a module artifact which was part of the
            // original request or not. We will include the first module as root and the next as direct dependencies
            Coordinates root = null;
            ArrayList<Coordinates> includeCoordinates = new ArrayList<>();
            for (ModuleLaunchRequest moduleLaunchRequest : moduleLaunchRequests) {
                Coordinates moduleCoordinates = toCoordinates(moduleLaunchRequest.getModule());
                if (root == null) {
                    root = moduleCoordinates;
                } else {
                    includeCoordinates.add(toCoordinates(moduleLaunchRequest.getModule()));
                }
                Resource moduleResource = resolveModule(moduleLaunchRequest.getModule());
                JarFileArchive moduleArchive = new JarFileArchive(moduleResource.getFile());
                mainClassNames.add(moduleArchive.getMainClass());
                arguments.add(toArgArray(moduleLaunchRequest.getArguments()));
            }
            for (String include : StringUtils
                    .commaDelimitedListToStringArray(aggregateArgs.get(INCLUDE_DEPENDENCIES_ARG))) {
                includeCoordinates.add(toCoordinates(include));
            }
            // Resolve all artifacts - since modules have been specified as direct dependencies, they will take
            // precedence in the resolution order, ensuring that the already resolved artifacts will be returned as
            // part of the response.
            Resource[] libraries = moduleResolver.resolve(root,
                    includeCoordinates.toArray(new Coordinates[includeCoordinates.size()]),
                    StringUtils.commaDelimitedListToStringArray(aggregateArgs.get(EXCLUDE_DEPENDENCIES_ARG)));
            for (Resource library : libraries) {
                jarURLs.add(library.getURL());
            }
            classLoader = new URLClassLoader(jarURLs.toArray(new URL[jarURLs.size()]));
        }

        final List<Class<?>> mainClasses = new ArrayList<>();
        for (String mainClass : mainClassNames) {
            mainClasses.add(ClassUtils.forName(mainClass, classLoader));
        }
        Runnable moduleAggregatorRunner = new ModuleAggregatorRunner(classLoader, mainClasses,
                toArgArray(aggregateArgs), arguments);
        Thread moduleAggregatorRunnerThread = new Thread(moduleAggregatorRunner);
        moduleAggregatorRunnerThread.setContextClassLoader(classLoader);
        moduleAggregatorRunnerThread.setName(MODULE_AGGREGATOR_RUNNER_THREAD_NAME);
        moduleAggregatorRunnerThread.start();
    } catch (Exception e) {
        throw new RuntimeException("failed to start aggregated modules: "
                + StringUtils.collectionToCommaDelimitedString(moduleLaunchRequests), e);
    }
}

From source file:org.springframework.cloud.dataflow.app.launcher.ModuleLauncher.java

private void launchModule(String module, String[] args) {
    try {/*  www.  jav a2 s .  com*/
        Resource resource = resolveModule(module);
        JarFileArchive jarFileArchive = new JarFileArchive(resource.getFile());
        ModuleJarLauncher jarLauncher = new ModuleJarLauncher(jarFileArchive);
        jarLauncher.launch(args);
    } catch (IOException e) {
        throw new RuntimeException("failed to launch module: " + module, e);
    }
}

From source file:org.springframework.cloud.dataflow.app.launcher.ModuleLauncher.java

private void launchModuleWithDependencies(String module, String[] args, String[] includes, String[] excludes) {
    try {/* w w w  .  j a  va2  s .c o  m*/
        Resource[] libraries = this.moduleResolver.resolve(toCoordinates(module), toCoordinateArray(includes),
                excludes);
        List<Archive> archives = new ArrayList<>();
        for (Resource library : libraries) {
            archives.add(new JarFileArchive(library.getFile()));
        }
        MultiArchiveLauncher jarLauncher = new MultiArchiveLauncher(archives);
        jarLauncher.launch(args);
    } catch (IOException e) {
        throw new RuntimeException("failed to launch module: " + module, e);
    }
}

From source file:org.springframework.cloud.dataflow.server.service.impl.validation.DefaultValidationService.java

private static Archive resolveAsArchive(Resource app) throws IOException {
    Assert.notNull(app, "The resource specified for the app must not be null");
    File moduleFile = app.getFile();
    return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}

From source file:org.springframework.cloud.deployer.thin.ThinJarAppWrapper.java

public void run(Map<String, String> properties, List<String> args) {
    if (this.app == null) {
        this.state = LaunchState.launching;
        ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
        try {/*from  w ww. ja  v a 2s  .  co m*/
            Archive child = new JarFileArchive(resource.getFile());
            Class<?> cls = createContextRunnerClass(child, args);
            this.app = cls.newInstance();
            runContext(getMainClass(child), properties, args.toArray(new String[0]));
            boolean running = isRunning();
            this.state = running ? LaunchState.running
                    : (getError() != null ? LaunchState.failed : LaunchState.complete);
        } catch (Exception e) {
            this.state = LaunchState.failed;
            logger.error("Cannot deploy " + resource, e);
        } finally {
            ClassUtils.overrideThreadContextClassLoader(contextLoader);
        }
    }
}

From source file:org.springframework.cloud.deployer.thin.ThinJarAppWrapper.java

protected final Archive createArchive() {
    try {/* w  w  w.j av a  2s  . c o m*/
        ProtectionDomain protectionDomain = getClass().getProtectionDomain();
        CodeSource codeSource = protectionDomain.getCodeSource();
        URI location;
        location = (codeSource == null ? null : codeSource.getLocation().toURI());
        String path = (location == null ? null : location.getSchemeSpecificPart());
        if (path == null) {
            throw new IllegalStateException("Unable to determine code source archive");
        }
        File root = new File(path);
        if (!root.exists()) {
            throw new IllegalStateException("Unable to determine code source archive from " + root);
        }
        return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
    } catch (Exception e) {
        throw new IllegalStateException("Cannt create local archive", e);
    }
}