public static synchronized Iterable<String> getResourceList() throws IOException { if (resourceList == null) { resourceList = ClassLoaders.getAPIResources(ApiResourceListHolder.class.getClassLoader()); } return resourceList; }
@Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // Try to load it from bootstrap class loader first try { return bootstrapClassLoader.loadClass(name); } catch (ClassNotFoundException e) { if (isValidResource(classNameToResourceName(name))) { return super.loadClass(name, resolve); } throw e; } }
flowSpec); Flowlet flowlet = new InstantiatorFactory(false).get(TypeToken.of(flowletClass)).create(); TypeToken<? extends Flowlet> flowletType = TypeToken.of(flowletClass); new PropertyFieldSetter(flowletDef.getFlowletSpec().getProperties()), new MetricsFieldSetter(flowletContext.getMetrics()), new OutputEmitterFieldSetter(outputEmitterFactory(flowletContext, flowletName,
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); } }
@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; }
public static ProgramClassLoader newProgramClassLoader(File unpackedJarDir, Iterable<String> apiResourceList) throws IOException { Predicate<String> predicate = Predicates.in(Sets.newHashSet(apiResourceList)); ClassLoader filterParent = Objects.firstNonNull(Thread.currentThread().getContextClassLoader(), ClassLoaders.class.getClassLoader()); return new ProgramClassLoader(unpackedJarDir, new FilterClassLoader(predicate, filterParent)); }
@SuppressWarnings("unchecked") public ReflectionDatumReader(Schema schema, TypeToken<T> type) { this.schema = schema; this.type = type; this.creatorFactory = new InstantiatorFactory(true); this.creators = Maps.newIdentityHashMap(); this.fieldAccessorFactory = new ReflectionFieldAccessorFactory(); }
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); }
@Override public URL findResource(String name) { if (isValidResource(name)) { return super.findResource(name); } return null; }
@Override public void visit(Object instance, TypeToken<?> inspectType, TypeToken<?> declareType, Field field) throws Exception { if (field.isAnnotationPresent(Property.class)) { String key = declareType.getRawType().getName() + '.' + field.getName(); String value = properties.get(key); if (value == null) { return; } setValue(instance, field, value); } }
@Override public T create() { try { Object instance = UNSAFE.allocateInstance(type.getRawType()); Reflections.visit(instance, type, new FieldInitializer()); return (T) instance; } catch (InstantiationException e) { throw Throwables.propagate(e); } } };
/** * 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); } }
return parent; return new CombineClassLoader(parent, classLoaders);
@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()); } }
@Override public Enumeration<URL> findResources(String name) throws IOException { if (isValidResource(name)) { return super.findResources(name); } return new CompoundEnumeration<URL>(new Enumeration[0]); }