Search in sources :

Example 1 with JarFileContext

use of com.datatorrent.stram.client.ClassPathResolvers.JarFileContext in project apex-core by apache.

the class StramAppLauncher method init.

private void init(String tmpName) throws Exception {
    File baseDir = StramClientUtils.getUserDTDirectory();
    baseDir = new File(new File(baseDir, "appcache"), tmpName);
    baseDir.mkdirs();
    LinkedHashSet<URL> clUrls;
    List<String> classFileNames = new ArrayList<>();
    if (jarFile != null) {
        JarFileContext jfc = new JarFileContext(new java.util.jar.JarFile(jarFile), mvnBuildClasspathOutput);
        jfc.cacheDir = baseDir;
        java.util.Enumeration<JarEntry> entriesEnum = jfc.jarFile.entries();
        while (entriesEnum.hasMoreElements()) {
            java.util.jar.JarEntry jarEntry = entriesEnum.nextElement();
            if (!jarEntry.isDirectory()) {
                if (jarEntry.getName().endsWith("pom.xml")) {
                    jfc.pomEntry = jarEntry;
                } else if (jarEntry.getName().endsWith(".app.properties")) {
                    File targetFile = new File(baseDir, jarEntry.getName());
                    FileUtils.copyInputStreamToFile(jfc.jarFile.getInputStream(jarEntry), targetFile);
                    appResourceList.add(new PropertyFileAppFactory(targetFile));
                } else if (jarEntry.getName().endsWith(".class")) {
                    classFileNames.add(jarEntry.getName());
                }
            }
        }
        URL mainJarUrl = new URL("jar", "", "file:" + jarFile.getAbsolutePath() + "!/");
        jfc.urls.add(mainJarUrl);
        deployJars = Sets.newLinkedHashSet();
        // add all jar files from same directory
        Collection<File> jarFiles = FileUtils.listFiles(jarFile.getParentFile(), new String[] { "jar" }, false);
        for (File lJarFile : jarFiles) {
            jfc.urls.add(lJarFile.toURI().toURL());
            deployJars.add(lJarFile);
        }
        // resolve dependencies
        List<Resolver> resolvers = Lists.newArrayList();
        String resolverConfig = this.propertiesBuilder.conf.get(CLASSPATH_RESOLVERS_KEY_NAME, null);
        if (!StringUtils.isEmpty(resolverConfig)) {
            resolvers = new ClassPathResolvers().createResolvers(resolverConfig);
        } else {
            // default setup if nothing was configured
            String manifestCp = jfc.jarFile.getManifest().getMainAttributes().getValue(ManifestResolver.ATTR_NAME);
            if (manifestCp != null) {
                File repoRoot = new File(System.getProperty("user.home") + "/.m2/repository");
                if (repoRoot.exists()) {
                    LOG.debug("Resolving manifest attribute {} based on {}", ManifestResolver.ATTR_NAME, repoRoot);
                    resolvers.add(new ClassPathResolvers.ManifestResolver(repoRoot));
                } else {
                    LOG.warn("Ignoring manifest attribute {} because {} does not exist.", ManifestResolver.ATTR_NAME, repoRoot);
                }
            }
        }
        for (Resolver r : resolvers) {
            r.resolve(jfc);
        }
        jfc.jarFile.close();
        URLConnection urlConnection = mainJarUrl.openConnection();
        if (urlConnection instanceof JarURLConnection) {
            // JDK6 keeps jar file shared and open as long as the process is running.
            // we want the jar file to be opened on every launch to pick up latest changes
            // http://abondar-howto.blogspot.com/2010/06/howto-unload-jar-files-loaded-by.html
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4167874
            ((JarURLConnection) urlConnection).getJarFile().close();
        }
        clUrls = jfc.urls;
    } else {
        clUrls = new LinkedHashSet<>();
    }
    // add the jar dependencies
    /*
    if (cp == null) {
      // dependencies from parent loader, if classpath can't be found from pom
      ClassLoader baseCl = StramAppLauncher.class.getClassLoader();
      if (baseCl instanceof URLClassLoader) {
        URL[] baseUrls = ((URLClassLoader)baseCl).getURLs();
        // launch class path takes precedence - add first
        clUrls.addAll(Arrays.asList(baseUrls));
      }
    }
*/
    String libjars = propertiesBuilder.conf.get(LIBJARS_CONF_KEY_NAME);
    if (libjars != null) {
        processLibJars(libjars, clUrls);
    }
    for (URL baseURL : clUrls) {
        LOG.debug("Dependency: {}", baseURL);
    }
    this.launchDependencies = clUrls;
    // we have the classpath dependencies, scan for java configurations
    findAppConfigClasses(classFileNames);
}
Also used : Resolver(com.datatorrent.stram.client.ClassPathResolvers.Resolver) ManifestResolver(com.datatorrent.stram.client.ClassPathResolvers.ManifestResolver) JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) JarURLConnection(java.net.JarURLConnection) URLConnection(java.net.URLConnection) JarEntry(java.util.jar.JarEntry) JarFileContext(com.datatorrent.stram.client.ClassPathResolvers.JarFileContext) ManifestResolver(com.datatorrent.stram.client.ClassPathResolvers.ManifestResolver) File(java.io.File)

Aggregations

JarFileContext (com.datatorrent.stram.client.ClassPathResolvers.JarFileContext)1 ManifestResolver (com.datatorrent.stram.client.ClassPathResolvers.ManifestResolver)1 Resolver (com.datatorrent.stram.client.ClassPathResolvers.Resolver)1 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1