Search in sources :

Example 6 with FastClasspathScanner

use of io.github.lukehutch.fastclasspathscanner.FastClasspathScanner in project scheduler by btrplace.

the class TestScanner method testGroups.

public List<TestCampaign> testGroups(String... groups) throws IllegalAccessException, InvocationTargetException, InstantiationException {
    List<Executable> ms = new ArrayList<>();
    Set<String> ok = Stream.of(groups).collect(Collectors.toSet());
    FastClasspathScanner scanner = new FastClasspathScanner();
    scanner.matchClassesWithMethodAnnotation(CstrTest.class, (cl, m) -> {
        CstrTest a = m.getAnnotation(CstrTest.class);
        for (String g : a.groups()) {
            if (ok.contains(g)) {
                ms.add(m);
            }
        }
    }).scan(Runtime.getRuntime().availableProcessors() - 1);
    return test(ms.toArray(new Method[ms.size()]));
}
Also used : Constraint(org.btrplace.safeplace.spec.Constraint) Arrays(java.util.Arrays) List(java.util.List) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) Stream(java.util.stream.Stream) Executable(java.lang.reflect.Executable) Set(java.util.Set) Method(java.lang.reflect.Method) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable)

Example 7 with FastClasspathScanner

use of io.github.lukehutch.fastclasspathscanner.FastClasspathScanner in project uavstack by uavorg.

the class MSCPProfileHandler method doProfiling.

@Override
public void doProfiling(ProfileElement elem, ProfileContext context) {
    UAVServer.ServerVendor sv = (UAVServer.ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    // only support MSCP Application
    if (sv != UAVServer.ServerVendor.MSCP) {
        return;
    }
    if (!ProfileConstants.PROELEM_COMPONENT.equals(elem.getElemId())) {
        return;
    }
    InterceptContext ic = context.get(InterceptContext.class);
    if (ic == null) {
        this.logger.warn("Profile:Annotation FAILs as No InterceptContext available", null);
        return;
    }
    /**
     * 1. get webappclassloader
     */
    ClassLoader webappclsLoader = (ClassLoader) ic.get(InterceptConstants.WEBAPPLOADER);
    if (null == webappclsLoader) {
        this.logger.warn("Profile:JARS FAILs as No webappclsLoader available", null);
        return;
    }
    Collection<ClassLoader> clList = ConfigurationManager.getInstance().getFeatureClassLoader();
    clList.add(webappclsLoader);
    ClassLoader[] allcl = new ClassLoader[clList.size()];
    allcl = clList.toArray(allcl);
    /**
     * 2. see what kind of components we could get via annotations or interface or parentClass
     */
    FastClasspathScanner fcs = new FastClasspathScanner(allcl, "com.creditease.uav", "com.creditease.agent", "org.uavstack");
    fcs.scan();
    /**
     * 3. get MSCPServlets profile info
     */
    InterceptContext itContext = context.get(InterceptContext.class);
    String appRootPath = (String) itContext.get(InterceptConstants.BASEPATH);
    String appName = (String) itContext.get(InterceptConstants.APPNAME);
    for (String componentClassName : componentClassMap.keySet()) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        ComponentProcessor cp = componentClassMap.get(componentClassName);
        cp.handle(componentClassName, appName, inst, fcs);
    }
    /**
     * 4. load application info
     */
    loadAppInfo(elem, appRootPath, appName);
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) UAVServer(com.creditease.monitor.UAVServer)

Example 8 with FastClasspathScanner

use of io.github.lukehutch.fastclasspathscanner.FastClasspathScanner in project uavstack by uavorg.

the class ComponentProfileHandler method doProfiling.

@Override
public void doProfiling(ProfileElement elem, ProfileContext context) {
    UAVServer.ServerVendor sv = (UAVServer.ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    // only support JEE Application, not support MSCP Application
    if (sv == UAVServer.ServerVendor.MSCP) {
        return;
    }
    if (!ProfileConstants.PROELEM_COMPONENT.equals(elem.getElemId())) {
        return;
    }
    InterceptContext ic = context.get(InterceptContext.class);
    if (ic == null) {
        this.logger.warn("Profile:Annotation FAILs as No InterceptContext available", null);
        return;
    }
    /**
     * 1.get webappclassloader
     */
    ClassLoader webappclsLoader = (ClassLoader) ic.get(InterceptConstants.WEBAPPLOADER);
    if (null == webappclsLoader) {
        this.logger.warn("Profile:JARS FAILs as No webappclsLoader available", null);
        return;
    }
    /**
     * 1.5 for other none JEE or Spring tech profiling
     */
    dubboProfileHandler.doProfiling(elem, context);
    /**
     * 2.load available annotation classes
     */
    Map<String, Class<?>> annoAvailableClasses = new HashMap<String, Class<?>>();
    for (String annoClsName : componentClassNames) {
        try {
            Class<?> c = webappclsLoader.loadClass(annoClsName);
            annoAvailableClasses.put(annoClsName, c);
        } catch (ClassNotFoundException e) {
            // ignore
            if (this.logger.isDebugable()) {
                this.logger.warn("Annotation Class [" + annoClsName + "] is not found in web application [" + elem.getRepository().getProfile().getId() + "]", e);
            }
        }
    }
    /**
     * 2.1 load available interface classes
     */
    Map<String, Class<?>> interfaceAvailableClasses = new HashMap<String, Class<?>>();
    for (String interfaceClsName : componentInterfaceNames) {
        try {
            Class<?> c = webappclsLoader.loadClass(interfaceClsName);
            interfaceAvailableClasses.put(interfaceClsName, c);
        } catch (ClassNotFoundException e) {
            // ignore
            if (this.logger.isDebugable()) {
                this.logger.warn("Interface Class [" + interfaceClsName + "] is not found in web application [" + elem.getRepository().getProfile().getId() + "]", e);
            }
        }
    }
    /**
     * 3. see what kind of components we could get via annotations
     */
    UAVServer.ServerVendor vendor = (ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    /**
     * NOTE: currently for spring boot, we use its base classloader as the webappclassloader, and should scan all
     * packages
     */
    ClassLoader[] classLoaders = null;
    if (vendor != UAVServer.ServerVendor.SPRINGBOOT) {
        classLoaders = new ClassLoader[] { webappclsLoader };
        String scanPackages = System.getProperty("com.creditease.uav.uavmof.profile.package.header");
        if (StringHelper.isEmpty(scanPackages)) {
            scanPackage[0] = "com";
        } else {
            scanPackage = scanPackages.split(",");
        }
    } else {
        scanPackage[0] = "";
    }
    FastClasspathScanner fcs = new FastClasspathScanner(classLoaders, scanPackage);
    fcs.scan();
    // store FastClasspathScanner instance into ProfileContext
    context.put(FastClasspathScanner.class, fcs);
    /**
     * 4.tide components we get from annotations & deployment descriptors
     */
    // store the DescriptorProcessor instance for better performance
    Map<String, DescriptorProcessor> dpInstances = new LinkedHashMap<String, DescriptorProcessor>();
    // get web application root
    InterceptContext itContext = context.get(InterceptContext.class);
    String webAppRoot = (String) itContext.get(InterceptConstants.BASEPATH);
    for (String componentClassName : componentClassNames) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // load componentsByAnno first
        loadComponentsByAnno(context, webappclsLoader, fcs, annoAvailableClasses, componentClassName, inst);
    }
    for (String componentClassName : componentInterfaceNames) {
        // set the instance id = simple name of the annotation class
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // load componentsByInterface
        loadComponentsByInterface(context, webappclsLoader, fcs, interfaceAvailableClasses, componentClassName, inst);
    }
    String[] allComponentNames = new String[componentClassNames.length + componentInterfaceNames.length];
    System.arraycopy(componentClassNames, 0, allComponentNames, 0, componentClassNames.length);
    System.arraycopy(componentInterfaceNames, 0, allComponentNames, componentClassNames.length, componentInterfaceNames.length);
    for (String componentClassName : allComponentNames) {
        ProfileElementInstance inst = elem.getInstance(componentClassName);
        // try to load componentsByDescriptor
        loadComponentsByDescriptor(dpInstances, webAppRoot, context, componentClassName, inst);
        // try to load componentsByDynamic Creation, currently is for servlet 3.x and webservice
        loadComponentsByDynamic(itContext, componentClassName, inst, context, annoAvailableClasses, fcs);
        /**
         * NOTE: in order to control the monitor data url, we need collect ther servlet url to help to identify if
         * an url is a service url
         */
        if (componentClassName.equalsIgnoreCase("javax.servlet.annotation.WebServlet")) {
            collectServletInfoForMonitor(inst);
        }
        /**
         * collectProfileServiceMap
         */
        collectProfileServiceMap(componentClassName, inst);
    }
    /**
     * 4.1 add common info instance from descriptor to profile element
     */
    // web.xml related common info
    loadCommonInfoFromWebXML(elem, dpInstances, webAppRoot, context);
    /**
     * 5.confirm there is update
     */
    elem.getRepository().setUpdate(true);
    // release resources quickly
    dpInstances.clear();
}
Also used : ProfileElementInstance(com.creditease.uav.profiling.spi.ProfileElementInstance) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UAVServer(com.creditease.monitor.UAVServer) LinkedHashMap(java.util.LinkedHashMap) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) ServerVendor(com.creditease.monitor.UAVServer.ServerVendor) ServerVendor(com.creditease.monitor.UAVServer.ServerVendor)

Example 9 with FastClasspathScanner

use of io.github.lukehutch.fastclasspathscanner.FastClasspathScanner in project uavstack by uavorg.

the class FastScanServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    FastClasspathScanner scanner = new FastClasspathScanner("com.creditease");
    long st = System.nanoTime();
    FastClasspathScanner scannerm1 = scanner.scan();
    long end = System.nanoTime() - st;
    System.out.println(end);
    List<String> lista = scannerm1.getNamesOfClassesWithAnnotation(WebService.class);
    for (String str : lista) {
        System.out.println("webservice m1 -" + str);
    }
    st = System.nanoTime();
    FastClasspathScanner scanner2 = new FastClasspathScanner("com.creditease");
    scanner2.matchClassesWithAnnotation(WebService.class, new ClassAnnotationMatchProcessor() {

        @SuppressWarnings("rawtypes")
        @Override
        public void processMatch(Class arg0) {
            System.out.println("webservice m1 -" + arg0.getName());
        }
    }).scan();
    end = System.nanoTime() - st;
    System.out.println(end);
    resp.getWriter().write("FastClasspathScanner cost:" + end);
}
Also used : ClassAnnotationMatchProcessor(io.github.lukehutch.fastclasspathscanner.matchprocessor.ClassAnnotationMatchProcessor) FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) WebService(javax.jws.WebService)

Example 10 with FastClasspathScanner

use of io.github.lukehutch.fastclasspathscanner.FastClasspathScanner in project VoxelGamesLibv2 by VoxelGamesLib.

the class VoxelGamesLibModule method getScannerWithAddons.

@Provides
@Named("IncludeAddons")
public FastClasspathScanner getScannerWithAddons(ModuleHandler moduleHandler) {
    FastClasspathScanner scanner = getScanner();
    moduleHandler.getModuleClassLoaders().forEach(scanner::addClassLoader);
    return scanner;
}
Also used : FastClasspathScanner(io.github.lukehutch.fastclasspathscanner.FastClasspathScanner) Named(com.google.inject.name.Named) Provides(com.google.inject.Provides)

Aggregations

FastClasspathScanner (io.github.lukehutch.fastclasspathscanner.FastClasspathScanner)13 ScanResult (io.github.lukehutch.fastclasspathscanner.scanner.ScanResult)3 UAVServer (com.creditease.monitor.UAVServer)2 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)2 ProfileElementInstance (com.creditease.uav.profiling.spi.ProfileElementInstance)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ServerVendor (com.creditease.monitor.UAVServer.ServerVendor)1 DynamicProxyProcessor (com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor)1 DPMethod (com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod)1 WebServiceListenerIT (com.creditease.uav.monitorframework.webservice.interceptors.WebServiceListenerIT)1 OrchidOption (com.eden.orchid.api.options.OrchidOption)1 AbstractModule (com.google.inject.AbstractModule)1 Provides (com.google.inject.Provides)1 Named (com.google.inject.name.Named)1 ClassAnnotationMatchProcessor (io.github.lukehutch.fastclasspathscanner.matchprocessor.ClassAnnotationMatchProcessor)1 PrintWriter (java.io.PrintWriter)1 Executable (java.lang.reflect.Executable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1