use of co.cask.cdap.common.internal.guava.ClassPath in project cdap by caskdata.
the class ClassPathResources method findClassDependencies.
/**
* Finds all resource names that the given set of classes depends on.
*
* @param classLoader class loader for looking up .class resources
* @param classes set of class names that need to trace dependencies from
* @param result collection to store the resulting resource names
* @param <T> type of the result collection
* @throws IOException if fails to load class bytecode during tracing
*/
private static <T extends Collection<String>> T findClassDependencies(final ClassLoader classLoader, Iterable<String> classes, final T result) throws IOException {
final Set<String> bootstrapClassPaths = getBootstrapClassPaths();
final Set<URL> classPathSeen = Sets.newHashSet();
Dependencies.findClassDependencies(classLoader, new ClassAcceptor() {
@Override
public boolean accept(String className, URL classUrl, URL classPathUrl) {
// Ignore bootstrap classes
if (bootstrapClassPaths.contains(classPathUrl.getFile())) {
return false;
}
// visible through the program classloader.
if (className.startsWith("org.slf4j.impl.")) {
return false;
}
if (!classPathSeen.add(classPathUrl)) {
return true;
}
// Add all resources in the given class path
try {
ClassPath classPath = ClassPath.from(classPathUrl.toURI(), classLoader);
for (ClassPath.ResourceInfo resourceInfo : classPath.getResources()) {
result.add(resourceInfo.getResourceName());
}
} catch (Exception e) {
// If fail to get classes/resources from the classpath, ignore this classpath.
}
return true;
}
}, classes);
return result;
}
use of co.cask.cdap.common.internal.guava.ClassPath in project cdap by caskdata.
the class RouterAuditLookUp method createMatcher.
private int createMatcher() {
List<Class<?>> handlerClasses;
try {
handlerClasses = getAllHandlerClasses();
} catch (IOException e) {
LOG.error("Failed to get all handler classes for audit logging: {}", e.getCause());
return -1;
}
int count = 0;
for (Class<?> handlerClass : handlerClasses) {
Path classPath = handlerClass.getAnnotation(Path.class);
String classPathStr = classPath == null ? "" : classPath.value();
for (Method method : handlerClass.getMethods()) {
Path methodPath = method.getAnnotation(Path.class);
AuditPolicy auditPolicy = method.getAnnotation(AuditPolicy.class);
HttpMethod httpMethod = getHttpMethod(method);
if (methodPath == null || auditPolicy == null || httpMethod == null) {
continue;
}
String methodPathStr = methodPath.value();
String completePath = classPathStr.endsWith("/") || methodPathStr.startsWith("/") ? classPathStr + methodPathStr : classPathStr + "/" + methodPathStr;
List<AuditDetail> auditContents = Arrays.asList(auditPolicy.value());
List<String> headerNames = new ArrayList<>();
if (auditContents.contains(AuditDetail.HEADERS)) {
Annotation[][] annotations = method.getParameterAnnotations();
for (Annotation[] annotationArr : annotations) {
if (annotationArr.length > 0) {
for (Annotation annotation : annotationArr) {
if (annotation instanceof HeaderParam) {
headerNames.add(((HeaderParam) annotation).value());
}
}
}
}
}
AuditLogContent auditLogContent = new AuditLogContent(httpMethod, auditContents.contains(AuditDetail.REQUEST_BODY), auditContents.contains(AuditDetail.RESPONSE_BODY), headerNames);
LOG.trace("Audit log lookup: bootstrapped with path: {}", completePath);
patternMatcher.add(completePath, auditLogContent);
count++;
}
}
LOG.debug("Audit log lookup: bootstrapped with {} paths", count);
return count;
}
use of co.cask.cdap.common.internal.guava.ClassPath in project cdap by caskdata.
the class RouterAuditLookUp method getAllHandlerClasses.
private List<Class<?>> getAllHandlerClasses() throws IOException {
ClassLoader cl = getClass().getClassLoader();
Map<String, Boolean> cache = new HashMap<>();
Function<String, URL> lookup = ClassLoaders.createClassResourceLookup(cl);
ClassPath cp = ClassPath.from(cl);
List<Class<?>> results = new ArrayList<>();
for (ClassPath.ClassInfo info : cp.getAllClasses()) {
if (!info.getPackageName().startsWith("co.cask.cdap")) {
continue;
}
if (Classes.isSubTypeOf(info.getName(), HttpHandler.class.getName(), lookup, cache)) {
results.add(info.load());
}
}
return results;
}
use of co.cask.cdap.common.internal.guava.ClassPath in project cdap by caskdata.
the class ClassPathResources method getResourcesWithDependencies.
/**
* Returns the base set of resources needed to load the specified {@link Class} using the
* specified {@link ClassLoader}. Also traces and includes the dependencies for the specified class.
*
* @param classLoader the {@link ClassLoader} to use to generate the set of resources
* @param classz the {@link Class} to generate the set of resources for
* @return the set of resources needed to load the specified {@link Class} using the specified {@link ClassLoader}
* @throws IOException
*/
public static Set<String> getResourcesWithDependencies(ClassLoader classLoader, Class<?> classz) throws IOException {
ClassPath classPath = getClassPath(classLoader, classz);
// Add everything in the classpath as visible resources
Set<String> result = Sets.newHashSet(Iterables.transform(classPath.getResources(), RESOURCE_INFO_TO_RESOURCE_NAME));
// Trace dependencies for all classes in the classpath
findClassDependencies(classLoader, Iterables.transform(classPath.getAllClasses(), CLASS_INFO_TO_CLASS_NAME), result);
return result;
}
Aggregations