@Override public void load(String name, Location targetLocation) throws IOException { bundler.createBundle(targetLocation, ApplicationMasterMain.class, TwillContainerMain.class, OptionSpec.class); } });
private ApplicationBundler createBundler(ClassAcceptor classAcceptor, Path stagingDir) { return new ApplicationBundler(classAcceptor).setTempDir(stagingDir.toFile()); }
ApplicationBundler bundler = new ApplicationBundler(ImmutableList.of("co.cask.cdap.api", "org.apache.hadoop", "org.apache.hbase", "org.apache.hive")); bundler.createBundle(Locations.toLocation(tempFile), clz); } finally { ClassLoaders.setContextClassLoader(remembered);
saveDirEntry(libDir, entries, jarOut); saveEntry(libDir + entryName, classPathUrl, entries, jarOut, false); } else { saveDirEntry(classesDir, entries, jarOut); if ("file".equals(classPathUrl.getProtocol())) { copyDir(new File(classPathUrl.toURI()), classesDir, entries, jarOut); } catch (Exception e) { throw Throwables.propagate(e); saveDirEntry(entry.substring(0, entry.lastIndexOf('/') + 1), entries, jarOut); saveEntry(entry, classUrl, entries, jarOut, true);
private void copyResource(URI resource, Set<String> entries, JarOutputStream jarOut) throws IOException { if ("file".equals(resource.getScheme())) { File file = new File(resource); if (file.isDirectory()) { saveDirEntry(resourcesDir, entries, jarOut); copyDir(file, resourcesDir, entries, jarOut); return; } } URL url = resource.toURL(); String path = url.getFile(); String prefix = path.endsWith(".jar") ? libDir : resourcesDir; path = prefix + path.substring(path.lastIndexOf('/') + 1); if (entries.add(path)) { saveDirEntry(prefix, entries, jarOut); jarOut.putNextEntry(new JarEntry(path)); try (InputStream is = url.openStream()) { ByteStreams.copy(is, jarOut); } } }
try (JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(tmpJar))) { findDependencies(classes, entries, jarOut); copyResource(resource, entries, jarOut);
ApplicationBundler bundler = new ApplicationBundler(ImmutableList.of("co.cask.cdap.api", "org.apache.hadoop", "org.apache.hbase", "org.apache.hive")); bundler.createBundle(Locations.toLocation(tempFile), clz); } finally { ClassLoaders.setContextClassLoader(remembered);
saveDirEntry(libDir, entries, jarOut); saveEntry(libDir + entryName, classPathUrl, entries, jarOut, false); } else { saveDirEntry(classesDir, entries, jarOut); if ("file".equals(classPathUrl.getProtocol())) { copyDir(new File(classPathUrl.toURI()), classesDir, entries, jarOut); } catch (Exception e) { throw Throwables.propagate(e); saveDirEntry(entry.substring(0, entry.lastIndexOf('/') + 1), entries, jarOut); saveEntry(entry, classUrl, entries, jarOut, true);
private void copyResource(URI resource, Set<String> entries, JarOutputStream jarOut) throws IOException { if ("file".equals(resource.getScheme())) { File file = new File(resource); if (file.isDirectory()) { saveDirEntry(resourcesDir, entries, jarOut); copyDir(file, resourcesDir, entries, jarOut); return; } } URL url = resource.toURL(); String path = url.getFile(); String prefix = path.endsWith(".jar") ? libDir : resourcesDir; path = prefix + path.substring(path.lastIndexOf('/') + 1); if (entries.add(path)) { saveDirEntry(prefix, entries, jarOut); jarOut.putNextEntry(new JarEntry(path)); try (InputStream is = url.openStream()) { ByteStreams.copy(is, jarOut); } } }
try (JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(tmpJar))) { findDependencies(classes, entries, jarOut); copyResource(resource, entries, jarOut);
throws IOException, InstantiationException, IllegalAccessException { ApplicationBundler bundler = new ApplicationBundler(ImmutableList.of("co.cask.tigon.api", "org.apache.hadoop", "org.apache.hbase")); Location jarLocation = locationFactory.create(clz.getName()).getTempFile(".jar"); bundler.createBundle(jarLocation, ImmutableSet.<Class<?>>builder().add(clz).addAll(classes).build());
@Override public void load(String name, Location targetLocation) throws IOException { bundler.createBundle(targetLocation, classes); } });
private ApplicationBundler createBundler(ClassAcceptor classAcceptor, Path stagingDir) { return new ApplicationBundler(classAcceptor).setTempDir(stagingDir.toFile()); }
ApplicationBundler appBundler = new ApplicationBundler(new ClassAcceptor() { @Override public boolean accept(String className, URL classUrl, URL classPathUrl) { appBundler.createBundle(Locations.toLocation(jobJar), classes); } finally { ClassLoaders.setContextClassLoader(oldCLassLoader);
@Override public void load(String name, Location targetLocation) throws IOException { bundler.createBundle(targetLocation, ApplicationMasterMain.class, TwillContainerMain.class, OptionSpec.class); } });
private ApplicationBundler createBundler(ClassAcceptor classAcceptor) { return new ApplicationBundler(classAcceptor).setTempDir(getLocalStagingDir()); } }
ApplicationBundler appBundler = new ApplicationBundler(new ClassAcceptor() { @Override public boolean accept(String className, URL classUrl, URL classPathUrl) { appBundler.createBundle(Locations.toLocation(jobJar), classes); } finally { ClassLoaders.setContextClassLoader(oldCLassLoader);
@Override public void load(String name, Location targetLocation) throws IOException { bundler.createBundle(targetLocation, classes); } });
private ApplicationBundler createBundler(ClassAcceptor classAcceptor) { return new ApplicationBundler(classAcceptor).setTempDir(getLocalStagingDir()); } }
@Test public void testExtraClassPath() throws IOException, ClassNotFoundException { File tmpDir = TMP_FOLDER.newFolder(); // Create two jars, one with guava, one with gson ApplicationBundler bundler = new ApplicationBundler(new ClassAcceptor()); Location guavaJar = Locations.toLocation(new File(tmpDir, "guava.jar")); bundler.createBundle(guavaJar, ImmutableList.class); Location gsonJar = Locations.toLocation(new File(tmpDir, "gson.jar")); bundler.createBundle(gsonJar, Gson.class); // Unpack them File guavaDir = BundleJarUtil.unJar(guavaJar, TMP_FOLDER.newFolder()); File gsonDir = BundleJarUtil.unJar(gsonJar, TMP_FOLDER.newFolder()); // Create a DirectoryClassLoader using guava dir as the main directory, with the gson dir in the extra classpath String extraClassPath = gsonDir.getAbsolutePath() + File.pathSeparatorChar + gsonDir.getAbsolutePath() + "/lib/*"; ClassLoader cl = new DirectoryClassLoader(guavaDir, extraClassPath, null, Arrays.asList("lib")); // Should be able to load both guava and gson class from the class loader cl.loadClass(ImmutableList.class.getName()); cl.loadClass(Gson.class.getName()); }