Search in sources :

Example 1 with ClassPath

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;
}
Also used : ClassPath(co.cask.cdap.common.internal.guava.ClassPath) ResourceInfo(co.cask.cdap.common.internal.guava.ClassPath.ResourceInfo) ClassAcceptor(org.apache.twill.api.ClassAcceptor) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 2 with ClassPath

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;
}
Also used : Path(javax.ws.rs.Path) ClassPath(co.cask.cdap.common.internal.guava.ClassPath) HeaderParam(javax.ws.rs.HeaderParam) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) AuditLogContent(co.cask.cdap.common.logging.AuditLogContent) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) AuditDetail(co.cask.cdap.common.security.AuditDetail) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod)

Example 3 with ClassPath

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;
}
Also used : ClassPath(co.cask.cdap.common.internal.guava.ClassPath) HttpHandler(co.cask.http.HttpHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 4 with ClassPath

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;
}
Also used : ClassPath(co.cask.cdap.common.internal.guava.ClassPath)

Aggregations

ClassPath (co.cask.cdap.common.internal.guava.ClassPath)4 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ResourceInfo (co.cask.cdap.common.internal.guava.ClassPath.ResourceInfo)1 AuditLogContent (co.cask.cdap.common.logging.AuditLogContent)1 AuditDetail (co.cask.cdap.common.security.AuditDetail)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 HttpHandler (co.cask.http.HttpHandler)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 HeaderParam (javax.ws.rs.HeaderParam)1 Path (javax.ws.rs.Path)1 ClassAcceptor (org.apache.twill.api.ClassAcceptor)1 HttpMethod (org.jboss.netty.handler.codec.http.HttpMethod)1