Search in sources :

Example 1 with Named

use of org.b3log.latke.ioc.inject.Named in project latke by b3log.

the class Discoverer method discover.

/**
 * Scans classpath to discover bean classes.
 *
 * @param scanPath the paths to scan, using ',' as the separator. There are two types of the scanPath:
 * <ul>
 *   <li>package: org.b3log.process</li>
 *   <li>ant-style classpath: org/b3log/** /*process.class</li>
 * </ul>
 * @return discovered classes
 * @throws Exception exception
 */
public static Collection<Class<?>> discover(final String scanPath) throws Exception {
    if (Strings.isEmptyOrNull(scanPath)) {
        throw new IllegalStateException("Please specify the [scanPath]");
    }
    LOGGER.debug("scanPath[" + scanPath + "]");
    // See issue #17 (https://github.com/b3log/latke/issues/17) for more details
    final Collection<Class<?>> ret = new HashSet<Class<?>>();
    final String[] splitPaths = scanPath.split(",");
    // Adds some built-in components
    final String[] paths = ArrayUtils.concatenate(splitPaths, BUILT_IN_COMPONENT_PKGS);
    final Set<URL> urls = new LinkedHashSet<URL>();
    for (String path : paths) {
        /*
             * the being two types of the scanPath.
             *  1 package: org.b3log.process
             *  2 ant-style classpath: org/b3log/** /*process.class
             */
        if (!AntPathMatcher.isPattern(path)) {
            path = path.replaceAll("\\.", "/") + "/**/*.class";
        }
        urls.addAll(ClassPathResolver.getResources(path));
    }
    for (URL url : urls) {
        final DataInputStream classInputStream = new DataInputStream(url.openStream());
        final ClassFile classFile = new ClassFile(classInputStream);
        final String className = classFile.getName();
        final AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
        if (null == annotationsAttribute) {
            LOGGER.log(Level.TRACE, "The class[name={0}] is not a bean", className);
            continue;
        }
        final ConstPool constPool = classFile.getConstPool();
        final Annotation[] annotations = annotationsAttribute.getAnnotations();
        boolean maybeBeanClass = false;
        for (final Annotation annotation : annotations) {
            if (annotation.getTypeName().equals(RequestProcessor.class.getName())) {
                // Request Processor is singleton scoped
                final Annotation singletonAnnotation = new Annotation("javax.inject.Singleton", constPool);
                annotationsAttribute.addAnnotation(singletonAnnotation);
                classFile.addAttribute(annotationsAttribute);
                classFile.setVersionToJava5();
                maybeBeanClass = true;
                break;
            }
            if (annotation.getTypeName().equals(Service.class.getName()) || (annotation.getTypeName()).equals(Repository.class.getName())) {
                // Service and Repository is singleton scoped by default
                maybeBeanClass = true;
                break;
            }
            if (annotation.getTypeName().equals(Named.class.getName())) {
                // Annoatated with Named maybe a bean class
                maybeBeanClass = true;
                break;
            }
        // Others will not load
        }
        if (maybeBeanClass) {
            Class<?> clz = null;
            try {
                clz = Thread.currentThread().getContextClassLoader().loadClass(className);
            } catch (final ClassNotFoundException e) {
                LOGGER.log(Level.ERROR, "some error to load the class[" + className + "]", e);
            }
            ret.add(clz);
        }
    }
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConstPool(javassist.bytecode.ConstPool) Named(org.b3log.latke.ioc.inject.Named) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) DataInputStream(java.io.DataInputStream) URL(java.net.URL) Annotation(javassist.bytecode.annotation.Annotation) RequestProcessor(org.b3log.latke.servlet.annotation.RequestProcessor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Named

use of org.b3log.latke.ioc.inject.Named in project latke by b3log.

the class BeanImpl method named.

@Override
public LatkeBean<T> named(final String name) {
    final Named namedQualifier = new NamedLiteral(name);
    addQualifier(namedQualifier);
    return this;
}
Also used : Named(org.b3log.latke.ioc.inject.Named) NamedLiteral(org.b3log.latke.ioc.literal.NamedLiteral)

Aggregations

Named (org.b3log.latke.ioc.inject.Named)2 DataInputStream (java.io.DataInputStream)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ClassFile (javassist.bytecode.ClassFile)1 ConstPool (javassist.bytecode.ConstPool)1 Annotation (javassist.bytecode.annotation.Annotation)1 NamedLiteral (org.b3log.latke.ioc.literal.NamedLiteral)1 RequestProcessor (org.b3log.latke.servlet.annotation.RequestProcessor)1