public static Iterable<String> getAPIResources(ClassLoader classLoader) throws IOException { // Get the bootstrap classpath. This is for exclusion. Set<String> bootstrapPaths = Sets.newHashSet(); for (String classpath : Splitter.on(File.pathSeparatorChar).split(System.getProperty("sun.boot.class.path"))) { File file = new File(classpath); bootstrapPaths.add(file.getAbsolutePath()); try { bootstrapPaths.add(file.getCanonicalPath()); } catch (IOException e) { // Ignore the exception and proceed. } } Set<String> resources = getResources(classLoader, getAPIClassPath(), TIGON_API_PACKAGES, true, bootstrapPaths, Sets.<String>newHashSet()); return getResources(classLoader, ClassPath.from(classLoader, JAR_ONLY_URI), HADOOP_PACKAGES, false, bootstrapPaths, resources); }
public static synchronized Iterable<String> getResourceList() throws IOException { if (resourceList == null) { resourceList = ClassLoaders.getAPIResources(ApiResourceListHolder.class.getClassLoader()); } return resourceList; }
/** * Gathers all resources for api classes. */ private static ClassPath getAPIClassPath() throws IOException { ClassLoader classLoader = Flow.class.getClassLoader(); String resourceName = Flow.class.getName().replace('.', '/') + ".class"; URL url = classLoader.getResource(resourceName); if (url == null) { throw new IOException("Resource not found for " + resourceName); } try { URI classPathURI = getClassPathURL(resourceName, url).toURI(); return ClassPath.from(classPathURI, classLoader); } catch (URISyntaxException e) { throw new IOException(e); } }
@Override public synchronized ClassLoader getClassLoader() { if (classLoader == null) { expandIfNeeded(); try { classLoader = ClassLoaders.newProgramClassLoader(expandFolder, ApiResourceListHolder.getResourceList()); } catch (IOException e) { throw Throwables.propagate(e); } } return classLoader; }
@SuppressWarnings("unchecked") @Override public Class<DatumWriter<?>> load(CacheKey key) throws Exception { ClassDefinition classDef = new DatumWriterGenerator().generate(key.getType(), key.getSchema()); ClassLoader typeClassloader = ClassLoaders.getClassLoader(key.getType()); ByteCodeClassLoader classloader = classloaders.get(key.getType()); if (classloader == null) { classloader = new ByteCodeClassLoader(typeClassloader); classloaders.put(key.getType(), classloader); } return (Class<DatumWriter<?>>) classloader.addClass(classDef, key.getType().getRawType()) .loadClass(classDef.getClassName()); } }
public Program createProgram(File jarPath, String classToLoad, File jarUnpackDir) throws Exception { expandJar(jarPath, jarUnpackDir); ProgramClassLoader classLoader = ClassLoaders.newProgramClassLoader(jarUnpackDir, ApiResourceListHolder.getResourceList()); Class<?> clz = classLoader.loadClass(classToLoad); if (!(clz.newInstance() instanceof Flow)) { throw new Exception("Expected Flow class"); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Location deployJar = jarForTestBase(clz); LOG.info("Deploy Jar location : {}", deployJar.toURI()); try { return Programs.create(deployJar, classLoader); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } }